| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {Platform} from '../platform'; |
| 9 | import type {ThemeColor} from '../theme'; |
| 10 | import type {OneIndexedLineNumber, RepoRelativePath} from '../types'; |
| 11 | |
| 12 | import {makeBrowserLikePlatformImpl} from './browserPlatformImpl'; |
| 13 | |
| 14 | // important: this file should not try to import other code from 'isl', |
| 15 | // since it will end up getting duplicated when bundling. |
| 16 | |
| 17 | const obsidianPlatform: Platform = { |
| 18 | ...makeBrowserLikePlatformImpl('obsidian'), |
| 19 | |
| 20 | // Override file opening to send messages to Obsidian via postMessage |
| 21 | openFile: (path: RepoRelativePath, options?: {line?: OneIndexedLineNumber}) => { |
| 22 | window.parent.postMessage( |
| 23 | { |
| 24 | type: 'isl/platform/openFile', |
| 25 | path, |
| 26 | line: options?.line, |
| 27 | }, |
| 28 | '*', |
| 29 | ); |
| 30 | }, |
| 31 | |
| 32 | openFiles: (paths: ReadonlyArray<RepoRelativePath>, options?: {line?: OneIndexedLineNumber}) => { |
| 33 | window.parent.postMessage( |
| 34 | { |
| 35 | type: 'isl/platform/openFiles', |
| 36 | paths, |
| 37 | line: options?.line, |
| 38 | }, |
| 39 | '*', |
| 40 | ); |
| 41 | }, |
| 42 | |
| 43 | canCustomizeFileOpener: false, // Obsidian controls file opening |
| 44 | upsellExternalMergeTool: false, // Obsidian is the editor |
| 45 | |
| 46 | openExternalLink(url: string): void { |
| 47 | window.parent.postMessage( |
| 48 | { |
| 49 | type: 'isl/platform/openExternal', |
| 50 | url, |
| 51 | }, |
| 52 | '*', |
| 53 | ); |
| 54 | }, |
| 55 | |
| 56 | // Theme integration |
| 57 | theme: { |
| 58 | getTheme(): ThemeColor { |
| 59 | // Default to dark, will be updated by Obsidian |
| 60 | return 'dark'; |
| 61 | }, |
| 62 | |
| 63 | onDidChangeTheme(callback: (theme: ThemeColor) => unknown) { |
| 64 | const handleMessage = (event: MessageEvent) => { |
| 65 | if (event.data?.type === 'obsidian/themeChanged') { |
| 66 | const theme: ThemeColor = event.data.theme === 'dark' ? 'dark' : 'light'; |
| 67 | callback(theme); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | window.addEventListener('message', handleMessage); |
| 72 | |
| 73 | return { |
| 74 | dispose: () => { |
| 75 | window.removeEventListener('message', handleMessage); |
| 76 | }, |
| 77 | }; |
| 78 | }, |
| 79 | }, |
| 80 | }; |
| 81 | |
| 82 | window.islPlatform = obsidianPlatform; |
| 83 | |
| 84 | // Debug: Log when platform is initialized |
| 85 | console.log('[ISL Obsidian] Platform initialized'); |
| 86 | |
| 87 | // Forward all server messages to Obsidian parent window for event logging |
| 88 | // This allows the Obsidian plugin to monitor all ISL server events |
| 89 | obsidianPlatform.messageBus.onMessage(event => { |
| 90 | console.log('[ISL Obsidian] Received server message'); |
| 91 | try { |
| 92 | const data = JSON.parse(event.data as string); |
| 93 | window.parent.postMessage( |
| 94 | { |
| 95 | type: 'isl/serverMessage', |
| 96 | data, |
| 97 | }, |
| 98 | '*', |
| 99 | ); |
| 100 | } catch (e) { |
| 101 | console.log('[ISL Obsidian] Failed to parse message:', e); |
| 102 | } |
| 103 | }); |
| 104 | |
| 105 | // Debug: Log before importing index |
| 106 | console.log('[ISL Obsidian] About to import index'); |
| 107 | |
| 108 | // Load the actual app entry, which must be done after the platform has been set up. |
| 109 | import('../index') |
| 110 | .then(() => { |
| 111 | console.log('[ISL Obsidian] Index imported successfully'); |
| 112 | }) |
| 113 | .catch(e => { |
| 114 | console.error('[ISL Obsidian] Failed to import index:', e); |
| 115 | }); |
| 116 | |