-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Real-time collaboration: Fix "Show Template" view #72405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Real-time collaboration: Fix "Show Template" view #72405
Conversation
…n a template for UUID-based awareness features to work
|
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. |
| // original blocks to maintain UUIDs used for | ||
| // multi-user collaboration | ||
| // | ||
| // Unsure: Why are these blocks being cloned? Do they need to be? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have this code change in for discussion, but this comment should probably be removed before merging.
|
I think this is probably not the right fix. The idea here is that the "block-editor" store was not built originally to support rendering the same block twice. So without the cloning of UID which tricks the block-editor store to think that these blocks are different, we had bugs when rendering the same "template part" or same "synced pattern" twice in a page. It is also a use-case that can happen with postContent blocks, for instance when you have multiple query blocks in a page with the same posts rendered on each query... So I think for this change we need to thread very carefully. I'm actually a bit surprised that no e2e test failed, cause I'm pretty sure we had this covered (maybe that e2e test has been skipped or something) I think we should test this a bit more, I'd love to be able to remove the cloning, but I feel like it requires more important changes to the block-editor store and its selectors. To clarify, like what does it mean to "select a block" using a clientUID if that block is present twice in the block tree, which block are you going to highlight, where to show the block toolbar. This is just an example, the same question can be asked for any selection/action that relies on clientUID in the block editor. |
|
@youknowriad Thank you for the feedback on this change! I assumed there was purpose to the original cloning, and you gave me a good place to start with patterns. More detail below, but in short: I was able to reproduce the issues you mentioned if I changed the code to never run Reproducing block cloning issues with synced patternsHere's how to reproduce render issues with synced patterns if
HoweverSo far these issues can be recreated by completely disabling block cloning. If we switch back to this PR's code that does not clone blocks scoped within post-content-blocks-only-1.movI can also confirm via DOM that each of these pattern blocks are still assigned an unique UUID. This problem doesn't seem to be reproducible with the limited scope of the changes here, only if we remove cloning blocks altogether. I can understand the potential problem, but I can not reproduce it within this PR. Follow-up questions
|
|
@alecgeatches Did you try the same steps you did here for the "pattern block" buy inserting the "post content" block twice within the template editor (in the site editor)? |
|
So I'm having trouble reproducing the issue myself. In theory the steps would be:
In theory, this should be showing the post with template in question, so the post content should be showing twice.
I think there's probably an unrelated bug here (unrelated to the current PR), but if that bug didn't exist and we were able to assign that template properly, I bet we'll see a very similar issue to the "pattern" block. (one or both of the post content block would appear empty) I'm honestly not sure what's the path forward here. |
|
@youknowriad That reproduces it! If I create two post content blocks:
and then use "Show Template" mode, typing in either post content jumps around and doesn't work correctly: Screen.Recording.2025-10-23.at.3.40.48.PM.movBlocks in post content that have the same UUID cause the issue you suspected. I can think of some possible next steps:
I'll play around some more in the code to see if there's another way we can do this, but I'm happy to hear if you have any other ideas. |
Why do the uuids used within the block-editor store matter? Maybe these are temporary uids, and the cloning persists but it also can persist in both ways. In other words when |
|
The suggestion I made above could potentially lead to a small performance impact (mapping through blocks multiple times), worth exploring but there could be solutions where we keep two block trees in the block-editor store (once with cloned uids and one with the real ones) to avoid the mapping or something. Anyway, these are just crazy ideas :) |
@youknowriad Thank you for the response and idea above! I just wanted to let you know I haven't forgotten about this PR, but I have been busy working on some other undo and diffing changes since your comment. I'll get back to this soon and explore how this could be implemented. |





What?
This modifies
useBlockSync()to persist post content block IDs when "Show Template" mode is enabled.Why?
Currently, when "Show Template" mode is enabled, cursors and other block-specific awareness information doesn't sync across clients. Awareness cursors and block highlights do not show when in "Show Template" mode:
Screen.Recording.2025-10-16.at.5.03.21.PM.mov
How?
When "Show Template" is enabled, this causes all post content blocks to run through
cloneBlock(), which gives each block a random UUID each time the mode is enabled. In our awareness implementation, we use synced block IDs to show cursor state and block highlights. Any time blocks arecloneBlock()ed we lose the ability to easily join block state with the awareness overlay.In the changed code below, the
clientIdonly has a value when it represents a block with an underlying controlled entity, such as a template or post content. TheclientIdpassed intouseBlockSync()is the parent block, like acore/post-contentor template header or template footer.For the solution, we determine if the block being synced represents post content, and if so, we keep the default UUIDs.
The way that
cloneBlock()is currently implemented (without using themergeAttributesornewInnerBlocksparameters), it functions entirely to recursively replace block UUIDs:gutenberg/packages/blocks/src/api/factory.js
Lines 135 to 159 in 91c4492
Because we only apply this change to Yjs-synced post content and
cloneBlock()doesn't appear to do anything other than randomize the IDs of the blocks, this allows users to use the same block IDs used in non-template mode. Here's the fix in action:Screen.Recording.2025-10-16.at.5.10.23.PM.mov
Open questions
As mentioned in the code, it's not clear why
cloneBlocks()is used to randomize block IDs within thepost-contentblock. Does this break any existing functionality that expects different block UUIDs?Testing Instructions
fix/show-template-block-awareness) along with the vip-real-time-collaboration plugin.