Skip to content

Commit

Permalink
Add block class
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Mar 9, 2020
1 parent bf97c84 commit 6a6fc9f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 29 deletions.
11 changes: 8 additions & 3 deletions packages/block-editor/src/components/block-list/block-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { focus, isTextField, placeCaretAtHorizontalEdge } from '@wordpress/dom';
import { BACKSPACE, DELETE, ENTER } from '@wordpress/keycodes';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { BlockPropsFilterContext } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -236,9 +237,13 @@ const elements = [
const BlockSaveComponent = forwardRef(
( { children, tagName: TagName = 'div', ...props }, wrapper ) => {
return (
<TagName { ...props } ref={ wrapper }>
{ children }
</TagName>
<BlockPropsFilterContext>
{ ( filter ) => (
<TagName ref={ wrapper } { ...filter( props ) }>
{ children }
</TagName>
) }
</BlockPropsFilterContext>
);
}
);
Expand Down
13 changes: 9 additions & 4 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,
__experimentalBlock as Block,
} from '@wordpress/block-editor';

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

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

return <figure className={ classes }>{ figure }</figure>;
return (
<Block.Save.figure className={ classes }>{ figure }</Block.Save.figure>
);
}
4 changes: 4 additions & 0 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ In the random image block above, we've given the `alt` attribute of the image a

<!-- START TOKEN(Autogenerated API docs) -->

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

Undocumented declaration.

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

Given a block object, returns a copy of the block object, optionally merging
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,
BlockPropsFilterContext,
} from './serializer';
export { isValidBlockContent } from './validation';
export { getCategories, setCategories, updateCategory } from './categories';
Expand Down
69 changes: 47 additions & 22 deletions packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { isEmpty, reduce, isObject, castArray, startsWith } from 'lodash';
/**
* WordPress dependencies
*/
import { Component, cloneElement, renderToString } from '@wordpress/element';
import {
Component,
cloneElement,
renderToString,
createContext,
} from '@wordpress/element';
import { hasFilter, applyFilters } from '@wordpress/hooks';
import isShallowEqual from '@wordpress/is-shallow-equal';

Expand All @@ -17,6 +22,7 @@ import {
getBlockType,
getFreeformContentHandlerName,
getUnregisteredTypeHandlerName,
hasBlockSupport,
} from './registration';
import { normalizeBlockType } from './utils';
import BlockContentProvider from '../block-content-provider';
Expand Down Expand Up @@ -68,6 +74,8 @@ export function getBlockMenuDefaultClassName( blockName ) {
);
}

export const BlockPropsFilterContext = createContext();

/**
* 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 Down Expand Up @@ -95,27 +103,42 @@ export function getSaveElement(
}

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

if ( hasFilter( 'blocks.getSaveContent.extraProps' ) ) {
if ( hasBlockSupport( blockType, 'lightBlockWrapper', false ) ) {
blockPropsFilter = ( props ) =>
/**
* Filters the props applied to the block save result element.
*
* @param {Object} props Props applied to save element.
* @param {WPBlock} blockType Block type definition.
* @param {Object} attributes Block attributes.
*/
applyFilters(
'blocks.getSaveContent.extraProps',
{ ...props },
blockType,
attributes
);
} else if ( isObject( element ) ) {
/**
* Filters the props applied to the block save result element.
*
* @param {Object} props Props applied to save element.
* @param {WPBlock} blockType Block type definition.
* @param {Object} attributes Block attributes.
*/
const props = applyFilters(
'blocks.getSaveContent.extraProps',
{ ...element.props },
blockType,
attributes
);

if (
isObject( element ) &&
hasFilter( 'blocks.getSaveContent.extraProps' )
) {
/**
* Filters the props applied to the block save result element.
*
* @param {Object} props Props applied to save element.
* @param {WPBlock} blockType Block type definition.
* @param {Object} attributes Block attributes.
*/
const props = applyFilters(
'blocks.getSaveContent.extraProps',
{ ...element.props },
blockType,
attributes
);

if ( ! isShallowEqual( props, element.props ) ) {
element = cloneElement( element, props );
if ( ! isShallowEqual( props, element.props ) ) {
element = cloneElement( element, props );
}
}
}

Expand All @@ -135,7 +158,9 @@ export function getSaveElement(

return (
<BlockContentProvider innerBlocks={ innerBlocks }>
{ element }
<BlockPropsFilterContext.Provider value={ blockPropsFilter }>
{ element }
</BlockPropsFilterContext.Provider>
</BlockContentProvider>
);
}
Expand Down

0 comments on commit 6a6fc9f

Please sign in to comment.