1
0

Remove SyscallContext

This commit is contained in:
Zef Hemel 2022-02-26 18:02:09 +01:00
parent bfd77de2d5
commit 158e1cb2ef
5 changed files with 45 additions and 53 deletions

View File

@ -39,6 +39,7 @@ import customMarkdownStyle from "./style";
import dbSyscalls from "./syscalls/db.localstorage"; import dbSyscalls from "./syscalls/db.localstorage";
import { Plugin } from "./plugins/runtime"; import { Plugin } from "./plugins/runtime";
import editorSyscalls from "./syscalls/editor.browser"; import editorSyscalls from "./syscalls/editor.browser";
import spaceSyscalls from "./syscalls/space.native";
import { import {
Action, Action,
AppCommand, AppCommand,
@ -99,7 +100,11 @@ export class Editor {
async loadPlugins() { async loadPlugins() {
const system = new BrowserSystem("plugin"); const system = new BrowserSystem("plugin");
system.registerSyscalls(dbSyscalls, editorSyscalls(this)); system.registerSyscalls(
dbSyscalls,
editorSyscalls(this),
spaceSyscalls(this)
);
await system.bootServiceWorker(); await system.bootServiceWorker();
console.log("Now loading core plugin"); console.log("Now loading core plugin");
@ -203,16 +208,16 @@ export class Editor {
run: commands.insertMarker("_"), run: commands.insertMarker("_"),
}, },
{ {
key: "Ctrl-e", key: "Ctrl-p",
mac: "Cmd-e", mac: "Cmd-p",
run: (): boolean => { run: (): boolean => {
window.open(location.href, "_blank")!.focus(); window.open(location.href, "_blank")!.focus();
return true; return true;
}, },
}, },
{ {
key: "Ctrl-p", key: "Ctrl-e",
mac: "Cmd-p", mac: "Cmd-e",
run: (target): boolean => { run: (target): boolean => {
this.viewDispatch({ type: "start-navigate" }); this.viewDispatch({ type: "start-navigate" });
return true; return true;

View File

@ -1,13 +1,5 @@
import { Manifest } from "./types"; import { Manifest } from "./types";
export class SyscallContext {
public plugin: Plugin;
constructor(Plugin: Plugin) {
this.plugin = Plugin;
}
}
interface SysCallMapping { interface SysCallMapping {
// TODO: Better typing // TODO: Better typing
[key: string]: any; [key: string]: any;
@ -51,12 +43,7 @@ export class FunctionWorker {
this.initCallback(); this.initCallback();
break; break;
case "syscall": case "syscall":
const ctx = new SyscallContext(this.plugin); let result = await this.plugin.system.syscall(data.name, data.args);
let result = await this.plugin.system.syscall(
ctx,
data.name,
data.args
);
this.worker.postMessage({ this.worker.postMessage({
type: "syscall-response", type: "syscall-response",
@ -169,11 +156,7 @@ export class System {
} }
} }
async syscall( async syscall(name: string, args: Array<any>): Promise<any> {
ctx: SyscallContext,
name: string,
args: Array<any>
): Promise<any> {
const callback = this.registeredSyscalls[name]; const callback = this.registeredSyscalls[name];
if (!name) { if (!name) {
throw Error(`Unregistered syscall ${name}`); throw Error(`Unregistered syscall ${name}`);
@ -181,7 +164,7 @@ export class System {
if (!callback) { if (!callback) {
throw Error(`Registered but not implemented syscall ${name}`); 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<Plugin> { async load(name: string, manifest: Manifest): Promise<Plugin> {

View File

@ -1,5 +1,4 @@
import { Editor } from "../editor"; import { Editor } from "../editor";
import { SyscallContext } from "../plugins/runtime";
import { syntaxTree } from "@codemirror/language"; import { syntaxTree } from "@codemirror/language";
import { Transaction } from "@codemirror/state"; import { Transaction } from "@codemirror/state";
@ -11,16 +10,16 @@ type SyntaxNode = {
}; };
export default (editor: Editor) => ({ export default (editor: Editor) => ({
"editor.getText": (ctx: SyscallContext) => { "editor.getText": () => {
return editor.editorView?.state.sliceDoc(); return editor.editorView?.state.sliceDoc();
}, },
"editor.getCursor": (ctx: SyscallContext): number => { "editor.getCursor": (): number => {
return editor.editorView!.state.selection.main.from; return editor.editorView!.state.selection.main.from;
}, },
"editor.navigate": async (ctx: SyscallContext, name: string) => { "editor.navigate": async (name: string) => {
await editor.navigate(name); await editor.navigate(name);
}, },
"editor.insertAtPos": (ctx: SyscallContext, text: string, pos: number) => { "editor.insertAtPos": (text: string, pos: number) => {
editor.editorView!.dispatch({ editor.editorView!.dispatch({
changes: { changes: {
insert: text, insert: text,
@ -28,12 +27,7 @@ export default (editor: Editor) => ({
}, },
}); });
}, },
"editor.replaceRange": ( "editor.replaceRange": (from: number, to: number, text: string) => {
ctx: SyscallContext,
from: number,
to: number,
text: string
) => {
editor.editorView!.dispatch({ editor.editorView!.dispatch({
changes: { changes: {
insert: text, insert: text,
@ -42,14 +36,14 @@ export default (editor: Editor) => ({
}, },
}); });
}, },
"editor.moveCursor": (ctx: SyscallContext, pos: number) => { "editor.moveCursor": (pos: number) => {
editor.editorView!.dispatch({ editor.editorView!.dispatch({
selection: { selection: {
anchor: pos, anchor: pos,
}, },
}); });
}, },
"editor.insertAtCursor": (ctx: SyscallContext, text: string) => { "editor.insertAtCursor": (text: string) => {
let editorView = editor.editorView!; let editorView = editor.editorView!;
let from = editorView.state.selection.main.from; let from = editorView.state.selection.main.from;
editorView.dispatch({ editorView.dispatch({
@ -62,9 +56,7 @@ export default (editor: Editor) => ({
}, },
}); });
}, },
"editor.getSyntaxNodeUnderCursor": ( "editor.getSyntaxNodeUnderCursor": (): SyntaxNode | undefined => {
ctx: SyscallContext
): SyntaxNode | undefined => {
const editorState = editor.editorView!.state; const editorState = editor.editorView!.state;
let selection = editorState.selection.main; let selection = editorState.selection.main;
if (selection.empty) { if (selection.empty) {
@ -79,10 +71,7 @@ export default (editor: Editor) => ({
} }
} }
}, },
"editor.getSyntaxNodeAtPos": ( "editor.getSyntaxNodeAtPos": (pos: number): SyntaxNode | undefined => {
ctx: SyscallContext,
pos: number
): SyntaxNode | undefined => {
const editorState = editor.editorView!.state; const editorState = editor.editorView!.state;
let node = syntaxTree(editorState).resolveInner(pos); let node = syntaxTree(editorState).resolveInner(pos);
if (node) { if (node) {
@ -94,7 +83,7 @@ export default (editor: Editor) => ({
}; };
} }
}, },
"editor.dispatch": (ctx: SyscallContext, change: Transaction) => { "editor.dispatch": (change: Transaction) => {
editor.editorView!.dispatch(change); editor.editorView!.dispatch(change);
}, },
}); });

View File

@ -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);
},
};

View File

@ -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<PageMeta> => {
return await editor.fs.writePage(name, text);
},
});