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
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2022-06-23 23:08:42 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
|
2023-01-08 22:29:34 +08:00
|
|
|
// 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> {
|
2023-11-22 22:33:25 +08:00
|
|
|
return syscall("editor.uploadFile", accept, capture);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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,
|
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);
|
|
|
|
}
|
|
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
|
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,
|
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
|
|
|
|
2022-11-18 23:04:37 +08:00
|
|
|
export function confirm(
|
|
|
|
message: string,
|
|
|
|
): Promise<boolean> {
|
|
|
|
return syscall("editor.confirm", message);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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");
|
|
|
|
}
|