Repository navigation
Conversation
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Updated as activity occurs, without notifying anyone named here. Add the 🏷️ LabelsThis pull request needs exactly one label indicating its type, and has 0.
Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you. 📦 Bundle sizeSize Change: +309 B (0%) Total Size: 8.88 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. should leave the rest of the list intact when merging into an empty list item in
|
| // belongs and there is no need to walk the tree for it. | ||
| if ( | ||
| idMappingRef.current.internalToExternal.has( currentStartClientId ) | ||
| ) { |
There was a problem hiding this comment.
I'll get back to this later, but my guess is that this PR probably breaks undo/redo selection restoration when you do multiple consecutive changes within the same controlled container. Basically an undo call might ask for the selection to move to another block within that same controlled container but the current PR will prevent it from happening.
There was a problem hiding this comment.
I'm at my EOD and had my agent write a test to try to cover this. I'll be back tomorrow to give it another try if it is an issue.
There was a problem hiding this comment.
I tested this out and pushed more Claude Fable work. It's testing well but I'm unsure about the direction without diving deeply into it since I'm not familiar with this part of the code. I've got it pushed up and will start going through it. I've tested it, and it is in a better state than trunk. Here's the current state:
- Fixes duplicate container stealing the block selection
- Doesn't cause the "backwards typing" bug
- Does not cause an undo regression. There is already an issue on trunk with undo in a container not appropriately selecting and moving the caret to the correct location. I'll file that issue shortly.
Fable's summary of the fixes:
- Selection guard,
useBlockSync. After a sync, skip restoring the entity's recorded selection when the current selection already sits in a different controller. Stops the other copy taking the caret.- In-place update,
useBlockSync. When the incoming value has the same structure as the current clones, apply the changed attributes to them as one history-ignored update instead of re-cloning and replacing. Removes the per-keystroke rebuild that caused every symptom. Structural changes still rebuild.- Persistence reducer. A history-ignored change that leaves the typed block untouched no longer resets the "last action", so a typing run survives the other copy's mirror update. An undo still ends it.
useMarkPersistent. Read the live RichText record via getValue instead of the render-time one, which is a change behind when the html changes from outside. Otherwise an external text change is mistaken for a formatting change and ends the run.
Per review on #82706: check that an undo can still move the selection from one block of a controlled container to another. Post Content is an inner block controller, so two of its blocks exercise that path. Passes identically with the guard enabled and disabled, five runs each. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
Adds failing coverage for #79096 and regression guards for the behaviour a fix must not break. Two blocks rendering the same entity (two Navigation blocks on one menu, two Post Content instances) each clone that entity's blocks privately but build identical external-to-internal ID maps. In useBlockSync's restoreSelection, ownership is decided by whether the external ID appears in that map, which every such controller answers true to, so an edit in one instance is followed by the other claiming the selection. Failing (the defects): - editing a menu item leaves the caret and selection in the edited block - undoing a menu edit returns the caret to the block that made it - undo moves the caret back into the Navigation block when the caret has moved outside it - typing in one Post Content instance leaves the caret and selection in that instance The last two are a second, separate gap: a nested controller records its selection against its own entity, but the only consumer of a recorded selection reads the root record, so cross-controller undo never restores the caret. Passing (guards for existing behaviour): - undo restores the caret to the undone block when another block is selected - selection is restored after returning from a template part The duplicate tests record every selection entering the other instance rather than asserting on the final selection: the steal is often momentary, and the canvas scrolls as soon as it happens even when the selection recovers. A final-state assertion passed 4 runs in 5. The Navigation spec is adapted from #79929. Also makes Template Part Focus mode activate its theme per test; the theme was restored after each test but only activated in beforeAll, so any second test in the file ran against the wrong theme. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
Fixes #79096. Two blocks can render the same entity — two Navigation blocks using one menu, a Post Content block at a template root and another inside a template part. Each is an inner block controller holding private clones, but both clone the same blocks, so both build the same external-to-internal ID mapping and both recognise the entity's selection as their own. An edit in one instance therefore reaches the other as an incoming entity change. That controller re-clones and calls restoreSelection, which converts the external ID to its own copy and dispatches resetSelection. The canvas scrolled to the duplicate and the caret left the block being typed in, reliably enough to reorder characters. Guard the restoreSelection call instead: when the selection already sits inside a different inner block controller, this sync is a consequence of an edit made there, so leave it alone. The controller that made the edit takes the outgoing branch and never reaches this code, so a selection held by another controller is always one that has been dealt with. A selection held by the root block list belongs to no controller and still restores as before, which is what keeps entity navigation and undo working. The guard sits at the call site rather than inside restoreSelection: the question is about the sync that just happened, not about the selection object, and restoreSelection keeps its existing contract. This reuses getBlockParents and areInnerBlocksControlled, already selected in this hook, so there is no new store state, no new action or selector, and nothing persisted into entity data or collaborative state. Revives the approach in #79422 by @ramonjd, which asked the same question with the same selectors and was closed by its author in favour of #79097. Two differences: the check moved to the call site, and the WeakMap remembering the last controller to hold the selection is left out. Also removes two tests added with the failing coverage. They asserted that undo returns the caret to the block whose edit was undone. Trunk does not do that for any nested entity, with or without duplicates: a controller records its selection against its own entity, but the only consumer of a recorded selection reads the root record. That is a separate, pre-existing gap and out of scope here. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
…root getBlockParents collects every ancestor up to the document root and is memoized on state.blocks.parents, which changes on each re-clone, so the memo never hits on this path. The answer is almost always the immediate parent, since a menu item sits directly inside its Navigation block. Walk up with getBlockRootClientId instead, stopping at the first controller, and short-circuit on the controller's own mapping when the selected block is one of its clones. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
The existing comment describes restoreSelection, so it belongs inside the guard rather than above it. Also records why the guard is checked after the blocks are set. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
Per review on #82706: check that an undo can still move the selection from one block of a controlled container to another. Post Content is an inner block controller, so two of its blocks exercise that path. Passes identically with the guard enabled and disabled, five runs each. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
Two Template Parts pointing at the same entity are both inner block controllers synced from one record. These pin the two behaviours that matter when typing into one of them. The first passes: every typed character survives, in both copies. That is the behaviour the preceding commits on this branch fixed, and it regresses silently, so it is worth holding down. The second fails. One undo removes a single character rather than the whole typed run, because each keystroke makes the *duplicate* run setControlledBlocks() and dispatch replaceInnerBlocks. That action lands between keystrokes and breaks the isUpdatingSameBlockAttribute merge chain, so every character becomes its own undo level. Marking the change not-persistent does not help: any intervening action on another block breaks the chain regardless. restoreSelection is not involved — the existing guard keeps it from firing during typing. Both tests type at a human cadence on purpose. An instant keyboard.type() outruns the re-sync and passes, so the delay is load-bearing rather than incidental. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UTowaGipHSMef7nUQAnnJJ
Two containers rendering one entity, such as two Template Parts on the same slug, are both inner block controllers. Typing in one edits the entity, and the other mirrors the edit onto its own clones as a change ignored by history. The persistence reducer still recorded that change as the last action, so the next keystroke no longer looked like a continuation of the run and was filed as a new undo level. Undo then removed one character at a time. A history-ignored change that leaves the last-updated block's entry and attributes untouched now keeps the previous last action, so the run continues across it. A change that does replace that block, such as the reset after an undo, still starts a new level. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A6LxkvDuGJg68a7c8KJKry
useMarkPersistent decides between "text input, wait a second" and "formatting change, end the run now" by comparing the record's text with the text it saw last. When the html changes from outside the rich text, useRichText applies it to its record in a layout effect of its own, so the record handed to the hook at render time is one change behind. The hook then saw new html with the old text and ended the run at once. Read the live record inside the effect instead, through the getValue that useRichText already returns. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A6LxkvDuGJg68a7c8KJKry
…tes change An inner block controller rebuilt its whole subtree whenever its entity changed: it cleared the ID mapping, cloned every block under a fresh client ID and replaced the inner blocks. With two containers rendering one entity, that happened on every keystroke made in the other one. Each keystroke tore down and remounted every block and rich text in the duplicate, briefly left it without a selection, and dispatched a block replacement into the store between two keystrokes. When the incoming value has the same structure as the current clones, the same external IDs in the same order, the controller now applies the changed attributes to the existing clones as one history-ignored update. The mapping, the mounted components and the selection stay valid. A structural change still takes the rebuild path. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A6LxkvDuGJg68a7c8KJKry
An undo that reverts an edit in one Template Part while the caret sits in another should bring the caret back to the reverted block, as it does at the root of the document. It does not yet: a nested controller only restores the selection recorded on the root record, which an undo of its own entity never changes. The test is marked fixme to document the gap. The duplicated-container tests now also delete saved templates when they finish, since a leftover saved index template made their setup fail. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A6LxkvDuGJg68a7c8KJKry
70180df to
13b223b
Compare
The nearest controlled ancestor of the selected block is its holder. The store already has an ascending parent list, so use that instead of walking the tree by hand. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UpQLN4sQKUDBA45FqsyUQY
A field that is not selected is never typed or formatted in, so a change reaching it comes from elsewhere, such as another container editing the same entity. Ignore it instead of reading the live rich text record to tell it apart from formatting. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UpQLN4sQKUDBA45FqsyUQY
What?
Closes #79096
When two blocks render the same entity — two Navigation blocks using the same menu, or two Post Content blocks in "Show template" mode — editing one made the other take the selection, scrolling the canvas to it and pulling the caret out of the block being typed in. In the Site Editor the caret snapped back after every keystroke, so typed text came out reversed, and each character became its own undo level.
Why?
Each inner block controller keeps private clones of its entity's blocks, but every controller of the same entity maps the same external IDs. When one of them is edited, the others receive the change as an incoming value, re-clone their whole subtree, and then restore the entity's recorded selection onto their own clones. Each of them recognises that selection as its own, so the last one to sync steals it.
The re-clone on every keystroke also caused the rest: replacing the duplicate's blocks between two keystrokes broke the
isUpdatingSameBlockAttributechain in the persistence reducer, and the duplicate's RichText fields saw their html change from outside and marked the last change persistent, ending the typing run.How?
Four changes, one per layer:
useBlockSync. After syncing an incoming value, skip restoring the recorded selection when the block-editor's current selection already sits inside a different controller. The holder is the nearest controlled ancestor of the selected block, found withgetBlockParents. A selection in the root block list belongs to no controller, so it never blocks a restore, and a selection this controller just destroyed by re-cloning is still repaired.useBlockSync. When the incoming value has the same structure as the current clones (same external IDs, same block names, same order, all the way down), apply only the changed attributes to the existing clones as one history-ignored update instead of re-cloning. The mapping, the mounted components and the selection all stay valid between keystrokes. Structural changes still re-clone.useMarkPersistent. Only a selected field is typed or formatted in, so a change reaching an unselected field comes from elsewhere. The hook now takesisSelectedand only marks from a selected or multi-selected field. Unselected fields keep tracking the value so nothing is misjudged once they are selected again.Credit
Similar to @ramonjd's approach from #79422 and #79929.
Testing Instructions
Automated
navigation-selection-sync.spec.js(new) — the reproduction. It records every selection entering the duplicate rather than asserting on the final selection: the steal is often momentary, and the canvas scrolls as soon as it happens even when the selection recovers. A final-state assertion passed 4 runs in 5.post-content-focus-mode.spec.js— the same for duplicate Post Content instances, plus undo moving the caret between blocks of one Post Content.site-editor/undo.spec.js— typing in a duplicated container keeps every character, and undo removes the whole run. Atest.fixmepins the pre-existing gap described under "Out of scope".patterns.spec.js→ "should restore selection after navigating back from pattern editing", which restores a selection inside the Post Content controller.use-block-sync), the reducer rule for ignored changes to other blocks (store/test/reducer.ts), and the unselected-field rule (use-mark-persistent).Out of scope
test.fixmeinsite-editor/undo.spec.js.isValidis not mirrored by the in-place update. Recovering an invalid block in one container leaves the duplicate's clone flagged invalid until the next structural sync. Edge case; the previous full re-clone did carry it over.🤖 Generated with Claude Code
https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
https://claude.ai/code/session_01UpQLN4sQKUDBA45FqsyUQY