diff --git a/pkgs/id/web/src/goto-line.test.ts b/pkgs/id/web/src/goto-line.test.ts index 94168ccef2..d552eaaa54 100644 --- a/pkgs/id/web/src/goto-line.test.ts +++ b/pkgs/id/web/src/goto-line.test.ts @@ -17,13 +17,7 @@ import { createGotoLinePlugin, destroyGotoLineDialog } from "./goto-line"; function createStateWithGotoLine(docContent?: string): EditorState { const plugin = createGotoLinePlugin(); const doc = docContent - ? rawSchema.node("doc", null, [ - rawSchema.node( - "code_block", - null, - docContent ? [rawSchema.text(docContent)] : [], - ), - ]) + ? rawSchema.node("doc", null, [rawSchema.node("code_block", null, docContent ? [rawSchema.text(docContent)] : [])]) : undefined; return EditorState.create({ schema: rawSchema, @@ -53,11 +47,7 @@ function triggerGotoLine(view: EditorView): boolean { // eslint-disable-next-line @typescript-eslint/no-explicit-any -- handleKeyDown this context workaround const handler = (plugin.props as any).handleKeyDown; if (!handler) return false; - return handler.call( - plugin, - view, - new KeyboardEvent("keydown", { key: "g", ctrlKey: true }), - ); + return handler.call(plugin, view, new KeyboardEvent("keydown", { key: "g", ctrlKey: true })); } // ── Tracked resources for cleanup ────────────────────────────────── @@ -204,9 +194,7 @@ describe("go-to-line command", () => { triggerGotoLine(view); - const input = container.querySelector( - ".goto-line-input", - ) as HTMLInputElement; + const input = container.querySelector(".goto-line-input") as HTMLInputElement; expect(input).not.toBeNull(); // 3 lines in doc → placeholder should contain "3" expect(input.placeholder).toContain("3"); @@ -239,13 +227,9 @@ describe("dialog input interaction", () => { const dialog = container.querySelector(".goto-line-dialog") as HTMLElement; expect(dialog).not.toBeNull(); - const input = container.querySelector( - ".goto-line-input", - ) as HTMLInputElement; + const input = container.querySelector(".goto-line-input") as HTMLInputElement; // Simulate pressing Escape on the input - input.dispatchEvent( - new KeyboardEvent("keydown", { key: "Escape", bubbles: true }), - ); + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true })); expect(dialog.style.display).toBe("none"); }); @@ -258,14 +242,10 @@ describe("dialog input interaction", () => { triggerGotoLine(view); const dialog = container.querySelector(".goto-line-dialog") as HTMLElement; - const input = container.querySelector( - ".goto-line-input", - ) as HTMLInputElement; + const input = container.querySelector(".goto-line-input") as HTMLInputElement; input.value = "1"; - input.dispatchEvent( - new KeyboardEvent("keydown", { key: "Enter", bubbles: true }), - ); + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); expect(dialog.style.display).toBe("none"); }); @@ -276,17 +256,13 @@ describe("dialog input interaction", () => { triggerGotoLine(view); - const input = container.querySelector( - ".goto-line-input", - ) as HTMLInputElement; + const input = container.querySelector(".goto-line-input") as HTMLInputElement; input.value = "2"; // Before: cursor is at default position const _posBefore = view.state.selection.from; - input.dispatchEvent( - new KeyboardEvent("keydown", { key: "Enter", bubbles: true }), - ); + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); // After: cursor should have moved (to line 2 start) // In rawSchema: doc > code_block > text "first\nsecond\nthird" @@ -304,36 +280,25 @@ describe("dialog input interaction", () => { ["2.9", "fractional"], ["999", "out of range"], ["", "empty"], - ])( - "Enter with %s (%s) does not move the cursor and keeps the dialog open", - (value) => { - const { view, container } = createViewWithGotoLine( - "first\nsecond\nthird", - ); - views.push(view); - containers.push(container); - - triggerGotoLine(view); - - const dialog = container.querySelector( - ".goto-line-dialog", - ) as HTMLElement; - const input = container.querySelector( - ".goto-line-input", - ) as HTMLInputElement; - input.value = value; - const posBefore = view.state.selection.from; - - input.dispatchEvent( - new KeyboardEvent("keydown", { key: "Enter", bubbles: true }), - ); - - // #712: invalid input must not be silently reinterpreted (0 -> line 1, - // 2.9 -> line 2) or swallowed (999) — no move, dialog stays open. - expect(view.state.selection.from).toBe(posBefore); - expect(dialog.style.display).not.toBe("none"); - }, - ); + ])("Enter with %s (%s) does not move the cursor and keeps the dialog open", (value) => { + const { view, container } = createViewWithGotoLine("first\nsecond\nthird"); + views.push(view); + containers.push(container); + + triggerGotoLine(view); + + const dialog = container.querySelector(".goto-line-dialog") as HTMLElement; + const input = container.querySelector(".goto-line-input") as HTMLInputElement; + input.value = value; + const posBefore = view.state.selection.from; + + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); + + // #712: invalid input must not be silently reinterpreted (0 -> line 1, + // 2.9 -> line 2) or swallowed (999) — no move, dialog stays open. + expect(view.state.selection.from).toBe(posBefore); + expect(dialog.style.display).not.toBe("none"); + }); it("Enter with line 1 moves cursor to the first line start", () => { const { view, container } = createViewWithGotoLine("first\nsecond"); @@ -342,14 +307,10 @@ describe("dialog input interaction", () => { triggerGotoLine(view); - const input = container.querySelector( - ".goto-line-input", - ) as HTMLInputElement; + const input = container.querySelector(".goto-line-input") as HTMLInputElement; input.value = "1"; - input.dispatchEvent( - new KeyboardEvent("keydown", { key: "Enter", bubbles: true }), - ); + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); // Line 1 starts at the code_block's content start: the block sits // at pos 0, so its text starts at doc pos 1 (#745 doc-walk mapping). @@ -383,13 +344,9 @@ describe("go-to-line across textblocks (#745)", () => { } function gotoLine(container: HTMLElement, value: string): void { - const input = container.querySelector( - ".goto-line-input", - ) as HTMLInputElement; + const input = container.querySelector(".goto-line-input") as HTMLInputElement; input.value = value; - input.dispatchEvent( - new KeyboardEvent("keydown", { key: "Enter", bubbles: true }), - ); + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); } it("rich doc of three paragraphs reports 3 lines and jumps to the second paragraph", () => { @@ -401,9 +358,7 @@ describe("go-to-line across textblocks (#745)", () => { const { view, container } = createViewFromDoc(richSchema, doc); triggerGotoLine(view); - const input = container.querySelector( - ".goto-line-input", - ) as HTMLInputElement; + const input = container.querySelector(".goto-line-input") as HTMLInputElement; // textContent is "alphabetagamma" (no separators) — the old count said 1. expect(input.placeholder).toContain("3"); expect(input.max).toBe("3"); @@ -421,9 +376,7 @@ describe("go-to-line across textblocks (#745)", () => { const { view, container } = createViewFromDoc(rawSchema, doc); triggerGotoLine(view); - const input = container.querySelector( - ".goto-line-input", - ) as HTMLInputElement; + const input = container.querySelector(".goto-line-input") as HTMLInputElement; expect(input.max).toBe("4"); gotoLine(container, "3"); @@ -445,9 +398,7 @@ describe("go-to-line across textblocks (#745)", () => { const { view, container } = createViewFromDoc(richSchema, doc); triggerGotoLine(view); - const input = container.querySelector( - ".goto-line-input", - ) as HTMLInputElement; + const input = container.querySelector(".goto-line-input") as HTMLInputElement; expect(input.max).toBe("4"); gotoLine(container, "3"); diff --git a/pkgs/id/web/src/goto-line.ts b/pkgs/id/web/src/goto-line.ts index 544310faef..1c410d5e5e 100644 --- a/pkgs/id/web/src/goto-line.ts +++ b/pkgs/id/web/src/goto-line.ts @@ -99,20 +99,14 @@ function openGotoLine(view: EditorView): void { const line = Number(raw); const lineCount = activeView ? getLineCount(activeView) : 0; const pos = - activeView && - raw !== "" && - Number.isInteger(line) && - line >= 1 && - line <= lineCount + activeView && raw !== "" && Number.isInteger(line) && line >= 1 && line <= lineCount ? getLineStartPos(activeView, line) : null; if (pos === null || !activeView) { dialogInput?.select(); return; } - const tr = activeView.state.tr.setSelection( - TextSelection.create(activeView.state.doc, pos), - ); + const tr = activeView.state.tr.setSelection(TextSelection.create(activeView.state.doc, pos)); activeView.dispatch(tr); activeView.focus(); // Scroll the cursor into view diff --git a/pkgs/id/web/src/search-panel.ts b/pkgs/id/web/src/search-panel.ts index 4a044bf9a7..07e9155de3 100644 --- a/pkgs/id/web/src/search-panel.ts +++ b/pkgs/id/web/src/search-panel.ts @@ -69,18 +69,15 @@ function updateMatchCount(view: EditorView): void { return; } const total = countMatches(view); - panelState.matchCountEl.textContent = - total === 0 ? "No results" : `${total} match${total === 1 ? "" : "es"}`; + panelState.matchCountEl.textContent = total === 0 ? "No results" : `${total} match${total === 1 ? "" : "es"}`; } /** Push current input values into prosemirror-search state. */ function syncQuery(view: EditorView): void { const searchText = panelState.searchInput?.value ?? ""; const replaceText = panelState.replaceInput?.value ?? ""; - const caseCheckbox = - panelState.panelEl?.querySelector("#search-case"); - const regexCheckbox = - panelState.panelEl?.querySelector("#search-regex"); + const caseCheckbox = panelState.panelEl?.querySelector("#search-case"); + const regexCheckbox = panelState.panelEl?.querySelector("#search-regex"); const query = new SearchQuery({ search: searchText, @@ -192,12 +189,8 @@ function setupPanelEvents(): void { }); // Case/regex toggles - panelEl - .querySelector("#search-case") - ?.addEventListener("change", () => syncQuery(view)); - panelEl - .querySelector("#search-regex") - ?.addEventListener("change", () => syncQuery(view)); + panelEl.querySelector("#search-case")?.addEventListener("change", () => syncQuery(view)); + panelEl.querySelector("#search-regex")?.addEventListener("change", () => syncQuery(view)); } /** Open find panel, optionally showing replace. */ @@ -210,24 +203,16 @@ function openPanel(view: EditorView, showReplace: boolean): void { // is detached with the old container and its handlers still point at the // destroyed view — and no production code calls destroySearchPanel() on // teardown. Rebind instead of "opening" a detached node (#718). - if ( - panelState.panelEl && - (panelState.view !== view || panelState.panelEl.parentElement !== container) - ) { + if (panelState.panelEl && (panelState.view !== view || panelState.panelEl.parentElement !== container)) { destroySearchPanel(); } if (!panelState.panelEl) { panelState.panelEl = createPanel(container); panelState.searchInput = panelState.panelEl.querySelector("#search-input"); - panelState.replaceInput = - panelState.panelEl.querySelector("#replace-input"); - panelState.matchCountEl = panelState.panelEl.querySelector( - "#search-match-count", - ); - panelState.replaceRow = panelState.panelEl.querySelector( - "#search-replace-row", - ); + panelState.replaceInput = panelState.panelEl.querySelector("#replace-input"); + panelState.matchCountEl = panelState.panelEl.querySelector("#search-match-count"); + panelState.replaceRow = panelState.panelEl.querySelector("#search-replace-row"); panelState.view = view; setupPanelEvents(); }