2023-08-28 23:12:15 +08:00
|
|
|
import { clientStore, editor } from "$sb/syscalls.ts";
|
2022-09-16 20:26:47 +08:00
|
|
|
|
2022-12-16 19:44:04 +08:00
|
|
|
// Run on "editor:init"
|
|
|
|
export async function setEditorMode() {
|
2023-08-26 14:31:51 +08:00
|
|
|
if (await clientStore.get("vimMode")) {
|
2022-12-21 21:55:24 +08:00
|
|
|
await editor.setUiOption("vimMode", true);
|
|
|
|
}
|
2023-08-26 14:31:51 +08:00
|
|
|
if (await clientStore.get("darkMode")) {
|
2022-12-21 21:55:24 +08:00
|
|
|
await editor.setUiOption("darkMode", true);
|
2022-12-16 19:44:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 02:16:07 +08:00
|
|
|
export function openCommandPalette() {
|
|
|
|
return editor.openCommandPalette();
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function openPageNavigator() {
|
|
|
|
await editor.openPageNavigator("page");
|
|
|
|
}
|
|
|
|
|
2024-07-07 18:12:05 +08:00
|
|
|
export async function openMetaNavigator() {
|
|
|
|
await editor.openPageNavigator("meta");
|
2024-01-21 02:16:07 +08:00
|
|
|
}
|
|
|
|
|
2024-07-19 23:06:40 +08:00
|
|
|
export async function openAllNavigator() {
|
|
|
|
await editor.openPageNavigator("all");
|
|
|
|
}
|
|
|
|
|
2022-12-21 21:55:24 +08:00
|
|
|
export async function toggleDarkMode() {
|
2023-08-26 14:31:51 +08:00
|
|
|
let darkMode = await clientStore.get("darkMode");
|
2022-12-21 21:55:24 +08:00
|
|
|
darkMode = !darkMode;
|
2023-08-26 14:31:51 +08:00
|
|
|
await clientStore.set("darkMode", darkMode);
|
2023-10-29 19:26:57 +08:00
|
|
|
await editor.reloadUI();
|
2022-12-21 21:55:24 +08:00
|
|
|
}
|
2023-06-15 01:27:18 +08:00
|
|
|
|
2023-07-24 17:27:46 +08:00
|
|
|
export async function centerCursorCommand() {
|
|
|
|
const pos = await editor.getCursor();
|
|
|
|
await editor.moveCursor(pos, true);
|
|
|
|
}
|
2023-07-27 23:02:53 +08:00
|
|
|
|
|
|
|
export async function moveToPosCommand() {
|
|
|
|
const posString = await editor.prompt("Move to position:");
|
|
|
|
if (!posString) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const pos = +posString;
|
2024-07-29 02:31:37 +08:00
|
|
|
await editor.moveCursor(pos, true); // showing the movement for better UX
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function moveToLineCommand() {
|
|
|
|
const lineString = await editor.prompt(
|
|
|
|
"Move to line (and optionally column):",
|
|
|
|
);
|
|
|
|
if (!lineString) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Match sequence of digits at the start, optionally another sequence
|
|
|
|
const numberRegex = /^(\d+)(?:[^\d]+(\d+))?/;
|
|
|
|
const match = lineString.match(numberRegex);
|
|
|
|
if (!match) {
|
|
|
|
await editor.flashNotification(
|
|
|
|
"Could not parse line number in prompt",
|
|
|
|
"error",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let column = 1;
|
|
|
|
const line = parseInt(match[1]);
|
|
|
|
if (match[2]) {
|
|
|
|
column = parseInt(match[2]);
|
|
|
|
}
|
|
|
|
await editor.moveCursorToLine(line, column, true); // showing the movement for better UX
|
2023-07-27 23:02:53 +08:00
|
|
|
}
|
2023-11-26 01:57:00 +08:00
|
|
|
|
2024-01-15 23:43:12 +08:00
|
|
|
export async function customFlashMessage(_def: any, message: string) {
|
2023-11-26 01:57:00 +08:00
|
|
|
await editor.flashNotification(message);
|
|
|
|
}
|
2024-01-21 02:16:07 +08:00
|
|
|
|
2024-02-06 23:51:04 +08:00
|
|
|
export async function reloadSystem() {
|
2024-01-21 02:16:07 +08:00
|
|
|
await editor.reloadSettingsAndCommands();
|
2024-02-06 23:51:04 +08:00
|
|
|
await editor.flashNotification("Reloaded system");
|
2024-01-21 02:16:07 +08:00
|
|
|
}
|
2024-01-26 02:46:08 +08:00
|
|
|
|
|
|
|
export async function findInPageCommand() {
|
|
|
|
await editor.openSearchPanel();
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-02 19:14:27 +08:00
|
|
|
|
|
|
|
export function undoCommand() {
|
|
|
|
return editor.undo();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function redoCommand() {
|
|
|
|
return editor.redo();
|
|
|
|
}
|
2024-05-26 05:12:48 +08:00
|
|
|
|
|
|
|
export function deleteLineCommand() {
|
|
|
|
return editor.deleteLine();
|
|
|
|
}
|