From 158e1cb2ef8d1d5ca59eb18c6924e4833b304872 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sat, 26 Feb 2022 18:02:09 +0100 Subject: [PATCH] Remove SyscallContext --- webapp/src/editor.tsx | 15 ++++++++----- webapp/src/plugins/runtime.ts | 23 +++----------------- webapp/src/syscalls/editor.browser.ts | 31 +++++++++------------------ webapp/src/syscalls/event.native.ts | 7 ------ webapp/src/syscalls/space.native.ts | 22 +++++++++++++++++++ 5 files changed, 45 insertions(+), 53 deletions(-) delete mode 100644 webapp/src/syscalls/event.native.ts create mode 100644 webapp/src/syscalls/space.native.ts diff --git a/webapp/src/editor.tsx b/webapp/src/editor.tsx index 7d243a8..fe9ef37 100644 --- a/webapp/src/editor.tsx +++ b/webapp/src/editor.tsx @@ -39,6 +39,7 @@ import customMarkdownStyle from "./style"; import dbSyscalls from "./syscalls/db.localstorage"; import { Plugin } from "./plugins/runtime"; import editorSyscalls from "./syscalls/editor.browser"; +import spaceSyscalls from "./syscalls/space.native"; import { Action, AppCommand, @@ -99,7 +100,11 @@ export class Editor { async loadPlugins() { const system = new BrowserSystem("plugin"); - system.registerSyscalls(dbSyscalls, editorSyscalls(this)); + system.registerSyscalls( + dbSyscalls, + editorSyscalls(this), + spaceSyscalls(this) + ); await system.bootServiceWorker(); console.log("Now loading core plugin"); @@ -203,16 +208,16 @@ export class Editor { run: commands.insertMarker("_"), }, { - key: "Ctrl-e", - mac: "Cmd-e", + key: "Ctrl-p", + mac: "Cmd-p", run: (): boolean => { window.open(location.href, "_blank")!.focus(); return true; }, }, { - key: "Ctrl-p", - mac: "Cmd-p", + key: "Ctrl-e", + mac: "Cmd-e", run: (target): boolean => { this.viewDispatch({ type: "start-navigate" }); return true; diff --git a/webapp/src/plugins/runtime.ts b/webapp/src/plugins/runtime.ts index 5ff904e..29d27d3 100644 --- a/webapp/src/plugins/runtime.ts +++ b/webapp/src/plugins/runtime.ts @@ -1,13 +1,5 @@ import { Manifest } from "./types"; -export class SyscallContext { - public plugin: Plugin; - - constructor(Plugin: Plugin) { - this.plugin = Plugin; - } -} - interface SysCallMapping { // TODO: Better typing [key: string]: any; @@ -51,12 +43,7 @@ export class FunctionWorker { this.initCallback(); break; case "syscall": - const ctx = new SyscallContext(this.plugin); - let result = await this.plugin.system.syscall( - ctx, - data.name, - data.args - ); + let result = await this.plugin.system.syscall(data.name, data.args); this.worker.postMessage({ type: "syscall-response", @@ -169,11 +156,7 @@ export class System { } } - async syscall( - ctx: SyscallContext, - name: string, - args: Array - ): Promise { + async syscall(name: string, args: Array): Promise { const callback = this.registeredSyscalls[name]; if (!name) { throw Error(`Unregistered syscall ${name}`); @@ -181,7 +164,7 @@ export class System { if (!callback) { throw Error(`Registered but not implemented syscall ${name}`); } - return Promise.resolve(callback(ctx, ...args)); + return Promise.resolve(callback(...args)); } async load(name: string, manifest: Manifest): Promise { diff --git a/webapp/src/syscalls/editor.browser.ts b/webapp/src/syscalls/editor.browser.ts index 046de22..c582908 100644 --- a/webapp/src/syscalls/editor.browser.ts +++ b/webapp/src/syscalls/editor.browser.ts @@ -1,5 +1,4 @@ import { Editor } from "../editor"; -import { SyscallContext } from "../plugins/runtime"; import { syntaxTree } from "@codemirror/language"; import { Transaction } from "@codemirror/state"; @@ -11,16 +10,16 @@ type SyntaxNode = { }; export default (editor: Editor) => ({ - "editor.getText": (ctx: SyscallContext) => { + "editor.getText": () => { return editor.editorView?.state.sliceDoc(); }, - "editor.getCursor": (ctx: SyscallContext): number => { + "editor.getCursor": (): number => { return editor.editorView!.state.selection.main.from; }, - "editor.navigate": async (ctx: SyscallContext, name: string) => { + "editor.navigate": async (name: string) => { await editor.navigate(name); }, - "editor.insertAtPos": (ctx: SyscallContext, text: string, pos: number) => { + "editor.insertAtPos": (text: string, pos: number) => { editor.editorView!.dispatch({ changes: { insert: text, @@ -28,12 +27,7 @@ export default (editor: Editor) => ({ }, }); }, - "editor.replaceRange": ( - ctx: SyscallContext, - from: number, - to: number, - text: string - ) => { + "editor.replaceRange": (from: number, to: number, text: string) => { editor.editorView!.dispatch({ changes: { insert: text, @@ -42,14 +36,14 @@ export default (editor: Editor) => ({ }, }); }, - "editor.moveCursor": (ctx: SyscallContext, pos: number) => { + "editor.moveCursor": (pos: number) => { editor.editorView!.dispatch({ selection: { anchor: pos, }, }); }, - "editor.insertAtCursor": (ctx: SyscallContext, text: string) => { + "editor.insertAtCursor": (text: string) => { let editorView = editor.editorView!; let from = editorView.state.selection.main.from; editorView.dispatch({ @@ -62,9 +56,7 @@ export default (editor: Editor) => ({ }, }); }, - "editor.getSyntaxNodeUnderCursor": ( - ctx: SyscallContext - ): SyntaxNode | undefined => { + "editor.getSyntaxNodeUnderCursor": (): SyntaxNode | undefined => { const editorState = editor.editorView!.state; let selection = editorState.selection.main; if (selection.empty) { @@ -79,10 +71,7 @@ export default (editor: Editor) => ({ } } }, - "editor.getSyntaxNodeAtPos": ( - ctx: SyscallContext, - pos: number - ): SyntaxNode | undefined => { + "editor.getSyntaxNodeAtPos": (pos: number): SyntaxNode | undefined => { const editorState = editor.editorView!.state; let node = syntaxTree(editorState).resolveInner(pos); if (node) { @@ -94,7 +83,7 @@ export default (editor: Editor) => ({ }; } }, - "editor.dispatch": (ctx: SyscallContext, change: Transaction) => { + "editor.dispatch": (change: Transaction) => { editor.editorView!.dispatch(change); }, }); diff --git a/webapp/src/syscalls/event.native.ts b/webapp/src/syscalls/event.native.ts deleted file mode 100644 index bc0ad18..0000000 --- a/webapp/src/syscalls/event.native.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { SyscallContext } from "../plugins/runtime"; - -export default { - "event.publish": async (ctx: SyscallContext, name: string, data: any) => { - await ctx.plugin.dispatchEvent(name, data); - }, -}; diff --git a/webapp/src/syscalls/space.native.ts b/webapp/src/syscalls/space.native.ts new file mode 100644 index 0000000..ae19152 --- /dev/null +++ b/webapp/src/syscalls/space.native.ts @@ -0,0 +1,22 @@ +import { Editor } from "../editor"; +import { SyscallContext } from "../plugins/runtime"; +import { PageMeta } from "../types"; + +export default (editor: Editor) => ({ + "space.listPages": (ctx: SyscallContext): PageMeta[] => { + return editor.viewState.allPages; + }, + "space.readPage": async ( + ctx: SyscallContext, + name: string + ): Promise<{ text: string; meta: PageMeta }> => { + return await editor.fs.readPage(name); + }, + "space.writePage": async ( + ctx: SyscallContext, + name: string, + text: string + ): Promise => { + return await editor.fs.writePage(name, text); + }, +});