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 API: light block edit/save symmetry #25644

Merged
merged 4 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion packages/block-library/src/audio/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';
Copy link
Contributor

Choose a reason for hiding this comment

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

I actually wonder about the best place to export this, maybe useBlockProps.save()?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's possible too. I don't mind either way. I'll update it :)

Copy link
Member Author

Choose a reason for hiding this comment

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

This looks much nicer actually! :)

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how I feel about exposing it through useBlockProps.save(). It feels a little weird to me, but I also don't outright dislike it. I'm okay with it either way. I suppose one benefit is that it more closely ties it to useBlockProps.

Copy link
Contributor

Choose a reason for hiding this comment

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

The reasoning for proposing this is that later we might have useInnerBlocks.save() , useRichText.save()

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I like this pattern. Good idea!


export default function save( { attributes } ) {
const { autoplay, caption, loop, preload, src } = attributes;

return (
src && (
<figure>
<figure { ...getBlockProps() }>
<audio
controls="controls"
src={ src }
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -33,7 +34,7 @@ export default function save( { attributes } ) {
// A title will no longer be assigned for new or updated button block links.

return (
<div>
<div { ...getBlockProps() }>
<RichText.Content
tagName="a"
className={ buttonClasses }
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/buttons/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save() {
return (
<div>
<div { ...getBlockProps() }>
<InnerBlocks.Content />
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/code/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -10,7 +11,7 @@ import { escape } from './utils';

export default function save( { attributes } ) {
return (
<pre>
<pre { ...getBlockProps() }>
<RichText.Content
tagName="code"
value={ escape( attributes.content ) }
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/column/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const { verticalAlignment, width } = attributes;
Expand All @@ -21,7 +22,7 @@ export default function save( { attributes } ) {
}

return (
<div className={ wrapperClasses } style={ style }>
<div { ...getBlockProps( { className: wrapperClasses, style } ) }>
<InnerBlocks.Content />
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/columns/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const { verticalAlignment } = attributes;
Expand All @@ -16,7 +17,7 @@ export default function save( { attributes } ) {
} );

return (
<div className={ className ? className : undefined }>
<div { ...getBlockProps( { className } ) }>
<InnerBlocks.Content />
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/cover/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getColorClassName,
__experimentalGetGradientClass,
} from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -95,7 +96,7 @@ export default function save( { attributes } ) {
);

return (
<div className={ classes } style={ style }>
<div { ...getBlockProps( { className: classes, style } ) }>
{ url && ( gradient || customGradient ) && dimRatio !== 0 && (
<span
aria-hidden="true"
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/group/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const { tagName: Tag } = attributes;

return (
<Tag>
<Tag { ...getBlockProps() }>
<div className="wp-block-group__inner-container">
<InnerBlocks.Content />
</div>
Expand Down
11 changes: 5 additions & 6 deletions packages/block-library/src/heading/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const { align, content, level } = attributes;
const tagName = 'h' + level;
const TagName = 'h' + level;

const className = classnames( {
[ `has-text-align-${ align }` ]: align,
} );

return (
<RichText.Content
className={ className ? className : undefined }
tagName={ tagName }
value={ content }
/>
<TagName { ...getBlockProps( { className } ) }>
<RichText.Content value={ content } />
</TagName>
);
}
9 changes: 7 additions & 2 deletions packages/block-library/src/image/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { isEmpty } from 'lodash';
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const {
Expand Down Expand Up @@ -67,11 +68,15 @@ export default function save( { attributes } ) {

if ( 'left' === align || 'right' === align || 'center' === align ) {
return (
<div>
<div { ...getBlockProps() }>
<figure className={ classes }>{ figure }</figure>
</div>
);
}

return <figure className={ classes }>{ figure }</figure>;
return (
<figure { ...getBlockProps( { className: classes } ) }>
{ figure }
</figure>
);
}
14 changes: 5 additions & 9 deletions packages/block-library/src/list/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const { ordered, values, type, reversed, start } = attributes;
const tagName = ordered ? 'ol' : 'ul';
const TagName = ordered ? 'ol' : 'ul';

return (
<RichText.Content
tagName={ tagName }
value={ values }
type={ type }
reversed={ reversed }
start={ start }
multiline="li"
/>
<TagName { ...getBlockProps( { type, reversed, start } ) }>
<RichText.Content value={ values } multiline="li" />
</TagName>
);
}
3 changes: 2 additions & 1 deletion packages/block-library/src/media-text/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { noop, isEmpty } from 'lodash';
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -88,7 +89,7 @@ export default function save( { attributes } ) {
gridTemplateColumns,
};
return (
<div className={ className } style={ style }>
<div { ...getBlockProps( { className, style } ) }>
<figure
className="wp-block-media-text__media"
style={ backgroundStyles }
Expand Down
11 changes: 4 additions & 7 deletions packages/block-library/src/paragraph/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const { align, content, dropCap, direction } = attributes;

const className = classnames( {
'has-drop-cap': dropCap,
[ `has-text-align-${ align }` ]: align,
} );

return (
<RichText.Content
tagName="p"
className={ className ? className : undefined }
value={ content }
dir={ direction }
/>
<p { ...getBlockProps( { className, dir: direction } ) }>
<RichText.Content value={ content } />
</p>
);
}
7 changes: 6 additions & 1 deletion packages/block-library/src/preformatted/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const { content } = attributes;

return <RichText.Content tagName="pre" value={ content } />;
return (
<pre { ...getBlockProps() }>
<RichText.Content value={ content } />
</pre>
);
}
3 changes: 2 additions & 1 deletion packages/block-library/src/quote/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const { align, value, citation } = attributes;
Expand All @@ -16,7 +17,7 @@ export default function save( { attributes } ) {
} );

return (
<blockquote className={ className }>
<blockquote { ...getBlockProps( { className } ) }>
<RichText.Content multiline value={ value } />
{ ! RichText.isEmpty( citation ) && (
<RichText.Content tagName="cite" value={ citation } />
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/social-links/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { className } ) {
export default function save() {
return (
<ul className={ className }>
<ul { ...getBlockProps() }>
<InnerBlocks.Content />
</ul>
);
Expand Down
9 changes: 4 additions & 5 deletions packages/block-library/src/verse/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const { textAlign, content } = attributes;
Expand All @@ -16,10 +17,8 @@ export default function save( { attributes } ) {
} );

return (
<RichText.Content
tagName="pre"
className={ className }
value={ content }
/>
<pre { ...getBlockProps( { className } ) }>
<RichText.Content value={ content } />
</pre>
);
}
3 changes: 2 additions & 1 deletion packages/block-library/src/video/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { getBlockProps } from '@wordpress/blocks';

export default function save( { attributes } ) {
const {
Expand All @@ -16,7 +17,7 @@ export default function save( { attributes } ) {
playsInline,
} = attributes;
return (
<figure>
<figure { ...getBlockProps() }>
{ src && (
<video
autoPlay={ autoplay }
Expand Down
4 changes: 4 additions & 0 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ _Returns_

- `string`: The block's default menu item class.

<a name="getBlockProps" href="#getBlockProps">#</a> **getBlockProps**

Undocumented declaration.

<a name="getBlockSupport" href="#getBlockSupport">#</a> **getBlockSupport**

Returns the block support value for a feature, if defined.
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {
getBlockMenuDefaultClassName,
getSaveElement,
getSaveContent,
getBlockProps,
} from './serializer';
export { isValidBlockContent } from './validation';
export { getCategories, setCategories, updateCategory } from './categories';
Expand Down
19 changes: 18 additions & 1 deletion packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getBlockType,
getFreeformContentHandlerName,
getUnregisteredTypeHandlerName,
hasBlockSupport,
} from './registration';
import { normalizeBlockType } from './utils';
import BlockContentProvider from '../block-content-provider';
Expand Down Expand Up @@ -68,6 +69,18 @@ export function getBlockMenuDefaultClassName( blockName ) {
);
}

const blockPropsProvider = {};

export function getBlockProps( props ) {
const { blockType, attributes } = blockPropsProvider;
return applyFilters(
'blocks.getSaveContent.extraProps',
{ ...props },
blockType,
attributes
);
}

/**
* Given a block type containing a save render implementation and attributes, returns the
* enhanced element to be saved or string when raw HTML expected.
Expand All @@ -94,11 +107,15 @@ export function getSaveElement(
save = instance.render.bind( instance );
}

blockPropsProvider.blockType = blockType;
blockPropsProvider.attributes = attributes;

let element = save( { attributes, innerBlocks } );

if (
isObject( element ) &&
hasFilter( 'blocks.getSaveContent.extraProps' )
hasFilter( 'blocks.getSaveContent.extraProps' ) &&
! hasBlockSupport( blockType, 'lightBlockWrapper', false )
) {
/**
* Filters the props applied to the block save result element.
Expand Down