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