Files
RuneStone/src/lib/workspace/api.ts
T
Knotty Wyvern f32f60b540 Add a workspace session for marks, renames, the index, and events
Phase 4d. session.svelte.ts holds the workspace state of each window:
the marks, the note index, renames, and typed change events. It
replaces refreshToken.

- One rename path: session.rename serves the tree, the note title, the
  rename command, the link menu, plugins, and the template manager.
  Open documents, plugin views, the index, and the marks follow it.
  A title rename, the rename command, and a plugin rename now also
  move pins and bookmarks.
- marksStore.svelte.ts is the only writer of .runestone/marks.json.
  Changes run in order from the latest marks. A change from another
  window reloads the marks.
- The note index updates only the paths that change, does not rescan
  on window focus, reads in batches of 64, and keeps links, not text.
- Plugins also hear about .runestone/ changes from other windows.
- The outline uses the heading parser of heading links and Read, so
  lines in code blocks are no longer headings.
- New note is above New file in the tree menu (card a6032af5).
- New workspace/session smoke test and new unit tests for the index
  and the outline.

pnpm check, pnpm test (347 of 347), and the full smoke suite pass,
except workspace/manageTemplates, which failed one time under load and
passed on retry and 4 of 4 times alone.
2026-10-28 11:28:54 -07:00

129 lines
4.4 KiB
TypeScript

import { invoke } from "@tauri-apps/api/core";
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { open as openDialog } from "@tauri-apps/plugin-dialog";
import type { TreeEntry, WorkspaceEvent, WorkspaceInfo } from "./types";
import { tr } from "../i18n/index.ts";
export async function pickWorkspaceFolder(title = tr("Open workspace")): Promise<string | null> {
const selected = await openDialog({
directory: true,
multiple: false,
title,
});
if (selected === null || Array.isArray(selected)) {
return null;
}
return selected;
}
export function workspaceOpen(path: string): Promise<WorkspaceInfo> {
return invoke("workspace_open", { path });
}
export function workspaceOpenWindow(path: string, transfer?: string): Promise<void> {
return invoke("workspace_open_window", { path, transfer });
}
export function workspaceLastPath(): Promise<string | null> {
return invoke("workspace_last_path");
}
export function workspaceRecent(): Promise<string[]> {
return invoke("workspace_recent");
}
export function workspaceForget(path: string): Promise<string[]> {
return invoke("workspace_forget", { path });
}
export function workspaceNew(parent: string, name: string): Promise<void> {
return invoke("workspace_new", { parent, name });
}
export function workspaceList(rel: string, showInternal = false): Promise<TreeEntry[]> {
return invoke("workspace_list", { rel, showInternal });
}
export function workspaceRead(rel: string): Promise<string> {
return invoke("workspace_read", { rel });
}
/** Read at most 64 text files in one request. One failed file does not stop the others. */
export function workspaceReadMany(paths: string[]): Promise<{ path: string; source?: string; error?: string }[]> {
return invoke("workspace_read_many", { paths });
}
export async function workspaceReadBytes(rel: string): Promise<Uint8Array> {
return new Uint8Array(await invoke<ArrayBuffer>("workspace_read_bytes", { rel }));
}
export function workspaceWrite(rel: string, contents: string, expected?: string): Promise<void> {
return invoke("workspace_write", { rel, contents, expected });
}
export function workspaceCreate(rel: string, kind: "file" | "folder"): Promise<void> {
return invoke("workspace_create", { rel, kind });
}
export function workspaceRename(from: string, to: string, expectedRoot?: string): Promise<void> {
return invoke("workspace_rename", { from, to, expectedRoot });
}
/** Move an item to the system trash, or delete it permanently with `permanent`. */
export function workspaceDelete(rel: string, permanent = false): Promise<void> {
return invoke("workspace_delete", { rel, permanent });
}
export function workspaceReadDataUrl(rel: string): Promise<string> {
return invoke("workspace_read_data_url", { rel });
}
export function workspaceWriteBytes(rel: string, base64: string): Promise<void> {
return invoke("workspace_write_bytes", { rel, base64 });
}
export type CopyResult = {
name: string;
rel_path: string;
kind: "file" | "folder";
};
export function workspaceCopy(rel: string, parent?: string, expectedRoot?: string): Promise<CopyResult> {
return invoke("workspace_copy", { rel, parent, expectedRoot });
}
export function workspaceAbsPath(rel: string): Promise<string> {
return invoke("workspace_abs_path", { rel });
}
export function workspaceOpenDefault(rel: string): Promise<void> { return invoke("workspace_open_default", { rel }); }
export function workspaceOpenTerminal(rel: string): Promise<void> { return invoke("workspace_open_terminal", { rel }); }
export function workspaceReveal(rel: string): Promise<void> {
return invoke("workspace_reveal", { rel });
}
export type SearchHit = {
rel_path: string;
line: number;
text: string;
};
export function workspaceSearch(query: string): Promise<SearchHit[]> {
return invoke("workspace_search", { query });
}
/**
* Listen to file changes in the workspace of this window. The host sends each change only to
* the windows that have its workspace open (src-tauri/src/workspace.rs `emit_watch_event`).
* The target must be this window: a listener without a target receives the events of every window.
*/
export function onWorkspaceEvent(
handler: (event: WorkspaceEvent) => void,
): Promise<UnlistenFn> {
return listen<WorkspaceEvent>("workspace-event", (event) => {
handler(event.payload);
}, { target: getCurrentWindow().label });
}