Skip to content
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

templateLock={ false } breaks template in 5.2.1 #15965

Closed
mattjbrent opened this issue Jun 3, 2019 · 4 comments
Closed

templateLock={ false } breaks template in 5.2.1 #15965

mattjbrent opened this issue Jun 3, 2019 · 4 comments
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor

Comments

@mattjbrent
Copy link

Describe the bug
A block was created similar to core/columns and core/column. Where the child block was set to templateLock={ false } and the parent block was set to templateLock='all'. It contained very similar functionality to core/columns. Moving a slider adds or removes a child column block.

However changing the templateLock on the parent to false breaks the template when adding or removing new columns. Increasing the number of columns does not add in the additional blocks via the template.

To reproduce
Steps to reproduce the behavior:

  1. Duplicate a minimum viable block using core/column and core/columns as a guide.
  2. Change templateLock to false on the column parent block in the edit file.
  3. Save go out to the editor and add new columns, observe no new templated column blocks are being added

Expected behavior
Regardless of the templateLock setting, blocks added through templates should remain unchanged.

Screenshots
The block hierarchy with templateLock="all"
Screen Shot 2019-06-03 at 9 11 55 AM

The same code, changing templateLock={ false }
Screen Shot 2019-06-03 at 9 13 17 AM

These are slightly modified but error is reproducible using fresh install of 5.2.1 and creating minimum viable block

Additional context

@swissspidy swissspidy added Needs Technical Feedback Needs testing from a developer perspective. [Feature] Templates API Related to API powering block template functionality in the Site Editor labels Jun 4, 2019
@jorgefilipecosta jorgefilipecosta removed the Needs Technical Feedback Needs testing from a developer perspective. label Jun 27, 2019
@jorgefilipecosta
Copy link
Member

Hi @mattjbrent, thank you for reporting this issue.

Templates were implemented as a mechanism to prefill an InnerBlocks area with some pre-created blocks, in a similar way to what we had with CPT templates.
The template is applied when an InnerBlock area is empty, changing the template does not change the InnerBlocks inside the area unless the area is empty or we have templateLock all.
When the template lock is not all e.g: is false or insert, we don't know if the difference between the template and the current inner block area state is because the user explicitly changed the area, and doing a synchronization, in that case, may end up destroying content.

In the last version of Gutenberg when there is a change in the number of columns the update to the InnerBlocks happens because the columns block dispatches an action to replace its InnerBlocks:
const { replaceInnerBlocks } = dispatch( 'core/block-editor' );.

In the previous version of the columns block before resizing functionality was added, when the number of columns changed the template was updated but the synchronization only happened because columns contains a templateLock all.

So in order to fix the problem, you are facing I guess the best way is using a template lock all, so when the template changes a synchronization happens (you may then use child blocks inside without any locking), or you can explicitly call an action replaceInnerBlocks to change the InnerBlocks of your block when something happens (like an input is changed). Columns now follows the second approach:
https://github.com/WordPress/gutenberg//blob/855be7927f31cf89229e954829e78e23d6a871e8/packages/block-library/src/columns/edit.js#L215

I hope you find this explanation clear if I missed something or you need additional information feel free to comment/reopen the issue so we can look further into this issue.

@mattjbrent
Copy link
Author

@jorgefilipecosta thanks for your detailed reply. I solved this by also using replaceInnerBlocks and dealing with it more granularly. I'm also finding using the various exposed APIs reasonably sane to work with (although in some cases documentation is sparse and not particularly detailed)

Are there more detailed documentation pertaining to more advanced APIs like this - I feel like the documentation on the site is very approachable but lacks depth and some of the inline documentation is not the clearest. If there was clearer documentation - i probably wouldnt have needed to ask this question.

Thanks again for your time.

@jorgefilipecosta
Copy link
Member

Hi @mattjbrent I'm glad you managed to fix the problem. More advanced data api's are documented in https://developer.wordpress.org/block-editor/data/. The specific action is documented at replaceInnerBlocks https://developer.wordpress.org/block-editor/data/data-core-block-editor/#replaceInnerBlocks.

@firxworx
Copy link

firxworx commented Aug 2, 2019

Hmmm... this looks to be the reason why insertBlock() was apparently silently failing (!) if I set templateLock to anything other than all in my parent block.

I'm working on an accordion-type block and found this behaviour to be completely unexpected and undocumented. I'm glad I finally noticed that templateLock was the culprit and that's what led me to this issue. Otherwise, of course I would think that my inner blocks (accordion expand/collapse items) should be removable and rearrangeable (templateLock={ false }). Honestly Gutenberg currently has one of the worst developer experiences of the entire React ecosystem... learning it's boundless nuances an absolute chore, as if everything to do with InnerBlocks wasn't bad enough.

(hopefully my rant helps inject a few more keywords to anyone trying to make an accordion, tab / tabs / tabbed, or similar type block ;) )

Thanks for submitting the bug @mattjbrent and MANY THANKS to @jorgefilipecosta for the detailed reply. Your valuable insights are hugely appreciated.

Interestingly when investigating my issue, I looked over the coblocks accordion implementation, which does NOT set templateLock to all on the parent element but it still works (you can delete accordion items within the parent block). At first glance it looks like it might have something to do with his use of transforms in index.js.
https://github.com/godaddy/coblocks/blob/master/src/blocks/accordion/components/edit.js#L113

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor
Projects
None yet
Development

No branches or pull requests

4 participants