Repository navigation
Navigation: Prevent duplicate blocks from scrolling into view after updates - #79097
Mustafabharmal wants to merge 2 commits into
Conversation
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If 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. |
| getBlockName( currentSelectionStartId ) | ||
| ) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
Thanks for the PR! It'd be great to have some test coverage for this. I suppose in test/e2e/specs/editor/various/post-content-focus-mode.spec.js would be appropriate.
Namely to cover the core bug case, but also other focus "stealers" like undo/redo. See comment here: #79096 (comment)
When you say "valid block" here, what does it mean exactly? I'm wondering if this can introduce an issue for things like undo/redo or entity navigation... when the editor has a valid block selected but it's not the right one that should be selected after the action. |
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
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
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
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
|
Thanks for the PR! This is fixed now that #82706 is merged. |
What?
Closes #79096
Prevents duplicate Navigation block instances from taking over the editor selection when they reference the same navigation entity.
Previously, editing one Navigation block could cause another Navigation block using the same menu to become selected and scroll into view unexpectedly.
Why?
Navigation blocks that reference the same menu share a single
wp_navigationentity.When one instance updates the shared entity, other instances synchronize their internal blocks through
useBlockSync. During this synchronization, duplicate instances can restore the shared selection state and incorrectly assume ownership of the selection.As a result:
How?
Update the selection restoration logic in
useBlockSyncto avoid restoring selection when the editor is already actively selecting a valid block.This ensures that duplicate block controllers do not take over selection ownership when synchronizing shared entity updates.
Selection restoration still occurs when necessary, such as when the previously selected block has been removed during synchronization.
Testing Instructions
Screenshots or screencast
Before:
Before.Nav.Fix.mov
After:
After.Nav.Fix.mov