silverbullet/plug-api/syscalls/editor.ts

226 lines
5.7 KiB
TypeScript
Raw Normal View History

2024-07-30 23:33:33 +08:00
import type { UploadFile } from "../types.ts";
import { syscall } from "../syscall.ts";
import type { PageRef } from "../lib/page_ref.ts";
import type { FilterOption } from "@silverbulletmd/silverbullet/type/client";
2022-04-01 23:07:08 +08:00
export function getCurrentPage(): Promise<string> {
return syscall("editor.getCurrentPage");
}
2022-04-25 00:06:34 +08:00
export function setPage(newName: string): Promise<void> {
return syscall("editor.setPage", newName);
}
2022-04-01 23:07:08 +08:00
export function getText(): Promise<string> {
return syscall("editor.getText");
}
2024-01-26 18:10:35 +08:00
/**
* This updates the editor text, but in a minimal-diff way:
* it compares the current editor text with the new text, and only sends the changes to the editor, thereby preserving cursor location
*/
2024-07-30 23:24:17 +08:00
export function setText(newText: string): Promise<void> {
2024-01-26 18:10:35 +08:00
return syscall("editor.setText", newText);
}
2022-04-01 23:07:08 +08:00
export function getCursor(): Promise<number> {
return syscall("editor.getCursor");
}
export function getSelection(): Promise<{ from: number; to: number }> {
return syscall("editor.getSelection");
}
export function setSelection(from: number, to: number): Promise<void> {
return syscall("editor.setSelection", from, to);
}
2022-04-01 23:07:08 +08:00
export function save(): Promise<void> {
return syscall("editor.save");
}
export function navigate(
pageRef: PageRef,
replaceState = false,
newWindow = false,
): Promise<void> {
return syscall("editor.navigate", pageRef, replaceState, newWindow);
2022-04-01 23:07:08 +08:00
}
export function openPageNavigator(
mode: "page" | "meta" | "all" = "page",
): Promise<void> {
return syscall("editor.openPageNavigator", mode);
}
export function openCommandPalette(): Promise<void> {
return syscall("editor.openCommandPalette");
}
2022-04-01 23:07:08 +08:00
export function reloadPage(): Promise<void> {
return syscall("editor.reloadPage");
}
2023-07-26 17:22:10 +08:00
export function reloadUI(): Promise<void> {
return syscall("editor.reloadUI");
}
export function reloadConfigAndCommands(): Promise<void> {
return syscall("editor.reloadConfigAndCommands");
}
export function openUrl(url: string, existingWindow = false): Promise<void> {
return syscall("editor.openUrl", url, existingWindow);
2022-04-01 23:07:08 +08:00
}
/**
* This is calling the `go()` method from the History Web API.
* @param delta Position in history to move to relative to the current page,
* where a negative value moves backwards, and positive forwards
*/
export function goHistory(delta: number): Promise<void> {
return syscall("editor.goHistory", delta);
}
// Force the client to download the file in dataUrl with filename as file name
export function downloadFile(filename: string, dataUrl: string): Promise<void> {
return syscall("editor.downloadFile", filename, dataUrl);
}
2023-11-25 20:40:27 +08:00
export function uploadFile(
accept?: string,
capture?: string,
): Promise<UploadFile> {
return syscall("editor.uploadFile", accept, capture);
}
export function flashNotification(
message: string,
type: "info" | "error" = "info",
): Promise<void> {
return syscall("editor.flashNotification", message, type);
2022-04-01 23:07:08 +08:00
}
export function filterBox(
label: string,
options: FilterOption[],
helpText = "",
placeHolder = "",
): Promise<FilterOption | undefined> {
return syscall("editor.filterBox", label, options, helpText, placeHolder);
}
export function showPanel(
id: "lhs" | "rhs" | "bhs" | "modal",
mode: number,
2022-05-09 20:59:12 +08:00
html: string,
script = "",
2022-05-09 20:59:12 +08:00
): Promise<void> {
return syscall("editor.showPanel", id, mode, html, script);
2022-04-27 01:04:36 +08:00
}
export function hidePanel(
id: "lhs" | "rhs" | "bhs" | "modal",
): Promise<void> {
return syscall("editor.hidePanel", id);
2022-04-27 01:04:36 +08:00
}
2022-04-01 23:07:08 +08:00
export function insertAtPos(text: string, pos: number): Promise<void> {
return syscall("editor.insertAtPos", text, pos);
}
export function replaceRange(
from: number,
to: number,
text: string,
2022-04-01 23:07:08 +08:00
): Promise<void> {
return syscall("editor.replaceRange", from, to, text);
}
export function moveCursor(pos: number, center = false): Promise<void> {
return syscall("editor.moveCursor", pos, center);
2022-04-01 23:07:08 +08:00
}
export function moveCursorToLine(
line: number,
column = 1,
center = false,
): Promise<void> {
return syscall("editor.moveCursorToLine", line, column, center);
}
2022-04-01 23:07:08 +08:00
export function insertAtCursor(text: string): Promise<void> {
return syscall("editor.insertAtCursor", text);
}
export function dispatch(change: any): Promise<void> {
return syscall("editor.dispatch", change);
}
export function prompt(
message: string,
defaultValue = "",
2022-04-01 23:07:08 +08:00
): Promise<string | undefined> {
return syscall("editor.prompt", message, defaultValue);
}
2022-09-16 20:26:47 +08:00
export function confirm(
message: string,
): Promise<boolean> {
return syscall("editor.confirm", message);
}
export function getUiOption(key: string): Promise<any> {
return syscall("editor.getUiOption", key);
2022-12-16 19:44:04 +08:00
}
export function setUiOption(key: string, value: any): Promise<void> {
return syscall("editor.setUiOption", key, value);
2022-12-16 19:44:04 +08:00
}
2023-01-24 01:52:17 +08:00
// Vim specific
export function vimEx(exCommand: string): Promise<any> {
return syscall("editor.vimEx", exCommand);
}
2023-06-15 01:27:18 +08:00
// Folding
2024-07-30 23:24:17 +08:00
export function fold(): Promise<void> {
2023-06-15 01:27:18 +08:00
return syscall("editor.fold");
}
2024-07-30 23:24:17 +08:00
export function unfold(): Promise<void> {
2023-06-15 01:27:18 +08:00
return syscall("editor.unfold");
}
2024-07-30 23:24:17 +08:00
export function toggleFold(): Promise<void> {
2023-06-17 15:01:32 +08:00
return syscall("editor.toggleFold");
}
2024-07-30 23:24:17 +08:00
export function foldAll(): Promise<void> {
2023-06-15 01:27:18 +08:00
return syscall("editor.foldAll");
}
2024-07-30 23:24:17 +08:00
export function unfoldAll(): Promise<void> {
2023-06-15 01:27:18 +08:00
return syscall("editor.unfoldAll");
}
2024-01-26 02:46:08 +08:00
2024-03-02 19:14:27 +08:00
// Undo/redo
2024-07-30 23:24:17 +08:00
export function undo(): Promise<void> {
2024-03-02 19:14:27 +08:00
return syscall("editor.undo");
}
2024-07-30 23:24:17 +08:00
export function redo(): Promise<void> {
2024-03-02 19:14:27 +08:00
return syscall("editor.redo");
}
2024-07-30 23:24:17 +08:00
export function openSearchPanel(): Promise<void> {
2024-01-26 02:46:08 +08:00
return syscall("editor.openSearchPanel");
}
2024-02-28 19:16:51 +08:00
export function copyToClipboard(data: string | Blob): Promise<void> {
return syscall("editor.copyToClipboard", data);
}
2024-05-26 05:12:48 +08:00
2024-07-30 23:24:17 +08:00
export function deleteLine(): Promise<void> {
2024-05-26 05:12:48 +08:00
return syscall("editor.deleteLine");
}