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 { 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;

View File

@ -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<any>
): Promise<any> {
async syscall(name: string, args: Array<any>): Promise<any> {
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<Plugin> {

View File

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

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