Skip to content

Commit

Permalink
Block editor: hooks: manage save props in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 14, 2023
1 parent d9ea8d0 commit 221f43f
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 61 deletions.
6 changes: 1 addition & 5 deletions packages/block-editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export default {
shareWithChildBlocks: true,
edit: BlockEditAlignmentToolbarControlsPure,
useBlockProps,
addSaveProps: addAssignedAlign,
attributeKeys: [ 'align' ],
hasSupport( name ) {
return hasBlockSupport( name, 'align', false );
Expand Down Expand Up @@ -209,8 +210,3 @@ addFilter(
'core/editor/align/addAttribute',
addAttribute
);
addFilter(
'blocks.getSaveContent.extraProps',
'core/editor/align/addAssignedAlign',
addAssignedAlign
);
6 changes: 1 addition & 5 deletions packages/block-editor/src/hooks/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ function BlockEditAnchorControlPure( {
}

export default {
addSaveProps,
edit: BlockEditAnchorControlPure,
attributeKeys: [ 'anchor' ],
hasSupport( name ) {
Expand Down Expand Up @@ -147,8 +148,3 @@ export function addSaveProps( extraProps, blockType, attributes ) {
}

addFilter( 'blocks.registerBlockType', 'core/anchor/attribute', addAttribute );
addFilter(
'blocks.getSaveContent.extraProps',
'core/editor/anchor/save-props',
addSaveProps
);
10 changes: 5 additions & 5 deletions packages/block-editor/src/hooks/aria-label.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export function addSaveProps( extraProps, blockType, attributes ) {
return extraProps;
}

export default {
addSaveProps,
attributeKeys: [ 'ariaLabel' ],
};

addFilter(
'blocks.registerBlockType',
'core/ariaLabel/attribute',
addAttribute
);
addFilter(
'blocks.getSaveContent.extraProps',
'core/ariaLabel/save-props',
addSaveProps
);
7 changes: 1 addition & 6 deletions packages/block-editor/src/hooks/border.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ function useBlockProps( { name, borderColor, style } ) {

export default {
useBlockProps,
addSaveProps,
attributeKeys: [ 'borderColor', 'style' ],
hasSupport( name ) {
return hasBorderSupport( name, 'color' );
Expand All @@ -359,9 +360,3 @@ addFilter(
'core/border/addAttributes',
addAttributes
);

addFilter(
'blocks.getSaveContent.extraProps',
'core/border/addSaveProps',
addSaveProps
);
7 changes: 1 addition & 6 deletions packages/block-editor/src/hooks/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ function useBlockProps( {

export default {
useBlockProps,
addSaveProps,
attributeKeys: [ 'backgroundColor', 'textColor', 'gradient', 'style' ],
hasSupport: hasColorSupport,
};
Expand Down Expand Up @@ -437,12 +438,6 @@ addFilter(
addAttributes
);

addFilter(
'blocks.getSaveContent.extraProps',
'core/color/addSaveProps',
addSaveProps
);

addFilter(
'blocks.switchToBlockType.transformedBlock',
'core/color/addTransforms',
Expand Down
6 changes: 1 addition & 5 deletions packages/block-editor/src/hooks/custom-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function CustomClassNameControlsPure( { className, setAttributes } ) {

export default {
edit: CustomClassNameControlsPure,
addSaveProps,
attributeKeys: [ 'className' ],
hasSupport( name ) {
return hasBlockSupport( name, 'customClassName', true );
Expand Down Expand Up @@ -140,11 +141,6 @@ addFilter(
'core/editor/custom-class-name/attribute',
addAttribute
);
addFilter(
'blocks.getSaveContent.extraProps',
'core/editor/custom-class-name/save-props',
addSaveProps
);

addFilter(
'blocks.switchToBlockType.transformedBlock',
Expand Down
7 changes: 1 addition & 6 deletions packages/block-editor/src/hooks/font-family.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function useBlockProps( { name, fontFamily } ) {

export default {
useBlockProps,
addSaveProps,
attributeKeys: [ 'fontFamily' ],
hasSupport( name ) {
return hasBlockSupport( name, FONT_FAMILY_SUPPORT_KEY );
Expand All @@ -105,9 +106,3 @@ addFilter(
'core/fontFamily/addAttribute',
addAttributes
);

addFilter(
'blocks.getSaveContent.extraProps',
'core/fontFamily/addSaveProps',
addSaveProps
);
7 changes: 1 addition & 6 deletions packages/block-editor/src/hooks/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ function useBlockProps( { name, fontSize, style } ) {

export default {
useBlockProps,
addSaveProps,
attributeKeys: [ 'fontSize', 'style' ],
hasSupport( name ) {
return hasBlockSupport( name, FONT_SIZE_SUPPORT_KEY );
Expand Down Expand Up @@ -245,12 +246,6 @@ addFilter(
addAttributes
);

addFilter(
'blocks.getSaveContent.extraProps',
'core/font/addSaveProps',
addSaveProps
);

addFilter(
'blocks.switchToBlockType.transformedBlock',
'core/font-size/addTransforms',
Expand Down
13 changes: 7 additions & 6 deletions packages/block-editor/src/hooks/generated-class-name.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport, getBlockDefaultClassName } from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -38,8 +37,10 @@ export function addGeneratedClassName( extraProps, blockType ) {
return extraProps;
}

addFilter(
'blocks.getSaveContent.extraProps',
'core/generated-class-name/save-props',
addGeneratedClassName
);
export default {
addSaveProps: addGeneratedClassName,
attributeKeys: [ 'className' ],
hasSupport( name ) {
return hasBlockSupport( name, 'className', true );
},
};
22 changes: 19 additions & 3 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/**
* Internal dependencies
*/
import { createBlockEditFilter, createBlockListBlockFilter } from './utils';
import {
createBlockEditFilter,
createBlockListBlockFilter,
createBlockSaveFilter,
} from './utils';
import './compat';
import align from './align';
import './lock';
import anchor from './anchor';
import './aria-label';
import ariaLabel from './aria-label';
import customClassName from './custom-class-name';
import './generated-class-name';
import generatedClassName from './generated-class-name';
import style from './style';
import './settings';
import color from './color';
Expand Down Expand Up @@ -50,6 +54,18 @@ createBlockListBlockFilter( [
position,
childLayout,
] );
createBlockSaveFilter( [
align,
anchor,
ariaLabel,
border,
color,
customClassName,
fontFamily,
fontSize,
generatedClassName,
style,
] );

export { useCustomSides } from './dimensions';
export { useLayoutClasses, useLayoutStyles } from './layout';
Expand Down
7 changes: 1 addition & 6 deletions packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ function BlockStyleControls( {
export default {
edit: BlockStyleControls,
hasSupport: hasStyleSupport,
addSaveProps,
attributeKeys: [ 'style' ],
useBlockProps,
};
Expand Down Expand Up @@ -455,9 +456,3 @@ addFilter(
'core/style/addAttribute',
addAttribute
);

addFilter(
'blocks.getSaveContent.extraProps',
'core/style/addSaveProps',
addSaveProps
);
35 changes: 33 additions & 2 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,10 @@ export function createBlockListBlockFilter( features ) {
}

if (
! hasSupport( props.name ) ||
// Skip rendering if none of the needed attributes are
// set.
! Object.keys( neededProps ).length
! Object.keys( neededProps ).length ||
! hasSupport( props.name )
) {
return null;
}
Expand Down Expand Up @@ -543,3 +543,34 @@ export function createBlockListBlockFilter( features ) {
withBlockListBlockHooks
);
}

export function createBlockSaveFilter( features ) {
function extraPropsFromHooks( props ) {
features.reduce( ( accu, feature ) => {
const { hasSupport, attributeKeys = [], addSaveProps } = feature;

const neededProps = {};
for ( const key of attributeKeys ) {
if ( props.attributes[ key ] ) {
neededProps[ key ] = props.attributes[ key ];
}
}

if (
// Skip rendering if none of the needed attributes are
// set.
! Object.keys( neededProps ).length ||
! hasSupport( props.name )
) {
return accu;
}

return addSaveProps( accu, props.name, neededProps );
}, props );
}
addFilter(
'blocks.getSaveContent.extraProps',
'core/editor/hooks',
extraPropsFromHooks
);
}

0 comments on commit 221f43f

Please sign in to comment.