2024-07-30 23:33:33 +08:00
|
|
|
import type { Client } from "../client.ts";
|
|
|
|
import type { SysCallMapping } from "../../lib/plugos/system.ts";
|
2024-08-02 23:14:40 +08:00
|
|
|
import type {
|
|
|
|
AttachmentMeta,
|
|
|
|
FileMeta,
|
|
|
|
PageMeta,
|
|
|
|
} from "../../plug-api/types.ts";
|
2022-03-20 16:56:28 +08:00
|
|
|
|
2024-01-27 00:05:10 +08:00
|
|
|
export function spaceReadSyscalls(editor: Client): SysCallMapping {
|
2023-05-24 02:53:53 +08:00
|
|
|
return {
|
|
|
|
"space.listPages": (): Promise<PageMeta[]> => {
|
2023-07-14 19:44:30 +08:00
|
|
|
return editor.space.fetchPageList();
|
2023-05-24 02:53:53 +08:00
|
|
|
},
|
2024-01-15 23:43:12 +08:00
|
|
|
"space.readPage": async (_ctx, name: string): Promise<string> => {
|
2023-07-14 19:44:30 +08:00
|
|
|
return (await editor.space.readPage(name)).text;
|
2023-05-24 02:53:53 +08:00
|
|
|
},
|
|
|
|
"space.getPageMeta": (_ctx, name: string): Promise<PageMeta> => {
|
2023-07-14 19:44:30 +08:00
|
|
|
return editor.space.getPageMeta(name);
|
2023-05-24 02:53:53 +08:00
|
|
|
},
|
2024-01-27 00:05:10 +08:00
|
|
|
"space.listPlugs": (): Promise<FileMeta[]> => {
|
|
|
|
return editor.space.listPlugs();
|
|
|
|
},
|
|
|
|
"space.listAttachments": async (): Promise<AttachmentMeta[]> => {
|
|
|
|
return await editor.space.fetchAttachmentList();
|
|
|
|
},
|
|
|
|
"space.readAttachment": async (_ctx, name: string): Promise<Uint8Array> => {
|
|
|
|
return (await editor.space.readAttachment(name)).data;
|
|
|
|
},
|
|
|
|
"space.getAttachmentMeta": async (
|
|
|
|
_ctx,
|
|
|
|
name: string,
|
|
|
|
): Promise<AttachmentMeta> => {
|
|
|
|
return await editor.space.getAttachmentMeta(name);
|
|
|
|
},
|
|
|
|
// FS
|
|
|
|
"space.listFiles": (): Promise<FileMeta[]> => {
|
|
|
|
return editor.space.spacePrimitives.fetchFileList();
|
|
|
|
},
|
|
|
|
"space.getFileMeta": (_ctx, name: string): Promise<FileMeta> => {
|
|
|
|
return editor.space.spacePrimitives.getFileMeta(name);
|
|
|
|
},
|
|
|
|
"space.readFile": async (_ctx, name: string): Promise<Uint8Array> => {
|
|
|
|
return (await editor.space.spacePrimitives.readFile(name)).data;
|
|
|
|
},
|
2024-08-24 18:35:09 +08:00
|
|
|
"space.fileExists": (_ctx, name: string): boolean => {
|
|
|
|
return editor.clientSystem.allKnownFiles.has(name);
|
|
|
|
},
|
2024-01-27 00:05:10 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function spaceWriteSyscalls(editor: Client): SysCallMapping {
|
|
|
|
return {
|
2023-05-24 02:53:53 +08:00
|
|
|
"space.writePage": (
|
|
|
|
_ctx,
|
|
|
|
name: string,
|
|
|
|
text: string,
|
|
|
|
): Promise<PageMeta> => {
|
2023-07-14 19:44:30 +08:00
|
|
|
return editor.space.writePage(name, text);
|
2023-05-24 02:53:53 +08:00
|
|
|
},
|
|
|
|
"space.deletePage": async (_ctx, name: string) => {
|
|
|
|
// If we're deleting the current page, navigate to the index page
|
|
|
|
if (editor.currentPage === name) {
|
2024-01-24 18:58:33 +08:00
|
|
|
await editor.navigate({ page: "" });
|
2023-05-24 02:53:53 +08:00
|
|
|
}
|
|
|
|
// Remove page from open pages in editor
|
2024-01-24 18:58:33 +08:00
|
|
|
// editor.openPages.openPages.delete(name);
|
2023-05-24 02:53:53 +08:00
|
|
|
console.log("Deleting page");
|
|
|
|
await editor.space.deletePage(name);
|
|
|
|
},
|
2023-05-26 20:04:32 +08:00
|
|
|
"space.writeAttachment": (
|
2023-05-24 02:53:53 +08:00
|
|
|
_ctx,
|
|
|
|
name: string,
|
2023-05-26 20:04:32 +08:00
|
|
|
data: Uint8Array,
|
2023-05-24 02:53:53 +08:00
|
|
|
): Promise<AttachmentMeta> => {
|
2023-07-14 19:44:30 +08:00
|
|
|
return editor.space.writeAttachment(name, data);
|
2023-05-24 02:53:53 +08:00
|
|
|
},
|
|
|
|
"space.deleteAttachment": async (_ctx, name: string) => {
|
2023-07-14 19:44:30 +08:00
|
|
|
await editor.space.deleteAttachment(name);
|
2023-05-24 02:53:53 +08:00
|
|
|
},
|
2023-08-20 23:51:00 +08:00
|
|
|
"space.writeFile": (
|
|
|
|
_ctx,
|
|
|
|
name: string,
|
|
|
|
data: Uint8Array,
|
|
|
|
): Promise<FileMeta> => {
|
|
|
|
return editor.space.spacePrimitives.writeFile(name, data);
|
|
|
|
},
|
|
|
|
"space.deleteFile": (_ctx, name: string) => {
|
|
|
|
return editor.space.spacePrimitives.deleteFile(name);
|
|
|
|
},
|
2022-04-04 00:12:16 +08:00
|
|
|
};
|
|
|
|
}
|