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

Block editor: hooks: manage save props in one place #57043

Merged
merged 10 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
13 changes: 8 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,16 @@ export function addSaveProps( extraProps, blockType, attributes ) {
return extraProps;
}

export default {
addSaveProps,
attributeKeys: [ 'ariaLabel' ],
hasSupport( name ) {
return hasBlockSupport( name, '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
19 changes: 17 additions & 2 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/**
* 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 style from './style';
Expand Down Expand Up @@ -50,6 +54,17 @@ createBlockListBlockFilter( [
position,
childLayout,
] );
createBlockSaveFilter( [
align,
anchor,
ariaLabel,
border,
color,
customClassName,
fontFamily,
fontSize,
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
);
13 changes: 4 additions & 9 deletions packages/block-editor/src/hooks/test/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { applyFilters } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import '../anchor';
import anchor from '../anchor';

const noop = () => {};

Expand Down Expand Up @@ -62,14 +62,9 @@ describe( 'anchor', () => {
} );

describe( 'addSaveProps', () => {
const getSaveContentExtraProps = applyFilters.bind(
null,
'blocks.getSaveContent.extraProps'
);

it( 'should do nothing if the block settings do not define anchor support', () => {
const attributes = { anchor: 'foo' };
const extraProps = getSaveContentExtraProps(
const extraProps = anchor.addSaveProps(
{},
blockSettings,
attributes
Expand All @@ -80,7 +75,7 @@ describe( 'anchor', () => {

it( 'should inject anchor attribute ID', () => {
const attributes = { anchor: 'foo' };
const extraProps = getSaveContentExtraProps(
const extraProps = anchor.addSaveProps(
{},
{
...blockSettings,
Expand All @@ -96,7 +91,7 @@ describe( 'anchor', () => {

it( 'should remove an anchor attribute ID when field is cleared', () => {
const attributes = { anchor: '' };
const extraProps = getSaveContentExtraProps(
const extraProps = anchor.addSaveProps(
{},
{
...blockSettings,
Expand Down
11 changes: 3 additions & 8 deletions packages/block-editor/src/hooks/test/custom-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { applyFilters } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import '../custom-class-name';
import customClassName from '../custom-class-name';

describe( 'custom className', () => {
const blockSettings = {
Expand Down Expand Up @@ -40,14 +40,9 @@ describe( 'custom className', () => {
} );

describe( 'addSaveProps', () => {
const addSaveProps = applyFilters.bind(
null,
'blocks.getSaveContent.extraProps'
);

it( 'should do nothing if the block settings do not define custom className support', () => {
const attributes = { className: 'foo' };
const extraProps = addSaveProps(
const extraProps = customClassName.addSaveProps(
{},
{
...blockSettings,
Expand All @@ -63,7 +58,7 @@ describe( 'custom className', () => {

it( 'should inject the custom className', () => {
const attributes = { className: 'bar' };
const extraProps = addSaveProps(
const extraProps = customClassName.addSaveProps(
{ className: 'foo' },
blockSettings,
attributes
Expand Down
18 changes: 4 additions & 14 deletions packages/block-editor/src/hooks/test/style.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/**
* WordPress dependencies
*/
import { applyFilters } from '@wordpress/hooks';

/**
* Internal dependencies
*/
import { getInlineStyles, omitStyle } from '../style';
import _style, { getInlineStyles, omitStyle } from '../style';

describe( 'getInlineStyles', () => {
it( 'should return an empty object when called with undefined', () => {
Expand Down Expand Up @@ -120,11 +115,6 @@ describe( 'getInlineStyles', () => {
} );

describe( 'addSaveProps', () => {
const addSaveProps = applyFilters.bind(
null,
'blocks.getSaveContent.extraProps'
);

const blockSettings = {
save: () => <div className="default" />,
category: 'text',
Expand Down Expand Up @@ -166,7 +156,7 @@ describe( 'addSaveProps', () => {
};

it( 'should serialize all styles by default', () => {
const extraProps = addSaveProps( {}, blockSettings, attributes );
const extraProps = _style.addSaveProps( {}, blockSettings, attributes );

expect( extraProps.style ).toEqual( {
background:
Expand All @@ -183,7 +173,7 @@ describe( 'addSaveProps', () => {
const settings = applySkipSerialization( {
typography: true,
} );
const extraProps = addSaveProps( {}, settings, attributes );
const extraProps = _style.addSaveProps( {}, settings, attributes );

expect( extraProps.style ).toEqual( {
background:
Expand All @@ -198,7 +188,7 @@ describe( 'addSaveProps', () => {
color: [ 'gradient' ],
typography: [ 'textDecoration', 'textTransform' ],
} );
const extraProps = addSaveProps( {}, settings, attributes );
const extraProps = _style.addSaveProps( {}, settings, attributes );

expect( extraProps.style ).toEqual( {
color: '#d92828',
Expand Down
48 changes: 46 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,47 @@ export function createBlockListBlockFilter( features ) {
withBlockListBlockHooks
);
}

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

const neededAttributes = {};
for ( const key of attributeKeys ) {
if ( attributes[ key ] ) {
neededAttributes[ key ] = attributes[ key ];
}
}

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

return addSaveProps( accu, name, neededAttributes );
},
{
...props,
// Previously we had a filter setting the className to undefined
// while it was an empty string. This filter is no longer
// running, so now we need to set it to undefined here.
className: props.className ? props.className : undefined,
}
);
}
addFilter(
'blocks.getSaveContent.extraProps',
'core/editor/hooks',
extraPropsFromHooks
);
}
Loading