From 5f45fad27060f890abd9fbbf52aa5d892e88b51b Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 12 Nov 2021 16:29:11 +1100 Subject: [PATCH 01/12] Add useBlockPreview, fix Query Loop wide alignment in the editor --- packages/block-editor/README.md | 17 ++++ .../src/components/block-preview/index.js | 61 ++++++++++++ .../src/components/block-preview/style.scss | 23 +++++ packages/block-editor/src/components/index.js | 2 +- .../block-library/src/post-template/edit.js | 36 ++++++-- .../compose/src/hooks/use-disabled/index.js | 92 +++++++++++++++++++ packages/compose/src/index.js | 1 + 7 files changed, 221 insertions(+), 11 deletions(-) create mode 100644 packages/compose/src/hooks/use-disabled/index.js diff --git a/packages/block-editor/README.md b/packages/block-editor/README.md index 94a227890243b..06acf28404dd2 100644 --- a/packages/block-editor/README.md +++ b/packages/block-editor/README.md @@ -664,6 +664,23 @@ _Returns_ Undocumented declaration. +### useBlockPreview + +This hook is used to lightly mark an element as a block preview wrapper +element. Call this hook and pass the returned props to the element to mark as +a block preview wrapper, automatically rendering inner blocks as children. If +you define a ref for the element, it is important to pass the ref to this +hook, which the hook in turn will pass to the component through the props it +returns. Optionally, you can also pass any other props through this hook, and +they will be merged and returned. + +_Parameters_ + +- _options_ `Object`: Preview options. +- _options.blocks_ `WPBlock[]`: Block objects. +- _options.props_ `Object`: Optional. Props to pass to the element. Must contain the ref if one is defined. +- _options.\_\_experimentalLayout_ `Object`: Layout settings to be used in the preview. + ### useBlockProps This hook is used to lightly mark an element as a block element. The element diff --git a/packages/block-editor/src/components/block-preview/index.js b/packages/block-editor/src/components/block-preview/index.js index cea2e8ee9b62c..e8ea15edf9271 100644 --- a/packages/block-editor/src/components/block-preview/index.js +++ b/packages/block-editor/src/components/block-preview/index.js @@ -2,10 +2,15 @@ * External dependencies */ import { castArray } from 'lodash'; +import classnames from 'classnames'; /** * WordPress dependencies */ +import { + __experimentalUseDisabled as useDisabled, + useMergeRefs, +} from '@wordpress/compose'; import { useSelect } from '@wordpress/data'; import { memo, useMemo } from '@wordpress/element'; @@ -16,6 +21,7 @@ import BlockEditorProvider from '../provider'; import LiveBlockPreview from './live'; import AutoHeightBlockPreview from './auto'; import { store as blockEditorStore } from '../../store'; +import { BlockListItems } from '../block-list'; export function BlockPreview( { blocks, @@ -63,3 +69,58 @@ export function BlockPreview( { * @return {WPComponent} The component to be rendered. */ export default memo( BlockPreview ); + +/** + * This hook is used to lightly mark an element as a block preview wrapper + * element. Call this hook and pass the returned props to the element to mark as + * a block preview wrapper, automatically rendering inner blocks as children. If + * you define a ref for the element, it is important to pass the ref to this + * hook, which the hook in turn will pass to the component through the props it + * returns. Optionally, you can also pass any other props through this hook, and + * they will be merged and returned. + * + * @param {Object} options Preview options. + * @param {WPBlock[]} options.blocks Block objects. + * @param {Object} options.props Optional. Props to pass to the element. Must contain + * the ref if one is defined. + * @param {Object} options.__experimentalLayout Layout settings to be used in the preview. + * + */ +export function useBlockPreview( { + blocks, + props = {}, + __experimentalLayout, +} ) { + const originalSettings = useSelect( + ( select ) => select( blockEditorStore ).getSettings(), + [] + ); + const disabledRef = useDisabled(); + const ref = useMergeRefs( [ props.ref, disabledRef ] ); + const settings = useMemo( () => { + const _settings = { ...originalSettings }; + _settings.__experimentalBlockPatterns = []; + return _settings; + }, [ originalSettings ] ); + const renderedBlocks = useMemo( () => castArray( blocks ), [ blocks ] ); + + const children = ( + + + + ); + + return { + ...props, + ref, + className: classnames( + props.className, + 'block-editor-block-preview__live-content', + 'components-disabled' + ), + children: blocks?.length ? children : null, + }; +} diff --git a/packages/block-editor/src/components/block-preview/style.scss b/packages/block-editor/src/components/block-preview/style.scss index bbcccc58a7d8a..7c522fec5c38c 100644 --- a/packages/block-editor/src/components/block-preview/style.scss +++ b/packages/block-editor/src/components/block-preview/style.scss @@ -41,3 +41,26 @@ .block-editor-block-preview__content-iframe .block-list-appender { display: none; } + +.block-editor-block-preview__live-content { + * { + pointer-events: none; + } + + // Hide the block appender, as the block is not editable in this context. + .block-list-appender { + display: none; + } + + // Revert button disable styles to ensure that button styles render as they will on the + // front end of the site. For example, this ensures that Social Links icons display correctly. + .components-button:disabled { + opacity: initial; + } + + // Hide placeholders. + .components-placeholder, + .block-editor-block-list__block[data-empty="true"] { + display: none; + } +} diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js index b1f991ce5c4d5..f7128d2de0698 100644 --- a/packages/block-editor/src/components/index.js +++ b/packages/block-editor/src/components/index.js @@ -104,7 +104,7 @@ export { default as BlockList } from './block-list'; export { useBlockProps } from './block-list/use-block-props'; export { LayoutStyle as __experimentalLayoutStyle } from './block-list/layout'; export { default as BlockMover } from './block-mover'; -export { default as BlockPreview } from './block-preview'; +export { default as BlockPreview, useBlockPreview } from './block-preview'; export { default as BlockSelectionClearer, useBlockSelectionClearer as __unstableUseBlockSelectionClearer, diff --git a/packages/block-library/src/post-template/edit.js b/packages/block-library/src/post-template/edit.js index 75c7046eba142..7a36083b5d77b 100644 --- a/packages/block-library/src/post-template/edit.js +++ b/packages/block-library/src/post-template/edit.js @@ -11,7 +11,7 @@ import { useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { BlockContextProvider, - BlockPreview, + useBlockPreview, useBlockProps, useInnerBlocksProps, store as blockEditorStore, @@ -30,6 +30,23 @@ function PostTemplateInnerBlocks() { return
  • ; } +function PostTemplateBlockPreview( { blocks, onClick } ) { + const blockPreviewProps = useBlockPreview( { + blocks, + } ); + + return ( +
  • + ); +} + export default function PostTemplateEdit( { clientId, context: { @@ -144,6 +161,8 @@ export default function PostTemplateEdit( { return

    { __( 'No results found.' ) }

    ; } + // Insert what we need right here. + return (