Repository navigation
Block Editor: Stop duplicate controllers from taking the selection - #82706
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. 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: +184 B (0%) Total Size: 8.85 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
|
| // 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
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
70180df to
13b223b
Compare
…nc fix Remove the `useBlockSync` early return for a block that is gone and no longer controlled, and the detach step that let go of the controlled inner blocks so the early return could tell a detach apart. Both only kept the typing that converts a Page List in one undo step. The cleanup between two keystrokes now starts a new undo step again, so typing a new label takes two undos to bring the Page List back. #82706 fixes this: its undo grouping keeps a typing run across a change outside the history that leaves the typed block alone. The e2e test for it is pinned with `test.fixme` until then. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
acbbdb3 to
ab749f7
Compare
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
b20be87 to
57d7e4f
Compare
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
ramonjd
left a comment
There was a problem hiding this comment.
I ran some e2e scenarios locally on this and found no side effects, except the navigation blocks menu bug, outlined below.
Test cases:
| Case | Trunk | PR |
|---|---|---|
| Issue’s own steps: add a menu item to Navigation A while B uses the same menu | Selection went A → B and the canvas scrolled 1657 px | Stayed in A, no scroll |
| Press Enter in one of two Post Content blocks | Selection went A → B | Stayed in A |
| Type in A, click into B, undo | Text reverted. Selection went B → A → B | Text reverted. Selection stayed in B |
| Redo with a duplicate | Text correct. Selection went back and forth between A and B 6 times | Text correct. Selection stayed in A |
| Two menus (one per Navigation block): type in A, click B, undo | A reverted, selection stayed in B | Same as trunk |
| One Navigation block, no duplicate: type, undo, redo | Text correct. Selection was cleared and restored on every undo and redo | Text correct. Selection never cleared |
I also tested manually again, concentrating on undo/redo.
redo-3456x1944.mp4
Some ideas for further test coverage (optional)
- Adding a menu item doesn’t move the selection into the duplicate
- Undo then redo in a duplicated container (Both nav containers show changes, then all restored by a single redo)
I guess the question is whether @youknowriad's store-based version in #79929 is preferred?
For the record, I hammered undo/redo and didn't see anything strange.
| * | ||
| * @return Whether the updated blocks are untouched. | ||
| */ | ||
| function leavesBlocksUntouched( |
There was a problem hiding this comment.
Nit and 100% optional. Going by the function name alone, I'm not sure what it does.
Without consulting an agent, my meat brain is trying piece together the puzzle of "on update block attributes" and "block list ids match previous block ids state"
How to turn that into something comprehensible though 🤔
| * @param clone Its clone in the block-editor store. | ||
| * @return Whether the blocks have the same fields. | ||
| */ | ||
| function hasSameFields( block: Block, clone: Block ): boolean { |
There was a problem hiding this comment.
Same here. It could be just me, but I think the function name is missing what it acts upon, e.g., areBlock[Keys|Fields]Equal
| await editor.canvas | ||
| .locator( `[data-block="${ rootParagraph }"]` ) | ||
| .click(); | ||
|
|
There was a problem hiding this comment.
Maybe Post Content hasn't finished loading 🤷🏻
Do we also need to use waitForPostContentVisible here too?
| // Two controllers of one type that render different entities, such | ||
| // as Template Parts on different slugs, count as copies too: telling | ||
| // them apart would need each controller to record its entity. | ||
| const parents = getBlockParents( currentStartClientId ); |
There was a problem hiding this comment.
So this only searches the selected block’s parents... do we need to include the block itself?
So a selection on another Navigation block, for example, counts?
I think I found a bug.
- Click a link in the header menu ("Visit us"), so the link itself is selected.
- Click the header menu's "Add page" button.
The search popover opens in the header, but the selection moves to the footer's "Visit us"
nav-add-3456x1944.mp4
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
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>
57d7e4f to
907e332
Compare
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>
There was a problem hiding this comment.
Another round of testing caught something else.
Basically, undoing an unsynced pattern creation removes the container's metadata but the inner blocks still have contentOnly restrictions (at least on the toolbars).
I think it should be fixed in this PR, given the changes to set controlled blocks.
I've run it on trunk to confirm.
How to reproduce:
- Use a block theme and turn on View → "Show template". Without "Show template" the post's blocks load through a different code path and the bug doesn't happen.
- Insert a Group containing two paragraphs. Don't give the Group a custom name first: a name is stored in metadata, and then the metadata isn't empty after Undo, so the reducer handles it correctly.
- Click a child paragraph. Note its toolbar and its Block inspector. On my run the toolbar had "Select parent block: Group", "Align block" and "Align text", and the inspector listed Typography, Background, Dimensions, Borders, Elements and Advanced.
- Select the Group, then Options → Create pattern, turn Synced off, and click Add.
- Click Undo once.
- Click the same child paragraph again. Bug: the toolbar has lost those three buttons, and the inspector shows only Typography. Step 3 is what you should see.
- Reloading the page brings the full controls back.
2026-12-02.10.14.56.mov
I tested a fix locally that works.
A property presence check lets an explicit metadata: undefined clear the old restrictions while an update that omits metadata preserves them.
Object.hasOwn( attributes, 'metadata' ) &&
…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>
…er-selection-tag Brings in #82706's re-clone when an attribute is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…roller Several writers record a selection without `controllerClientId`: core-data, when a collaborator's edit shifts the caret, and the post and site editors, when they restore the selection on returning from editing a pattern or a template part. A controller that remounted leaves a client ID that is gone. Every copy restored those, as on trunk, so the steal came back for them. When the selection names a controller on the canvas, only that controller restores it. Otherwise, the guard from #82706 decides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Moves the changelog entry to Unreleased, since trunk released 20.2.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| const [ clientId ] = getBlocksByName( 'core/post-content' ); | ||
| return __unstableGetVisibleBlocks().has( clientId ); |
There was a problem hiding this comment.
| const [ clientId ] = getBlocksByName( 'core/post-content' ); | |
| return __unstableGetVisibleBlocks().has( clientId ); | |
| const visible = __unstableGetVisibleBlocks(); | |
| const clientIds = getBlocksByName( 'core/post-content' ); | |
| return ( | |
| clientIds.length > 0 && | |
| clientIds.every( ( clientId ) => visible.has( clientId ) ) | |
| ); |
I ran this test locally and it's flaky the same way it was on the CI.
https://github.com/WordPress/gutenberg/actions/runs/45475581103/job/138736443713#step:8:54
The fix seems to be to make the helper wait for every Post Content.
I ran it 25 times in a row with no fails. Before I had a couple.
| name: 'core/paragraph', | ||
| attributes: { content: 'Shared' }, | ||
| } ); | ||
| await postContentFocusMode.enableShowTemplate(); |
There was a problem hiding this comment.
| await postContentFocusMode.enableShowTemplate(); | |
| await postContentFocusMode.enableShowTemplate(); | |
| await postContentFocusMode.waitForPostContentVisible(); |
Related to below.
…tion "Show template" mounts the nested Post Content after the root one. When it mounts after the test clears the selection, it restores the post's selection into its own copy, and that copy's block toolbar covers the root paragraph the test then clicks. Wait for both instances and for both to be visible, as ramonjd suggested in review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
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 recognizes 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, so every character became its own undo level.How?
Three 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, found withgetBlockParents. The selected block counts as well as its parents, since adding a link to a Navigation block can leave that Navigation block selected. A selection anywhere inside this controller is its own, even one inside a controller nested in it, such as a synced pattern inside Post Content, so undo still moves the caret out of it. Outside it, only a controller of the same block type counts as another copy, since a copy of one entity is always rendered by the same kind of block: a Template Part around Post Title renders a different entity, so it does not stop an undo from moving the caret back into Post Content. 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 in the same order, all the way down) and differs from them in attributes only, 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. As after a re-clone, the next undo can still restore its recorded selection, even when it is the one restored last time, as on undo, type, undo. Structural changes still re-clone, and so do a change to any other block field, such as theinnerContentthat the Custom HTML block edits withupdateBlock, and a removed attribute. Removing an attribute can change more than the attribute: a Navigation block whoserefan undo removes no longer controls its inner blocks, so they have to come from the incoming value, and the in-place update leaves the inner blocks of a controller alone. The reducer that derives block editing modes also treats ametadataattribute that is present butundefinedas removed. It used to count only a truthymetadata, so when an earlier version of this PR set a removedmetadatatoundefinedin place, undoing the creation of an unsynced pattern left its blocks locked.Why copies are found by block type
On #79929, youknowriad suggested that each controller register the entity it renders in the block-editor store, so that the guard can tell exactly which controllers are copies of each other. This PR counts any controller of the same block type as a copy instead, as a first step that can move to a registered entity later if needed:
useBlockSyncand uses existing selectors. It needs no new store state, no new API and no changes to the controller blocks.An earlier version also changed
useMarkPersistent, so that a change reaching an unselected field could not end the typing run in the other container. Trunk has since removed that hook in #86007 and starts undo levels from a field's own change handler, which a change arriving from another container never calls, so that change was dropped when rebasing.Credit
Similar to @ramonjd's approach from #79422 and #79929.
Testing Instructions
Automated
E2e tests added:
site-editor/navigation-selection-sync.spec.ts(new) — the reproduction: with two Navigation blocks using one menu, editing a menu item leaves the caret and selection in the edited block. 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.editor/various/post-content-focus-mode.spec.js, all with "Show template" on:refbut kept the menu's links, and the block then picked the menu again.site-editor/undo.spec.js— in a container duplicated from one entity, typing keeps every character, undo removes the whole typed run, and one redo brings it back in both containers. Atest.fixmepins the pre-existing gap described under "Out of scope".editor/various/undo.spec.js— undo restores the caret to the undone block even when another block is selected, so the selection recorded with the undo level wins over the current one.site-editor/template-part-focus-mode.spec.js— the Template Part is selected again after returning from editing it. The theme setup moved frombeforeAlltobeforeEach, since the theme is restored after every test.Existing coverage constrains the guard from being too broad: forcing it to always skip fails
patterns.spec.js→ "should restore selection after navigating back from pattern editing", which restores a selection inside the Post Content controller.Unit tests:
use-block-sync— the in-place update, and a re-clone after a structural change, a reorder, a change to a field other than attributes or a removed attribute; typing in one copy staying in one undo level while another copy mirrors it; the selection guard with a duplicate controller, with a duplicate controller's own block selected, with a controller nested inside this one and with a controller of another type; an undo returning to the selection restored before.store/test/reducer.ts— the rule for ignored changes to other blocks, a new undo level after an ignored change resets or updates the edited block, and typing in a second block staying in one undo level. An update that setsmetadatatoundefinedremoves an unsynced pattern's editing modes, and one that leavesmetadataout keeps them.Out of scope
test.fixmeinsite-editor/undo.spec.js.🤖 Generated with Claude Code
https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
https://claude.ai/code/session_01UpQLN4sQKUDBA45FqsyUQY