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.
This commit is contained in:
Knotty Wyvern
2026-10-28 00:19:00 -07:00
parent 5d5ba9e1f7
commit 777cab2cc1
2 changed files with 118 additions and 4 deletions
+51 -4
View File
@@ -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<string, string>, actions: TableContextActions = {}, images: ImageContext = {}) {
const selecting = StateEffect.define<boolean>();
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<boolean>();
const bodyFocused = StateField.define({
create: () => false,
@@ -321,16 +330,54 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
}
return Decoration.set(ranges, true);
}
return [imageSource, linkTooltips, bodyFocused, syncBodyFocus, EditorView.focusChangeEffect.of((_state, focusing) => focusBody.of(focusing)), StateField.define<DecorationSet>({
const decorations = StateField.define<DecorationSet>({
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))];
}