Skip to content

fix(RichTextEditor): keep the caret and undo through a new value - #2937

Draft
jcosta33 wants to merge 61 commits into
fix/rte-host-value-resetfrom
fix/rte-host-value-sync
Draft

jcosta33 wants to merge 61 commits into
fix/rte-host-value-resetfrom
fix/rte-host-value-sync

Conversation

@jcosta33

@jcosta33 jcosta33 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Hosts that control the editor through value, such as comment editing and guideline autosave, hand back a cleaned-up or saved copy of the document. Each new copy replaced the whole content, so every save moved the caret, wiped undo and broke IME input.

The editor now applies a new value as a change to the current document, the way y-prosemirror applies remote edits.

Before and after

  • Caret: before, each new value moved the caret to the end of the document. Now the caret and the selection stay where they were, as Google Docs keeps a local caret through remote edits.
  • Undo: before, each new value wiped the user's undo history. Now undo takes back only the user's own edits, as the Yjs undo manager does. An edit that the host change replaces leaves the history.
  • IME input (the input method for languages such as Japanese): before, a new value broke the word being composed. Now the editor waits until the word is done.

Blocks are matched with jsdiff's diffArrays, a new dependency (diff). The text diff needs Intl.Segmenter, which every browser in the defaults browserslist query supports.

How to test

Each case needs a new value while the editor has focus, which no story does. The editing.spec.tsx blocks "a document set by the host", "a document set by the host that changes the blocks around the caret" and "a document set by the host while text is being composed" cover them.

Stacked on #3110.

FP-756

@changeset-bot

changeset-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8010026

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@frontify/fondue-rich-text-editor Patch
@frontify/fondue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

…development

Unknown blocks, inlines and marks are handed out as the objects the engine
holds, and values nested past one level stayed writable, so a host mutation
there changed engine state without throwing.
…after a host change

A caret inside the changed range maps to its end, and the end found by the
diff can sit inside an emoji sequence or between a letter and its accent.
Move the end to the next grapheme boundary.
…wer than what was reported

A document the host hands over mid-composition did not move the reported
sequence until it was applied, so restoring a reported document right after
it was taken for a late echo and ignored.
…ment

A host that mutated an emitted document and handed it back had the change
ignored as an echo, with no error in development.
… or ticks

A node that differs only in type or attributes was replaced whole, which
pushed a caret inside it into the next block. Change it in place with
setNodeMarkup instead. The specs now also check the caret over generated
documents, next to inline elements and in lists.
Turning an inline element into text threw, and a block inserted or removed
next to one with the same text retyped the user's block, moving the caret.
Restyle only when neither node is text and the parent's child count is
unchanged. The generated documents now hold soft breaks.
A user's undo of an attribute change carried the whole old node and so
reverted a host restyle of the same block. Host and user attribute
changes now use one step per attribute, so undo reverts only what the
user changed; a host type change drops the user's step instead.
…nged blocks alone

Two separate host changes were applied as one span, which swallowed the
blocks between them: a caret there moved and the user's edits there fell
out of undo. Unchanged children now anchor the change, changed ones are
restyled in place and synced recursively, and only text uses a character
diff. A node view stores only attributes that changed.
…t's blocks

The comparison limit counted every block, so inserting one block into a
document of about 500 or more replaced all of them and moved the caret.
The shared start and end are now matched first, and only the stretch
between them is compared and counted.
…en syncing

Past the comparison limit nothing was matched, so a change at both ends of
a long document replaced every block in between. Myers' diff costs grow
with the changes, not the length. A block that differs only by an open
draft now counts as unchanged, and an inline element whose value alone
changed is updated in place, keeping its view and selection.
… of a long document

Matching blocks stops past 100 inserted and removed blocks, and a stretch of
more than 100 changed blocks is replaced in one step, but for the block
holding the caret. A host swapping a 1000-block document took seconds.
…nd add the changeset

Myers' forward and backward passes, and the same-length stretch sync, become
their own functions so each stays under the cognitive complexity limit. The
reversals are statements of their own. Behaviour is unchanged.
… drive composition specs with real events

Development freezing tracks the objects it walked instead of reading
Object.isFrozen, so a host object frozen only at its top no longer leaves what
it holds writable. The host value diff reads Node.children instead of copying
them, and the composition specs dispatch compositionstart and compositionend.
…e and specs

Ternaries in the specs become statements, the specs' block helpers are typed so
no cast is needed, and inline types, literals and the casts that remain are
named or justified in one line.
The nested switches inside mapEdit and overlaps become functions of their own,
so each outer case is a plain return and SonarCloud's S128 no longer fires.
# Conflicts:
#	packages/rich-text-editor/src/editor/engines/prosemirror/editing.spec.tsx
#	packages/rich-text-editor/src/editor/engines/prosemirror/editing.ts
# Conflicts:
#	packages/rich-text-editor/src/editor/engines/prosemirror/editing.spec.tsx
#	packages/rich-text-editor/src/editor/engines/prosemirror/editing.ts
@netlify

netlify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for fondue-docs ready!

Name Link
🔨 Latest commit 01c54fa
🔍 Latest deploy log https://app.netlify.com/projects/fondue-docs/deploys/6b04556bd3e04f53d5162bfa
😎 Deploy Preview https://deploy-preview-2937.fondue-components.frontify.com
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@jcosta33
jcosta33 changed the base branch from main to v14 November 12, 2026 10:52
@noahwaldner
noahwaldner changed the base branch from v14 to main November 13, 2026 09:32
The freeze is out of scope for host value sync; the documented rule not to
mutate a handed-out document stays.
Cancel hands the saved document back, the way a host discards an edit.
# Conflicts:
#	packages/rich-text-editor/src/editor/engines/prosemirror/controlApi.ts
#	packages/rich-text-editor/src/editor/engines/prosemirror/editing.spec.tsx
#	packages/rich-text-editor/src/editor/engines/prosemirror/editing.ts
# Conflicts:
#	packages/rich-text-editor/src/editor/RichTextEditor.tsx
Move the sequence diff to its own module, let the composition retry ask
the same decision as a new value, keep the report registry out of the
pure record, and keep transaction meta keys in one place.
# Conflicts:
#	packages/rich-text-editor/src/editor/RichTextEditor.tsx
#	packages/rich-text-editor/src/editor/engines/prosemirror/editing.spec.tsx
#	packages/rich-text-editor/src/editor/engines/prosemirror/editing.ts
#	packages/rich-text-editor/src/editor/engines/prosemirror/hostRecord.ts
@jcosta33
jcosta33 changed the base branch from main to fix/rte-host-value-reset November 23, 2026 12:58
@jcosta33
jcosta33 added this pull request to stack #3111 November 23, 2026 12:58
@jcosta33 jcosta33 changed the title fix(RichTextEditor): apply host value changes in place fix(RichTextEditor): keep the caret and undo through a new value Nov 23, 2026
…d-written diff

jsdiff's diffArrays takes a comparator and an edit-length limit, which is
all the hand-written Myers pass did.
# Conflicts:
#	packages/rich-text-editor/src/editor/engines/prosemirror/editing.ts
#	packages/rich-text-editor/src/editor/engines/prosemirror/hostRecord.ts
# Conflicts:
#	packages/rich-text-editor/src/editor/engines/prosemirror/editing.ts

This branch has not been deployed

No deployments
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