silverbullet/web/syscalls/editor.ts

344 lines
9.6 KiB
TypeScript
Raw Normal View History

2024-07-30 23:33:33 +08:00
import type { Client } from "../client.ts";
2023-06-15 01:27:18 +08:00
import {
foldAll,
foldCode,
2023-06-17 15:01:32 +08:00
toggleFold,
2023-06-15 01:27:18 +08:00
unfoldAll,
unfoldCode,
} from "@codemirror/language";
import { deleteLine, isolateHistory, redo, undo } from "@codemirror/commands";
2024-07-30 23:33:33 +08:00
import type { Transaction } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { getCM as vimGetCm, Vim } from "@replit/codemirror-vim";
2024-07-30 23:33:33 +08:00
import type { SysCallMapping } from "$lib/plugos/system.ts";
import type { FilterOption } from "@silverbulletmd/silverbullet/type/client";
2025-01-09 00:09:09 +08:00
import type { PageMeta, UploadFile } from "../../plug-api/types.ts";
import type { PageRef } from "@silverbulletmd/silverbullet/lib/page_ref";
import { openSearchPanel } from "@codemirror/search";
import { diffAndPrepareChanges } from "../cm_util.ts";
export function editorSyscalls(client: Client): SysCallMapping {
const syscalls: SysCallMapping = {
"editor.getCurrentPage": (): string => {
2024-01-24 21:44:39 +08:00
return client.currentPage;
2022-04-04 00:12:16 +08:00
},
2025-01-09 00:09:09 +08:00
"editor.getCurrentPageMeta": (): PageMeta | undefined => {
return client.ui.viewState.currentPageMeta;
},
"editor.getText": () => {
return client.editorView.state.sliceDoc();
2022-04-04 00:12:16 +08:00
},
"editor.setText": (_ctx, newText: string, shouldIsolateHistory = false) => {
2024-01-26 18:10:35 +08:00
const currentText = client.editorView.state.sliceDoc();
const allChanges = diffAndPrepareChanges(currentText, newText);
client.editorView.dispatch({
changes: allChanges,
annotations: shouldIsolateHistory
? isolateHistory.of("full")
: undefined,
2024-01-26 18:10:35 +08:00
});
},
"editor.getCursor": (): number => {
return client.editorView.state.selection.main.from;
2022-04-04 00:12:16 +08:00
},
"editor.getSelection": (): { from: number; to: number } => {
return client.editorView.state.selection.main;
},
2022-10-16 01:02:56 +08:00
"editor.save": () => {
return client.save(true);
2022-04-04 00:12:16 +08:00
},
"editor.navigate": async (
2022-10-16 01:02:56 +08:00
_ctx,
pageRef: PageRef | string,
replaceState = false,
newWindow = false,
) => {
if (typeof pageRef === "string") {
pageRef = { page: pageRef };
}
await client.navigate(pageRef, replaceState, newWindow);
2022-04-04 00:12:16 +08:00
},
2022-10-11 00:19:08 +08:00
"editor.reloadPage": async () => {
await client.reloadPage();
2022-04-04 00:12:16 +08:00
},
2023-07-26 17:22:10 +08:00
"editor.reloadUI": () => {
location.reload();
},
"editor.reloadConfigAndCommands": async () => {
await client.loadConfig();
await client.clientSystem.system.localSyscall(
"system.loadSpaceScripts",
[],
);
await client.clientSystem.system.localSyscall(
"system.loadSpaceStyles",
[],
);
},
"editor.openUrl": (_ctx, url: string, existingWindow = false) => {
if (!existingWindow) {
2024-07-30 21:17:34 +08:00
const win = globalThis.open(url, "_blank");
if (win) {
win.focus();
}
} else {
location.href = url;
2022-05-09 16:45:36 +08:00
}
2022-04-04 00:12:16 +08:00
},
2024-09-10 20:38:41 +08:00
"editor.newWindow": () => {
globalThis.open(
location.href,
"rnd" + Math.random(),
`width=${globalThis.innerWidth},heigh=${globalThis.innerHeight}`,
);
},
"editor.goHistory": (_ctx, delta: number) => {
2024-10-10 18:52:28 +08:00
globalThis.history.go(delta);
},
"editor.downloadFile": (_ctx, filename: string, dataUrl: string) => {
const link = document.createElement("a");
link.href = dataUrl;
link.download = filename;
link.click();
},
"editor.uploadFile": (
_ctx,
accept?: string,
2023-11-22 22:51:44 +08:00
capture?: string,
): Promise<UploadFile> => {
return new Promise<UploadFile>((resolve, reject) => {
const input = document.createElement("input");
input.type = "file";
if (accept) {
input.accept = accept;
}
if (capture) {
input.capture = capture;
}
input.onchange = () => {
const file = input.files?.item(0);
if (!file) {
reject(new Error("No file found"));
} else {
2023-11-22 22:51:44 +08:00
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onloadend = async (evt) => {
if (evt.target?.readyState == FileReader.DONE) {
resolve({
name: file.name,
contentType: file.type,
2023-11-22 22:51:44 +08:00
content: new Uint8Array(await file.arrayBuffer()),
});
}
};
2023-11-22 22:51:44 +08:00
reader.onabort = (e) => {
reject(e);
};
reader.onerror = (e) => {
reject(e);
};
}
2023-11-22 22:51:44 +08:00
};
input.onabort = (e) => {
reject(e);
};
input.click();
});
},
"editor.flashNotification": (
2022-10-16 01:02:56 +08:00
_ctx,
message: string,
type: "error" | "info" = "info",
) => {
client.flashNotification(message, type);
2022-04-04 00:12:16 +08:00
},
"editor.filterBox": (
2022-10-16 01:02:56 +08:00
_ctx,
label: string,
options: FilterOption[],
2022-10-16 01:02:56 +08:00
helpText = "",
placeHolder = "",
): Promise<FilterOption | undefined> => {
return client.filterBox(label, options, helpText, placeHolder);
},
"editor.showPanel": (
2022-10-16 01:02:56 +08:00
_ctx,
id: string,
mode: number,
html: string,
script: string,
) => {
client.ui.viewDispatch({
type: "show-panel",
id: id as any,
config: { html, script, mode },
2022-04-04 00:12:16 +08:00
});
setTimeout(() => {
// Dummy dispatch to rerender the editor and toggle the panel
client.editorView.dispatch({});
});
2022-04-04 00:12:16 +08:00
},
2022-10-16 01:02:56 +08:00
"editor.hidePanel": (_ctx, id: string) => {
client.ui.viewDispatch({
type: "hide-panel",
id: id as any,
2022-04-04 21:25:07 +08:00
});
setTimeout(() => {
// Dummy dispatch to rerender the editor and toggle the panel
client.editorView.dispatch({});
});
2022-04-04 21:25:07 +08:00
},
2022-10-16 01:02:56 +08:00
"editor.insertAtPos": (_ctx, text: string, pos: number) => {
client.editorView.dispatch({
2022-04-04 00:12:16 +08:00
changes: {
insert: text,
from: pos,
},
});
},
2022-10-16 01:02:56 +08:00
"editor.replaceRange": (_ctx, from: number, to: number, text: string) => {
client.editorView.dispatch({
2022-04-04 00:12:16 +08:00
changes: {
insert: text,
from: from,
to: to,
},
});
},
"editor.moveCursor": (_ctx, pos: number, center = false) => {
client.editorView.dispatch({
2022-04-04 00:12:16 +08:00
selection: {
anchor: pos,
},
});
if (center) {
client.editorView.dispatch({
effects: [
EditorView.scrollIntoView(
pos,
{
y: "center",
},
),
],
});
}
client.editorView.focus();
2022-04-04 00:12:16 +08:00
},
"editor.moveCursorToLine": (
_ctx,
line: number,
column = 1,
center = false,
) => {
// CodeMirror already keeps information about lines
const cmLine = client.editorView.state.doc.line(line);
// How much to move inside the line, column number starts from 1
const offset = Math.max(0, Math.min(cmLine.length, column - 1));
// Just reuse the implementation above
syscalls["editor.moveCursor"](_ctx, cmLine.from + offset, center);
},
2022-10-16 01:02:56 +08:00
"editor.setSelection": (_ctx, from: number, to: number) => {
client.editorView.dispatch({
selection: {
anchor: from,
head: to,
},
});
},
2022-10-16 01:02:56 +08:00
"editor.insertAtCursor": (_ctx, text: string) => {
const editorView = client.editorView;
2022-10-16 01:02:56 +08:00
const from = editorView.state.selection.main.from;
2022-04-04 00:12:16 +08:00
editorView.dispatch({
changes: {
insert: text,
from: from,
},
selection: {
anchor: from + text.length,
},
});
},
2022-10-16 01:02:56 +08:00
"editor.dispatch": (_ctx, change: Transaction) => {
client.editorView.dispatch(change);
2022-04-04 00:12:16 +08:00
},
"editor.prompt": (
2022-10-16 01:02:56 +08:00
_ctx,
message: string,
defaultValue = "",
2022-12-21 23:08:51 +08:00
): Promise<string | undefined> => {
return client.prompt(message, defaultValue);
2022-04-04 00:12:16 +08:00
},
"editor.confirm": (_ctx, message: string): Promise<boolean> => {
return client.confirm(message);
},
"editor.getUiOption": (_ctx, key: string): any => {
return (client.ui.viewState.uiOptions as any)[key];
},
"editor.setUiOption": (_ctx, key: string, value: any) => {
client.ui.viewDispatch({
type: "set-ui-option",
key,
value,
2022-09-16 20:26:47 +08:00
});
},
2023-01-24 01:52:17 +08:00
"editor.vimEx": (_ctx, exCommand: string) => {
const cm = vimGetCm(client.editorView)!;
2023-01-24 01:52:17 +08:00
return Vim.handleEx(cm, exCommand);
},
"editor.openPageNavigator": (
_ctx,
mode: "page" | "meta" | "all" = "page",
) => {
client.startPageNavigate(mode);
},
"editor.openCommandPalette": () => {
client.ui.viewDispatch({
type: "show-palette",
context: client.getContext(),
});
},
2024-05-26 05:12:48 +08:00
"editor.deleteLine": () => {
deleteLine(client.editorView);
},
2023-06-15 01:27:18 +08:00
// Folding
"editor.fold": () => {
foldCode(client.editorView);
2023-06-15 01:27:18 +08:00
},
"editor.unfold": () => {
unfoldCode(client.editorView);
2023-06-15 01:27:18 +08:00
},
2023-06-17 15:01:32 +08:00
"editor.toggleFold": () => {
toggleFold(client.editorView);
2023-06-17 15:01:32 +08:00
},
2023-06-15 01:27:18 +08:00
"editor.foldAll": () => {
foldAll(client.editorView);
2023-06-15 01:27:18 +08:00
},
"editor.unfoldAll": () => {
unfoldAll(client.editorView);
2023-06-15 01:27:18 +08:00
},
2024-03-02 19:14:27 +08:00
"editor.undo": () => {
return undo(client.editorView);
},
"editor.redo": () => {
return redo(client.editorView);
},
2024-01-26 02:46:08 +08:00
"editor.openSearchPanel": () => {
openSearchPanel(client.editorView);
},
2024-02-28 19:16:51 +08:00
"editor.copyToClipboard": (_ctx, data: string | Blob) => {
if (typeof data === "string") {
navigator.clipboard.writeText(data);
} else {
navigator.clipboard.write([new ClipboardItem({ [data.type]: data })]);
}
},
2022-04-04 00:12:16 +08:00
};
return syscalls;
2022-04-04 00:12:16 +08:00
}