Repository navigation
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>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The general approach of passing an id seems good. Not disimilar to passing an The way its done using
I don't really understand this part. How is it different to a template part? |
…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>
The selection guard counted any inner block controller of the same block type as a copy. That is wrong for two controllers of one type that render different entities, such as Template Parts on different slugs, or a Post Content block inside a block that features another post: an undo in one of them could not move the caret back while it sat in the other. Each controller now records what it renders in the block-editor store, one ID per controller, as suggested in review of #79929. The guard compares those IDs, and falls back to the block type for a controller that records none. The ID reaches useBlockSync through a private context that the controller block provides around its inner blocks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nder Provide each controller's entity to its inner blocks, so that the selection guard can tell copies of one entity from controllers of the same block type that render another. Synced patterns are left out: each instance parses the pattern with its own overrides and never shares edits with the others, so they fall back to the block type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A block that features another post renders that post's Post Content, a controller of the same type as the current post's but of another entity. A caret in it must not stop an undo in the current post's Post Content from moving the caret back. The test plugin gains a block that provides the post context, standing in for such a block. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The comment above the guard still said it looked for controllers of the same block type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Nothing on the canvas shows a Navigation block's or a Template Part's source yet, so a missing one would only make the guard fall back to the block type, and every other test would still pass. Read the sources from the store instead: copies record the same one, different entities differ, and a synced pattern inside a template part records none rather than taking on the template part's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A block passes what its controlled inner blocks render as `syncSource`, next to `value`, `onInput` and `onChange`, in place of a private context around its inner blocks. Nothing is inherited, so a nested controller no longer needs the context reset, and third-party controllers can pass one too. Synced patterns now pass theirs as well: like a Template Part, each renders one entity, and two different patterns are not copies. Suggested by talldan in review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dba73a0 to
5e3fa64
Compare
What?
Alternative to #82706, for #79096.
This is the same fix as #82706 except for one part: how the selection guard finds the other copies of an entity. #82706 treats every inner block controller of the same block type as a copy. Here, each controller records what it renders in the block-editor store, and the guard compares those. This follows youknowriad's suggestion on #79929 to register one ID per controller in the store. He wrote "the external clientId of the controller". Taken literally, that can't tell that two separately inserted Navigation blocks on one menu are copies, so the ID recorded here is the entity the controller renders.
Why?
Counting copies by block type is wrong when two controllers of one type render different entities. That can be seen today with a block that features another post. No core block does that outside a Query Loop, where Post Content is read-only, so it takes a plugin block:
It also removes the limitation #82706 lists under "Out of scope": Template Parts on different slugs and Navigation blocks on different menus no longer count as copies of each other.
On #79929, ramonjd pushed a demo that registered the external ID of every cloned block. youknowriad replied that he meant one ID per controller, which is lighter to keep and clear. Nobody had built that version.
How?
Everything in #82706 (the in-place update, the persistence reducer rule and the selected-block check), plus:
setControlledInnerBlocksSourceaction andgetControlledInnerBlocksSourceselector.useBlockSyncrecords the controller's source when it mounts, updates it when it changes, such as a Navigation block switching menus, and clears it when it unmounts. The sources live in their own slice of the store, not instate.blocksbeside the controlled flag: the persistence reducer takes any change to the blocks state as the last action, so recording a source there while someone types would end their typing run.syncSourceoption ofuseInnerBlocksPropsandInnerBlocks, next tovalue,onInputandonChange. It's an opaque string, soblock-editorstill knows nothing about entities. Each controller passes its own, so a nested controller never takes on its parent's. Navigation passespostType/wp_navigation/<ref>, Post ContentpostType/<type>/<id>, Template PartpostType/wp_template_part/<id>and a synced patternpostType/wp_block/<ref>. Third-party controllers can pass their own.The changes on top of #82706 are the commits after 99e86da: compare.
Compared with #82706
block-editorandblock-library, with new private store state, against one condition in one hook.syncSourceis public API on a stable hook, documented in theInnerBlocksREADME, so its name and meaning are kept for good.useEntityBlockEditor, which keeps copies consistent.Testing Instructions
The manual steps from #82706 apply unchanged. The featured-post case needs a plugin block that provides post context, so it's covered by the e2e test below.
Automated
All of the tests from #82706, plus:
editor/various/post-content-focus-mode.spec.js— "undo moves the caret back into Post Content from the Post Content of another post". It fails with Block Editor: Stop duplicate controllers from taking the selection #82706 (the caret stays in the other post) and passes here. Theblock-contexte2e test plugin gains a "Test Post Context Provider" block that providespostIdandpostType, standing in for a block that features a post.site-editor/controller-sources.spec.ts(new) — reads the source each controller records: two Navigation blocks on one menu record the same one, another menu and two Template Parts record their own, and a synced pattern inside a Template Part records its own, not the Template Part's. Nothing on the canvas shows these sources yet, so without this a missing one would only make the guard fall back to the block type, and every other test would still pass.use-block-syncunit tests — a controller of the same type rendering another source doesn't block a restore, a controller of another type rendering the same source counts as a copy, and the source is recorded, updated and cleared on unmount.Out of scope
test.fixmeinsite-editor/undo.spec.js.🤖 Generated with Claude Code