import { parseToRef, type Ref } from "@silverbulletmd/silverbullet/lib/ref"; import type { Client } from "../../client.ts"; import type { SysCallMapping } from "../system.ts"; import type { DocumentMeta, FileMeta, PageMeta, } from "@silverbulletmd/silverbullet/type/index"; export function spaceReadSyscalls(client: Client): SysCallMapping { return { "space.listPages": (): Promise => { return client.space.fetchPageList(); }, "space.readPage": async (_ctx, name: string): Promise => { return (await client.space.readPage(name)).text; }, "space.readPageWithMeta": ( _ctx, name: string, ): Promise<{ text: string; meta: PageMeta }> => { return client.space.readPage(name); }, "space.readRef": async (_ctx, ref: string | Ref): Promise => { if (typeof ref === "string") { ref = parseToRef(ref)!; if (!ref) { throw new Error(`Invalid ref: ${ref}`); } } return (await client.space.readRef(ref)).text; }, "space.pageExists": (_ctx, name: string): boolean => { return client.clientSystem.allKnownFiles.has(`${name}.md`); }, "space.getPageMeta": (_ctx, name: string): Promise => { return client.space.getPageMeta(name); }, "space.listPlugs": (): Promise => { return client.space.listPlugs(); }, "space.listDocuments": async (): Promise => { return await client.space.fetchDocumentList(); }, "space.readDocument": async (_ctx, name: string): Promise => { return (await client.space.readDocument(name)).data; }, "space.getDocumentMeta": async ( _ctx, name: string, ): Promise => { return await client.space.getDocumentMeta(name); }, // DEPRECATED, please use document versions instead, left here for backwards compatibility "space.listAttachments": async (): Promise => { return await client.space.fetchDocumentList(); }, "space.readAttachment": async (_ctx, name: string): Promise => { return (await client.space.readDocument(name)).data; }, "space.getAttachmentMeta": async ( _ctx, name: string, ): Promise => { return await client.space.getDocumentMeta(name); }, // FS "space.listFiles": (): Promise => { return client.space.spacePrimitives.fetchFileList(); }, "space.getFileMeta": (_ctx, name: string): Promise => { return client.space.spacePrimitives.getFileMeta(name); }, "space.readFile": async (_ctx, name: string): Promise => { return (await client.space.spacePrimitives.readFile(name)).data; }, "space.readFileWithMeta": async ( _ctx, name: string, ): Promise<{ data: Uint8Array; meta: FileMeta }> => { return await client.space.spacePrimitives.readFile(name); }, "space.fileExists": async (_ctx, name: string): Promise => { // If a full sync has successfully completed (so we know what files exist) // and we have a snapshot, let's use the snapshot if ( client.fullSyncCompleted && !client.eventedSpacePrimitives.isSnapshotEmpty() ) { return !!client.eventedSpacePrimitives.getSnapshot()[name]; } try { await client.space.spacePrimitives.getFileMeta(name); // If this returned the file exists return true; } catch { // Assumption: any error means the file does not exist return false; } }, }; } export function spaceWriteSyscalls(editor: Client): SysCallMapping { return { "space.writePage": ( _ctx, name: string, text: string, ): Promise => { return editor.space.writePage(name, text); }, "space.deletePage": async (_ctx, name: string) => { console.log("Deleting page"); await editor.space.deletePage(name); }, "space.writeDocument": ( _ctx, name: string, data: Uint8Array, ): Promise => { return editor.space.writeDocument(name, data); }, "space.deleteDocument": async (_ctx, name: string) => { await editor.space.deleteDocument(name); }, "space.writeFile": ( _ctx, name: string, data: Uint8Array, ): Promise => { return editor.space.spacePrimitives.writeFile(name, data); }, "space.deleteFile": (_ctx, name: string) => { return editor.space.spacePrimitives.deleteFile(name); }, }; }