Repository navigation
Block Editor: Reconcile a restored selection once for the whole canvas - #87861
draganescu wants to merge 46 commits into
Conversation
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
…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
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
Two fixes to the duplicate controller guard and in-place update. The guard skipped restoring the selection whenever the nearest controlled ancestor of the selected block was not this controller. A controller nested inside this one, such as a synced pattern inside Post Content, is still part of this copy, so an undo in Post Content no longer moved the caret back while it sat in the pattern. A selection anywhere inside this controller now counts as its own. The in-place update compared only attributes, so a change to any other block field never reached the clones. The Custom HTML block keeps its markup in innerContent and edits it with updateBlock: undoing that edit in a container left the new markup on screen, and the next edit there wrote it back into the entity. A value whose blocks differ in any field other than attributes and inner blocks now re-clones, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The in-place update and its tests were written in JavaScript before useBlockSync moved to TypeScript on trunk. Give the helpers and the test harness their types so the dev project type checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Trunk now requires new end-to-end spec files to be TypeScript. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two regressions against trunk in how a controller restores the selection after an incoming change. The in-place update left the record of the last restored selection in place, so an undo handing back that same selection object skipped the restore. That happens on undo, type, undo: typing without moving the caret first records the selection the first undo restored. Clear it after an in-place update, as re-cloning does. The guard took any controller above the selected block for another copy of this entity. A Template Part holding Post Title and Post Content then stopped an undo in Post Content from moving the caret back while it sat on Post Title. Another copy is always a block of the same type, so only count controllers of this controller's block type. Also trims the changelog entry to what changes for users of the package, and names duplicate Post Content blocks in it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two end-to-end tests for the selection restore fixed in the previous commits, both with the page's template shown: - With Post Title and Post Content in one Template Part, an undo in Post Content brings the caret back from Post Title. - On undo, type, undo, the second undo brings the caret back to where the typing started. Both fail without those fixes. Each first types a second after the editor loads: the first typing after loading does not always get an undo level of its own, on trunk too, which made the tests flaky. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The guard counts any controller of the same block type as a copy, so two Template Parts on different slugs count as copies of each other too. Say so next to the guard, and in the undo test pinned as fixme, which the guard would also block once its other cause is fixed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Until the canvas reports Post Content as visible, a moment after it renders, the block reads the post asynchronously. Typing then undone quickly is missed altogether, leaving its blocks with the typing while the post no longer has it. Wait for that state instead of a timeout, and undo once per typing now that no undo can go missing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Like the other two undo tests, this one typed before Post Content was reported visible, so the first undo could be missed. An undo-until loop absorbed that, but it would equally have accepted the two edits being merged into one undo level. Wait for visibility instead, and check that each undo reverts exactly one edit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two checks had no test that fails without them: - a reorder of the same blocks must re-clone, or each clone stays mapped to the block that used to be in its place, and an edit to one is reported as an edit to another; - an undo that updates the edited block in place must start a new undo level for the next typing, or the next undo also takes back the edit before it. Also correct comments that still describe a duplicate re-cloning between keystrokes, drop a claim that the selection guard has to run after the blocks are set (either order restores the selection), and name the nested-controller re-clone case for what it covers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The hook ignores changes to the clones of a controller that has no block in the store, so the check that an in-place update is not reported back passed whatever the hook did. With a host block, it fails when the hook stops marking the update as incoming. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Three conditions had no test that fails without them: - a copy's in-place mirror of another copy's keystroke must be ignored by history, or each keystroke starts an undo level of its own; - the reducer rule must apply only to changes history ignores, or typing in a second block splits into one level per keystroke; - the nested-controller selection test must take the in-place path when it says it does. Also correct a reducer test comment that credited the block entry for what its attributes catch, and reuse the Post Content text helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rename hasSameFields to areBlockFieldsEqual and leavesBlocksUntouched to areLastUpdatedBlocksUnchanged, so that each name says what it compares, as suggested in review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lection The selection guard checked only the parents of the selected block, so a selection on another copy's own block did not count. Adding a link to a Navigation block can leave that Navigation block selected, and review caught the duplicate then restoring the previous link onto its own clone, pulling the selection into it. Count the selected block as well as its parents. Also say in the guard why it counts copies by block type rather than by a registered entity. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One redo must bring the whole typed run back, in both containers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ts metadata When a container updates its inner blocks in place, an undo that removes the metadata of an unsynced pattern arrives as `metadata: undefined`. The derived editing modes only treated a truthy `metadata` as a change, so the pattern's blocks stayed content only until the page was reloaded. Count `metadata` as changed whenever the update includes it. Reported by ramonjd in review, with the fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…removed Removing an attribute can change more than the attribute. A Navigation block whose `ref` an undo removes no longer controls its inner blocks, so they have to come from the incoming value. The in-place update leaves the inner blocks of a controller alone, so the block kept the menu's links, lost its own, and then picked the menu again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One pure function that every controller on the canvas asks, so that all copies of the same blocks get the same answer. Its tests include two cases where every copy clones its blocks again before asking, which fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nTarget names Controllers register their ID mappings in the selection context, and restoreSelection asks pickSelectionTarget instead of testing only its own mapping. Removes the guard by block type from #82706. The structural change tests fail when the edited copy is second on the canvas. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two controllers of one type on different entities, where the caret has to go back to the one that can show the recorded block, and two copies of one entity, where it stays in the copy the person clicked into. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lone After a structural change each copy clones its blocks again in its own effect, so a copy could ask pickSelectionTarget while the store's selection still named a clone that another copy had just replaced, and the copies disagreed on who restores the selection. A copy's mapping now translates the clones it replaced until its next re-clone, and maps the blocks created in it when it reports them, so the canvas always knows which copy holds the selected block. A selection on a replaced clone is not in place, so its holder moves it to the new one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings over the case and the Test Post Context Provider block from #87777. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds a page with the block appender, undoes from the other block, and moves an item and undoes the move in either block, checking that the selection never enters the other block and the canvas does not scroll. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🤖 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +1.02 kB (+0.01%) Total Size: 8.86 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. Actions are reachable via RIGHT/LEFT arrow keys in
|
The 20.2.0 release published the section the entry was in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ncern Restoring a recorded selection is a decision about the whole canvas, but each inner block controller made it on its own, in its own effect, while other controllers were still replacing their clones. The fixes for that needed retired clone IDs, identity mappings for created blocks and an exception in the picking rules. The root provider now owns one selection reconciler. Controllers register their ID mappings with it, ask it to run when they take blocks from outside, and tell it when they replace the clones the selection was on. It runs once every controller has synced and makes at most one selection change. It acts on a recorded selection only when the canvas did not report that selection itself, such as one an undo brings back, so a record that falls behind the caret never pulls the caret back. The controller that holds a block is the nearest parent whose inner blocks are controlled, for restoring and reporting alike. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t synced Whether typing in a controller updates the recorded selection depends on its entity, which the block editor cannot tell. Typing in one Navigation block leaves the record on the last click, and the other copy mirroring the typing would bring the caret back to it. The reconciler now applies a recorded selection only through a controller that just took its blocks from outside, as after an undo, which is the rule the controllers used to follow on their own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…utenberg into try/reconcile-selection-root
Each controller now tells the canvas's reconciler where the selection was before and after every edit it sends out, keyed by the blocks value it replaced and the one it sent. An undo or a redo gives the controller one of those values back by reference, so the reconciler knows which copy was edited and where its caret was, and that copy shows the selection again. This covers a copy of the same entity and a controller of another entity, such as a second Template Part, whose own entity's selection the root never reads. Nothing is added to the entity, the store or any public API. The history applies only when the edited copy itself took the value back: a copy that merely mirrors another copy's keystroke can sync it late and must not pull the caret back. When the selection moves into another controller, the focused field gives up focus, since it would otherwise report its own caret as the selection when the undo changes its text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Undo from the other Navigation block now expects the caret back in the edited block, and redo too. The undo across two Template Parts is no longer a fixme; it only clicks into the second part, since typing there is not recorded as an undo level of its own, on trunk as well. Adds the Post Content case from #87845, and waits for every Post Content to be visible, as suggested on #82706. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Trunk now has #82706, which this branch already builds on, so its side of the conflicts in useBlockSync, its tests and the e2e specs is kept. From trunk's later #82706 test commit, the Post Content spec also waits for both instances before clearing the selection. Also types the focused element in blurFocusedField, which failed the type check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#82706 is in trunk with its own entry for the typing fixes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ion-once # Conflicts: # packages/block-editor/CHANGELOG.md
What?
Follow-up to #82706, for #79096.
#82706 stopped blocks that show the same saved blocks, such as two Navigation blocks on one menu or two Post Content blocks, from taking the caret from each other while you type. This PR fixes where the caret goes on undo and redo. An undo or a redo now brings the caret back to the block it changed, in the copy where the edit was made, even when the caret has since moved to another copy or to another inner block controller, such as a second Template Part or the Post Content of another post. It does this by replacing #82706's guard by block type with one place that decides the selection for the whole canvas, with no public API and no new store state.
Why?
Each inner block controller keeps private clones of the blocks it shows and restores the recorded selection on its own. Its only test is whether the recorded block is in its own mapping, which is true for every copy of the same entity, so on its own the last copy to sync takes the caret. #82706 added a guard: a controller skips the restore while the caret is in another controller of the same block type. That guard leaves three gaps:
test.fixme.How?
Where the selection goes is a question about the whole canvas, so the root provider now answers it once. The in-place update of a controller's clones, the undo reducer rule and the metadata fix from #82706 stay as they are. Its guard by block type is replaced by the following.
BlockEditorProvidercreates a privateSelectionReconcilerin a ref, outside the block-editor store, because any change to the blocks state would end a typing run. Inner controllers no longer restore the selection themselves. Each inneruseBlockSynconly keeps the reconciler informed: it registers its ID mapping while mounted, asks for a reconciliation after it takes blocks from outside, and notes when a re-clone replaced the clones the selection was on.resetSelectionat most once. The order the copies sync in no longer matters.pickSelectionTarget. Given the recorded selection, the store's selection, the controllers in canvas order and which controller holds the selected block, it names the one controller that should show the recorded selection, the root block list, nobody (already in place), or that no block on the canvas shows it. The copy holding the caret keeps it when it can show the recorded block; otherwise the first copy on the canvas that can show it takes it.WeakMapinside the reconciler: nothing is added to the entity, the selection record or the store. It applies only when the edited copy itself took the value back, so a copy that mirrors another copy's keystroke a render late never pulls the caret back.Compared with the closed alternatives
syncSourceoption. This PR adds neither.One behaviour change on the reporting side: ownership comes from the block tree, so a selection change inside a block just created in a copy is reported to the entity. Before, no controller reported those.
Testing Instructions
Steps 2, 4 and 5 check that #82706's fixes still hold; steps 3 and 6 show what this PR changes.
Proofs run
The Trunk column was run before #82706 landed, with trunk's
packages/block-editor/srcbuilt against this branch's specs, and then on this branch. P1 to P3, P11 and P14 are #82706's fixes, which trunk now has; they are kept here as regression checks. P8, P16, P17 and P18 are what this PR changes. The e2e runs used--trace=on.site-editor/navigation-selection-sync.spec.ts)site-editor/undo.spec.js)editor/various/post-content-focus-mode.spec.js)patterns.spec.jsfiles,template-part-focus-mode.spec.js,editor/various/undo.spec.js, and the provider and reducer unit suitesnpm run docs:buildleaves no diffsite-editor/undo.spec.js, wastest.fixme)With
--repeat-each=3, the Navigation, Post Content, editor undo and site editor undo specs passed 132 of 132 runs. Unit: 360 of 360. The full set above also includes "Synced pattern › can be inserted after refresh" ineditor/various/patterns.spec.js, which fails about 2 runs in 3 when run on its own: it types into the slash inserter before the new pattern is available, as #87845 also notes. No selection code is involved.Testing Instructions for Keyboard
Repeat steps 2 to 4 with the keyboard: Tab into the link, type, use the arrow keys, and undo with Cmd/Ctrl+Z.
Out of scope
packages/block-libraryorpackages/core-data, and any public API.Use of AI Tools
Written with Claude Code (Claude Opus 5.5): the implementation, the tests and this description. I reviewed the changes and ran the unit and e2e proofs above.
🤖 Generated with Claude Code