1
0
silverbullet/packages/web/syscalls/editor.ts

197 lines
5.1 KiB
TypeScript
Raw Normal View History

2022-04-05 15:02:17 +00:00
import { Editor } from "../editor";
import { SelectionRange, Transaction } from "@codemirror/state";
2022-04-25 08:33:38 +00:00
import { SysCallMapping } from "@plugos/plugos/system";
import { FilterOption } from "@silverbulletmd/common/types";
type SyntaxNode = {
name: string;
text: string;
from: number;
to: number;
};
function ensureAnchor(expr: any, start: boolean) {
var _a;
let { source } = expr;
let addStart = start && source[0] != "^",
addEnd = source[source.length - 1] != "$";
if (!addStart && !addEnd) return expr;
return new RegExp(
`${addStart ? "^" : ""}(?:${source})${addEnd ? "$" : ""}`,
(_a = expr.flags) !== null && _a !== void 0
? _a
: expr.ignoreCase
? "i"
: ""
);
}
2022-04-03 16:12:16 +00:00
export function editorSyscalls(editor: Editor): SysCallMapping {
return {
"editor.getCurrentPage": (): string => {
2022-04-03 16:12:16 +00:00
return editor.currentPage!;
},
"editor.getText": () => {
2022-04-03 16:12:16 +00:00
return editor.editorView?.state.sliceDoc();
},
"editor.getCursor": (): number => {
2022-04-03 16:12:16 +00:00
return editor.editorView!.state.selection.main.from;
},
"editor.getSelection": (): { from: number; to: number } => {
return editor.editorView!.state.selection.main;
},
"editor.save": async () => {
2022-04-03 16:12:16 +00:00
return editor.save(true);
},
"editor.navigate": async (
ctx,
name: string,
2022-08-30 08:44:20 +00:00
pos: number | string,
replaceState = false
) => {
await editor.navigate(name, pos, replaceState);
2022-04-03 16:12:16 +00:00
},
"editor.reloadPage": async (ctx) => {
2022-04-03 16:12:16 +00:00
await editor.reloadPage();
},
"editor.openUrl": async (ctx, url: string) => {
2022-05-09 08:45:36 +00:00
let win = window.open(url, "_blank");
if (win) {
win.focus();
}
2022-04-03 16:12:16 +00:00
},
"editor.flashNotification": (
ctx,
message: string,
type: "error" | "info" = "info"
) => {
editor.flashNotification(message, type);
2022-04-03 16:12:16 +00:00
},
"editor.filterBox": (
ctx,
label: string,
options: FilterOption[],
helpText: string = "",
placeHolder: string = ""
): Promise<FilterOption | undefined> => {
return editor.filterBox(label, options, helpText, placeHolder);
},
2022-05-09 12:59:12 +00:00
"editor.showRhs": (ctx, html: string, script: string, flex: number) => {
2022-04-03 16:12:16 +00:00
editor.viewDispatch({
type: "show-rhs",
2022-04-04 16:33:13 +00:00
flex,
html,
2022-05-09 12:59:12 +00:00
script,
2022-04-03 16:12:16 +00:00
});
},
2022-04-04 16:33:13 +00:00
"editor.hideRhs": (ctx) => {
2022-04-04 13:25:07 +00:00
editor.viewDispatch({
type: "hide-rhs",
});
},
2022-05-09 12:59:12 +00:00
"editor.showLhs": (ctx, html: string, script: string, flex: number) => {
2022-04-04 16:33:13 +00:00
editor.viewDispatch({
type: "show-lhs",
flex,
html,
2022-05-09 12:59:12 +00:00
script,
2022-04-04 16:33:13 +00:00
});
},
"editor.hideLhs": (ctx) => {
editor.viewDispatch({
type: "hide-lhs",
});
},
2022-05-09 12:59:12 +00:00
"editor.showBhs": (ctx, html: string, script: string, flex: number) => {
2022-04-26 17:04:36 +00:00
editor.viewDispatch({
type: "show-bhs",
flex,
html,
2022-05-09 12:59:12 +00:00
script,
2022-04-26 17:04:36 +00:00
});
},
"editor.hideBhs": (ctx) => {
editor.viewDispatch({
type: "hide-bhs",
});
},
"editor.insertAtPos": (ctx, text: string, pos: number) => {
2022-04-03 16:12:16 +00:00
editor.editorView!.dispatch({
changes: {
insert: text,
from: pos,
},
});
},
"editor.replaceRange": (ctx, from: number, to: number, text: string) => {
2022-04-03 16:12:16 +00:00
editor.editorView!.dispatch({
changes: {
insert: text,
from: from,
to: to,
},
});
},
"editor.moveCursor": (ctx, pos: number) => {
2022-04-03 16:12:16 +00:00
editor.editorView!.dispatch({
selection: {
anchor: pos,
},
});
},
"editor.setSelection": (ctx, from: number, to: number) => {
let editorView = editor.editorView!;
editorView.dispatch({
selection: {
anchor: from,
head: to,
},
});
},
"editor.insertAtCursor": (ctx, text: string) => {
2022-04-03 16:12:16 +00:00
let editorView = editor.editorView!;
let from = editorView.state.selection.main.from;
editorView.dispatch({
changes: {
insert: text,
from: from,
},
selection: {
anchor: from + text.length,
},
});
},
2022-04-04 13:25:07 +00:00
"editor.matchBefore": (
2022-04-03 16:12:16 +00:00
ctx,
regexp: string
): { from: number; to: number; text: string } | null => {
const editorState = editor.editorView!.state;
let selection = editorState.selection.main;
let from = selection.from;
if (selection.empty) {
let line = editorState.doc.lineAt(from);
let start = Math.max(line.from, from - 250);
let str = line.text.slice(start - line.from, from - line.from);
let found = str.search(ensureAnchor(new RegExp(regexp), false));
// console.log("Line", line, start, str, new RegExp(regexp), found);
return found < 0
? null
: { from: start + found, to: from, text: str.slice(found) };
}
return null;
},
"editor.dispatch": (ctx, change: Transaction) => {
2022-04-03 16:12:16 +00:00
editor.editorView!.dispatch(change);
},
"editor.prompt": (
ctx,
message: string,
defaultValue = ""
): string | null => {
2022-04-03 16:12:16 +00:00
return prompt(message, defaultValue);
},
};
}