silverbullet/plug-api/syscalls/editor.ts

408 lines
10 KiB
TypeScript
Raw Permalink Normal View History

2025-01-09 00:09:09 +08:00
import type { PageMeta, 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
2024-08-07 19:27:25 +08:00
/**
* Exposes various editor utilities.
* Important: These syscalls are only available in the client.
2024-08-09 15:27:58 +08:00
* @module
2024-08-07 19:27:25 +08:00
*/
/**
* Returns the name of the page currently open in the editor.
* @returns the current page name
*/
2022-04-01 23:07:08 +08:00
export function getCurrentPage(): Promise<string> {
return syscall("editor.getCurrentPage");
}
2025-01-09 00:09:09 +08:00
/**
* Returns the meta data of the page currently open in the editor.
* @returns the current page meta data
*/
export function getCurrentPageMeta(): Promise<PageMeta | undefined> {
return syscall("editor.getCurrentPageMeta");
}
2024-08-07 19:27:25 +08:00
/**
* Returns the full text of the currently open page
*/
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
*/
export function setText(
newText: string,
isolateHistory = false,
): Promise<void> {
return syscall("editor.setText", newText, isolateHistory);
2024-01-26 18:10:35 +08:00
}
2024-08-07 19:27:25 +08:00
/**
* Returns the position (in # of characters from the beginning of the file) of the cursor in the editor
*/
2022-04-01 23:07:08 +08:00
export function getCursor(): Promise<number> {
return syscall("editor.getCursor");
}
2024-08-07 19:27:25 +08:00
/**
* Returns the line number and column number of the cursor in the editor
*/
export function getSelection(): Promise<{ from: number; to: number }> {
return syscall("editor.getSelection");
}
2024-08-07 19:27:25 +08:00
/**
* Sets the position of the cursor in the editor
* @param from the start position of the selection
* @param to the end position of the selection
*/
export function setSelection(from: number, to: number): Promise<void> {
return syscall("editor.setSelection", from, to);
}
2024-08-07 19:27:25 +08:00
/**
* Forces a save of the current page
*/
2022-04-01 23:07:08 +08:00
export function save(): Promise<void> {
return syscall("editor.save");
}
2024-08-07 19:27:25 +08:00
/**
* Navigates to the specified page reference
* @param pageRef the page reference to navigate to
* @param replaceState whether to replace the current history state in the browser history
* @param newWindow whether to open the page in a new window
*/
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
}
2024-08-07 19:27:25 +08:00
/**
* Opens the page navigator
* @param mode the mode to open the navigator in
*/
export function openPageNavigator(
mode: "page" | "meta" | "all" = "page",
): Promise<void> {
return syscall("editor.openPageNavigator", mode);
}
2024-08-07 19:27:25 +08:00
/**
* Opens the command palette
*/
export function openCommandPalette(): Promise<void> {
return syscall("editor.openCommandPalette");
}
2024-08-07 19:27:25 +08:00
/**
* Force reloads the current page
*/
2022-04-01 23:07:08 +08:00
export function reloadPage(): Promise<void> {
return syscall("editor.reloadPage");
}
2024-08-07 19:27:25 +08:00
/**
* Force reloads the browser UI
*/
2023-07-26 17:22:10 +08:00
export function reloadUI(): Promise<void> {
return syscall("editor.reloadUI");
}
2024-08-07 19:27:25 +08:00
/**
* Reloads the config and commands, also in the server
*/
export function reloadConfigAndCommands(): Promise<void> {
return syscall("editor.reloadConfigAndCommands");
}
2024-08-07 19:27:25 +08:00
/**
* Opens the specified URL in the browser
* @param url the URL to open
* @param existingWindow whether to open the URL in an existing window
*/
export function openUrl(url: string, existingWindow = false): Promise<void> {
return syscall("editor.openUrl", url, existingWindow);
2022-04-01 23:07:08 +08:00
}
2024-09-10 20:38:41 +08:00
export function newWindow(): Promise<void> {
return syscall("editor.newWindow");
}
/**
* 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);
}
2024-08-07 19:27:25 +08:00
/**
* Force the client to download the file in dataUrl with filename as file name
* @param filename the name of the file to download
* @param dataUrl the dataUrl of the file to download
*/
export function downloadFile(filename: string, dataUrl: string): Promise<void> {
return syscall("editor.downloadFile", filename, dataUrl);
}
2024-08-07 19:27:25 +08:00
/**
* Triggers the browser's native file upload dialog/popup
* @param accept the file types to accept
* @param capture the capture mode for the file input
*/
2023-11-25 20:40:27 +08:00
export function uploadFile(
accept?: string,
capture?: string,
): Promise<UploadFile> {
return syscall("editor.uploadFile", accept, capture);
}
2024-08-07 19:27:25 +08:00
/**
* Shows a flash notification to the user (top right corner)
* @param message the message to show
* @param type the type of notification to show
*/
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
}
2024-08-07 19:27:25 +08:00
/**
* Exposes a filter box UI (similar to the page navigator and command palette)
* @param label the label to show left of the input box
* @param options the options to show and to filter on
* @param helpText the help text to show below the input box
* @param placeHolder the placeholder text to show in the input box
*/
export function filterBox(
label: string,
options: FilterOption[],
helpText = "",
placeHolder = "",
): Promise<FilterOption | undefined> {
return syscall("editor.filterBox", label, options, helpText, placeHolder);
}
2024-08-07 19:27:25 +08:00
/**
* Shows a panel in the editor
* @param id the location of the panel to show
* @param mode the mode or "size" of the panel
* @param html the html content of the panel
* @param script the script content of the panel
*/
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
}
2024-08-07 19:27:25 +08:00
/**
* Hides a panel in the editor
* @param id the location of the panel to hide
*/
export function hidePanel(
id: "lhs" | "rhs" | "bhs" | "modal",
): Promise<void> {
return syscall("editor.hidePanel", id);
2022-04-27 01:04:36 +08:00
}
2024-08-07 19:27:25 +08:00
/**
* Insert text at the specified position into the editor
* @param text the text to insert
* @param pos
*/
2022-04-01 23:07:08 +08:00
export function insertAtPos(text: string, pos: number): Promise<void> {
return syscall("editor.insertAtPos", text, pos);
}
2024-08-07 19:27:25 +08:00
/**
* Replace the text in the specified range in the editor
* @param from the start position of the range
* @param to the end position of the range
* @param text the text to replace with
*/
2022-04-01 23:07:08 +08:00
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);
}
2024-08-07 19:27:25 +08:00
/**
* Move the cursor to the specified position in the editor
* @param pos the position to move the cursor to
* @param center whether to center the cursor in the editor after moving
*/
export function moveCursor(pos: number, center = false): Promise<void> {
return syscall("editor.moveCursor", pos, center);
2022-04-01 23:07:08 +08:00
}
2024-08-07 19:27:25 +08:00
/**
* Move the cursor to the specified line and column in the editor
* @param line the line number to move the cursor to
* @param column the column number to move the cursor to
* @param center whether to center the cursor in the editor after moving
*/
export function moveCursorToLine(
line: number,
column = 1,
center = false,
): Promise<void> {
return syscall("editor.moveCursorToLine", line, column, center);
}
2024-08-07 19:27:25 +08:00
/**
* Insert text at the cursor position in the editor
* @param text the text to insert
*/
2022-04-01 23:07:08 +08:00
export function insertAtCursor(text: string): Promise<void> {
return syscall("editor.insertAtCursor", text);
}
2024-08-07 19:27:25 +08:00
/**
* Dispatch a CodeMirror transaction: https://codemirror.net/docs/ref/#state.Transaction
*/
2022-04-01 23:07:08 +08:00
export function dispatch(change: any): Promise<void> {
return syscall("editor.dispatch", change);
}
2024-08-07 19:27:25 +08:00
/**
* Prompt the user for text input
* @param message the message to show in the prompt
* @param defaultValue a default value pre-filled in the prompt
* @returns
*/
2022-04-01 23:07:08 +08:00
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
2024-08-07 19:27:25 +08:00
/**
* Prompt the user for confirmation
* @param message the message to show in the confirmation dialog
* @returns
*/
export function confirm(
message: string,
): Promise<boolean> {
return syscall("editor.confirm", message);
}
2024-08-07 19:27:25 +08:00
/**
* Get the value of a UI option
* @param key the key of the UI option to get
* @returns
*/
export function getUiOption(key: string): Promise<any> {
return syscall("editor.getUiOption", key);
2022-12-16 19:44:04 +08:00
}
2024-08-07 19:27:25 +08:00
/**
* Set the value of a UI option
* @param key the key of the UI option to set
* @param value the value to set the UI option to
*/
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
2024-08-07 19:27:25 +08:00
/**
* Perform a fold at the current cursor position
*/
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-08-07 19:27:25 +08:00
/**
* Perform an unfold at the current cursor position
*/
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-08-07 19:27:25 +08:00
/**
* Toggle the fold at the current cursor position
*/
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-08-07 19:27:25 +08:00
/**
* Fold all code blocks in the editor
*/
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-08-07 19:27:25 +08:00
/**
* Unfold all code blocks in the editor
*/
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-08-07 19:27:25 +08:00
/**
* Perform an undo operation of the last edit in the editor
*/
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-08-07 19:27:25 +08:00
/**
* Perform a redo operation of the last undo in the editor
*/
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-08-07 19:27:25 +08:00
/**
* Open the editor's native search panel
*/
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
2024-08-07 19:27:25 +08:00
/**
* Copy the specified data to the clipboard
* @param data the data to copy
*/
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-08-07 19:27:25 +08:00
/**
* Delete the current line in the editor
*/
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");
}
2024-08-07 19:27:25 +08:00
// Vim-mode specific syscalls
/**
* Execute a Vim ex command
* @param exCommand the ex command to execute
*/
export function vimEx(exCommand: string): Promise<any> {
return syscall("editor.vimEx", exCommand);
}