1
0
silverbullet/web/syscalls/editor.ts

198 lines
5.1 KiB
TypeScript
Raw Normal View History

import { Editor } from "../editor.tsx";
import { EditorView, Transaction } from "../deps.ts";
import { SysCallMapping } from "../../plugos/system.ts";
import { FilterOption } from "../../common/types.ts";
type SyntaxNode = {
name: string;
text: string;
from: number;
to: number;
};
function ensureAnchor(expr: any, start: boolean) {
2022-10-15 17:02:56 +00:00
let _a;
const { source } = expr;
const 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 {
const syscalls: SysCallMapping = {
"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;
},
2022-10-15 17:02:56 +00:00
"editor.save": () => {
2022-04-03 16:12:16 +00:00
return editor.save(true);
},
"editor.navigate": async (
2022-10-15 17:02:56 +00:00
_ctx,
name: string,
2022-08-30 08:44:20 +00:00
pos: number | string,
replaceState = false,
newWindow = false,
) => {
await editor.navigate(name, pos, replaceState, newWindow);
2022-04-03 16:12:16 +00:00
},
2022-10-10 16:19:08 +00:00
"editor.reloadPage": async () => {
2022-04-03 16:12:16 +00:00
await editor.reloadPage();
},
2022-10-14 13:11:33 +00:00
"editor.openUrl": (_ctx, url: string) => {
2022-10-15 17:02:56 +00:00
const win = window.open(url, "_blank");
2022-05-09 08:45:36 +00:00
if (win) {
win.focus();
}
2022-04-03 16:12:16 +00:00
},
"editor.flashNotification": (
2022-10-15 17:02:56 +00:00
_ctx,
message: string,
type: "error" | "info" = "info",
) => {
editor.flashNotification(message, type);
2022-04-03 16:12:16 +00:00
},
"editor.filterBox": (
2022-10-15 17:02:56 +00:00
_ctx,
label: string,
options: FilterOption[],
2022-10-15 17:02:56 +00:00
helpText = "",
placeHolder = "",
): Promise<FilterOption | undefined> => {
return editor.filterBox(label, options, helpText, placeHolder);
},
"editor.showPanel": (
2022-10-15 17:02:56 +00:00
_ctx,
id: string,
mode: number,
html: string,
script: string,
) => {
2022-04-03 16:12:16 +00:00
editor.viewDispatch({
type: "show-panel",
id: id as any,
config: { html, script, mode },
2022-04-03 16:12:16 +00:00
});
},
2022-10-15 17:02:56 +00:00
"editor.hidePanel": (_ctx, id: string) => {
2022-04-04 13:25:07 +00:00
editor.viewDispatch({
type: "hide-panel",
id: id as any,
2022-04-04 13:25:07 +00:00
});
},
2022-10-15 17:02:56 +00:00
"editor.insertAtPos": (_ctx, text: string, pos: number) => {
2022-04-03 16:12:16 +00:00
editor.editorView!.dispatch({
changes: {
insert: text,
from: pos,
},
});
},
2022-10-15 17:02:56 +00:00
"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, center = false) => {
2022-04-03 16:12:16 +00:00
editor.editorView!.dispatch({
selection: {
anchor: pos,
},
});
if (center) {
editor.editorView!.dispatch({
effects: [
EditorView.scrollIntoView(
pos,
{
y: "center",
},
),
],
});
}
2022-04-03 16:12:16 +00:00
},
2022-10-15 17:02:56 +00:00
"editor.setSelection": (_ctx, from: number, to: number) => {
const editorView = editor.editorView!;
editorView.dispatch({
selection: {
anchor: from,
head: to,
},
});
},
2022-10-15 17:02:56 +00:00
"editor.insertAtCursor": (_ctx, text: string) => {
const editorView = editor.editorView!;
const from = editorView.state.selection.main.from;
2022-04-03 16:12:16 +00:00
editorView.dispatch({
changes: {
insert: text,
from: from,
},
selection: {
anchor: from + text.length,
},
});
},
2022-04-04 13:25:07 +00:00
"editor.matchBefore": (
2022-10-15 17:02:56 +00:00
_ctx,
regexp: string,
2022-04-03 16:12:16 +00:00
): { from: number; to: number; text: string } | null => {
const editorState = editor.editorView!.state;
2022-10-15 17:02:56 +00:00
const selection = editorState.selection.main;
const from = selection.from;
2022-04-03 16:12:16 +00:00
if (selection.empty) {
2022-10-15 17:02:56 +00:00
const line = editorState.doc.lineAt(from);
const start = Math.max(line.from, from - 250);
const str = line.text.slice(start - line.from, from - line.from);
const found = str.search(ensureAnchor(new RegExp(regexp), false));
2022-04-03 16:12:16 +00:00
// 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;
},
2022-10-15 17:02:56 +00:00
"editor.dispatch": (_ctx, change: Transaction) => {
2022-04-03 16:12:16 +00:00
editor.editorView!.dispatch(change);
},
"editor.prompt": (
2022-10-15 17:02:56 +00:00
_ctx,
message: string,
defaultValue = "",
): string | null => {
2022-04-03 16:12:16 +00:00
return prompt(message, defaultValue);
},
2022-10-15 17:02:56 +00:00
"editor.enableReadOnlyMode": (_ctx, enabled: boolean) => {
2022-09-16 12:26:47 +00:00
editor.viewDispatch({
type: "set-editor-ro",
enabled,
});
},
2022-04-03 16:12:16 +00:00
};
return syscalls;
2022-04-03 16:12:16 +00:00
}