addons/isl/src/platform/obsidianPlatform.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {Platform} from '../platform';
b69ab319import type {ThemeColor} from '../theme';
b69ab3110import type {OneIndexedLineNumber, RepoRelativePath} from '../types';
b69ab3111
b69ab3112import {makeBrowserLikePlatformImpl} from './browserPlatformImpl';
b69ab3113
b69ab3114// important: this file should not try to import other code from 'isl',
b69ab3115// since it will end up getting duplicated when bundling.
b69ab3116
b69ab3117const obsidianPlatform: Platform = {
b69ab3118 ...makeBrowserLikePlatformImpl('obsidian'),
b69ab3119
b69ab3120 // Override file opening to send messages to Obsidian via postMessage
b69ab3121 openFile: (path: RepoRelativePath, options?: {line?: OneIndexedLineNumber}) => {
b69ab3122 window.parent.postMessage(
b69ab3123 {
b69ab3124 type: 'isl/platform/openFile',
b69ab3125 path,
b69ab3126 line: options?.line,
b69ab3127 },
b69ab3128 '*',
b69ab3129 );
b69ab3130 },
b69ab3131
b69ab3132 openFiles: (paths: ReadonlyArray<RepoRelativePath>, options?: {line?: OneIndexedLineNumber}) => {
b69ab3133 window.parent.postMessage(
b69ab3134 {
b69ab3135 type: 'isl/platform/openFiles',
b69ab3136 paths,
b69ab3137 line: options?.line,
b69ab3138 },
b69ab3139 '*',
b69ab3140 );
b69ab3141 },
b69ab3142
b69ab3143 canCustomizeFileOpener: false, // Obsidian controls file opening
b69ab3144 upsellExternalMergeTool: false, // Obsidian is the editor
b69ab3145
b69ab3146 openExternalLink(url: string): void {
b69ab3147 window.parent.postMessage(
b69ab3148 {
b69ab3149 type: 'isl/platform/openExternal',
b69ab3150 url,
b69ab3151 },
b69ab3152 '*',
b69ab3153 );
b69ab3154 },
b69ab3155
b69ab3156 // Theme integration
b69ab3157 theme: {
b69ab3158 getTheme(): ThemeColor {
b69ab3159 // Default to dark, will be updated by Obsidian
b69ab3160 return 'dark';
b69ab3161 },
b69ab3162
b69ab3163 onDidChangeTheme(callback: (theme: ThemeColor) => unknown) {
b69ab3164 const handleMessage = (event: MessageEvent) => {
b69ab3165 if (event.data?.type === 'obsidian/themeChanged') {
b69ab3166 const theme: ThemeColor = event.data.theme === 'dark' ? 'dark' : 'light';
b69ab3167 callback(theme);
b69ab3168 }
b69ab3169 };
b69ab3170
b69ab3171 window.addEventListener('message', handleMessage);
b69ab3172
b69ab3173 return {
b69ab3174 dispose: () => {
b69ab3175 window.removeEventListener('message', handleMessage);
b69ab3176 },
b69ab3177 };
b69ab3178 },
b69ab3179 },
b69ab3180};
b69ab3181
b69ab3182window.islPlatform = obsidianPlatform;
b69ab3183
b69ab3184// Debug: Log when platform is initialized
b69ab3185console.log('[ISL Obsidian] Platform initialized');
b69ab3186
b69ab3187// Forward all server messages to Obsidian parent window for event logging
b69ab3188// This allows the Obsidian plugin to monitor all ISL server events
b69ab3189obsidianPlatform.messageBus.onMessage(event => {
b69ab3190 console.log('[ISL Obsidian] Received server message');
b69ab3191 try {
b69ab3192 const data = JSON.parse(event.data as string);
b69ab3193 window.parent.postMessage(
b69ab3194 {
b69ab3195 type: 'isl/serverMessage',
b69ab3196 data,
b69ab3197 },
b69ab3198 '*',
b69ab3199 );
b69ab31100 } catch (e) {
b69ab31101 console.log('[ISL Obsidian] Failed to parse message:', e);
b69ab31102 }
b69ab31103});
b69ab31104
b69ab31105// Debug: Log before importing index
b69ab31106console.log('[ISL Obsidian] About to import index');
b69ab31107
b69ab31108// Load the actual app entry, which must be done after the platform has been set up.
b69ab31109import('../index')
b69ab31110 .then(() => {
b69ab31111 console.log('[ISL Obsidian] Index imported successfully');
b69ab31112 })
b69ab31113 .catch(e => {
b69ab31114 console.error('[ISL Obsidian] Failed to import index:', e);
b69ab31115 });