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:
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
/* Rendered Markdown. Read, the Write previews, and table widgets use these rules. */
|
||||
.md-edit-block { display: contents; cursor: text; }
|
||||
.md-edit-block:focus-visible > :first-child { outline: 1px solid var(--rs-accent-deep); outline-offset: 4px; }
|
||||
.md-edit-block:empty { display: block; min-height: 3em; }
|
||||
.md-mermaid { margin: 1.4em 0; }
|
||||
.md-diagram { overflow: auto; text-align: center; }
|
||||
.md-diagram svg { display: block; max-width: 100%; height: auto; margin: 0 auto; }
|
||||
.md-diagram-source { margin-top: 10px; color: var(--rs-text-dim); font: 12px/1.5 var(--rs-font-ui); }
|
||||
.md-diagram-source summary { cursor: pointer; }
|
||||
.md-diagram-source pre { text-align: left; }
|
||||
.reading {
|
||||
max-width: 740px;
|
||||
min-width: 0;
|
||||
margin: 0 auto;
|
||||
color: var(--rs-text);
|
||||
font: var(--rs-font-weight) var(--rs-prose-size)/var(--rs-prose-line) var(--rs-font-prose);
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.reading h1, .reading h2, .reading h3,
|
||||
.reading h4, .reading h5, .reading h6, .live-writer .writer-heading {
|
||||
color: var(--rs-text-strong);
|
||||
font-family: var(--rs-font-heading);
|
||||
font-weight: var(--rs-heading-weight);
|
||||
line-height: 1.4;
|
||||
letter-spacing: -0.02em;
|
||||
margin: 1.6em 0 .55em;
|
||||
scroll-margin-top: 24px;
|
||||
}
|
||||
.reading h1, .live-writer .writer-h1 { font-size: 1.6em; }
|
||||
.reading h2, .live-writer .writer-h2 { font-size: 1.35em; }
|
||||
.reading h3, .live-writer .writer-h3 { font-size: 1.15em; }
|
||||
.reading h4, .live-writer .writer-h4 { font-size: 1.05em; }
|
||||
.reading h5, .reading h6, .live-writer .writer-h5, .live-writer .writer-h6 { font-size: 1em; color: var(--rs-text-dim); }
|
||||
.live-writer .writer-heading { margin: 0; padding-top: .7em; padding-bottom: .3em; }
|
||||
.document-title, .reading .document-title { margin: 0 0 .85em; color: var(--rs-text-strong); font: var(--rs-heading-weight) 1.8em/1.3 var(--rs-font-heading); overflow-wrap: anywhere; }
|
||||
.reading > :first-child { margin-top: 0; }
|
||||
.reading p { margin: 0 0 .75em; white-space: pre-line; }
|
||||
.reading strong { font-weight: 600; color: var(--rs-text-strong); }
|
||||
.reading a { color: var(--rs-accent); text-decoration-thickness: 1px; text-underline-offset: 3px; }
|
||||
.reading a:hover { color: var(--rs-accent-2); }
|
||||
.reading hr { border: 0; border-top: 1px solid var(--rs-border); margin: 2em 0; }
|
||||
|
||||
.reading ul, .reading ol { padding-left: 1.6em; margin: 0 0 .75em; }
|
||||
.reading li { padding-left: 0.25em; margin: .1em 0; }
|
||||
.reading li::marker { color: var(--rs-text-dim); }
|
||||
.reading li > p { margin-bottom: 0.6em; }
|
||||
.reading li > ul, .reading li > ol { margin: 0.35em 0 0.5em; }
|
||||
/* Nested ordered lists use letters, then Roman numerals. Write labels its markers the same way. */
|
||||
.reading ol ol { list-style-type: lower-alpha; }
|
||||
.reading ol ol ol { list-style-type: lower-roman; }
|
||||
.reading ol ol ol ol { list-style-type: decimal; }
|
||||
.reading ol ol ol ol ol { list-style-type: lower-alpha; }
|
||||
.reading ol ol ol ol ol ol { list-style-type: lower-roman; }
|
||||
.reading .task-list-item { list-style: none; position: relative; }
|
||||
.reading .task-list-item > input,
|
||||
.reading .task-list-item > p > input {
|
||||
position: absolute;
|
||||
left: -1.3em;
|
||||
top: 0.4em;
|
||||
margin: 0;
|
||||
width: 15px;
|
||||
height: 15px;
|
||||
accent-color: var(--rs-accent);
|
||||
pointer-events: none;
|
||||
}
|
||||
.reading .is-checked { color: var(--rs-text-dim); text-decoration: line-through; }
|
||||
.reading s, .reading del { color: var(--rs-text-dim); }
|
||||
|
||||
.reading blockquote {
|
||||
margin: 1.4em 0;
|
||||
padding: 0.75em 1.2em;
|
||||
border-left: 3px solid var(--rs-accent-deep);
|
||||
background: var(--rs-line-highlight);
|
||||
border-radius: 0 6px 6px 0;
|
||||
color: var(--rs-text-dim);
|
||||
}
|
||||
.reading blockquote > :last-child { margin-bottom: 0; }
|
||||
.reading .md-callout { --callout-color: var(--rs-accent); color: var(--rs-text); border-color: var(--callout-color); }
|
||||
.reading .md-callout[data-callout="warning"],
|
||||
.reading .md-callout[data-callout="important"] { --callout-color: var(--rs-warn); }
|
||||
.reading .md-callout[data-callout="danger"] { --callout-color: var(--rs-error); }
|
||||
.reading .md-callout[data-callout="success"],
|
||||
.reading .md-callout[data-callout="tip"] { --callout-color: var(--rs-ok); }
|
||||
.reading .md-callout-title { display: block; color: var(--callout-color); margin-bottom: 0.3em; }
|
||||
|
||||
.reading code {
|
||||
font-family: var(--rs-font-mono);
|
||||
font-size: 0.85em;
|
||||
color: var(--rs-accent-2);
|
||||
background: var(--rs-bg-active);
|
||||
padding: 0.15em 0.35em;
|
||||
border-radius: 4px;
|
||||
}
|
||||
.reading pre {
|
||||
position: relative;
|
||||
background: var(--rs-code-block-bg);
|
||||
border: 1px solid var(--rs-border);
|
||||
border-radius: 8px;
|
||||
padding: 18px 20px;
|
||||
overflow: auto;
|
||||
margin: 1.4em 0;
|
||||
font: 13px/1.65 var(--rs-font-mono);
|
||||
tab-size: 4;
|
||||
}
|
||||
.reading pre.md-code { padding-top: 44px; }
|
||||
|
||||
.reading pre code {
|
||||
background: transparent;
|
||||
padding: 0;
|
||||
font-size: inherit;
|
||||
color: var(--rs-text);
|
||||
display: block;
|
||||
white-space: pre;
|
||||
overflow-wrap: normal;
|
||||
}
|
||||
.reading pre[data-lang]::before {
|
||||
content: attr(data-lang);
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
left: 20px;
|
||||
color: var(--rs-text-dim);
|
||||
font: 11px/1.6 var(--rs-font-ui);
|
||||
}
|
||||
.rs-fence-copy {
|
||||
position: absolute;
|
||||
top: 6px;
|
||||
right: 8px;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
padding: 5px;
|
||||
border: 0;
|
||||
border-radius: 5px;
|
||||
background: transparent;
|
||||
color: var(--rs-text-dim);
|
||||
}
|
||||
.rs-fence-copy svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.6; }
|
||||
.rs-fence-copy:hover { color: var(--rs-text-strong); background: var(--rs-bg-active); }
|
||||
.rs-fence-copy[data-copied] { color: var(--rs-ok); }
|
||||
|
||||
.reading .md-table-scroll { width: fit-content; max-width: 100%; overflow-x: auto; margin: 1.4em 0; border: 1px solid var(--rs-border); border-radius: 0; }
|
||||
.reading table { border-collapse: collapse; width: auto; max-width: 100%; font-size: 0.95em; line-height: var(--rs-table-line-height); overflow-wrap: normal; word-break: normal; }
|
||||
.reading th, .reading td { padding: var(--rs-table-cell-padding); text-align: left; vertical-align: top; border-bottom: 1px solid var(--rs-border); min-width: 3rem; max-width: 26rem; white-space: normal; overflow-wrap: break-word; }
|
||||
.reading :is(th, td):empty::before,
|
||||
.reading :is(th, td) > p:empty::before { content: "\200b"; }
|
||||
.reading :is(th, td) > p { margin: 0; }
|
||||
.reading th { background: var(--rs-bg-active); font-weight: 600; color: var(--rs-text-strong); }
|
||||
.reading tbody tr:nth-child(even) { background: var(--rs-line-highlight); }
|
||||
.reading tbody tr:last-child td { border-bottom: 0; }
|
||||
.reading th + th, .reading td + td { border-left: 1px solid var(--rs-border); }
|
||||
.reading img { max-width: 100%; height: auto; display: block; margin: 1.4em 0; border-radius: 6px; }
|
||||
.reading .missing-image { display: inline-block; padding: 8px 12px; border: 1px dashed var(--rs-border); border-radius: 6px; color: var(--rs-text-dim); font-size: 13px; }
|
||||
.reading .md-properties { margin: 0 0 2em; border: 1px solid var(--rs-border); border-radius: 6px; font: 12px/1.5 var(--rs-font-ui); color: var(--rs-text-dim); }
|
||||
.reading .md-properties summary { padding: 9px 12px; cursor: pointer; }
|
||||
.reading .md-properties summary:hover { color: var(--rs-text-strong); }
|
||||
.reading .md-properties pre { margin: 0; padding: 12px; border: 0; border-top: 1px solid var(--rs-border); border-radius: 0 0 6px 6px; white-space: pre-wrap; }
|
||||
.reading mark { background: color-mix(in srgb, #edc65b 30%, transparent); color: inherit; border-radius: 2px; }
|
||||
.reading .md-inline-math { font-family: var(--rs-font-mono); font-style: italic; }
|
||||
.reading .md-inline-comment { display: none; }
|
||||
.reading:not(.cm-content) :is(p, h1, h2, h3, h4, h5, h6):not(td *, th *) { position: relative; }
|
||||
.reading:not(.cm-content) :is(p, h1, h2, h3, h4, h5, h6):not(td *, th *):hover::before { content: ""; position: absolute; left: -14px; top: 2px; bottom: 2px; width: 3px; border-radius: var(--rs-control-radius); background: var(--rs-text-dim); opacity: .5; pointer-events: none; }
|
||||
@@ -0,0 +1,60 @@
|
||||
/** The commands of the workbench shortcuts. Workbench.svelte runs them. */
|
||||
export type WorkbenchCommand =
|
||||
| "quick-open" | "new-tab" | "search-workspace" | "save" | "open-workspace" | "new-file" | "toggle-layout"
|
||||
| "toggle-terminal" | "close-tab" | "toggle-sidebar" | "rename" | "next-tab" | "previous-tab" | "settings" | "help";
|
||||
|
||||
/**
|
||||
* One shortcut. `mod` is Ctrl, or Cmd on macOS. `ctrl` is Ctrl only.
|
||||
* `shift` and `alt` must match when they are set. A missing value accepts both states.
|
||||
* `outsideFields` ignores the key in text inputs, where it has its own meaning.
|
||||
*/
|
||||
export type WorkbenchKey = {
|
||||
key: string;
|
||||
command: WorkbenchCommand;
|
||||
mod?: boolean;
|
||||
ctrl?: boolean;
|
||||
shift?: boolean;
|
||||
alt?: boolean;
|
||||
outsideFields?: boolean;
|
||||
};
|
||||
|
||||
/** Shortcuts of the main window. The first match wins. */
|
||||
export const workbenchKeys: WorkbenchKey[] = [
|
||||
{ key: "p", mod: true, command: "quick-open" },
|
||||
{ key: "t", mod: true, shift: false, alt: false, command: "new-tab" },
|
||||
{ key: "f", mod: true, shift: true, command: "search-workspace" },
|
||||
{ key: "s", mod: true, command: "save" },
|
||||
{ key: "o", mod: true, command: "open-workspace" },
|
||||
{ key: "n", mod: true, command: "new-file" },
|
||||
{ key: "e", mod: true, command: "toggle-layout" },
|
||||
{ key: "`", mod: true, command: "toggle-terminal" },
|
||||
{ key: "w", mod: true, command: "close-tab" },
|
||||
{ key: "b", mod: true, command: "toggle-sidebar" },
|
||||
{ key: "F2", mod: false, alt: false, outsideFields: true, command: "rename" },
|
||||
];
|
||||
|
||||
/** Shortcuts that also work in the Settings dialog. They run before the editor gets the key. */
|
||||
export const preferenceKeys: WorkbenchKey[] = [
|
||||
{ key: "Tab", ctrl: true, shift: true, alt: false, command: "previous-tab" },
|
||||
{ key: "Tab", ctrl: true, shift: false, alt: false, command: "next-tab" },
|
||||
{ key: ",", mod: true, shift: false, alt: false, command: "settings" },
|
||||
{ key: "F1", mod: false, shift: false, alt: false, command: "help" },
|
||||
];
|
||||
|
||||
type KeyEvent = Pick<KeyboardEvent, "key" | "ctrlKey" | "metaKey" | "shiftKey" | "altKey" | "target">;
|
||||
|
||||
function matches(binding: WorkbenchKey, event: KeyEvent): boolean {
|
||||
const key = binding.key.length === 1 ? event.key.toLowerCase() : event.key;
|
||||
if (key !== binding.key) return false;
|
||||
if (binding.mod !== undefined && (event.ctrlKey || event.metaKey) !== binding.mod) return false;
|
||||
if (binding.ctrl && (!event.ctrlKey || event.metaKey)) return false;
|
||||
if (binding.shift !== undefined && event.shiftKey !== binding.shift) return false;
|
||||
if (binding.alt !== undefined && event.altKey !== binding.alt) return false;
|
||||
const field = typeof HTMLInputElement !== "undefined" && (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement);
|
||||
return !(binding.outsideFields && field);
|
||||
}
|
||||
|
||||
/** The command of a key event, or null when no shortcut matches. */
|
||||
export function workbenchCommand(event: KeyEvent, keys: readonly WorkbenchKey[] = workbenchKeys): WorkbenchCommand | null {
|
||||
return keys.find(binding => matches(binding, event))?.command ?? null;
|
||||
}
|
||||
Reference in New Issue
Block a user