Issue Overview
This issue explores ways to give more flexibility for extending core/* block types.
Context: https://wordpress.org/gutenberg/handbook/extensibility/
The current set of available hooks is very limited and is not enough for providing meaningful customizations.
Specifically:
- I see no way to add more raw attributes (the ones that are NOT computed via HTML selectors).
getSaveContent.extraProps provides no help here. It only gives us a way to add inline HTML attributes to the final block output.
- We need
BlockSave which would be similar to BlockEdit hook. Ideally, BlockSave should be called in getSaveContent which would properly allow us to proxy the final result through a component (via HOCs/render props) that will modify the resulting HTML.
|
export function getSaveContent( blockType, attributes ) { |
|
const { save } = blockType; |
|
let saveContent; |
|
|
|
if ( save.prototype instanceof Component ) { |
|
saveContent = createElement( save, { attributes } ); |
|
} else { |
|
saveContent = save( { attributes } ); |
|
|
|
// Special-case function render implementation to allow raw HTML return |
|
if ( 'string' === typeof saveContent ) { |
|
return saveContent; |
|
} |
|
} |
|
|
|
const addExtraContainerProps = ( element ) => { |
|
if ( ! element || ! isObject( element ) ) { |
|
return element; |
|
} |
|
|
|
// Applying the filters adding extra props |
|
const props = applyFilters( 'getSaveContent.extraProps', { ...element.props }, blockType, attributes ); |
|
|
|
return cloneElement( element, props ); |
|
}; |
|
const contentWithExtraProps = Children.map( saveContent, addExtraContainerProps ); |
|
|
|
// Otherwise, infer as element |
|
return renderToString( contentWithExtraProps ); |
|
} |
Probably more once I get more deeply into that...
Related Issues and/or PRs
#2474 #3318
I understand that giving such powerful things into the users hands is quite dangerous. The posts may break in case the third-party plugin gets disabled in many unexpected ways (a missing BlockSave, in that particular example, would potentially blow up the attributes parsing with selector | src sources).
But, I really believe that there is a lot more to be done in that area that waits to be explored.
Issue Overview
This issue explores ways to give more flexibility for extending
core/*block types.Context: https://wordpress.org/gutenberg/handbook/extensibility/
The current set of available hooks is very limited and is not enough for providing meaningful customizations.
Specifically:
getSaveContent.extraPropsprovides no help here. It only gives us a way to add inline HTML attributes to the final block output.BlockSavewhich would be similar toBlockEdithook. Ideally,BlockSaveshould be called ingetSaveContentwhich would properly allow us to proxy the final result through a component (via HOCs/render props) that will modify the resulting HTML.gutenberg/blocks/api/serializer.js
Lines 37 to 66 in ff9c02a
Probably more once I get more deeply into that...
Related Issues and/or PRs
#2474 #3318
I understand that giving such powerful things into the users hands is quite dangerous. The posts may break in case the third-party plugin gets disabled in many unexpected ways (a missing
BlockSave, in that particular example, would potentially blow up theattributesparsing withselector | srcsources).But, I really believe that there is a lot more to be done in that area that waits to be explored.