// The desktop app's bridge (mollusc, see mollusc.json at the repository // root), when Notable runs as one; undefined in a browser tab. Only what // Notable asks for is typed. type Filter = { name: string; extensions: string[] }; type OpenProperty = 'openFile' | 'openDirectory' | 'multiSelections' | 'createDirectory'; export interface Mollusc { env: { name: string; version: string; platform: string; port?: number | null; token?: string; sidecarOrigin?: string | null; appOrigin?: string }; on(name: string, fn: (payload: never) => void): () => void; pathForFile(file: File): Promise; fs: { stat(path: string): Promise<{ type: string; size: number; mtime: number } | null> }; dialog: { open(opts: { title?: string; defaultPath?: string; filters?: Filter[]; properties?: OpenProperty[] }): Promise; save(opts: { title?: string; defaultPath?: string; filters?: Filter[] }): Promise; message(opts: { type?: 'none' | 'info' | 'warning' | 'error' | 'question'; title?: string; message: string; detail?: string; buttons?: string[]; defaultId?: number; cancelId?: number }): Promise<{ response: number }>; }; shell: { openExternal(url: string): Promise; showInFolder(path: string): Promise }; window: { close(): Promise }; } export const mollusc = (globalThis as { mollusc?: Mollusc }).mollusc; export const isDesktop = !!mollusc; export const isMac = isDesktop ? mollusc!.env.platform === 'darwin' : /Mac/.test(navigator.platform); /** The sidecar the page may call: in the app, mollusc's; in a plain tab, one named in localStorage (`notable-sidecar`: { origin, token }) for development. */ export function sidecar(): { origin: string; token: string } | null { const env = mollusc?.env; if (env?.sidecarOrigin && env.token) return { origin: env.sidecarOrigin, token: env.token }; try { const raw = localStorage.getItem('notable-sidecar'); if (raw) return JSON.parse(raw) as { origin: string; token: string }; } catch { // no sidecar named } return null; } /** The app menu is declared in mollusc.json; each command arrives as a `menu:` event and lands here. */ export function installMenu(actions: Record void>): void { if (!mollusc) return; for (const [name, action] of Object.entries(actions)) mollusc.on(`menu:${name}`, () => action()); } /** The shell asks before closing when the page listens; `fn` finishes up and closes the window itself. */ export function onCloseRequested(fn: () => Promise | void): void { if (!mollusc) return; mollusc.on('close-requested', () => { void Promise.resolve(fn()).finally(() => mollusc!.window.close()); }); } export async function pickFiles(filters?: Filter[], title = 'Upload'): Promise { if (!mollusc) return []; return (await mollusc.dialog.open({ title, properties: ['openFile', 'multiSelections'], filters })) ?? []; } export async function pickDirectory(title = 'Choose a folder', defaultPath?: string): Promise { if (!mollusc) return null; const picked = await mollusc.dialog.open({ title, defaultPath, properties: ['openDirectory', 'createDirectory'] }); return picked?.[0] ?? null; } export async function pickSave(defaultPath: string, filters?: Filter[], title = 'Export'): Promise { if (!mollusc) return null; return mollusc.dialog.save({ title, defaultPath, filters }); } export async function confirm(message: string, detail?: string, ok = 'OK', type: 'question' | 'warning' = 'question'): Promise { if (!mollusc) return window.confirm(detail ? `${message}\n\n${detail}` : message); const { response } = await mollusc.dialog.message({ type, message, detail, buttons: [ok, 'Cancel'], defaultId: 0, cancelId: 1 }); return response === 0; } export async function alert(message: string, detail?: string, type: 'info' | 'warning' | 'error' = 'error'): Promise { if (!mollusc) { window.alert(detail ? `${message}\n\n${detail}` : message); return; } await mollusc.dialog.message({ type, message, detail, buttons: ['OK'] }); } export function openExternal(url: string): void { if (mollusc) void mollusc.shell.openExternal(url); else window.open(url, '_blank', 'noopener'); } export function showInFolder(path: string): void { if (mollusc) void mollusc.shell.showInFolder(path); } /** The local paths of files dropped on the page, when the app can tell. */ export async function droppedPaths(files: FileList | File[]): Promise { if (!mollusc) return []; const out: string[] = []; for (const f of Array.from(files)) { const p = await mollusc.pathForFile(f).catch(() => null); if (p) out.push(p); } return out; }