Repository navigation
Copy markdown source across node boundaries (#278 part 2) - #2026
Conversation
Part 1 stopped the view-container click handler from collapsing a cross-node text selection. This adds the copy half: a `copy` listener on the viewer scroll container turns a selection spanning more than one node into the underlying node *source* markdown (with syntax) instead of the browser default — rendered text with markdown already stripped and no node boundaries. - Detects a multi-node span via the `[data-node-id]` ancestors of the range endpoints; a single-node selection is left to native copy (returns null). - Clips the first and last node to the selection offsets by mapping the rendered offset (counted exactly like extractTextWithLineBreaks — text length, +1 per <br>) to a source offset via mapViewPositionToEditPosition, the same mapping click-to-edit-at-character uses. Middle nodes are copied whole. - Preserves nesting as indentation relative to the shallowest node in the range. - Sets text/plain only: the markdown source is the useful representation; a rendered-text HTML flavor would just re-strip what we're trying to preserve. The offset logic lives in a pure, DOM-testable module (cross-node-copy.ts) with tests for partial-first/partial-last, full middle nodes, cross-hierarchy indentation, single-node passthrough, and <br>/markdown-syntax edge mapping. ClipboardEvent added to the eslint DOM globals alongside its event siblings. Co-Authored-By: Claude <[email protected]>
…chips
Inline references render as `<a class="ns-noderef" data-node-id={refId}>` inside
a node's view, so `[data-node-id]` is not unique to node roots. The span
collection and endpoint resolution now scope to `.node[data-node-id]` (node roots
carry class `node`), so a spanned node containing a reference no longer injects a
phantom line or resolves an endpoint to the referenced node instead of the
containing one. Added two tests with reference chips.
Also removed the dead selection-direction swap (a DOM Range is always normalized,
so the first spanned node is always the range start), and documented the two
source-slice limitations at a clip boundary (reference/link source syntax isn't
offset-mapped; a boundary inside a paired inline marker can slice mid-pair).
Co-Authored-By: Claude <[email protected]>
The two reference-chip tests passed against the pre-fix code, so they locked in
nothing. Strengthen them so they fail without the node-root scoping fix:
- make the referenced node 'z' resolvable with distinct content and assert the
output contains no phantom line from it (pre-fix would splice a third line);
- assert the clipped value of the last node ("ref he"), which the pre-fix code
got wrong (it left the node unclipped as "ref here" because the chip was
treated as the end node).
Verified: both tests fail against the pre-fix `[data-node-id]` selector and pass
against `.node[data-node-id]`.
Co-Authored-By: Claude <[email protected]>
|
APPROVE (self-review, mstomar125) Two-pass adversarial review. First pass caught a real MAJOR correctness bug: Second pass confirmed the production fix correct and complete, but flagged that the two new reference-chip tests passed against the pre-fix code (locked in nothing). Fixed in Gate green on each push; full suite 196 files / 4511 tests; lint + svelte-check clean. |
Summary
Part 2 of #278 (the copy half). Part 1 (#2021) stopped the view-container click handler from collapsing a cross-node text selection. This turns that selection into useful clipboard content on
copy: the underlying node source markdown (with syntax), clipped to the selection at the first/last node and indented to preserve nesting — instead of the browser default, which is the rendered text (markdown already stripped byViewModeRenderer) with no node boundaries.Behavior
copylistener on the viewer scroll container detects a selection spanning more than one node (via the[data-node-id]ancestors of the range endpoints). A single-node selection is left entirely to native copy.text/plainonly. The markdown source is the useful representation here; a rendered-text HTML flavor would just re-strip what we're preserving.The delicate part: rendered ↔ source offsets
Clipping maps a DOM selection offset (in the syntax-stripped rendered view) back to an offset in the source markdown. Rendered offsets are counted exactly the way
base-node.svelte'sextractTextWithLineBreakscounts them — text length, +1 per<br>— then mapped withmapViewPositionToEditPosition, the same mapping the click-to-edit-at-character handler already uses. So a click landing the caret on character N and a selection endpoint at character N resolve to the same source position.This logic lives in a pure, DOM-testable module (
lib/utils/cross-node-copy.ts), separate from the component wiring.Tests
cross-node-copy.test.tscovers: partial-first/partial-last clipping, full middle nodes, cross-hierarchy indentation, single-node passthrough (returns null → native copy), collapsed-selection passthrough, out-of-element offset handling, and the two edge cases that matter most — markdown syntax at the edges (**Hello** world→ clip at renderedw→ sourceworld) and<br>counted as one char.Full suite: 196 files / 4510 tests green; lint +
svelte-checkclean. (ClipboardEventadded to the eslint DOM globals alongside its event siblings.)Acceptance criteria
Part of #278. (Paste and block/bulk selection remain out of scope per the issue.)
🤖 Generated with Claude Code