Repository navigation
PT-4126: Copy hidden-marker views' notes as prose, without layout spacers - #49
Conversation
7041370 to
1c43c18
Compare
…cers Paste as Plain Text of a footnote copied in Simple mode (Paragraph Structure view) left two spaces at each end of the footnote text. The hidden-marker views lay a collapsed note out with an engine-owned NBSP spacer after its caller and after each run, and Lexical's copy wrote those into text/plain next to the real spaces around the note. HiddenMarkersViewCopyPlugin now writes these views' copies and cuts. Its text/plain is $selectionPlainText: the shown text without the spacers or an empty span's placeholder, with one ordinary space where a note's text meets the text around it and neither side has whitespace. text/html and the internal flavor stay Lexical's own, because the editor's own Ctrl+V reads text/html back and rebuilds the note from it. A range with no text to write, or a browser copy whose page selection runs outside the editor, is left to Lexical's copy. With invisible characters not allowed, the plugin also writes a stored no-break space as `~`, the respelling InvisibleCharactersPlugin's copy handler used to do over Lexical's text, spacers included. That handler ran first at the same priority, so it is removed and the plugin is the one owner of these views' copies; InvisibleCharactersPlugin keeps its typing and paste rules. CopyLimitPlugin's over-limit writer and fit use the same prose in these views, tildes included. MarkersViewCopyPlugin and the new plugin share one registration helper, registerViewCopyWriter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1c43c18 to
c207853
Compare
- Write a cut whose page selection runs outside the editor instead of declining it: Lexical's cut removes its range either way, so declining lost the text. - Count layout bytes (empty-span placeholders, note spacers) for nothing when the copy-limit fit places its end, so a limited copy is not short and never writes only raw U+00A0s. - Hold back text that displays as nothing (a direction mark) until the next visible text, so the space a note's edge adds lands on the right side of it. - Reuse $shouldIgnoreNodeForContentIndexes for $isLayoutText, share the `~` spelling choice (hiddenViewCopySpelling), drop the unused copyLimit prop and a clause that could never be false. - Correct the docs: the text/html spacer rationale, the over-limit copy's prose, the read-only cut in the CHANGELOG, where copy events come from, and ViewOptions.allowInvisibleCharacters for hidden-marker views. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # packages/platform/dist/index.js.map
The copy-limit fit leaves a copy or cut alone when the page selection runs outside the editor. Now that the hidden-view writer takes such a cut rather than declining it, dropping its copyLimit backstop let that cut write the whole range past the limit. Pass the limit again so $writeCopyPayload declines it, and pin it with a test. Also hoist the note key out of the loop callback (no-loop-func). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
katherinejensen00
left a comment
There was a problem hiding this comment.
Great work on this fix, Tim! Everything Claude found was either unlikely to happen or small and nit picky. I am attaching its findings in case any of them are helpful, but the code could merge as is so I am approving the pr.
1. Low: under a copy limit, an outside-the-editor cut removes its range with nothing copied
Attach at: packages/platform/src/editor/HiddenMarkersViewCopyPlugin.tsx:30 (also relevant: the test at HiddenMarkersViewCopyPlugin.test.tsx:462)
With a copy limit set, a cut whose page selection runs outside the editor writes nothing, but Lexical's cut still deletes the range. The doc's "declines it rather than write it whole" reads as if the limit protects the user here, when the text is actually lost.
Confirmed (jsdom).
- Repro:
- Setup:
PSALM_1_2,copyLimit: 10, select "but his…night.", page selection on a<p>outside the editor, thenCUT_COMMAND. text/plainis""anddefaultPreventedis false.- The document becomes
\id Psalms\n\n\q1, so the verse is gone. - In a real browser, the native cut, which no handler prevents and nothing limits, is the only thing that could write anything.
CopyLimitPlugin's page listener handlescopyonly.
- Setup:
- Pre-existing.
maindoes the same through Lexical's own cut, and Standard view does the same through$writeCopyPayload. But f71e781 changed the no-limit case for exactly this reason ("declining lost the text"), and the limited case still has the problem. - The test pins the backstop, not the decline:
- Passing
undefinedforcopyLimitfails it. That is what c78fd80 meant to pin. - Deleting the
limit !== undefined && isCopyFromOutsideEditordecline in$writeCopyPayloadstill passes. - Without that line the outcome is safer: the backstop writes a 10-character slice, claims the event and removes nothing.
- Passing
- Why only Low: for an editable root, Chromium normally clamps a selection at the editing boundary, and a read-only editor never removes. So this is hard to reach.
Suggested fix:
- Under a limit, have such a cut write the backstop slice without removing anything, or claim and block it (
preventDefault, no removal). Either way the range survives. - Add
expect(...).toContain("delight")on the document to the test, so it pins the outcome the user cares about. - If neither is in scope, reword the doc ("…declines it, and Lexical's cut then removes the range uncopied; see ticket …") and ticket it.
2. Low: registerViewCopyWriter's TSDoc gets the null dispatchers wrong
Attach at: packages/platform/src/editor/markerEdit/whitespaceDisplay.plugin.utils.ts:1457
The doc says HiddenContentDeleteGuardPlugin and ParaMarkerSelectionPlugin re-dispatch a copy with null. Both pass on the payload they were given, which can be a real ClipboardEvent.
Traced.
HiddenContentDeleteGuardPlugin.tsx:561:editor.dispatchCommand(COPY_COMMAND, event), which is the cut's own event from Lexical's rootcutlistener.ParaMarkerSelectionPlugin.tsx:367:editor.dispatchCommand(command, payload), often theKeyboardEventfrom Lexical's key-down path.- Only
copySelection/cutSelection(clipboard.utils.ts:122,128) passnull. Those are whatClipboardPlugin,ContextMenuPluginandEditorRefcall. - A reader relying on the current wording would assume
eventis alwaysundefinedon a guard's re-dispatch.
Suggested replacement:
* `event` is the command's payload when it has `clipboardData`, otherwise `undefined`. Lexical's
* root `copy`/`cut` listeners pass a `ClipboardEvent`, its key-down handler a `KeyboardEvent`, and
* `copySelection`/`cutSelection` (behind `ClipboardPlugin`, `ContextMenuPlugin`, `EditorRef`)
* `null`. Guards that re-dispatch (`HiddenContentDeleteGuardPlugin`, `ParaMarkerSelectionPlugin`)
* pass on whatever they were given.3. Low: a comment this PR edited still says Ctrl+X reaches a read-only editor
Attach at: packages/platform/src/editor/MarkersViewCopyPlugin.tsx:125
Line 126's "Ctrl+X reaches the command there too" is no longer true, and your new $writeCopyPayload doc (whitespaceDisplay.plugin.utils.ts:1399-1403) now says the opposite.
Traced.
ClipboardPluginreturns beforecutSelectionwhen the editor isn't editable.- Lexical's root
cutlistener isisEditable && dispatch…. - Changing the
{@link}to backticks on line 125 was right, since$writeCopyPayloadis no longer imported here.
Suggested replacement for lines 125–126:
* A cut in a read-only editor copies and removes nothing (`$writeCopyPayload` owns that rule).4. Nit: two defensive branches in the copy-limit fit can't be reached in hidden mode
Attach at: packages/platform/src/editor/copyLimit/CopyLimitPlugin.tsx:285
The Math.max(…, $selectionPlainText(selection).length) in $size, and the || $selectionShownText(…) fallback in $overLimitText (line 312), never change the result in a hidden-marker view. No test exercises either.
Confirmed (jsdom).
- Mutations that left every test passing:
- Replacing the
Math.maxterm with0: all 232 related tests pass. - Replacing the
||fallback with"FALLBACK": all 233 tests pass (HiddenMarkersViewCopyPlugin,copyLimit/*,Editor.test).
- Replacing the
- Why the
Math.maxterm never matters:- In hidden mode every note gets a caller and spacers (the adaptor's
addSpaceNodes), so the prose is never longer than the text the view shows. - Probes tried caller
-with loose text, caller-with an\xtrun, caller+with loose text, andnoteMode: "expanded". Each gave prose no longer thangetTextContent(). - So the comment at lines 280–281 ("a note without a caller can make one longer") describes a tree hidden mode doesn't build.
- In hidden mode every note gets a caller and spacers (the adaptor's
- Why the
||fallback never matters: a layout-only over-limit range measures 0, so the fit collapses and blocks it before$overLimitTextwrites anything.
Suggested fix: drop both branches and the comment. If you'd rather keep them as defence in depth, add a test that reaches each one.
5. Nit: LEADING_SPACE's U+FEFF exclusion has no test
Attach at: packages/platform/src/editor/copyLimit/copyLimit.utils.ts:439
"U+FEFF is not whitespace" is tested only on the trailing side. A regression on the leading side would silently drop the space after a note.
Confirmed (jsdom).
- Changing
LEADING_SPACEto plain/^\s/left all 25 tests passing. - The same change to
TRAILING_SPACEfails "sees past invisible format characters". - The untested case is the text after a note starting with U+FEFF and then a letter (
Name.\f*rest). It should copy asName. rest, but would copy asName.rest.
Suggested fix: add that leading case to "sees past invisible format characters".
6. Nit: $withStoredNbspsAsTilde is exported but only safe after its caller's filtering
Attach at: packages/platform/src/editor/markerEdit/whitespaceDisplay.plugin.utils.ts:93
The helper is correct only because $selectionPlainText has already dropped layout text. Its own textTypeState === undefined check adds no protection, so as an export it invites a caller that would write spacers as ~.
Confirmed (jsdom).
- The only
textTypeStatevalues ever set aremarker-trailing-spaceandattribute(grep of$setState(…, textTypeState, …)).$isLayoutTextfilters both before$spellruns. - Replacing the condition with plain
$isTextNode(node)left all 232 related tests passing. - Any second caller, such as a future drag writer, would need to know that layout text must be filtered out first. The TSDoc says so, but the code doesn't enforce it.
Suggested fix: pick one.
- Make it module-private next to its one use.
- Or have it check
!$isLayoutText(node)itself and drop thetextTypeStatetest.
If you touch this, hiddenViewCopySpelling returning fn | undefined only to hit a default parameter (forcing the positional undefined at HiddenMarkersViewCopyPlugin.tsx:59) could become an options object. That part is taste.
7. Nit: the regex comment reads as if U+00A0 were default-ignorable
Attach at: packages/platform/src/editor/copyLimit/copyLimit.utils.ts:436
"(a zero-width space or no-break space, a direction mark)" reads as listing U+00A0. It isn't default-ignorable, and the regex rightly treats it as whitespace. The comment also says nothing about INVISIBLE_ONLY.
Traced.
Suggested replacement:
// Whitespace at the text's edge, looking past default-ignorable characters (ZWSP, U+FEFF, direction
// marks), which display as nothing, so they neither are a space nor hide one. `\s` matches U+FEFF,
// so it is excluded. INVISIBLE_ONLY: text made only of such characters.8. Nit: a test comment contradicts its assertion, plus two small test-file fixes
Attach at: packages/platform/src/editor/HiddenMarkersViewCopyPlugin.test.tsx:524
The comment says the over-limit copy keeps the note's "spacers", but the test expects PSALM_1_2_PROSE, which has none.
Traced.
- Line 524. The spacers matter only because they make the shown text longer than the prose. Suggested:
// An unclosed note is not collapsed, so the over-limit copy keeps its text. The limit is the // selection's prose length, which its shown text (spacers included) exceeds: the copy takes the // over-limit path, and the prose fits whole.
- Line 139. "Dispatches
COPY_COMMAND…", but the helper takescommandand the cut tests passCUT_COMMAND. Suggested: "Dispatchescommand(copy by default)…". CommandMenuPlugin.gate.test.tsx:141. The edited line is 112 characters, and its neighbours wrap at 100. Prettier doesn't rewrap comments.
9. Nit: a 127-character line in the CHANGELOG entry
Attach at: packages/platform/CHANGELOG.md:343
The new entry wraps at 100 except this line, which looks like an edit that was never rewrapped. Prettier's proseWrap: "preserve" won't fix it.
Traced.
- While you're there, the sentences "written by the editor rather than by Lexical's rich-text handler. Lexical's handler still takes…" could be one: "These views' copy and cut are now claimed at
COMMAND_PRIORITY_LOW, except a range with no text to copy." That is what a host with its own LOW or EDITOR listener needs to know.
10. Nit: several comments are long and repeat rationale held elsewhere
Attach at: packages/platform/src/editor/HiddenMarkersViewCopyPlugin.tsx:11
The plugin's 30-line TSDoc restates rules owned by other functions, explains the outside-the-editor cut twice (lines 27–30 and 39), and leaves "mount it only for markerMode: "hidden"" for the last line. Long comments like this are the ones that stop getting read.
Traced.
- What it repeats:
- "doubled spaces" is in
$selectionPlainText. - The
~/Paratext 9 reason is inhiddenViewCopySpelling. - The read-only cut rule (line 34) belongs to
$writeCopyPayload. - "the same prose except collapsed notes" is in
$overLimitText.
- "doubled spaces" is in
- Suggested replacement (17 lines):
/** * The hidden-marker views' copy and cut; mount only for `markerMode: "hidden"`. Writes Lexical's * own flavors with `text/plain` replaced by {@link $selectionPlainText} (prose without the NBSP * spacers laid out around a collapsed note), spelled by `hiddenViewCopySpelling`. * * `text/html` and `application/x-lexical-editor` stay Lexical's: the editor's own Ctrl+V reads * `text/html` back (`navigator.clipboard.read()` offers no internal flavor) and rebuilds notes from * it, so they keep the spacers and U+00A0 (a `~` would paste back as a literal tilde). * * Declines, leaving the command to Lexical, when the range has no text to write (a lone verse * number) and for a browser copy whose page selection runs outside the editor. Such a cut is * written, since Lexical's cut removes its range anyway, unless `copyLimit` is set (see item 1). * * Registered at `COMMAND_PRIORITY_LOW`: below `CopyLimitPlugin` (CRITICAL fit, NORMAL over-limit * writer), so it only sees copies that fit, and above Lexical's own copy (EDITOR), which it * replaces. */
- The same treatment would help in three other places:
CopyLimitPlugin.tsx:189-195. One 7-line sentence whose "so it only ever writes a copy that fits" reads as if "it" were$writePlainCopy.copyLimit.utils.ts:381-396. A 14-line paragraph that includes the ticket's repro string. Four bullets (edge space, runs,$spell, invisible text) would read faster.- The
~/Paratext 9 reason. It now appears in five places. Keeping it inhiddenViewCopySpellingand the publicViewOptionsdoc is enough; theViewOptionschange is good as is.InvisibleCharactersPlugin's header could point athiddenViewCopySpellinginstead of describing it.
11. Nit: the S5 "Copy-out is prose" bullet has become one ~330-word paragraph
Attach at: docs/clipboard-semantics.md:116
The bullet now mixes six topics: the text/plain rule, ~, the HTML rationale, the deferred HTML fix, the gutter-glyph gap, and the decline cases. That makes it hard to find any one of them.
Traced.
- Sub-bullets (
text/plain·text/htmland the internal flavor · left to Lexical · known gap) would let a reader jump to the part they need. - The "removing them from
text/htmlalone rebuilds such notes byte-for-byte" note could move to the Deferred list. - The S5 row of the Test Mapping table (line 273) still lists only the gate test. Add
HiddenMarkersViewCopyPlugin.test.tsx. - Line 121: "is fitted to the plain text the view shows and written as it" reads better as "is fitted to, and written as, the plain text the view shows".
12. Nit: the hidden view's copy serializer and ~ helpers live in modules named for other things
Attach at: packages/platform/src/editor/copyLimit/copyLimit.utils.ts:398
$selectionPlainText writes every hidden-view copy, limit or not, but it lives in copyLimit/. The ~ helpers sit in whitespaceDisplay.plugin.utils.ts, whose header (lines 12–15) says it is Standard-view-only apart from the three clipboard writers. The carve-out doesn't cover them.
Traced.
- There is precedent:
$selectionShownTextalready lives incopyLimit.utils.tsonmain, and$storedNbspslived inwhitespaceDisplay. So this is about discoverability, not a defect. - The cost: someone editing the grapheme-fit code can break plain copy with no limit set, and a reader can't tell from the header which code in a 1,500-line file must not run outside Standard view.
Suggested fix, cheapest now while the helpers are new:
- Move
$selectionShownParts,$selectionPlainTextand$isLayoutTextinto a view-neutral module thatcopyLimit.utilsimports. - Move
$withStoredNbspsAsTildeandhiddenViewCopySpellingnext toHiddenMarkersViewCopyPlugin. - Or, at minimum, add the spelling helpers to the header's carve-out.
13. Nit: a drag out of a hidden-marker view still carries the spacers
Attach at: PR description (top-level comment)
Standard view keeps "a drag carries what a copy writes" ($setStandardViewDragData, S1). The hidden-marker views have no DRAGSTART writer, so dragging a note's text into another app still brings the NBSP spacers.
Traced. This is pre-existing, since main has no such writer either. A line next to the text/html item in "Not in this PR" would keep it from being rediscovered later.
14. Nit: the description and two commit messages no longer match the code
Attach at: PR description (top-level comment)
Some statements in the PR text were overtaken by later commits. This matters most for the squash message.
Traced.
- The description.
- It says
$withStoredNbspsAsTildeis "the same rule as$storedNbsps". This PR deletes$storedNbsps, and the lone-NBSP test now lives in$isLayoutTextupstream. Suggested: "(replacing$storedNbsps/withStoredNbspsAsTildes)". - Its Testing list leaves out the copy-limit outside-cut test added in c78fd80.
- It says
- f71e781 says "drop the unused copyLimit prop", but c78fd80 restores it.
- c207853 says an outside-the-editor "copy" is left to Lexical's. That was later narrowed to copies only.
15. Nit: the tests have no blank line before the call under test
Attach at: packages/platform/src/editor/HiddenMarkersViewCopyPlugin.test.tsx:165
Our usual test shape is setup, blank line, the call under test, blank line, then checks. Here the copy(editor) call sits flush against the setup (e.g. lines 163–165 and 171–176), so the call being tested doesn't stand out.
Traced. Optional: MarkersViewCopyPlugin.test.tsx on main uses the same compact style. A // SUT marker helps on the longer tests, such as the outside-the-editor ones.
Brings in PT-4126 (#49), which copies the hidden-marker views' notes as prose without their layout spacers. It and this branch each added a writer for those views' copies; they are now one. - HiddenMarkersViewCopyPlugin (main's name, main's LOW priority and registerViewCopyWriter) writes the branch's flavors: text/html with the view's node copy in an attribute, and the node copy tagged for hidden-marker views; a cut keeps book and chapter lines ($removeTextKeepingBookAndChapterLines, passed through registerViewCopyWriter). Its text/plain is $selectionPlainText with the branch's $isLeftOutOfHiddenViewCopy, so it leaves out gutter glyphs and hidden constructs as well as note spacers and empty-span placeholders. It declines, as main's did, a range with no text to write and a copy whose page selection runs outside the editor. HiddenViewCopyPlugin is removed; its tests move to hiddenViewCopy.test.tsx. - The `~` respelling is main's (hiddenViewCopySpelling); the branch's $storedNbsps / withStoredNbspsAsTildes users and InvisibleCharacters- Plugin's copy handler go, as on main. - CopyLimitPlugin keeps main's prose over-limit text and fit, with the branch's hiddenTextPredicate leaving out what the view's copy leaves out. - $selectionShownText places an element point at either end by the child it stands before ($offsetInText, moved to copyLimit.utils.ts from the Standard copy walker, which now imports it). A range ending right before an optional break in paragraph-structure copied no text; with main's decline-on-empty that handed it to Lexical's copy, which wrote the wrong text. That case now writes its text and a node copy without the break. - Tests: main's flavor tests expect the view's own html and node copy; the corpus and copy tests clear a stale page selection before copying from a read-only view, which draws none of its own. - Docs: clipboard-semantics S5 merges both descriptions; the CHANGELOG's PT-4126 entry says the html and node copy keep the spacers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The bug
In Simple mode (Paragraph Structure view), copying a range that includes a footnote and using Paste as Plain Text put the footnote text in with two spaces at each end:
Yahweh’s<NBSP>1:2 <NBSP>“Yahweh”…Name.<NBSP> law.Why
The hidden-marker views (Formatted, Paragraph Structure, Block verse) lay a collapsed note out as its caller, then an engine-owned NBSP spacer after the caller and after each run. Those spacers are display only and never reach the file, but these views had no copy handler of their own, so Lexical's copy wrote
getTextContent(), spacers included, astext/plain, right beside the real spaces around the note.The fix
HiddenMarkersViewCopyPlugin(new, mounted formarkerMode: "hidden", atCOMMAND_PRIORITY_LOW, below the copy-limit fit and writer and above Lexical's own copy). It writes Lexical's flavors withtext/plainreplaced by$selectionPlainText.text/htmlandapplication/x-lexical-editorstay Lexical's own, deliberately. The editor's own Ctrl+V readstext/html(navigator.clipboard.read()offers no internal flavor) and rebuilds the note from it. An earlier version that cleaned the HTML broke that paste in the app, so changing the HTML is left to its own PR (see below).$selectionPlainText(copyLimit.utils.ts): the shown text without the tagged spacers or an empty span's placeholder. One ordinary space goes where a note's text meets the text around it and neither side already has whitespace (looking past default-ignorable characters). A note with no text adds none, and a note's runs follow each other as the file has them (\xt Gen 1:1\xt*, \xt Exo 2:3\xt*→Gen 1:1, Exo 2:3).~(merged with PT-4197, PT-4197: Data-fidelity audit — composed round trip, USX-first net, corpus #29): withallowInvisibleCharactersoff, the plugin writes an NBSP the text stores as~, using$withStoredNbspsAsTilde(the same rule as$storedNbsps).InvisibleCharactersPlugin's own copy handler did that respelling over Lexical's text, spacers included, and ran first at the same LOW priority, so it would have brought the doubled spaces back for any selection holding a stored NBSP. That handler is removed, so these views' copies have one owner;InvisibleCharactersPluginkeeps its typing and paste rules. Its hidden-view copy test now expects prose. Worth a look from the PT-4197: Data-fidelity audit — composed round trip, USX-first net, corpus #29 author.CopyLimitPlugin: in these views the over-limit writer and the fit use the same text ($overLimitText), so a limited copy never runs over the limit or cuts what it didn't copy. Layout bytes count for nothing as the fit places its end, so a limited copy isn't short by one character per placeholder. Both copies take the~choice from one helper (hiddenViewCopySpelling).registerViewCopyWriter: one registration helper shared byMarkersViewCopyPluginand the new plugin.clipboard-semantics.mdS5), comments, and a CHANGELOGUnreleased→ Fixed entry. Thedistis rebuilt.Testing
Unit:
HiddenMarkersViewCopyPlugin.test.tsxcovers:The Formatted-view gate test now asserts the prose spacing, so it fails if the plugin is unmounted. I broke the fix several ways and confirmed the tests catch each one.
Checks: platform-editor and shared-react tests, lint and typecheck are green. One unhandled
$getTextNodeOffseterror inEditor.test.tsx("covers the whole selection with one marker…") also happens without this PR.In the app: Platform.Bible dev build on paranext-core
mainwith this editor staged, WEBBLANK Psalm 1:2 in Simple mode.delight~is): Paste as Plain Text writes it as~, and ⌘V keeps it. The saved file differed from the original only in the pasted lines.Not in this PR (existing behavior, found along the way)
text/htmlstill carries the spacers, so pasting into Word still shows double spaces, and a ⌘V paste of a note holding loose text (a cross-reference's,between\xtruns,\f + Loose text\f*) saves NBSPs into it. A review probe found that removing (not replacing) the note-child spans fromtext/htmlrebuilt every tested note byte-for-byte (6 note shapes, 2 views); it needs its own in-app check, since an earlier HTML cleanup broke the paste in the app.$convertNoteCallerElementbuilds the caller without anonClick.\q2+NBSP) still reachestext/plain, soCommandMenuPlugin's backslash gate swallows any paste of a whole line. Dropping the glyph would let a copy holding the chapter line paste a second chapter, which these views have no strip for.…Name.\f*, law.) copies with a space before the punctuation (Name. , law.). It had a layout NBSP there before this PR too; what it should write is partly a product call.\figcaption still reachestext/plain.🤖 Generated with Claude Code
This change is