Skip to content

Preserve cross-node text selection on drag (#278 part 1) - #2021

Merged
mstomar125 merged 2 commits into
mainfrom
issue-278-cross-node-copy
Aug 10, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-278-cross-node-copy

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

Part 1 of #278. A page of nodes is ordinary DOM — only the focused node is a textarea, every other renders as a plain <div> — so native cross-node text selection is structurally possible. The bug: the view-container click handler focused the node (swapping in a textarea) on every click, including the mouseup that ends a drag, collapsing the selection before it could be copied.

Change

  • base-node.svelte: guard the view-container onclick — skip focusing when there's an active (non-collapsed) text selection, so a drag-select survives. A genuine click leaves a collapsed selection and still lands the caret on the clicked character, so click-to-edit is unchanged (existing click-to-cursor tests still pass).
  • New $lib/utils/text-selection.ts → isActiveTextSelection(selection) (rangeCount > 0 && !isCollapsed), extracted + tested, and reused by the part-2 copy handler.
  • a11y: the view content is selectable, editable prose, not a button — corrected role="button" → role="textbox" + aria-multiline.

Scope

This delivers the issue's core "Section 1 — Preserve the selection on drag" (the literal reported symptom: selection collapses before copy). Part 2 — the copy handler that assembles markdown source across the spanned nodes (offset-clipping the first/last, indenting by hierarchy) — is a larger, delicate piece and remains open under #278; its full design is documented on the issue. Kept separate so the selection-preservation fix ships now without rushing the copy-assembly offset mapping.

Tests

+4 text-selection unit tests (null / collapsed / non-empty range / no-range). click-to-cursor (17) still green. Full frontend suite green (193 files / 4463 tests); eslint + svelte-check clean; pre-push gate passed.

🤖 Generated with Claude Code

mstomar125 and others added 2 commits August 10, 2026 12:57
The view-container click handler focused the node (swapping the div for a
textarea) on every click, including the mouseup that ends a drag — collapsing
any text selection before it could be copied. A page is ordinary DOM (only the
focused node is a textarea), so native cross-node selection is structurally
possible; the interaction just had to stop stealing it.

Guard the handler: skip focusing when there is an active (non-collapsed) text
selection, so a drag-select survives; a genuine click leaves a collapsed
selection and still lands the caret on the clicked character (click-to-edit
unchanged). Extract the decision into a tested isActiveTextSelection helper
(reused by the copy handler in part 2). Also correct the view content's
role="button" to role="textbox"/aria-multiline — it is selectable, editable
prose, not a button.

Part 1 of #278; the copy handler (assemble markdown source across the spanned
nodes) follows in part 2.

Co-Authored-By: Claude <[email protected]>
The view content isn't editable until focus swaps in the real textarea, so
aria-readonly="true" makes the announced role match reality.

Co-Authored-By: Claude <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Pragmatic Code Review + /address-review

Verdict: APPROVE. Both load-bearing questions land favorably:

  • Click-to-edit does not regress — a single click's mousedown collapses any prior selection and click fires after mouseup, so the guard sees a collapsed selection and focus proceeds (caret on the clicked char, unchanged); a drag leaves a non-empty range → guard skips focus → selection survives. Click-immediately-after-a-selection is safe (the second mousedown collapses first).
  • Part 1 alone is a safe, net-positive intermediate state — before: cross-node drag collapsed → Cmd+C useless; after: selection survives → Cmd+C yields the browser default (stripped text). That's strictly more than nothing; Part 2 upgrades it to markdown source. Single-node selections keep native behavior. No garbled/worse-than-before path.

Guard placement (after readonly, before focus) correct; isActiveTextSelection well-tested (null/collapsed/range/zero-range); svelte-a11y lint sound.

🟡 addressed: added aria-readonly="true" to the role="textbox" view div — the view isn't editable until focus swaps in the real textarea, so this makes the announced role honest.

🟡 documented (intended behavior): shift/double/triple-click now leave the node unfocused (they produce non-collapsed ranges → treated as selection gestures, not edit gestures). Double-click-to-edit was already effectively unavailable via the collapsed path, so no real loss — noted so Part 2 treats it as known.

Re-Review Decision: NO RE-REVIEW NEEDED — the only follow-up commit adds one ARIA attribute.

@mstomar125
mstomar125 merged commit ff221ba into main Aug 10, 2026
@mstomar125
mstomar125 deleted the issue-278-cross-node-copy branch August 10, 2026 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant