1
0
silverbullet/web/syscalls/space.ts

71 lines
2.1 KiB
TypeScript
Raw Normal View History

import { Editor } from "../editor.tsx";
import { SysCallMapping } from "../../plugos/system.ts";
import { AttachmentMeta, PageMeta } from "../../common/types.ts";
2022-09-12 12:50:37 +00:00
import {
FileData,
FileEncoding,
} from "../../common/spaces/space_primitives.ts";
2022-04-03 16:12:16 +00:00
export function spaceSyscalls(editor: Editor): SysCallMapping {
return {
"space.listPages": (): PageMeta[] => {
2022-09-12 12:50:37 +00:00
return [...editor.space.listPages()];
2022-04-03 16:12:16 +00:00
},
"space.readPage": async (
_ctx,
name: string,
2022-10-14 13:11:33 +00:00
): Promise<string> => {
return (await editor.space.readPage(name)).text;
2022-04-03 16:12:16 +00:00
},
"space.getPageMeta": async (_ctx, name: string): Promise<PageMeta> => {
2022-08-08 11:33:20 +00:00
return await editor.space.getPageMeta(name);
},
"space.writePage": async (
_ctx,
name: string,
text: string,
): Promise<PageMeta> => {
2022-04-03 16:12:16 +00:00
return await editor.space.writePage(name, text);
},
"space.deletePage": async (_ctx, name: string) => {
2022-06-28 12:14:15 +00:00
// If we're deleting the current page, navigate to the index page
2022-04-03 16:12:16 +00:00
if (editor.currentPage === name) {
2022-08-02 10:43:39 +00:00
await editor.navigate("");
2022-04-03 16:12:16 +00:00
}
// Remove page from open pages in editor
editor.openPages.delete(name);
console.log("Deleting page");
await editor.space.deletePage(name);
},
"space.listPlugs": (): Promise<string[]> => {
return editor.space.listPlugs();
2022-09-12 12:50:37 +00:00
},
"space.listAttachments": (): Promise<AttachmentMeta[]> => {
return editor.space.fetchAttachmentList();
2022-09-05 11:36:04 +00:00
},
"space.readAttachment": async (
_ctx,
name: string,
2022-10-14 13:11:33 +00:00
): Promise<FileData> => {
return (await editor.space.readAttachment(name, "dataurl")).data;
2022-09-05 11:36:04 +00:00
},
"space.getAttachmentMeta": async (
_ctx,
name: string,
2022-09-05 11:36:04 +00:00
): Promise<AttachmentMeta> => {
return await editor.space.getAttachmentMeta(name);
},
"space.writeAttachment": async (
_ctx,
2022-09-05 11:36:04 +00:00
name: string,
2022-09-12 12:50:37 +00:00
encoding: FileEncoding,
data: FileData,
2022-09-05 11:36:04 +00:00
): Promise<AttachmentMeta> => {
2022-09-12 12:50:37 +00:00
return await editor.space.writeAttachment(name, encoding, data);
2022-09-05 11:36:04 +00:00
},
"space.deleteAttachment": async (_ctx, name: string) => {
2022-09-05 11:36:04 +00:00
await editor.space.deleteAttachment(name);
},
2022-04-03 16:12:16 +00:00
};
}