From 777cab2cc162c4fd7a5615ab934591c4a36373bb Mon Sep 17 00:00:00 2001 From: Knotty Wyvern Date: Wed, 28 Oct 2026 00:19:00 -0700 Subject: [PATCH] Include the 0.1.44a link selection fix A mouse drag across rendered links keeps the layout stable until the mouse button is released. The selection then includes the hidden link syntax at its ends. This fix shipped in the 0.1.44a installer, but it was not committed. editor/linkSelection is its smoke test on the harness. --- src/lib/editor/liveMarkdown.ts | 55 ++++++++++++++++-- tests/smoke/editor/linkSelection.smoke.mjs | 67 ++++++++++++++++++++++ 2 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 tests/smoke/editor/linkSelection.smoke.mjs diff --git a/src/lib/editor/liveMarkdown.ts b/src/lib/editor/liveMarkdown.ts index 18f3438..2d4b252 100644 --- a/src/lib/editor/liveMarkdown.ts +++ b/src/lib/editor/liveMarkdown.ts @@ -1,5 +1,5 @@ import { tr } from '../i18n/index.ts'; -import { StateEffect, StateField, type EditorState, type Range } from "@codemirror/state"; +import { EditorSelection, StateEffect, StateField, type EditorState, type Range } from "@codemirror/state"; import { Decoration, EditorView, ViewPlugin, WidgetType, type DecorationSet } from "@codemirror/view"; import { syntaxTree, foldedRanges, foldEffect, unfoldEffect } from "@codemirror/language"; import { renderMarkdownSections } from "../workspace/noteHtml"; @@ -158,6 +158,15 @@ class Preview extends WidgetType { } export function liveMarkdown(urls: Map, actions: TableContextActions = {}, images: ImageContext = {}) { + const selecting = StateEffect.define(); + const mouseSelecting = StateField.define({ + create: () => false, + update(value, transaction) { + if (transaction.docChanged) return false; + for (const effect of transaction.effects) if (effect.is(selecting)) value = effect.value; + return value; + }, + }); const focusBody = StateEffect.define(); const bodyFocused = StateField.define({ create: () => false, @@ -321,16 +330,54 @@ export function liveMarkdown(urls: Map, actions: TableContextAct } return Decoration.set(ranges, true); } - return [imageSource, linkTooltips, bodyFocused, syncBodyFocus, EditorView.focusChangeEffect.of((_state, focusing) => focusBody.of(focusing)), StateField.define({ + const decorations = StateField.define({ create: build, update(value, transaction) { + // Preserve the rendered geometry until the pointer gesture ends. + if (transaction.state.field(mouseSelecting) && !transaction.docChanged) return value; return transaction.docChanged || transaction.selection || syntaxTree(transaction.startState) !== syntaxTree(transaction.state) - || transaction.effects.some(effect => effect.is(focusBody) || effect.is(refreshImages) || effect.is(editImageSource) || effect.is(foldEffect) || effect.is(unfoldEffect)) + || transaction.effects.some(effect => effect.is(selecting) || effect.is(focusBody) || effect.is(refreshImages) || effect.is(editImageSource) || effect.is(foldEffect) || effect.is(unfoldEffect)) ? build(transaction.state, value.map(transaction.changes)) : value; }, provide: field => [EditorView.decorations.from(field), EditorView.atomicRanges.of(view => view.state.field(field).update({ filter: (_from, _to, decoration) => decoration.spec.atomic === true, }))], - })]; + }); + const mousePreview = ViewPlugin.define(view => { + const finish = () => { + if (!view.state.field(mouseSelecting)) return; + const selection = view.state.selection; + const ranges = selection.ranges.map(range => { + if (range.empty) return range; + let from = range.from, to = range.to; + // Include hidden syntax at the selected label's exposed boundaries. + view.state.field(decorations).between(0, view.state.doc.length, (start, end, decoration) => { + if (decoration.spec.atomic !== true || decoration.spec.widget) return; + if (range.from >= start && range.from <= end) from = Math.min(from, start); + if (range.to >= start && range.to <= end) to = Math.max(to, end); + }); + return EditorSelection.range(range.anchor <= range.head ? from : to, range.anchor <= range.head ? to : from); + }); + view.dispatch({ selection: EditorSelection.create(ranges, selection.mainIndex), effects: selecting.of(false) }); + }; + const release = () => queueMicrotask(finish); + const start = (event: MouseEvent) => { + if (event.button || event.ctrlKey || event.metaKey || !(event.target instanceof Element) + || event.target.closest(".writer-preview, button, input")) return; + view.dispatch({ effects: selecting.of(true) }); + }; + view.contentDOM.addEventListener("mousedown", start); + document.addEventListener("mouseup", release); + document.addEventListener("dragend", release); + window.addEventListener("blur", finish); + return { destroy() { + view.contentDOM.removeEventListener("mousedown", start); + document.removeEventListener("mouseup", release); + document.removeEventListener("dragend", release); + window.removeEventListener("blur", finish); + } }; + }); + return [imageSource, linkTooltips, bodyFocused, mouseSelecting, decorations, mousePreview, syncBodyFocus, + EditorView.focusChangeEffect.of((_state, focusing) => focusBody.of(focusing))]; } diff --git a/tests/smoke/editor/linkSelection.smoke.mjs b/tests/smoke/editor/linkSelection.smoke.mjs new file mode 100644 index 0000000..d4c3e98 --- /dev/null +++ b/tests/smoke/editor/linkSelection.smoke.mjs @@ -0,0 +1,67 @@ +// Mouse drag selection across a heading and a rendered link keeps the layout stable and selects the hidden link syntax. +import assert from "node:assert/strict"; +import path from "node:path"; +import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs"; + +await runSmoke("editor/linkSelection", async ({ page }) => { + page.setDefaultTimeout(15000); + // The original mock returned null for commands it did not handle. + await mockTauri(page, { + strict: false, + commands: test => ({ + ...test.workspace({ root: "C:\\tab-menu-workspace", files: { "a.md": "# Links\n\nOriginal text.\n" }, folders: ["notes"] }), + workspace_search: () => [], + "plugin:window|is_maximized": () => false, + plugins_snapshot: () => ({ root: "C:/plugins", plugins: [] }), + spelling_suggestions: () => ({ language: "en-US", words: [], misspelled: false }), + "plugin:dialog|message": () => "Cancel", + }), + }); + const output = await outputDir("editor/linkSelection"); + async function reset(text) { + await page.locator(".cm-content").evaluate(async (node, text) => { + const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js"); + const view = EditorView.findFromDOM(node); + view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: text }, selection: { anchor: 0 } }); + view.focus(); + }, text); + } + async function selected() { + return page.locator(".cm-content").evaluate(async node => { + const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js"); + const view = EditorView.findFromDOM(node), range = view.state.selection.main; + return view.state.sliceDoc(range.from, range.to); + }); + } + + await page.goto(smokeUrl); + await page.locator('[data-tree-rel="a.md"]').click(); + await page.locator(".cm-content").waitFor(); + await page.getByRole("button", { name: "Files", exact: true }).click(); + await page.getByRole("button", { name: "Hide outline", exact: true }).click(); + for (const theme of ["dark", "light"]) for (const width of [1440, 620]) { + await page.setViewportSize({ width, height: 1000 }); + await page.evaluate(async mode => (await import("/src/lib/theme/appearance.ts")).saveAppearance({ theme: "default", mode }), theme); + for (const link of ["[gpt-oss-120b-heretic](https://huggingface.co/kldzj/gpt-oss-120b-heretic)", "[[models/gpt-oss-120b-heretic|gpt-oss-120b-heretic]]"]) { + const heading = "### Agentic Coding (DeepSWE 1.1)"; + const text = "Outside.\n\n" + heading + "\n" + link + "\n\nEnd."; + for (const reverse of [false, true]) { + await reset(text); + const title = await page.locator(".cm-line").filter({ hasText: "Agentic Coding" }).boundingBox(); + const label = await page.locator(".writer-link").boundingBox(); + const start = { x: title.x + 1, y: title.y + title.height / 2 }; + const end = { x: label.x + label.width + 2, y: label.y + label.height / 2 }; + await page.mouse.move(reverse ? end.x : start.x, reverse ? end.y : start.y); + await page.mouse.down(); + await page.mouse.move(reverse ? start.x : end.x, reverse ? start.y : end.y, { steps: 12 }); + await page.mouse.up(); + assert.equal(await selected(), heading + "\n" + link, `${theme}/${width}/${reverse}/${link}`); + await page.screenshot({ path: path.join(output, `${theme}-${width}-${reverse}-${link.startsWith("[[") ? "wiki" : "markdown"}.png`) }); + } + await reset(text); + await page.locator(".writer-link").click({ position: { x: 30, y: 8 } }); + assert.equal(await selected(), "", "A click places the caret without selecting the link"); + assert.equal(await page.locator(".writer-link-source").count(), 1); + } + } +});