Skip to content

Fix Post Content selection jumping across template instances - #79422

Closed
ramonjd wants to merge 1 commit into
trunkfrom
fix/post-content-in-template-focus-theft
Closed

ramonjd wants to merge 1 commit into
trunkfrom
fix/post-content-in-template-focus-theft

Conversation

@ramonjd

@ramonjd ramonjd commented Jun 23, 2026 •

Copy link
Copy Markdown
Member

What?

Noticed while testing #79191 (review)

Related to #79096 (maybe)

This prevents a controlled inner block controller from restoring a shared post entity selection when the current editor selection already belongs to another controlled block list.

It also adds e2e regression coverage for Show template mode with one Post Content block at the template root and another nested inside a Template Part.

Why?

When the same post content is rendered in more than one editable Post Content instance, each instance has different cloned block client IDs, but they map back to the same external post block IDs. That allowed an inactive Post Content instance to treat the saved entity selection as its own and move focus to its cloned block.

Testing Instructions

  1. Use a block theme.
  2. Create a Template Part containing a Post Content block.
  3. Create a Single post template containing:
    • one Post Content block at the template root
    • the Template Part that also contains Post Content
  4. Create or edit a post with a paragraph.
  5. Enable Show template.
  6. Type in the paragraph in the root Post Content area.
  7. Confirm the caret and selected block stay in the area you are editing instead of jumping to the matching paragraph inside the Template Part.
  8. Press Ctrl+Z, or Cmd+Z on macOS.
  9. Confirm the undo happens and the caret/selected block stay in the same Post Content instance

Screenshots

Before

Kapture.2026-06-23.at.14.35.15.mp4

After

Kapture.2026-06-23.at.15.41.15.mp4

@ramonjd ramonjd self-assigned this Jun 23, 2026
@ramonjd
ramonjd requested a review from ellatrix as a code owner June 23, 2026 06:01
@github-actions

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Jun 23, 2026
@ramonjd
ramonjd force-pushed the fix/post-content-in-template-focus-theft branch from f15645b to fe50598 Compare June 23, 2026 06:03
const parents = blockClientId ? getBlockParents( blockClientId ) : [];

for ( let i = parents.length - 1; i >= 0; i-- ) {
if ( areInnerBlocksControlled( parents[ i ] ) ) {

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.

Answers the question: which controlled block area (if any) currently owns this selected block?

@github-actions

github-actions Bot commented Jun 23, 2026 •

Copy link
Copy Markdown

Size Change: +104 B (0%)

Total Size: 7.51 MB

📦 View Changed
Filename Size Change
build/scripts/block-editor/index.min.js 381 kB +104 B (+0.03%)

compressed-size-action

areInnerBlocksControlled
);

if ( selectedControllerId && selectedControllerId !== clientId ) {

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.

If selectedControllerId is undefined continue restore selection as before.

I'm not quite sure yet if this is the right way, I'm not familiar with this function. The logic I'm following is basically, if the user is actively selecting/typing inside another controlled area we should keep that selection.

);

if ( selectedControllerId && selectedControllerId !== clientId ) {
return;

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.

Before this change, restoreSelection() did this:

  1. Read the saved selection from SelectionContext.
  2. Check whether the saved selection’s client ID “belongs” to this useBlockSync controller.
  3. If yes, convert the saved external client ID into this controller’s internal cloned client ID.
  4. Call resetSelection() so the block editor selection moves there.

That works when there is only one rendered copy of a controlled block list.

The problem is step 2. For controlled inner blocks, "belongs to this controller" means:

idMappingRef.current.externalToInternal.has( startClientId )

If two Post Content blocks render the same post entity, both have clone maps for the same external paragraph ID. So both can say "yes, this selection belongs to me".

Before the guard, whichever controller restored later could call resetSelection() and move (jump) focus into its own cloned paragraph.

After the guard, a controller still restores selection when appropriate, but it first checks whether the current live selection is already inside another controlled area. If it is, this controller does not call resetSelection().

@ramonjd

ramonjd commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

I think this fixes #79096 as well.

Before

Kapture.2026-06-23.at.16.21.20.mp4

After

Kapture.2026-06-23.at.16.21.59.mp4

@talldan
talldan requested a review from youknowriad June 23, 2026 06:22
@ramonjd ramonjd added the [Type] Bug An existing feature does not function as intended label Jun 23, 2026
@github-actions

github-actions Bot commented Jun 23, 2026 •

Copy link
Copy Markdown

Flaky tests detected in 3ab6014.
Some 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.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/28007338808
📝 Reported issues:

@ramonjd
ramonjd force-pushed the fix/post-content-in-template-focus-theft branch from fe50598 to 3ab6014 Compare June 23, 2026 06:37
@ramonjd

ramonjd commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

Closing in favour of #79097

@ramonjd ramonjd closed this Jun 23, 2026
@ramonjd
ramonjd deleted the fix/post-content-in-template-focus-theft branch June 23, 2026 06:44
jeryj added a commit that referenced this pull request Oct 15, 2026
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
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 [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant