Skip to content

fix(editor): clamp paste mark range to document size in authored-tracker - #77

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-self-driving/fixeditor-clamp-paste-range-in-authored-4d3ddd
Draft

fix(editor): clamp paste mark range to document size in authored-tracker#77
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-self-driving/fixeditor-clamp-paste-range-in-authored-4d3ddd

Conversation

@posthog

@posthog posthog Bot commented Aug 27, 2026

Copy link
Copy Markdown

Problem

  • A user loses their paste entirely: the authored-tracker plugin owns the paste (returns true), then throws before the content lands, so nothing is inserted.
  • handlePaste marks the range insertFrom + slice.size, but slice.size counts the slice's open ends. Those ends merge into the surrounding blocks when you paste multiple blocks into a paragraph, so the real inserted range is shorter than slice.size.
  • With no clamp against the document size, removeMark walks past the end of the document, Fragment.nodesBetween reaches for a child that is absent, and you get TypeError: Cannot read properties of undefined (reading 'nodeSize').
  • Pasting multiple blocks into a paragraph is an everyday action, so the low occurrence count more likely reflects a fresh build than a rare edge case.

Changes

  • Clamp insertFrom and insertTo to tr.doc.content.size before the removeMark/addMark pair.
  • This makes the paste path consistent with the typing path: appendTransaction in the same file already clamps the same way.
-        const insertFrom = tr.mapping.map(from, -1);
-        const insertTo = insertFrom + slice.size;
+        const docSize = tr.doc.content.size;
+        const insertFrom = Math.max(0, Math.min(tr.mapping.map(from, -1), docSize));
+        const insertTo = Math.max(insertFrom, Math.min(insertFrom + slice.size, docSize));

Testing

  • npm test passes (74 checks).
  • tsc --noEmit reports no new errors for the changed file.

Created with PostHog Desktop from this inbox report.

handlePaste computed insertTo = insertFrom + slice.size with no clamp
against the document size. slice.size counts the slice's open ends, which
merge into surrounding blocks when you paste multiple blocks into a
paragraph, so removeMark walked past the end of the document and threw
"Cannot read properties of undefined (reading 'nodeSize')". The handler
returns true, so the plugin owned the paste and the user's content was
dropped.

Clamp insertFrom and insertTo to tr.doc.content.size before the
removeMark/addMark pair, matching the clamping the same file already does
in appendTransaction.

Generated-By: PostHog Desktop
Task-Id: ee2f2fca-abd7-4a4a-a035-2b37dea8bddc
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.

0 participants