Refactoring
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
export default {
|
||||
"db.put": (key: string, value: any) => {
|
||||
localStorage.setItem(key, value);
|
||||
},
|
||||
"db.get": (key: string) => {
|
||||
return localStorage.getItem(key);
|
||||
},
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Editor } from "../editor";
|
||||
import { syntaxTree } from "@codemirror/language";
|
||||
import { Transaction } from "@codemirror/state";
|
||||
import { PageMeta } from "../types";
|
||||
import { SysCallMapping } from "../../plugbox/system";
|
||||
|
||||
type SyntaxNode = {
|
||||
name: string;
|
||||
@@ -26,23 +26,23 @@ function ensureAnchor(expr: any, start: boolean) {
|
||||
);
|
||||
}
|
||||
|
||||
export default (editor: Editor) => ({
|
||||
"editor.getCurrentPage": (): string => {
|
||||
export default (editor: Editor): SysCallMapping => ({
|
||||
getCurrentPage: (): string => {
|
||||
return editor.currentPage!;
|
||||
},
|
||||
"editor.getText": () => {
|
||||
getText: () => {
|
||||
return editor.editorView?.state.sliceDoc();
|
||||
},
|
||||
"editor.getCursor": (): number => {
|
||||
getCursor: (): number => {
|
||||
return editor.editorView!.state.selection.main.from;
|
||||
},
|
||||
"editor.navigate": async (name: string) => {
|
||||
navigate: async (ctx, name: string) => {
|
||||
await editor.navigate(name);
|
||||
},
|
||||
"editor.openUrl": async (url: string) => {
|
||||
openUrl: async (ctx, url: string) => {
|
||||
window.open(url, "_blank")!.focus();
|
||||
},
|
||||
"editor.insertAtPos": (text: string, pos: number) => {
|
||||
insertAtPos: (ctx, text: string, pos: number) => {
|
||||
editor.editorView!.dispatch({
|
||||
changes: {
|
||||
insert: text,
|
||||
@@ -50,7 +50,7 @@ export default (editor: Editor) => ({
|
||||
},
|
||||
});
|
||||
},
|
||||
"editor.replaceRange": (from: number, to: number, text: string) => {
|
||||
replaceRange: (ctx, from: number, to: number, text: string) => {
|
||||
editor.editorView!.dispatch({
|
||||
changes: {
|
||||
insert: text,
|
||||
@@ -59,14 +59,14 @@ export default (editor: Editor) => ({
|
||||
},
|
||||
});
|
||||
},
|
||||
"editor.moveCursor": (pos: number) => {
|
||||
moveCursor: (ctx, pos: number) => {
|
||||
editor.editorView!.dispatch({
|
||||
selection: {
|
||||
anchor: pos,
|
||||
},
|
||||
});
|
||||
},
|
||||
"editor.insertAtCursor": (text: string) => {
|
||||
insertAtCursor: (ctx, text: string) => {
|
||||
let editorView = editor.editorView!;
|
||||
let from = editorView.state.selection.main.from;
|
||||
editorView.dispatch({
|
||||
@@ -79,7 +79,7 @@ export default (editor: Editor) => ({
|
||||
},
|
||||
});
|
||||
},
|
||||
"editor.getSyntaxNodeUnderCursor": (): SyntaxNode | undefined => {
|
||||
getSyntaxNodeUnderCursor: (): SyntaxNode | undefined => {
|
||||
const editorState = editor.editorView!.state;
|
||||
let selection = editorState.selection.main;
|
||||
if (selection.empty) {
|
||||
@@ -94,7 +94,8 @@ export default (editor: Editor) => ({
|
||||
}
|
||||
}
|
||||
},
|
||||
"editor.matchBefore": (
|
||||
matchBefore: (
|
||||
ctx,
|
||||
regexp: string
|
||||
): { from: number; to: number; text: string } | null => {
|
||||
const editorState = editor.editorView!.state;
|
||||
@@ -112,7 +113,7 @@ export default (editor: Editor) => ({
|
||||
}
|
||||
return null;
|
||||
},
|
||||
"editor.getSyntaxNodeAtPos": (pos: number): SyntaxNode | undefined => {
|
||||
getSyntaxNodeAtPos: (ctx, pos: number): SyntaxNode | undefined => {
|
||||
const editorState = editor.editorView!.state;
|
||||
let node = syntaxTree(editorState).resolveInner(pos);
|
||||
if (node) {
|
||||
@@ -124,10 +125,10 @@ export default (editor: Editor) => ({
|
||||
};
|
||||
}
|
||||
},
|
||||
"editor.dispatch": (change: Transaction) => {
|
||||
dispatch: (ctx, change: Transaction) => {
|
||||
editor.editorView!.dispatch(change);
|
||||
},
|
||||
"editor.prompt": (message: string, defaultValue = ""): string | null => {
|
||||
prompt: (ctx, message: string, defaultValue = ""): string | null => {
|
||||
return prompt(message, defaultValue);
|
||||
},
|
||||
});
|
||||
@@ -1,22 +0,0 @@
|
||||
import { Space, KV } from "../space";
|
||||
|
||||
export default (space: Space) => ({
|
||||
"indexer.scanPrefixForPage": async (pageName: string, keyPrefix: string) => {
|
||||
return await space.indexScanPrefixForPage(pageName, keyPrefix);
|
||||
},
|
||||
"indexer.scanPrefixGlobal": async (keyPrefix: string) => {
|
||||
return await space.indexScanPrefixGlobal(keyPrefix);
|
||||
},
|
||||
"indexer.get": async (pageName: string, key: string): Promise<any> => {
|
||||
return await space.indexGet(pageName, key);
|
||||
},
|
||||
"indexer.set": async (pageName: string, key: string, value: any) => {
|
||||
await space.indexSet(pageName, key, value);
|
||||
},
|
||||
"indexer.batchSet": async (pageName: string, kvs: KV[]) => {
|
||||
await space.indexBatchSet(pageName, kvs);
|
||||
},
|
||||
"indexer.delete": async (pageName: string, key: string) => {
|
||||
await space.indexDelete(pageName, key);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Space } from "../space";
|
||||
import { SysCallMapping } from "../../plugbox/system";
|
||||
import { transportSyscalls } from "../../plugbox/syscall/transport";
|
||||
|
||||
export default function indexerSyscalls(space: Space): SysCallMapping {
|
||||
return transportSyscalls(
|
||||
[
|
||||
"scanPrefixForPage",
|
||||
"scanPrefixGlobal",
|
||||
"get",
|
||||
"set",
|
||||
"batchSet",
|
||||
"delete",
|
||||
],
|
||||
(name, ...args) => space.wsCall(`index.${name}`, ...args)
|
||||
);
|
||||
}
|
||||
@@ -1,21 +1,21 @@
|
||||
import { Editor } from "../editor";
|
||||
import { PageMeta } from "../types";
|
||||
import { SysCallMapping } from "../../plugbox/system";
|
||||
|
||||
export default (editor: Editor) => ({
|
||||
"space.listPages": (): PageMeta[] => {
|
||||
export default (editor: Editor): SysCallMapping => ({
|
||||
listPages: (): PageMeta[] => {
|
||||
return [...editor.viewState.allPages];
|
||||
},
|
||||
"space.readPage": async (
|
||||
readPage: async (
|
||||
ctx,
|
||||
name: string
|
||||
): Promise<{ text: string; meta: PageMeta }> => {
|
||||
return await editor.space.readPage(name);
|
||||
},
|
||||
"space.writePage": async (name: string, text: string): Promise<PageMeta> => {
|
||||
writePage: async (ctx, name: string, text: string): Promise<PageMeta> => {
|
||||
return await editor.space.writePage(name, text);
|
||||
},
|
||||
"space.deletePage": async (name: string) => {
|
||||
console.log("Clearing page index", name);
|
||||
await editor.space.indexDeletePrefixForPage(name, "");
|
||||
deletePage: async (ctx, name: string) => {
|
||||
// If we're deleting the current page, navigate to the start page
|
||||
if (editor.currentPage === name) {
|
||||
await editor.navigate("start");
|
||||
@@ -0,0 +1,13 @@
|
||||
import { SysCallMapping } from "../../plugbox/system";
|
||||
import { Space } from "../space";
|
||||
|
||||
export function systemSyscalls(space: Space): SysCallMapping {
|
||||
return {
|
||||
async invokeFunctionOnServer(ctx, name: string, ...args: any[]) {
|
||||
if (!ctx.plug) {
|
||||
throw Error("No plug associated with context");
|
||||
}
|
||||
return await space.wsCall("invokeFunction", ctx.plug.name, name, ...args);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
// @ts-ignore
|
||||
let frameTest = document.getElementById("main-frame");
|
||||
|
||||
window.addEventListener("message", async (event) => {
|
||||
let messageEvent = event as MessageEvent;
|
||||
let data = messageEvent.data;
|
||||
if (data.type === "iframe_event") {
|
||||
// @ts-ignore
|
||||
window.mainPlug.dispatchEvent(data.data.event, data.data.data);
|
||||
}
|
||||
});
|
||||
|
||||
export default {
|
||||
"ui.update": function (doc: any) {
|
||||
// frameTest.contentWindow.postMessage({
|
||||
// type: "loadContent",
|
||||
// doc: doc,
|
||||
// });
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user