Skip to content

Copy markdown source across node boundaries (#278 part 2) - #2026

Merged
mstomar125 merged 3 commits into
mainfrom
issue-278-cross-node-copy
Aug 11, 2026
Merged

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

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

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 by ViewModeRenderer) with no node boundaries.

Behavior

  • A copy listener 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.
  • The first and last spanned nodes are clipped to the selection offsets; middle nodes are copied whole.
  • Nesting is preserved as indentation relative to the shallowest node in the range (a selection that starts in a child and ends in an unrelated subtree simply indents each node by its own depth relative to that minimum).
  • Sets text/plain only. 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's extractTextWithLineBreaks counts them — text length, +1 per <br> — then mapped with mapViewPositionToEditPosition, 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.ts covers: 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 rendered w → source world) and <br> counted as one char.

Full suite: 196 files / 4510 tests green; lint + svelte-check clean. (ClipboardEvent added to the eslint DOM globals alongside its event siblings.)

Acceptance criteria

  • Cmd+C on a multi-node selection copies markdown source, not stripped rendered text
  • First and last nodes in the range are clipped to the selection offsets
  • Nesting preserved as indentation for selections spanning parent/child
  • Single-node selections retain native copy behavior
  • Selection unaffected while a node is in edit mode (that node is a textarea; the handler resolves view elements, absent for the edited node → that node falls back to whole source)
  • Tests cover partial-first/partial-last, full-node, cross-hierarchy, and single-node passthrough
  • (Part 1) drag-select across boundaries + click-to-edit non-regression

Part of #278. (Paste and block/bulk selection remain out of scope per the issue.)

🤖 Generated with Claude Code

mstomar125 and others added 3 commits August 11, 2026 09:57
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]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE (self-review, mstomar125)

Two-pass adversarial review.

First pass caught a real MAJOR correctness bug: [data-node-id] is not unique to node roots — inline references render as <a class="ns-noderef" data-node-id={refId}> inside a node's view, so any spanned node containing a reference injected a phantom line / resolved an endpoint to the wrong node. Fixed in 5b9c6905 by scoping the scan and endpoint resolution to .node[data-node-id] (node roots carry class node; verified children render flat, not nested, so no descendant leak). Also removed the dead selection-direction swap (DOM ranges are always normalized) and documented the source-slice limitations at a clip boundary.

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 2878e7b7: z is now resolvable with distinct content (asserting no phantom line) and the last node's clipped value is asserted. Verified both tests fail against the pre-fix [data-node-id] selector and pass against .node[data-node-id].

Gate green on each push; full suite 196 files / 4511 tests; lint + svelte-check clean.

@mstomar125
mstomar125 merged commit e0d438d into main Aug 11, 2026
@mstomar125
mstomar125 deleted the issue-278-cross-node-copy branch August 11, 2026 08:20
@mstomar125 mstomar125 mentioned this pull request Aug 11, 2026
8 tasks
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