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 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ENTER, BACKSPACE, DELETE } from '@wordpress/keycodes';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import { getBlockProps } from '@wordpress/blocks';
ellatrix marked this conversation as resolved.
Show resolved Hide resolved

/**
* Internal dependencies
Expand Down Expand Up @@ -286,6 +287,13 @@ export function useBlockWrapperProps( props = {}, { __unstableIsHtml } = {} ) {
};
}

/**
* Call within a save function to get the props for the block wrapper.
*
* @param {Object} props Optional. Props to pass to the element.
*/
useBlockWrapperProps.save = getBlockProps;

const BlockComponent = forwardRef(
( { children, tagName: TagName = 'div', ...props }, ref ) => {
deprecated( 'wp.blockEditor.__experimentalBlock', {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { getBlockProps } from '@wordpress/blocks';

/**
* Internal dependencies
*/
Expand All @@ -7,6 +12,8 @@ export function useBlockWrapperProps( props = {} ) {
return props;
}

useBlockWrapperProps.save = getBlockProps;

const ExtendedBlockComponent = ELEMENTS.reduce( ( acc, element ) => {
acc[ element ] = element;
return acc;
Expand Down
7 changes: 5 additions & 2 deletions packages/block-library/src/audio/save.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import {
RichText,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';

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

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

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

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

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

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

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

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

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

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

return (
<div className={ className ? className : undefined }>
<div { ...useBlockWrapperProps.save( { 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 @@ -10,6 +10,7 @@ import {
InnerBlocks,
getColorClassName,
__experimentalGetGradientClass,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';

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

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

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

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

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 { ...useBlockWrapperProps.save( { className } ) }>
<RichText.Content value={ content } />
</TagName>
);
}
13 changes: 10 additions & 3 deletions packages/block-library/src/image/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { isEmpty } from 'lodash';
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import {
RichText,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';

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

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

return <figure className={ classes }>{ figure }</figure>;
return (
<figure { ...useBlockWrapperProps.save( { className: classes } ) }>
{ figure }
</figure>
);
}
18 changes: 8 additions & 10 deletions packages/block-library/src/list/save.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import {
RichText,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';

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 { ...useBlockWrapperProps.save( { type, reversed, start } ) }>
<RichText.Content value={ values } multiline="li" />
</TagName>
);
}
7 changes: 5 additions & 2 deletions packages/block-library/src/media-text/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { noop, isEmpty } from 'lodash';
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import {
InnerBlocks,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';

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

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 { ...useBlockWrapperProps.save( { className, dir: direction } ) }>
<RichText.Content value={ content } />
</p>
);
}
11 changes: 9 additions & 2 deletions packages/block-library/src/preformatted/save.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import {
RichText,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';

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

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

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

return (
<blockquote className={ className }>
<blockquote { ...useBlockWrapperProps.save( { className } ) }>
<RichText.Content multiline value={ value } />
{ ! RichText.isEmpty( citation ) && (
<RichText.Content tagName="cite" value={ citation } />
Expand Down
Loading