Skip to content

Extensibility: Introduce BlockSave which would work similar to BlockEdit #3800

@andreiglingeanu

Description

@andreiglingeanu

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:

  1. 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.
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] BlocksOverall functionality of blocks[Feature] ExtensibilityThe ability to extend blocks or the editing experience

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions