2023-07-14 14:56:20 +00:00
|
|
|
import { Client } from "../client.ts";
|
2023-06-14 17:27:18 +00:00
|
|
|
import {
|
|
|
|
EditorView,
|
|
|
|
foldAll,
|
|
|
|
foldCode,
|
2023-06-17 07:01:32 +00:00
|
|
|
toggleFold,
|
2023-06-14 17:27:18 +00:00
|
|
|
Transaction,
|
|
|
|
unfoldAll,
|
|
|
|
unfoldCode,
|
|
|
|
Vim,
|
|
|
|
vimGetCm,
|
|
|
|
} from "../deps.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { SysCallMapping } from "../../plugos/system.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import type { FilterOption } from "../types.ts";
|
2023-11-22 14:33:25 +00:00
|
|
|
import { UploadFile } from "../../plug-api/types.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2023-07-14 14:56:20 +00:00
|
|
|
export function editorSyscalls(editor: Client): SysCallMapping {
|
2022-09-30 14:59:57 +00:00
|
|
|
const syscalls: SysCallMapping = {
|
2022-04-03 16:42:12 +00:00
|
|
|
"editor.getCurrentPage": (): string => {
|
2022-04-03 16:12:16 +00:00
|
|
|
return editor.currentPage!;
|
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"editor.getText": () => {
|
2023-07-27 09:41:44 +00:00
|
|
|
return editor.editorView.state.sliceDoc();
|
2022-04-03 16:12:16 +00:00
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"editor.getCursor": (): number => {
|
2023-07-27 09:41:44 +00:00
|
|
|
return editor.editorView.state.selection.main.from;
|
2022-04-03 16:12:16 +00:00
|
|
|
},
|
2022-06-23 15:08:42 +00:00
|
|
|
"editor.getSelection": (): { from: number; to: number } => {
|
2023-07-27 09:41:44 +00:00
|
|
|
return editor.editorView.state.selection.main;
|
2022-06-23 15:08:42 +00:00
|
|
|
},
|
2022-10-15 17:02:56 +00:00
|
|
|
"editor.save": () => {
|
2022-04-03 16:12:16 +00:00
|
|
|
return editor.save(true);
|
|
|
|
},
|
2022-08-01 13:06:32 +00:00
|
|
|
"editor.navigate": async (
|
2022-10-15 17:02:56 +00:00
|
|
|
_ctx,
|
2022-08-01 13:06:32 +00:00
|
|
|
name: string,
|
2022-08-30 08:44:20 +00:00
|
|
|
pos: number | string,
|
2022-10-10 12:50:21 +00:00
|
|
|
replaceState = false,
|
2022-10-29 07:23:12 +00:00
|
|
|
newWindow = false,
|
2022-08-01 13:06:32 +00:00
|
|
|
) => {
|
2022-10-29 07:23:12 +00:00
|
|
|
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();
|
|
|
|
},
|
2023-07-26 09:22:10 +00:00
|
|
|
"editor.reloadUI": () => {
|
|
|
|
location.reload();
|
|
|
|
},
|
2023-06-13 18:47:05 +00:00
|
|
|
"editor.openUrl": (_ctx, url: string, existingWindow = false) => {
|
|
|
|
if (!existingWindow) {
|
|
|
|
const win = window.open(url, "_blank");
|
|
|
|
if (win) {
|
|
|
|
win.focus();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
location.href = url;
|
2022-05-09 08:45:36 +00:00
|
|
|
}
|
2022-04-03 16:12:16 +00:00
|
|
|
},
|
2023-01-08 14:29:34 +00:00
|
|
|
"editor.downloadFile": (_ctx, filename: string, dataUrl: string) => {
|
|
|
|
const link = document.createElement("a");
|
|
|
|
link.href = dataUrl;
|
|
|
|
link.download = filename;
|
|
|
|
link.click();
|
|
|
|
},
|
2023-11-22 14:33:25 +00:00
|
|
|
"editor.uploadFile": (
|
|
|
|
_ctx,
|
|
|
|
accept?: string,
|
2023-11-22 14:51:44 +00:00
|
|
|
capture?: string,
|
2023-11-22 14:33:25 +00:00
|
|
|
): 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 14:51:44 +00:00
|
|
|
const reader = new FileReader();
|
2023-11-22 14:33:25 +00:00
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
reader.onloadend = async (evt) => {
|
|
|
|
if (evt.target?.readyState == FileReader.DONE) {
|
|
|
|
resolve({
|
|
|
|
name: file.name,
|
2023-11-23 11:09:48 +00:00
|
|
|
contentType: file.type,
|
2023-11-22 14:51:44 +00:00
|
|
|
content: new Uint8Array(await file.arrayBuffer()),
|
2023-11-22 14:33:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2023-11-22 14:51:44 +00:00
|
|
|
reader.onabort = (e) => {
|
|
|
|
reject(e);
|
|
|
|
};
|
|
|
|
reader.onerror = (e) => {
|
|
|
|
reject(e);
|
|
|
|
};
|
2023-11-22 14:33:25 +00:00
|
|
|
}
|
2023-11-22 14:51:44 +00:00
|
|
|
};
|
|
|
|
input.onabort = (e) => {
|
|
|
|
reject(e);
|
|
|
|
};
|
2023-11-22 14:33:25 +00:00
|
|
|
|
|
|
|
input.click();
|
|
|
|
});
|
|
|
|
},
|
2022-07-14 11:32:28 +00:00
|
|
|
"editor.flashNotification": (
|
2022-10-15 17:02:56 +00:00
|
|
|
_ctx,
|
2022-07-14 11:32:28 +00:00
|
|
|
message: string,
|
2022-10-10 12:50:21 +00:00
|
|
|
type: "error" | "info" = "info",
|
2022-07-14 11:32:28 +00:00
|
|
|
) => {
|
|
|
|
editor.flashNotification(message, type);
|
2022-04-03 16:12:16 +00:00
|
|
|
},
|
2022-04-13 12:46:52 +00:00
|
|
|
"editor.filterBox": (
|
2022-10-15 17:02:56 +00:00
|
|
|
_ctx,
|
2022-04-13 12:46:52 +00:00
|
|
|
label: string,
|
|
|
|
options: FilterOption[],
|
2022-10-15 17:02:56 +00:00
|
|
|
helpText = "",
|
|
|
|
placeHolder = "",
|
2022-04-13 12:46:52 +00:00
|
|
|
): Promise<FilterOption | undefined> => {
|
|
|
|
return editor.filterBox(label, options, helpText, placeHolder);
|
|
|
|
},
|
2022-09-30 14:59:57 +00:00
|
|
|
"editor.showPanel": (
|
2022-10-15 17:02:56 +00:00
|
|
|
_ctx,
|
2022-09-30 14:59:57 +00:00
|
|
|
id: string,
|
|
|
|
mode: number,
|
|
|
|
html: string,
|
2022-10-10 12:50:21 +00:00
|
|
|
script: string,
|
2022-09-30 14:59:57 +00:00
|
|
|
) => {
|
2023-07-14 12:22:26 +00:00
|
|
|
editor.ui.viewDispatch({
|
2022-09-30 14:59:57 +00:00
|
|
|
type: "show-panel",
|
|
|
|
id: id as any,
|
|
|
|
config: { html, script, mode },
|
2022-04-03 16:12:16 +00:00
|
|
|
});
|
2023-10-03 12:16:33 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
// Dummy dispatch to rerender the editor and toggle the panel
|
|
|
|
editor.editorView.dispatch({});
|
|
|
|
});
|
2022-04-03 16:12:16 +00:00
|
|
|
},
|
2022-10-15 17:02:56 +00:00
|
|
|
"editor.hidePanel": (_ctx, id: string) => {
|
2023-07-14 12:22:26 +00:00
|
|
|
editor.ui.viewDispatch({
|
2022-09-30 14:59:57 +00:00
|
|
|
type: "hide-panel",
|
|
|
|
id: id as any,
|
2022-04-04 13:25:07 +00:00
|
|
|
});
|
2023-10-03 12:16:33 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
// Dummy dispatch to rerender the editor and toggle the panel
|
|
|
|
editor.editorView.dispatch({});
|
|
|
|
});
|
2022-04-04 13:25:07 +00:00
|
|
|
},
|
2022-10-15 17:02:56 +00:00
|
|
|
"editor.insertAtPos": (_ctx, text: string, pos: number) => {
|
2023-07-27 09:41:44 +00:00
|
|
|
editor.editorView.dispatch({
|
2022-04-03 16:12:16 +00:00
|
|
|
changes: {
|
|
|
|
insert: text,
|
|
|
|
from: pos,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2022-10-15 17:02:56 +00:00
|
|
|
"editor.replaceRange": (_ctx, from: number, to: number, text: string) => {
|
2023-07-27 09:41:44 +00:00
|
|
|
editor.editorView.dispatch({
|
2022-04-03 16:12:16 +00:00
|
|
|
changes: {
|
|
|
|
insert: text,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2022-11-01 14:01:28 +00:00
|
|
|
"editor.moveCursor": (_ctx, pos: number, center = false) => {
|
2023-07-27 09:41:44 +00:00
|
|
|
editor.editorView.dispatch({
|
2022-04-03 16:12:16 +00:00
|
|
|
selection: {
|
|
|
|
anchor: pos,
|
|
|
|
},
|
|
|
|
});
|
2022-11-01 14:01:28 +00:00
|
|
|
if (center) {
|
2023-07-27 09:41:44 +00:00
|
|
|
editor.editorView.dispatch({
|
2022-11-01 14:01:28 +00:00
|
|
|
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) => {
|
2023-07-27 09:41:44 +00:00
|
|
|
editor.editorView.dispatch({
|
2022-06-23 15:08:42 +00:00
|
|
|
selection: {
|
|
|
|
anchor: from,
|
|
|
|
head: to,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-10-15 17:02:56 +00:00
|
|
|
"editor.insertAtCursor": (_ctx, text: string) => {
|
2023-07-27 09:41:44 +00:00
|
|
|
const editorView = editor.editorView;
|
2022-10-15 17:02:56 +00:00
|
|
|
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-10-15 17:02:56 +00:00
|
|
|
"editor.dispatch": (_ctx, change: Transaction) => {
|
2023-07-27 09:41:44 +00:00
|
|
|
editor.editorView.dispatch(change);
|
2022-04-03 16:12:16 +00:00
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"editor.prompt": (
|
2022-10-15 17:02:56 +00:00
|
|
|
_ctx,
|
2022-04-03 16:42:12 +00:00
|
|
|
message: string,
|
2022-10-10 12:50:21 +00:00
|
|
|
defaultValue = "",
|
2022-12-21 15:08:51 +00:00
|
|
|
): Promise<string | undefined> => {
|
|
|
|
return editor.prompt(message, defaultValue);
|
2022-04-03 16:12:16 +00:00
|
|
|
},
|
2022-11-18 15:04:37 +00:00
|
|
|
"editor.confirm": (
|
|
|
|
_ctx,
|
|
|
|
message: string,
|
2022-12-21 15:08:51 +00:00
|
|
|
): Promise<boolean> => {
|
|
|
|
return editor.confirm(message);
|
2022-11-18 15:04:37 +00:00
|
|
|
},
|
2022-12-21 13:55:24 +00:00
|
|
|
"editor.getUiOption": (_ctx, key: string): any => {
|
2023-07-14 12:22:26 +00:00
|
|
|
return (editor.ui.viewState.uiOptions as any)[key];
|
2022-12-21 13:55:24 +00:00
|
|
|
},
|
|
|
|
"editor.setUiOption": (_ctx, key: string, value: any) => {
|
2023-07-14 12:22:26 +00:00
|
|
|
editor.ui.viewDispatch({
|
2022-12-21 13:55:24 +00:00
|
|
|
type: "set-ui-option",
|
|
|
|
key,
|
|
|
|
value,
|
2022-09-16 12:26:47 +00:00
|
|
|
});
|
|
|
|
},
|
2023-01-23 17:52:17 +00:00
|
|
|
"editor.vimEx": (_ctx, exCommand: string) => {
|
2023-07-27 09:41:44 +00:00
|
|
|
const cm = vimGetCm(editor.editorView)!;
|
2023-01-23 17:52:17 +00:00
|
|
|
return Vim.handleEx(cm, exCommand);
|
|
|
|
},
|
2023-06-14 17:27:18 +00:00
|
|
|
// Folding
|
|
|
|
"editor.fold": () => {
|
2023-07-27 09:41:44 +00:00
|
|
|
foldCode(editor.editorView);
|
2023-06-14 17:27:18 +00:00
|
|
|
},
|
|
|
|
"editor.unfold": () => {
|
2023-07-27 09:41:44 +00:00
|
|
|
unfoldCode(editor.editorView);
|
2023-06-14 17:27:18 +00:00
|
|
|
},
|
2023-06-17 07:01:32 +00:00
|
|
|
"editor.toggleFold": () => {
|
2023-07-27 09:41:44 +00:00
|
|
|
toggleFold(editor.editorView);
|
2023-06-17 07:01:32 +00:00
|
|
|
},
|
2023-06-14 17:27:18 +00:00
|
|
|
"editor.foldAll": () => {
|
2023-07-27 09:41:44 +00:00
|
|
|
foldAll(editor.editorView);
|
2023-06-14 17:27:18 +00:00
|
|
|
},
|
|
|
|
"editor.unfoldAll": () => {
|
2023-07-27 09:41:44 +00:00
|
|
|
unfoldAll(editor.editorView);
|
2023-06-14 17:27:18 +00:00
|
|
|
},
|
2022-04-03 16:12:16 +00:00
|
|
|
};
|
2022-09-30 14:59:57 +00:00
|
|
|
|
|
|
|
return syscalls;
|
2022-04-03 16:12:16 +00:00
|
|
|
}
|