Skip to content

Commit

Permalink
Block editor: hooks: manage save props in one place (#57043)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Dec 14, 2023
1 parent 0576c01 commit 8d13605
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 92 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
);
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
13 changes: 8 additions & 5 deletions packages/block-editor/src/hooks/custom-class-name.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ addFilter(
'core/custom-class-name/attribute',
addAttribute
);
addFilter(
'blocks.getSaveContent.extraProps',
'core/custom-class-name/save-props',
addSaveProps
);

export default {
addSaveProps,
attributeKeys: [ 'className' ],
hasSupport( name ) {
return hasBlockSupport( name, 'customClassName', true );
},
};
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,
customClassName,
border,
color,
style,
fontFamily,
fontSize,
] );

export { useCustomSides } from './dimensions';
export { useLayoutClasses, useLayoutStyles } from './layout';
Expand Down
16 changes: 14 additions & 2 deletions packages/block-editor/src/hooks/index.native.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/**
* Internal dependencies
*/
import { createBlockEditFilter, createBlockListBlockFilter } from './utils';
import {
createBlockEditFilter,
createBlockListBlockFilter,
createBlockSaveFilter,
} from './utils';
import './compat';
import align from './align';
import anchor from './anchor';
import './custom-class-name';
import customClassName from './custom-class-name';
import './generated-class-name';
import style from './style';
import color from './color';
Expand All @@ -14,6 +18,14 @@ import './layout';

createBlockEditFilter( [ align, anchor, style ] );
createBlockListBlockFilter( [ align, style, color, fontSize ] );
createBlockSaveFilter( [
align,
anchor,
customClassName,
color,
style,
fontSize,
] );

export { getBorderClassesAndStyles, useBorderProps } from './use-border-props';
export { getColorClassesAndStyles, useColorProps } from './use-color-props';
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
Loading

1 comment on commit 8d13605

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 8d13605.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7211810678
📝 Reported issues:

Please sign in to comment.