Repository navigation
Closed
Labels
[Feature] BlocksOverall functionality of blocks[Feature] Synced PatternsRelated to synced patterns (formerly reusable blocks)
Description
I'm splitting this out from #1516 (Turning a static block into a dynamic reusable block) since that issue is long and primarily focused on the UI aspects. Here's what I've been thinking of in terms of how such reusable blocks would work at a technical level:
- There would be a new
blockpost type. - The
blockpost would store in itspost_contentone serialized block. Potentially we could store the block's attributes individually in postmeta instead, and just the contents of the block inpost_content. That would improve the ability to do queries on the blocks. If a block type is registered with PHP, it could have serialize/deserialize logic that would dictate whether the attributes get stored in postmeta instead of in content. - A block would be identified by a UUID which would get saved in
post_name. Using a UUID instead of auto-incremented post ID would eliminate the requirement that a block post be created up-front to reserve its ID. The auto-incremented post ID would be for internal use only and not exposed in the API. - A
block_typetaxonomy would exist and each block would be associated with one and only one term that would store the block type as the term's slug. The sanitization logic for such terms could be overridden to allow slashes in the slug, e.g.core/text. - The block post type would be exposed in the REST API with endpoints such as:
/gutenberg/v1/block-types- Collection of registered block types./gutenberg/v1/blocks- Collection of all blocks./gutenberg/v1/blocks?type=text/core- Collection of blocks of thecore/texttype./gutenberg/v1/blocks/358b59ee-bab3-4d6f-8445-e8c6971a5601- Single core/text block.
- A
blockresource would look like:
{
"id": "358b59ee-bab3-4d6f-8445-e8c6971a5601",
"type": "core/text",
"attributes": {
"dropCap": true,
"align": "center"
},
"content": "<p style=\"text-align:center\" class=\"has-drop-cap\">Hello World!</p>"
}- External blocks would be embedded in a post via a
core/blockblock type which has a single attribute that contains the UUID (REST API resourceid) for the external block, for example:<!-- wp:core/block { "ref": "358b59ee-bab3-4d6f-8445-e8c6971a5601" } /--> - When a
core/blockblock initializes, it looks up the from the REST API via the UUID supplied in therefattribute, and then it creates a block component for thetypereturned for thatblockresource. - Modifications to such an external
core/block's attributes get written not into the containing post's content, but rather get written intoeditsfor that externalblockresource. - Saving the editor updates not only the main
postresource, but also any edits to theblockresources that were modified. - The
core/blockblock would be registered in PHP, and itsrender_callbackwould look up theblockpost by its UUIDref, and embed the content from thatblockpost. - When a non-reusable block (attached block) is converted into a reusable (detached) block it would go through a
transformationthat would result in a new REST APIblockresource being drafted among theedits, including a newly-generated UUID and all of the attributes from the block would be moved into this detached block resource, with the transformed block in content then getting the generated UUID as its solerefattribute. - Likewise, a reusable/detached could be re-attached to the content by reversing the transformation in the previous step.
Thoughts?
Reacted by Matias Ventura, Robert Anderson, Steffen Bang Nielsen and Rastislav Lamos
Metadata
Metadata
Assignees
Labels
[Feature] BlocksOverall functionality of blocks[Feature] Synced PatternsRelated to synced patterns (formerly reusable blocks)
Activity
westonruter commentedon Jul 29, 2017
Also: If a PHP-registered block had schema and validation logic defined, the endpoint could validate the incoming attributes upon updating. Without a block being registered in PHP or without any schema defined, then it would just have to accept any attributes and content blindly.
mtias commentedon Aug 1, 2017
This is more or less what I had in mind, thanks for diving into the details!
And also separately from the action of saving a post. Modifications to a global block would likely have to offer the ability to convert to regular block, or to edit globally. Maybe on blur the user is asked this.
I'd call this
core/global-block, perhaps, for clarity.noisysocks commentedon Aug 19, 2017
This approach makes a lot of sense! I'm going to take a crack at implementing this, starting with the REST API that you describe.
When would this endpoint be used? I don't think it would be necessary to build a flow like the one that @jasmussen described here.
noisysocks commentedon Aug 27, 2017
I experimented a little with implementing reusable blocks as a self-contained block type over in this branch. Here's a little demo:
This isn't a workable approach, though, because:
editcomponent's local state and not in the redux state tree.editcomponent.Here's a sketch of the approach I'm now considering:
core/reusable-blockblock type that doesn't define asaveoreditfunction—it exists solely so that the parser can interpret a<!-- wp:core/reusable-block { "ref": "358b59ee-bab3-4d6f-8445-e8c6971a5601" } /-->.reusableBlocksto the redux state tree, undereditorso that changes made to reusable blocks are undoable. Each reusable block in this object is keyed by its ID, and would look like:FETCH_REUSABLE_BLOCKis an action that causes an effect to fetch a reusable block from the API. The reusable block is marked as loading during this time. On success, the fetched block inserted atreusableBlocks[id].UPDATE_REUSABLE_BLOCKis an action that updates the block atreusableBlocks[id]. This also marks the block as dirty.PERSIST_REUSABLE_BLOCKis an action that causes an effect to save a reusable block to the API. On success, the block atreusableBlocks[id]is marked as clean.RESET_BLOCKS, an effect runs that dispatchesFETCH_REUSABLE_BLOCKfor every reusable block in the post.REQUEST_POST_UPDATE, an effect runs that dispatchesPERSIST_REUSABLE_BLOCKfor every reusable block that is marked dirty.<VisualEditorBlock>so that, ifblock.name === 'core/reusable-block:<BlockEdit>forreusableBlocks[ ref ]instead of forblock.<BlockEdit>asetAttributesprop that dispatchesUPDATE_REUSABLE_BLOCK.replaceBlock(id, createBlock(reusableBlocks[id].type, reusableBlocks[id].attributes)).updateReusableBlock(newUuid, block.name, block.attributes)andreplaceBlock(block.uid, createBlock('core/reusable-block', { ref: newUuid }).Thoughts?
mtias commentedon Aug 30, 2017
That's a great overview and plan, thanks for thinking it through.
This is an interesting design consideration, but I wouldn't label it a blocker for an initial implementation.
I agree this is not ideal, but global-blocks are different and ought to be saved separately from the post. (There are other previous examples of metaboxes in WordPress doing similar operations separately from post save actions.)
The proposed designs so far show the following dialog when attempting to edit a global block. I think this could be shown on blur the first time you edit instead, making it clear something will be saved right then and there:
Or we could have an "update globally" button within the block UI.
This is interesting and it makes sense—as a "reusable block" doesn't really have specific UI but that of the original block it was created from—so it works pretty much as a pointer to another block. That said, the options shown in the above screenshot (or whatever iterations we do from there) could be considered the
editrender definition of the reusable block. In that case, I might expect these to de declared on the edit method of the reusable block.Pseudo code:
Granted, this won't work given how
VisualBlockoperates, but just throwing it out there, as we could start considering global blocks as a kind of parent-child block, with parent being the global wrapper, and child being the original block.youknowriad commentedon Aug 30, 2017
An alternative could be to display the "save" output of the original block by default and have an "edit" button to enter Edit mode of this block. This makes it more obvious that it's separate from the post flow.
noisysocks commentedon Aug 30, 2017
Agree that it'd be nice to have the UI live in the
editcomponent. Although it would mean that we have to import selectors and action creators from withinblocks/reusable-block/index.js, which feels pretty weird.This is a neat idea! Would be great to get a design made for it.
andreiglingeanu commentedon Sep 20, 2017
@mtias Could you please point me into the direction of them? I'm looking for some patterns/examples for doing that?
This feature looks very nice, by the way. Nice work @noisysocks!
The idea of having the reusable block as a block type is very nice, but I'm thinking it would fail shortly with the arrival of groups of blocks (and maybe with nested blocks). #1516 (comment)
mtias commentedon Sep 20, 2017
Yes, core itself :) When you enter a new custom field, you don't have to save the post.
It is not very intuitive (there's a yellow background thing that sort of implies it has been added).
The group of blocks was clarified here: #1516 (comment)
It would use the same system because the group of blocks would always have a block as the parent, and that parent is the one you make reusable.
westonruter commentedon Sep 21, 2017
I really think we should avoid this if at all possible. The “Custom Fields” UI isn't exactly a heavily-trodden cowpath that serves as an ideal model to follow. In fact, until recently the Featured Image was another such metabox that updated instantly upon changing, even without saving the post as a whole. This was brought up in #12922 (Setting featured image does not require the post to be saved) and has been fixed so that now you have to explicitly save the post as a whole in order to get the featured image change to persist.
This is the same model that we should follow for reusable blocks. As a user, after I make a change to a regular block and make a change to a global block, I should then be able to click Preview to see changes to both of those blocks on the frontend. If then I decide I don't like the changes, I should be able to leave the post and the changes I made should be discarded—both to the regular block and the reusable block.
Essentially the redux state as a whole should be what is submitted to the preview, including the changes to the reusable blocks and the local blocks. In the Customizer, previewing would be implemented by putting the state into a changeset. This is what I was referring to in #1516 (comment). Submitting the entire redux state tree for the preview would allow for previewing changes to not only the post but also the postmeta, something which is not possible in core now either (#20299).
andreiglingeanu commentedon Sep 21, 2017
Oh, yeah, I totally forgot about them! Yes, they are a perfect example for that sort of use case and I'll agree that they are not very intuitive. A lot of corner cases and unnecessary code paths usually arise when one starts to go down that path.
But, sometimes, you plainly can't avoid that. And the reusable blocks are one instance of such a case.
aduth commentedon Oct 16, 2017
Can you elaborate on why, if we were to use a standard post ID, we would need to reserve an ID up-front? This as a follow-on to my review at #2659 (review) where my initial impression was that we're going far out of our way to avoid using IDs, and I'm not sure what we're gaining in doing so.
andreiglingeanu commentedon Oct 16, 2017
Probably because the frontend code will be able to create reusable blocks that won't collide with other ones, without having to touch the server for doing so.
aduth commentedon Oct 16, 2017
In that case, it seems like we could still assign a temporary ID or other identifier to mark the block as not-yet-persisted while manipulating it in the client, then update the local copy once the server-assigned ID becomes known.
In Calypso, we do similar for media where we want to be able to reference and display an item even prior to it being uploaded to the server, using a combination of a boolean flag property and prefixed ID:
https://github.com/Automattic/wp-calypso/blob/e3bca1e/client/lib/media/utils.js#L531-L586
westonruter commentedon Oct 16, 2017
@aduth @andreiglingeanu A few reasons for why I wanted to go with UUIDs:
auto-draft, but that means we have to do a request to “reserve” the ID so that someone else doesn't snatch it up concurrently. If we could use UUIDs instead, then we could avoid this, and there is no need to swap out the underlying ID when it gets saved into the DB.auto-draft(as is done for the edit post screen). There are other benefits, like having a unique universal identifier that can also be secret preview key.post_namefor a Block contain otherwise? It's already a unique indexible field, so storing a UUID is handy here for lookups and we get “for free” portability of suchblockposts when migrated across WordPress installs, if we only ever reference UUIDs and not the underlying integer IDs.joehoyle commentedon Nov 19, 2017
I might be missing a piece of this puzzle so apologies for wading into this late! The above REST spec and some associated features seem to make the assumption that the registered blocks are available server-side, but as I understand it, it's recommend to write / register custom blocks in JavaScript: https://wordpress.org/gutenberg/handbook/blocks/writing-your-first-block-type/
This is a "limitation" that struck me when reading the handbook - by having all block registration done client side, it becomes impossible to provide server-side functionality and abstractions, such as a
/block-typesREST API endpoint. Having them available server side allows us to do some more security wise validation, which would presumably be more important when blocks can be used outside of the post_content (which is safe, as already has all the reliability of existing sanitization).It would also open up the possibility of other block interfaces backed by a REST API, such as mobile apps. Again, if this has been discussed elsewhere, a link would be great. By having block registration as part of the Gutenberg app client side, the blocks and general Gutenberg concept will never be able to escape the confines of the WP Admin. While I don't think it will be possible to make blocks totally portable (UI / front end logic is always going to be more powerful expressed as JavaScript components), having registration happen at the server level (and only the server level) will ensure they are more portable.
aduth commentedon Nov 20, 2017
Related: #2751
mtias commentedon Dec 7, 2017
Closing as technical implementation is ready.