Move global CSS beside its code and make Documents reactive

Phase 4e.

- app.css holds only imports. Its rules moved into files beside their
  code, in the original cascade order. Dead rules of the deleted TipTap
  Writer are gone.
- workbenchKeys.ts is one table of the workbench shortcuts. Workbench
  runs the commands.
- session.linkTargets is the one link resolution for the link menu and
  for link opening.
- Documents is a rune module (documents.svelte.ts). Workbench reads the
  tabs and the active path from it, without a copy of all buffers on
  each change. The editor mount effect now copies the buffer inside
  untrack, so an edit does not remount the editor.
- Unit tests can import .svelte.ts modules through a Node module hook
  that compiles them with the Svelte compiler.

pnpm check, pnpm test (350 of 350), and the full smoke suite pass,
except tables/linkCaret, which failed one time under load and passed
on retry and 3 of 3 times alone.
This commit is contained in:
Knotty Wyvern
2026-10-28 11:50:33 -07:00
parent f32f60b540
commit 3e6bc5f088
23 changed files with 549 additions and 512 deletions
+46 -81
View File
@@ -19,7 +19,7 @@
import { getCurrentWindow } from "@tauri-apps/api/window";
import { confirm } from "@tauri-apps/plugin-dialog";
import { openUrl } from "@tauri-apps/plugin-opener";
import { Documents } from "$lib/workspace/documents";
import { Documents } from "$lib/workspace/documents.svelte";
import { documentSessionKey, parseDocumentSession, restoreDocumentSession, type DocumentSession } from "$lib/workspace/documentSession";
import { tabsToClose, type TabCloseScope } from "$lib/workspace/tabActions";
import { orderWorkbenchTabs, type WorkbenchTab } from "$lib/workspace/workbenchTabs";
@@ -37,6 +37,7 @@
import { TAB_TRANSFER_ID, cancelTabTransfer, finishTabTransfer, offerTabTransfer, onTabTransferAccepted, parseTabTransfer, takeTabTransfer } from "$lib/workspace/tabTransfer";
import { DEFAULT_WORKBENCH_LAYOUT, WORKBENCH_LAYOUT_KEY, parseWorkbenchLayout, saveWorkbenchLayout, type WorkbenchLayout } from "$lib/workspace/workbenchLayout";
import FileTree from "./FileTree.svelte";
import { preferenceKeys, workbenchCommand, type WorkbenchCommand } from "./workbenchKeys";
import EditorPane from "./EditorPane.svelte";
import TabBar from "./TabBar.svelte";
import OutlinePane from "./OutlinePane.svelte";
@@ -67,7 +68,6 @@
import { detectChrome } from "$lib/workspace/chrome";
import { planRename, planMarkdownTitleRename, planDocumentTitleRename, renameSelection } from "$lib/workspace/renamePath";
import { attachmentRel, isImagePath, isMarkdownPath } from "$lib/workspace/media";
import { linkCandidates } from "$lib/workspace/markdownLinks";
import { headingLinkLine, linkFragment } from "$lib/workspace/headingLinks";
import type { Chrome, OpenBuffer, TreeEntry, WorkspaceInfo } from "$lib/workspace/types";
@@ -88,8 +88,10 @@
let workspaceAnchor: HTMLDivElement | undefined = $state();
const desktop = isTauri();
const transferredWindow = /^[0-9a-f-]{36}$/i.test(new URLSearchParams(location.search).get("tabTransfer") ?? "");
let tabs = $state<OpenBuffer[]>([]);
let activePath = $state<string | null>(null);
// The documents of the open workspace. Their buffers are reactive, so the tabs read them directly.
let documents = $state.raw<Documents | null>(null);
const tabs = $derived<OpenBuffer[]>(documents?.buffers ?? []);
const activePath = $derived(documents?.activePath ?? null);
const buffer = $derived(tabs.find((tab) => tab.relPath === activePath) ?? null);
let status = $state($t("Open a workspace folder"));
let error = $state<string | null>(null);
@@ -183,7 +185,6 @@
let saving = $state(false);
let switching = false;
let workbenchReady = $state(false);
let documents: Documents | null = null;
let paneComponents = $state<Record<string, EditorPane | undefined>>({});
let pluginPaneComponents = $state<Record<string, PluginEditor | undefined>>({});
function activeEditor() { return paneComponents[documentPanes.active]; }
@@ -198,8 +199,6 @@
kind: path => isImagePath(path) ? "image" : "text", chrome: detectChrome,
}, () => {
if (documents !== next) return;
tabs = next.buffers.map(tab => ({ ...tab }));
activePath = next.activePath;
const selected = next.active;
documentPanes = syncDocumentPanes(documentPanes, next.buffers.map(tab => tab.id), selected?.id ?? null);
if (selected) view = selected.chromeOverride ?? selected.chrome;
@@ -843,22 +842,39 @@
window.addEventListener("pointerdown", outside);
}
/** Run a shortcut command. Returns false when the command does not apply now, so the key keeps its default. */
function runCommand(command: WorkbenchCommand): boolean {
switch (command) {
case "next-tab": case "previous-tab": {
if (!workingTabs.length) return false;
const index = workingTabs.findIndex(tab => tab.relPath === activeTabPath);
const next = (index + (command === "previous-tab" ? -1 : 1) + workingTabs.length) % workingTabs.length;
selectWorkingTab(workingTabs[next].relPath);
return true;
}
case "settings": void openSettings(); return true;
case "help": preferences = "help"; return true;
case "quick-open": if (workspace) quickOpen = true; return true;
case "new-tab": newTab(); return true;
case "search-workspace": if (!transferredWindow) { leftOpen = true; leftView = "search"; } return true;
case "save": if (!editorPluginVisible) void save(); return true;
case "open-workspace": void chooseWorkspace(); return true;
case "new-file": void newFile(); return true;
case "toggle-layout": if (!editorPluginVisible) toggleChrome(); return true;
case "toggle-terminal": if (!terminalEnabled) return false; toggleTerminal(); return true;
case "close-tab":
if (editorPluginVisible) { if (activeTabPath) void closeTab(activeTabPath); }
else if (activePath) closeTab(activePath);
return true;
case "toggle-sidebar": if (!transferredWindow) leftOpen = !leftOpen; return true;
case "rename": if (!editorPluginVisible && !(leftOpen && leftView === "files")) void renameActive(); return true;
}
}
function onPreferencesKeydown(event: KeyboardEvent) {
if (event.target instanceof Element && event.target.closest("dialog, [role=dialog], [role=menu]")) return;
if (event.ctrlKey && !event.altKey && !event.metaKey && event.key === "Tab" && workingTabs.length) {
event.preventDefault(); event.stopPropagation();
const index = workingTabs.findIndex(tab => tab.relPath === activeTabPath);
const next = (index + (event.shiftKey ? -1 : 1) + workingTabs.length) % workingTabs.length;
selectWorkingTab(workingTabs[next].relPath);
return;
}
if ((event.ctrlKey || event.metaKey) && !event.shiftKey && !event.altKey && event.key === ",") {
event.preventDefault(); event.stopPropagation(); void openSettings();
}
if (event.key === "F1" && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
event.preventDefault(); event.stopPropagation(); preferences = "help";
}
const command = workbenchCommand(event, preferenceKeys);
if (command && runCommand(command)) { event.preventDefault(); event.stopPropagation(); }
}
function onKeydown(event: KeyboardEvent) {
@@ -879,56 +895,8 @@
// Let shells and terminal applications own their control keys.
if (event.target instanceof Element && event.target.closest(".xterm") && event.key !== "`") return;
if (event.target instanceof Element && event.target.closest("dialog")) return;
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "p") {
event.preventDefault(); if (workspace) quickOpen = true; return;
}
if ((event.ctrlKey || event.metaKey) && !event.shiftKey && !event.altKey && event.key.toLowerCase() === "t") {
event.preventDefault(); newTab(); return;
}
if ((event.ctrlKey || event.metaKey) && event.shiftKey && event.key.toLowerCase() === "f") {
event.preventDefault(); if (!transferredWindow) { leftOpen = true; leftView = "search"; } return;
}
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "s") {
event.preventDefault();
if (!editorPluginVisible) void save();
}
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "o") {
event.preventDefault();
void chooseWorkspace();
}
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "n") {
event.preventDefault();
void newFile();
}
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "e") {
event.preventDefault();
if (!editorPluginVisible) toggleChrome();
}
if (terminalEnabled && (event.ctrlKey || event.metaKey) && event.key === "`") {
event.preventDefault();
toggleTerminal();
}
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "w") {
event.preventDefault();
if (editorPluginVisible) {
if (activeTabPath) void closeTab(activeTabPath);
} else if (activePath) {
closeTab(activePath);
}
}
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "b") {
event.preventDefault();
if (!transferredWindow) leftOpen = !leftOpen;
}
if (event.key === "F2" && !event.ctrlKey && !event.metaKey && !event.altKey) {
if (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement) {
return;
}
event.preventDefault();
if (!editorPluginVisible && !(leftOpen && leftView === "files")) {
void renameActive();
}
}
const command = workbenchCommand(event);
if (command && runCommand(command)) event.preventDefault();
}
async function renameNoteTitle(id: string, title: string) {
@@ -976,11 +944,9 @@
const current = documents, currentWorkspace = workspace;
const valid = () => documents === current && workspace === currentWorkspace && !!currentWorkspace;
const entry = async () => {
await session.indexReady;
const resolution = session.index.resolve(origin, link.target, link.kind);
if (resolution.status === "ambiguous") throw new Error($t("More than one note matches {path}. Use its workspace path: {candidates}", { path: link.target, candidates: resolution.candidates.join(", ") }));
const paths = resolution.status === "resolved" ? [resolution.path] : link.kind === "wiki" ? [] : linkCandidates(origin, link.target, link.kind);
for (const path of paths) {
const targets = await session.linkTargets(origin, link.target, link.kind);
if ("ambiguous" in targets) throw new Error($t("More than one note matches {path}. Use its workspace path: {candidates}", { path: link.target, candidates: targets.ambiguous.join(", ") }));
for (const path of targets.paths) {
if (!valid()) throw new Error("The workspace changed. Open the link menu again.");
const parent = path.split("/").slice(0, -1).join("/");
let files: TreeEntry[];
@@ -1043,14 +1009,13 @@
return;
}
const current = documents, root = workspace?.root;
await session.indexReady;
const targets = await session.linkTargets(origin, target, kind);
if (documents !== current || workspace?.root !== root) return;
const resolution = session.index.resolve(origin, target, kind);
if (resolution.status === "ambiguous") {
error = $t("More than one note matches {path}. Use its workspace path: {candidates}", { path: target, candidates: resolution.candidates.join(", ") });
if ("ambiguous" in targets) {
error = $t("More than one note matches {path}. Use its workspace path: {candidates}", { path: target, candidates: targets.ambiguous.join(", ") });
return;
}
const rels = resolution.status === "resolved" ? [resolution.path] : kind === "wiki" ? [] : linkCandidates(origin, target, kind);
const rels = targets.paths;
const fragment = linkFragment(target);
focusDocumentPane(paneId);
for (const rel of rels) {