Improve workspace navigation, search, and tab actions

This commit is contained in:
Knotty Wyvern
2026-09-29 07:48:24 -07:00
parent bdc15e952d
commit 43106ed9c7
34 changed files with 1798 additions and 100 deletions
+78
View File
@@ -0,0 +1,78 @@
<script lang="ts">
import { untrack } from "svelte";
import { runScopeHandlers, type EditorView } from "@codemirror/view";
import { SearchQuery, closeSearchPanel, findNext, findPrevious, getSearchQuery, replaceAll, replaceNext, selectMatches, setSearchQuery } from "@codemirror/search";
import Icon from "$lib/ui/Icon.svelte";
import IconButton from "$lib/ui/IconButton.svelte";
import "$lib/ui/find.css";
let { view }: { view: EditorView } = $props();
const initialQuery = untrack(() => getSearchQuery(view.state));
let query = $state.raw(initialQuery);
let searchText = $state(initialQuery.search);
let replacement = $state(initialQuery.replace);
let caseSensitive = $state(initialQuery.caseSensitive);
let regexp = $state(initialQuery.regexp);
let wholeWord = $state(initialQuery.wholeWord);
export function sync(next: SearchQuery) {
if (next.eq(query)) return;
query = next;
searchText = next.search;
replacement = next.replace;
caseSensitive = next.caseSensitive;
regexp = next.regexp;
wholeWord = next.wholeWord;
}
function commit() {
query = new SearchQuery({ search: searchText, replace: replacement, caseSensitive, regexp, wholeWord });
view.dispatch({ effects: setSearchQuery.of(query) });
}
function keydown(event: KeyboardEvent) {
if (runScopeHandlers(view, event, "search-panel")) {
event.preventDefault();
event.stopPropagation();
} else if (event.key === "Enter" && event.target instanceof HTMLInputElement && event.target.type === "text") {
event.preventDefault();
commit();
if (event.target.name === "replace") replaceNext(view);
else (event.shiftKey ? findPrevious : findNext)(view);
}
}
</script>
<!-- svelte-ignore a11y_no_noninteractive_element_interactions (Handle shortcuts from the search controls.) -->
<section class="rs-find-bar" aria-label="Find and replace" onkeydown={keydown}>
<div class="rs-find-row">
<div class="rs-find-field">
<Icon icon="search" />
<input type="text" name="search" {...{ "main-field": "true" }} aria-label="Find in document" placeholder="Find in document"
aria-invalid={searchText !== "" && !query.valid} bind:value={() => searchText, value => { searchText = value; commit(); }} />
</div>
<div class="rs-find-actions">
<IconButton icon="up" label="Previous match (Shift+Enter)" onclick={() => findPrevious(view)} disabled={!query.valid} />
<IconButton icon="down" label="Next match (Enter)" onclick={() => findNext(view)} disabled={!query.valid} />
<button class="rs-find-button" type="button" onclick={() => selectMatches(view)} disabled={!query.valid}>Select all</button>
<IconButton icon="close" label="Close find (Escape)" onclick={() => closeSearchPanel(view)} />
</div>
</div>
<div class="rs-find-options">
<label><input type="checkbox" bind:checked={() => caseSensitive, value => { caseSensitive = value; commit(); }} />Match case</label>
<label><input type="checkbox" bind:checked={() => wholeWord, value => { wholeWord = value; commit(); }} />Whole word</label>
<label><input type="checkbox" bind:checked={() => regexp, value => { regexp = value; commit(); }} />Regex</label>
{#if searchText && !query.valid}<span class="rs-find-error" role="status">Invalid expression</span>{/if}
</div>
{#if !view.state.readOnly}
<div class="rs-find-row">
<div class="rs-find-field">
<input type="text" name="replace" aria-label="Replace with" placeholder="Replace with" bind:value={() => replacement, value => { replacement = value; commit(); }} />
</div>
<div class="rs-find-actions">
<button class="rs-find-button" type="button" onclick={() => replaceNext(view)} disabled={!query.valid}>Replace</button>
<button class="rs-find-button" type="button" onclick={() => replaceAll(view)} disabled={!query.valid}>Replace all</button>
</div>
</div>
{/if}
</section>
+5
View File
@@ -5,6 +5,7 @@
import { defaultKeymap, history, historyField, historyKeymap, isolateHistory } from "@codemirror/commands";
import { markdown, markdownLanguage } from "@codemirror/lang-markdown";
import { searchKeymap } from "@codemirror/search";
import { writerSearch } from "./writerSearch";
import type { OpenBuffer } from "$lib/workspace/types";
import { workspaceReadDataUrl, workspaceReveal } from "$lib/workspace/api";
import { linkCandidates } from "$lib/workspace/markdownLinks";
@@ -159,6 +160,7 @@
history({ joinToEvent: (transaction, adjacent) => adjacent || transaction.isUserEvent("input.type.table") }), drawSelection(),
EditorView.theme({}, { dark: true }),
tableNavigation,
writerSearch,
keymap.of([{ key: "Escape", run: view => {
if (!view.state.field(imageSource)) return false;
view.dispatch({ effects: editImageSource.of(null) }); return true;
@@ -335,6 +337,9 @@
.live-writer :global(.writer-highlight) { background: #e7c56a38; color: var(--rs-text-strong); }
.live-writer :global(.writer-math) { font-family: var(--rs-font-mono); font-style: italic; }
.live-writer :global(.writer-comment) { color: var(--rs-text-dim); background: var(--rs-bg-active); }
.live-writer :global(.writer-divider-line) { display: flex; align-items: center; }
.live-writer :global(.writer-divider) { display: inline-flex; align-items: center; flex: 1; min-width: 0; height: 1.75em; cursor: text; }
.live-writer :global(.writer-divider::after) { content: ""; width: 100%; border-top: 1px solid var(--rs-border); }
.live-writer :global(.writer-checkbox) { display: inline-block; position: static; width: 15px; height: 15px; margin: 0 .55em 0 0; vertical-align: -.1em; accent-color: var(--rs-accent); cursor: pointer; }
/* Keep child margins inside the measured widget; collapsed margins otherwise
shift mouse hit-testing to the next line after a table or other preview. */
+29 -5
View File
@@ -13,6 +13,23 @@ import { imageSource, editImageSource, imageSourceRanges } from "./imageSource";
export const refreshImages = StateEffect.define<void>();
const imageCleanups = new WeakMap<HTMLElement, () => void>();
class Divider extends WidgetType {
eq(other: Divider) { return other instanceof Divider; }
toDOM(view: EditorView) {
const dom = document.createElement("span");
dom.className = "writer-divider";
dom.setAttribute("role", "separator");
dom.addEventListener("mousedown", event => {
if (event.button !== 0 || event.shiftKey) return;
event.preventDefault();
view.dispatch({ selection: { anchor: view.posAtDOM(dom) }, scrollIntoView: true });
view.focus();
});
return dom;
}
ignoreEvent() { return false; }
}
class Checkbox extends WidgetType {
constructor(readonly checked: boolean) { super(); }
eq(other: Checkbox) { return this.checked === other.checked; }
@@ -94,7 +111,7 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
if (revealed) ranges.push(Decoration.widget({ widget, block: true, side: 1 }).range(node.to));
else {
hiddenBlocks.push(node);
ranges.push(Decoration.replace({ widget, block }).range(node.from, node.to));
ranges.push(Decoration.replace({ widget, block, atomic: true }).range(node.from, node.to));
}
});
continue;
@@ -106,10 +123,10 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
continue;
}
hiddenBlocks.push({ from: section.start, to: end });
ranges.push(Decoration.replace({ widget: new Preview(section.html, source.slice(section.start, end), actions), block: true }).range(section.start, end));
ranges.push(Decoration.replace({ widget: new Preview(section.html, source.slice(section.start, end), actions), block: true, atomic: true }).range(section.start, end));
}
const hidden = (from: number, to: number) => hiddenBlocks.some(block => block.from <= from && block.to >= to);
const replace = (from: number, to: number) => { if (to > from) ranges.push(Decoration.replace({}).range(from, to)); };
const replace = (from: number, to: number) => { if (to > from) ranges.push(Decoration.replace({ atomic: true }).range(from, to)); };
const mark = (from: number, to: number, className: string) => { if (to > from) ranges.push(Decoration.mark({ class: className }).range(from, to)); };
const codeRanges: { from: number; to: number }[] = [];
syntaxTree(state).iterate({ enter(node) {
@@ -124,6 +141,11 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
ranges.push(Decoration.line({ class: `writer-heading writer-h${node.name.at(-1)}` }).range(line.from));
}
if (selected(node.from, node.to)) return;
if (node.name === "HorizontalRule") {
ranges.push(Decoration.line({ class: "writer-divider-line" }).range(line.from));
ranges.push(Decoration.replace({ widget: new Divider(), atomic: true }).range(node.from, node.to));
return false;
}
if (node.name === "HeaderMark") replace(node.from, Math.min(line.to, node.to + 1));
if (node.name === "StrongEmphasis") mark(node.from, node.to, "writer-strong");
if (node.name === "Emphasis") mark(node.from, node.to, "writer-emphasis");
@@ -148,7 +170,7 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
if (node.name === "TaskMarker") {
const match = /^(\s*)([-+*]\s+)\[([ xX])\]\s?/.exec(line.text);
if (!match) return;
ranges.push(Decoration.replace({ widget: new Checkbox(match[3].toLowerCase() === "x") }).range(line.from + match[1].length, line.from + match[0].length));
ranges.push(Decoration.replace({ widget: new Checkbox(match[3].toLowerCase() === "x"), atomic: true }).range(line.from + match[1].length, line.from + match[0].length));
if (match[3].toLowerCase() === "x") mark(line.from + match[0].length, line.to, "writer-completed");
}
} });
@@ -168,6 +190,8 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
update(value, transaction) {
return transaction.docChanged || transaction.selection || transaction.effects.some(effect => effect.is(refreshImages) || effect.is(editImageSource)) ? build(transaction.state) : value;
},
provide: field => [EditorView.decorations.from(field), EditorView.atomicRanges.of(view => view.state.field(field))],
provide: field => [EditorView.decorations.from(field), EditorView.atomicRanges.of(view => view.state.field(field).update({
filter: (_from, _to, decoration) => decoration.spec.atomic === true,
}))],
})];
}
+23
View File
@@ -0,0 +1,23 @@
import { flushSync, mount, unmount } from "svelte";
import { getSearchQuery, search, type SearchQuery } from "@codemirror/search";
import FindPanel from "./FindPanel.svelte";
export const writerSearch = search({
top: true,
createPanel(view) {
const dom = document.createElement("div");
dom.className = "rs-writer-search";
let panel: { sync(query: SearchQuery): void } | undefined;
return {
dom,
top: true,
mount() {
panel = mount(FindPanel, { target: dom, props: { view } });
flushSync();
dom.querySelector<HTMLInputElement>("[main-field]")?.select();
},
update() { panel?.sync(getSearchQuery(view.state)); },
destroy() { if (panel) void unmount(panel); },
};
},
});