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 library: Patterns API integration with Media & Text block #18343

Closed
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -42,7 +42,7 @@ function BlockPatternPicker( {
isLarge
icon={ pattern.icon }
size={ 48 }
onClick={ () => onSelect( pattern.innerBlocks ) }
onClick={ () => onSelect( pattern ) }
className="block-editor-block-pattern-picker__pattern"
label={ pattern.label }
/>
Expand All @@ -54,7 +54,7 @@ function BlockPatternPicker( {
<div className="block-editor-block-pattern-picker__skip">
<Button
isLink
onClick={ () => onSelect( undefined ) }
onClick={ () => onSelect() }
>
{ __( 'Skip' ) }
</Button>
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export function ColumnsEdit( {
<div className={ classes }>
<InnerBlocks
__experimentalPatterns={ patterns }
__experimentalOnSelectPattern={ ( nextTemplate = defaultPattern.innerBlocks ) => {
setTemplate( nextTemplate );
__experimentalOnSelectPattern={ ( nextPattern = defaultPattern ) => {
setTemplate( nextPattern.innerBlocks );
setForceUseTemplate( true );
} }
__experimentalAllowPatternSkip
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/media-text/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
/**
* Internal dependencies
*/
import { imageFillStyles } from './media-container';
import { imageFillStyles } from './shared';

const DEFAULT_MEDIA_WIDTH = 50;

Expand Down
72 changes: 72 additions & 0 deletions packages/block-library/src/media-text/edit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* External dependencies
*/
import { get, map } from 'lodash';

/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
import { __experimentalBlockPatternPicker } from '@wordpress/block-editor';
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import MediaTextContainer from './media-text-container';

const createBlocksFromInnerBlocksTemplate = ( innerBlocksTemplate ) => {
return map(
innerBlocksTemplate,
( [ name, attributes, innerBlocks = [] ] ) =>
createBlock( name, attributes, createBlocksFromInnerBlocksTemplate( innerBlocks ) )
);
};

const MediaTextEdit = ( props ) => {
const { clientId, name } = props;
const { blockType, defaultPattern, hasInnerBlocks, patterns } = useSelect( ( select ) => {
const {
__experimentalGetBlockPatterns,
getBlockType,
__experimentalGetDefaultBlockPattern,
} = select( 'core/blocks' );

return {
blockType: getBlockType( name ),
defaultPattern: __experimentalGetDefaultBlockPattern( name ),
hasInnerBlocks: select( 'core/block-editor' ).getBlocks( clientId ).length > 0,
patterns: __experimentalGetBlockPatterns( name ),
};
}, [ clientId, name ] );

const { replaceInnerBlocks } = useDispatch( 'core/block-editor' );

if ( hasInnerBlocks ) {
return (
<MediaTextContainer { ...props } />
);
}

return (
<__experimentalBlockPatternPicker
icon={ get( blockType, [ 'icon', 'src' ] ) }
label={ get( blockType, [ 'title' ] ) }
patterns={ patterns }
onSelect={ ( nextPattern = defaultPattern ) => {
if ( nextPattern.attributes ) {
props.setAttributes( nextPattern.attributes );
}
if ( nextPattern.innerBlocks ) {
replaceInnerBlocks(
props.clientId,
createBlocksFromInnerBlocksTemplate( nextPattern.innerBlocks )
);
}
} }
allowSkip
/>
);
};

export default MediaTextEdit;
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { withViewportMatch } from '@wordpress/viewport';
* Internal dependencies
*/
import MediaContainer from './media-container';
import styles from './style.scss';
import styles from '../style.scss';

/**
* Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ import { withDispatch } from '@wordpress/data';
* Internal dependencies
*/
import icon from './media-container-icon';
import { imageFillStyles } from '../shared';

/**
* Constants
*/
const ALLOWED_MEDIA_TYPES = [ 'image', 'video' ];

export function imageFillStyles( url, focalPoint ) {
return url ?
{
backgroundImage: `url(${ url })`,
backgroundPosition: focalPoint ? `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%` : `50% 50%`,
} :
{};
}

class MediaContainer extends Component {
constructor() {
super( ...arguments );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { compose, withPreferredColorScheme } from '@wordpress/compose';
/**
* Internal dependencies
*/
import styles from './style.scss';
import styles from '../style.scss';
import icon from './media-container-icon';
import SvgIconRetry from './icon-retry';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';
import {
BlockControls,
BlockVerticalAlignmentToolbar,
Expand All @@ -25,22 +25,17 @@ import {
ExternalLink,
FocalPointPicker,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import MediaContainer from './media-container';

/**
* Constants
*/
const TEMPLATE = [
[ 'core/paragraph', { fontSize: 'large', placeholder: _x( 'Content…', 'content placeholder' ) } ],
];
// this limits the resize to a safe zone to avoid making broken layouts
const WIDTH_CONSTRAINT_PERCENTAGE = 15;
const applyWidthConstraints = ( width ) => Math.max( WIDTH_CONSTRAINT_PERCENTAGE, Math.min( width, 100 - WIDTH_CONSTRAINT_PERCENTAGE ) );

class MediaTextEdit extends Component {
class MediaTextContainer extends Component {
constructor() {
super( ...arguments );

Expand Down Expand Up @@ -235,7 +230,6 @@ class MediaTextEdit extends Component {
<div className={ classNames } style={ style } >
{ this.renderMediaArea() }
<InnerBlocks
template={ TEMPLATE }
templateInsertUpdatesSelection={ false }
/>
</div>
Expand All @@ -244,4 +238,4 @@ class MediaTextEdit extends Component {
}
}

export default withColors( 'backgroundColor' )( MediaTextEdit );
export default withColors( 'backgroundColor' )( MediaTextContainer );
2 changes: 2 additions & 0 deletions packages/block-library/src/media-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import deprecated from './deprecated';
import edit from './edit';
import icon from './icon';
import metadata from './block.json';
import patterns from './patterns';
import save from './save';
import transforms from './transforms';

Expand All @@ -26,6 +27,7 @@ export const settings = {
align: [ 'wide', 'full' ],
html: false,
},
patterns,
example: {
attributes: {
mediaType: 'image',
Expand Down
59 changes: 59 additions & 0 deletions packages/block-library/src/media-text/patterns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';

const innerBlocks = [
[ 'core/paragraph', { fontSize: 'large', placeholder: _x( 'Content…', 'content placeholder' ) } ],
];

/**
* Template option choices for predefined columns layouts.
*
* @type {WPBlockPattern[]}
*/
const patterns = [
{
name: 'media-left-equal',
label: __( 'Media on left; equal split' ),
icon: 'align-pull-left',
isDefault: true,
attributes: {
mediaPosition: 'left',
mediaWidth: 50,
},
innerBlocks,
},
{
name: 'media-left-one-third-two-thirds',
label: __( 'Media on left; one-third, two-thirds split' ),
icon: 'align-pull-left',
attributes: {
mediaPosition: 'left',
mediaWidth: 33.33,
},
innerBlocks,
},
{
name: 'media-right-two-thirds-one-thirds',
label: __( 'Media on right; two-thirds, one-third split' ),
icon: 'align-pull-right',
attributes: {
mediaPosition: 'right',
mediaWidth: 33.33,
},
innerBlocks,
},
{
name: 'media-right-equal',
label: __( 'Media on right; equal split' ),
icon: 'align-pull-right',
attributes: {
mediaPosition: 'right',
mediaWidth: 50,
},
innerBlocks,
},
];

export default patterns;
2 changes: 1 addition & 1 deletion packages/block-library/src/media-text/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
/**
* Internal dependencies
*/
import { imageFillStyles } from './media-container';
import { imageFillStyles } from './shared';

const DEFAULT_MEDIA_WIDTH = 50;

Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/media-text/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function imageFillStyles( url, focalPoint ) {
return url ?
{
backgroundImage: `url(${ url })`,
backgroundPosition: focalPoint ? `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%` : `50% 50%`,
} :
{};
}
7 changes: 6 additions & 1 deletion packages/components/src/placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import classnames from 'classnames';
import { isString } from 'lodash';

/**
* WordPress dependencies
*/
import { cloneElement } from '@wordpress/element';

/**
* Internal dependencies
*/
Expand All @@ -27,7 +32,7 @@ function Placeholder( { icon, children, label, instructions, className, notices,
</div>
}
<div className="components-placeholder__label">
{ isString( icon ) ? <Dashicon icon={ icon } /> : icon }
{ isString( icon ) ? <Dashicon icon={ icon } /> : cloneElement( icon, { height: 24, width: 24 } ) }
{ label }
</div>
{ !! instructions && <div className="components-placeholder__instructions">{ instructions }</div> }
Expand Down