2024-07-30 23:33:33 +08:00
|
|
|
import type { UploadFile } from "../types.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { syscall } from "../syscall.ts";
|
2024-03-16 22:29:24 +08:00
|
|
|
import type { PageRef } from "../lib/page_ref.ts";
|
2024-08-07 02:11:38 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2022-06-23 23:08:42 +08:00
|
|
|
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
|
|
|
|
*/
|
2022-06-23 23:08:42 +08:00
|
|
|
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
|
|
|
|
*/
|
2022-08-01 21:06:32 +08:00
|
|
|
export function navigate(
|
2024-01-24 18:58:33 +08:00
|
|
|
pageRef: PageRef,
|
2022-10-10 20:50:21 +08:00
|
|
|
replaceState = false,
|
2022-10-29 15:23:12 +08:00
|
|
|
newWindow = false,
|
2022-08-01 21:06:32 +08:00
|
|
|
): Promise<void> {
|
2024-01-24 18:58:33 +08:00
|
|
|
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
|
|
|
|
*/
|
2024-01-21 02:16:07 +08:00
|
|
|
export function openPageNavigator(
|
2024-07-19 23:06:40 +08:00
|
|
|
mode: "page" | "meta" | "all" = "page",
|
2024-01-21 02:16:07 +08:00
|
|
|
): Promise<void> {
|
|
|
|
return syscall("editor.openPageNavigator", mode);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Opens the command palette
|
|
|
|
*/
|
2024-01-21 02:16:07 +08:00
|
|
|
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
|
|
|
|
*/
|
2024-08-02 22:47:36 +08:00
|
|
|
export function reloadConfigAndCommands(): Promise<void> {
|
|
|
|
return syscall("editor.reloadConfigAndCommands");
|
2024-01-21 02:16:07 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2023-06-14 02:47:05 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2024-02-07 16:33:47 +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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2023-01-08 22:29:34 +08:00
|
|
|
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> {
|
2023-11-22 22:33:25 +08:00
|
|
|
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
|
|
|
|
*/
|
2022-07-14 19:32:28 +08:00
|
|
|
export function flashNotification(
|
|
|
|
message: string,
|
2022-10-10 20:50:21 +08:00
|
|
|
type: "info" | "error" = "info",
|
2022-07-14 19:32:28 +08:00
|
|
|
): 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
|
|
|
|
*/
|
2022-04-13 20:46:52 +08:00
|
|
|
export function filterBox(
|
|
|
|
label: string,
|
|
|
|
options: FilterOption[],
|
2022-10-10 20:50:21 +08:00
|
|
|
helpText = "",
|
|
|
|
placeHolder = "",
|
2022-04-13 20:46:52 +08:00
|
|
|
): 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
|
|
|
|
*/
|
2022-09-30 22:59:57 +08:00
|
|
|
export function showPanel(
|
2023-12-28 01:05:47 +08:00
|
|
|
id: "lhs" | "rhs" | "bhs" | "modal",
|
2022-09-30 22:59:57 +08:00
|
|
|
mode: number,
|
2022-05-09 20:59:12 +08:00
|
|
|
html: string,
|
2022-10-10 20:50:21 +08:00
|
|
|
script = "",
|
2022-05-09 20:59:12 +08:00
|
|
|
): Promise<void> {
|
2022-09-30 22:59:57 +08:00
|
|
|
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
|
|
|
|
*/
|
2023-10-03 20:16:33 +08:00
|
|
|
export function hidePanel(
|
2023-12-28 01:05:47 +08:00
|
|
|
id: "lhs" | "rhs" | "bhs" | "modal",
|
2023-10-03 20:16:33 +08:00
|
|
|
): Promise<void> {
|
2022-09-30 22:59:57 +08:00
|
|
|
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,
|
2022-10-10 20:50:21 +08:00
|
|
|
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
|
|
|
|
*/
|
2022-11-01 22:01:28 +08:00
|
|
|
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
|
|
|
|
*/
|
2024-07-29 02:31:37 +08:00
|
|
|
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,
|
2022-10-10 20:50:21 +08:00
|
|
|
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
|
|
|
|
*/
|
2022-11-18 23:04:37 +08:00
|
|
|
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
|
|
|
|
*/
|
2022-12-21 21:55:24 +08:00
|
|
|
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
|
|
|
|
*/
|
2022-12-21 21:55:24 +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
|
|
|
|
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);
|
|
|
|
}
|