Skip to content

Block Editor: Prevent duplicate block controllers from stealing the selection - #79929

Closed
ramonjd wants to merge 2 commits into
trunkfrom
try/prevent-duplicate-blocks-stealing-focus
Closed

ramonjd wants to merge 2 commits into
trunkfrom
try/prevent-duplicate-blocks-stealing-focus

Conversation

@ramonjd

@ramonjd ramonjd commented Jul 7, 2026 •

Copy link
Copy Markdown
Member

What

Stops a Navigation block from taking over the selection and scrolling into view when a duplicate block using the same menu is edited.

Fixes #79096.

Why

Two Navigation blocks using the same menu each show their own private copies of the menu items. When one block updates the menu, the other re-syncs and restores the menu's remembered selection onto its own copies. Both blocks think they own the selection, so the last one to sync steals it. In the Site Editor this also breaks typing: the caret snaps back after every keystroke, so "New label" comes out as "lebal weN".

What changed

Each controller (the code that syncs one block's copies with the menu) now registers in the block-editor store which original menu item each of its copies was made from. Before restoring a selection, a controller checks: is the selected block already a copy of the item the selection points at? If yes, another block has handled it, so leave it alone. If no, restore as before, so the selection can still move between blocks when it should (undo, for example).

The first commit fixed this with a local check ("is the selected block one of my copies?"). The second commit replaces it with the store-based check suggested by @youknowriad in review, which also keeps undo working across blocks.

Unit tests cover the duplicate case, the undo case, and the selection moving between blocks. New e2e tests: one reproduces the bug in the Site Editor (fails on trunk), one checks that undo returns the caret to the undone block.

Manual testing

  1. In the Site Editor, add two Navigation blocks that use the same menu.
  2. Click into a menu item label in the first block and type.
  3. The text appears in the order typed, the selection stays in the first block, and the editor does not scroll to the duplicate.
  4. In the post editor, type in a paragraph, click a different paragraph, press Cmd+Z. The caret returns to the undone edit.

@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Size Change: +228 B (0%)

Total Size: 7.7 MB

📦 View Changed
Filename Size Change
build/scripts/block-editor/index.min.js 419 kB +228 B (+0.05%)

compressed-size-action

…election

When two controlled blocks are synced from the same entity (e.g. two
Navigation blocks using the same menu), both controllers pass the
ownership check in useBlockSync's restoreSelection, because they map
the same external client IDs. Whichever controller synced last would
restore the entity selection onto its own clones, moving the user's
selection, scrolling the duplicate into view, and reversing typed
text in the Site Editor.

Skip the restore in inner block controllers when the currently
selected block is a live block that belongs to another controller.
The root controller never skips, so selection restoration on
undo/redo keeps working.
@ramonjd
ramonjd force-pushed the try/prevent-duplicate-blocks-stealing-focus branch from 2cab300 to f5cb7dd Compare July 8, 2026 02:43
const currentClientId = getSelectionStart()?.clientId;
const isSelectionElsewhere =
currentClientId &&
! idMappingRef.current.internalToExternal.has(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

! idMappingRef.current.internalToExternal.has( currentClientId ) === the selected block is not one of this controller’s own clones

// would break caret restoration on undo/redo).
if ( clientId ) {
const currentClientId = getSelectionStart()?.clientId;
const isSelectionElsewhere =

@ramonjd ramonjd Jul 8, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guard answers one question "does the selection currently in the block-editor store belong to somebody else who is actively using it?"

var could also be named selectionBelongsToAnotherController

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right question and guard to add, the problem is that I don't understand how can idMappingRef.current.internalToExternal give us that information given that variable is specific to the current controller, which means my confidence in this check as implemented now is low. Am I wrong?

Assuming I'm not wrong, my suggestion would be to store the "external clientId" of the controller in the "block-editor" store when a new controller is added (so in addition to marking a block as a controller, we also tell it what is the right external id that it corresponds too), this would allow us to check if that "other controller" contains the selection already or not.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!

I'll couch this statement with the reality that I'm not an expert in how block sync is wired up, but yeah, you're right that (and something I at least know 😄) the map is local and it doesn't tell us anything about other controllers.

The entire check doesn't try to know about other controllers. And I think my question above was not explicit enough.Rather, it's asking "is the selected block one of mine?" If it isn't mine and it still exists, I leave it alone. If it no longer exists the restore goes ahead.

The name is clumsy. Perhaps isSelectionNotOurs is better? I don't know.

my suggestion would be to store the "external clientId" of the controller in the "block-editor" store when a new controller is added (so in addition to marking a block as a controller, we also tell it what is the right external id that it corresponds too), this would allow us to check if that "other controller" contains the selection already or not.

Ah, if I understand it right, this sounds better.

My check relies on the internal reference to the original item. With an id in the store, I'm guessing we could compare the currently selected block with a stored external ID. If it's the same, the right block is already selected.

One thing I'm not sure about: does each controller correspond to one external client id, or one per block it renders? Is that what should be registered/deregistered?

I don't know how much time I'll have to look at this - this PR was always just a YOLO try - but I can throw an agent at it to see what it looks like (maybe a separate commit on top of this draft).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire check doesn't try to know about other controllers. And I think my question above was not explicit enough.Rather, it's asking "is the selected block one of mine?" If it isn't mine and it still exists, I leave it alone. If it no longer exists the restore goes ahead.

Is that the right question thing to do though? I mean if the previous selection is not mine and the block still exists, we might still want to apply the "new selection" (coming from outside the block-editor store) if the new selection is mine. (For instance a programmatic selection change like undo/redo moving selection to another place entirely) no?

My check relies on the internal reference to the original item. With an id in the store, I'm guessing we could compare the currently selected block with a stored external ID. If it's the same, the right block is already selected.

My suggestion was to only store the external ID of the controllers, not all blocks. which allows us to say "is this internal id's controller, another controller for the same ID of the updated selection". What you suggest works too but it asks the question of when we clear these external Ids... cause there will be a ton, and for controllers, we already have logic tor the setting and cleaning of controllers If I'm not wrong.

Replaces the local ownership check in restoreSelection with the
approach suggested in review: each inner block controller registers
in the block-editor store which external block each of its clones
was made from. Before restoring a selection, a controller checks
whether the currently selected block is already a copy of the block
the selection targets, whichever controller made the copy. This
still prevents duplicate controllers from stealing the selection,
and unlike the previous check it lets the selection move between
controllers when it targets different content (e.g. undo).
@ramonjd

ramonjd commented Jul 12, 2026

Copy link
Copy Markdown
Member Author

Full discloure: I've pushed some changes just for demonstration. I haven't tested or reviewed the code. Feel free to take over, or I'll return to it when I get a window of time. Thanks!

@ramonjd

ramonjd commented Sep 9, 2026

Copy link
Copy Markdown
Member Author

Closing in favour of #82706

@ramonjd ramonjd closed this Sep 9, 2026
jeryj added a commit that referenced this pull request Oct 15, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding an item to a duplicate Navigation Block scrolls the second one into view

2 participants