From c6e666234cb97f5593cc766d5a20355155058308 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 May 2023 15:08:21 +1200 Subject: [PATCH 01/15] Add experimental flag and syncStatus attrib to allow testing of partial syncing --- docs/reference-guides/core-blocks.md | 4 +- lib/experimental/editor-settings.php | 3 + lib/experiments-page.php | 12 ++ .../inserter/hooks/use-patterns-state.js | 25 +++- packages/block-library/src/pattern/block.json | 20 +++- packages/block-library/src/pattern/edit.js | 107 +++++++++++++++--- packages/block-library/src/pattern/index.js | 10 +- packages/block-library/src/pattern/index.php | 13 ++- packages/block-library/src/pattern/save.js | 18 +++ packages/block-library/src/pattern/v1/edit.js | 51 +++++++++ .../fixtures/blocks/core__pattern.json | 7 +- 11 files changed, 237 insertions(+), 33 deletions(-) create mode 100644 packages/block-library/src/pattern/save.js create mode 100644 packages/block-library/src/pattern/v1/edit.js diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index e5d20bb7b1edd5..5194ca4ddd6b23 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -464,8 +464,8 @@ Show a block pattern. ([Source](https://github.com/WordPress/gutenberg/tree/trun - **Name:** core/pattern - **Category:** theme -- **Supports:** ~~html~~, ~~inserter~~ -- **Attributes:** slug +- **Supports:** +- **Attributes:** forcedAlignment, layout, slug, syncStatus ## Post Author diff --git a/lib/experimental/editor-settings.php b/lib/experimental/editor-settings.php index c7dd5850a505c3..8d4046530fbc95 100644 --- a/lib/experimental/editor-settings.php +++ b/lib/experimental/editor-settings.php @@ -101,6 +101,9 @@ function gutenberg_enable_experiments() { if ( $gutenberg_experiments && array_key_exists( 'gutenberg-theme-previews', $gutenberg_experiments ) ) { wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableThemePreviews = true', 'before' ); } + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-pattern-enhancements', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnablePatternEnhancements = true', 'before' ); + } } diff --git a/lib/experiments-page.php b/lib/experiments-page.php index ee51f5bea49f96..e39a9dbefb9c16 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -125,6 +125,18 @@ function gutenberg_initialize_experiments_settings() { ) ); + add_settings_field( + 'gutenberg-pattern-enhancements', + __( 'Pattern enhancements', 'gutenberg' ), + 'gutenberg_display_experiment_field', + 'gutenberg-experiments', + 'gutenberg_experiments_section', + array( + 'label' => __( 'Test the Pattern block enhancements', 'gutenberg' ), + 'id' => 'gutenberg-pattern-enhancements', + ) + ); + register_setting( 'gutenberg-experiments', 'gutenberg-experiments' diff --git a/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js b/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js index ca287b95c43b9b..2819997e196c5e 100644 --- a/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js +++ b/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { useCallback } from '@wordpress/element'; -import { cloneBlock } from '@wordpress/blocks'; +import { createBlock, cloneBlock } from '@wordpress/blocks'; import { useDispatch, useSelect } from '@wordpress/data'; import { __, sprintf } from '@wordpress/i18n'; import { store as noticesStore } from '@wordpress/notices'; @@ -35,10 +35,25 @@ const usePatternsState = ( onInsert, rootClientId ) => { ); const { createSuccessNotice } = useDispatch( noticesStore ); const onClickPattern = useCallback( ( pattern, blocks ) => { - onInsert( - ( blocks ?? [] ).map( ( block ) => cloneBlock( block ) ), - pattern.name - ); + if ( window?.__experimentalEnablePatternEnhancements ) { + onInsert( + blocks + ? [ + createBlock( + 'core/pattern', + { slug: pattern.name }, + blocks.map( ( block ) => cloneBlock( block ) ) + ), + ] + : [], + pattern.name + ); + } else { + onInsert( + ( blocks ?? [] ).map( ( block ) => cloneBlock( block ) ), + pattern.name + ); + } createSuccessNotice( sprintf( /* translators: %s: block pattern title. */ diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index da023142403c87..80ac618b068855 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -6,13 +6,29 @@ "category": "theme", "description": "Show a block pattern.", "supports": { - "html": false, - "inserter": false + "__experimentalLayout": { + "allowEditing": false + } }, "textdomain": "default", "attributes": { "slug": { "type": "string" + }, + "forcedAlignment": { + "type": "string", + "default": "full" + }, + "layout": { + "type": "object", + "default": { + "type": "constrained" + } + }, + "syncStatus": { + "type": [ "string", "boolean" ], + "enum": [ "full", "partial", "none" ], + "default": "none" } } } diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index be0b778eb4ae19..7ee3b3d8844688 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -1,31 +1,41 @@ /** * WordPress dependencies */ +import { cloneBlock } from '@wordpress/blocks'; import { useSelect, useDispatch } from '@wordpress/data'; import { useEffect } from '@wordpress/element'; import { store as blockEditorStore, useBlockProps, + useInnerBlocksProps, } from '@wordpress/block-editor'; -const PatternEdit = ( { attributes, clientId } ) => { - const selectedPattern = useSelect( - ( select ) => - select( blockEditorStore ).__experimentalGetParsedPattern( - attributes.slug - ), - [ attributes.slug ] +const PatternEdit = ( { attributes, clientId, setAttributes } ) => { + const { inheritedAlignment, slug, syncStatus } = attributes; + const { selectedPattern, innerBlocks } = useSelect( + ( select ) => { + return { + selectedPattern: + select( blockEditorStore ).__experimentalGetParsedPattern( + slug + ), + innerBlocks: + select( blockEditorStore ).getBlock( clientId ) + ?.innerBlocks, + }; + }, + [ slug, clientId ] ); - - const { replaceBlocks, __unstableMarkNextChangeAsNotPersistent } = - useDispatch( blockEditorStore ); + const { + replaceBlocks, + replaceInnerBlocks, + __unstableMarkNextChangeAsNotPersistent, + } = useDispatch( blockEditorStore ); // Run this effect when the component loads. // This adds the Pattern's contents to the post. - // This change won't be saved. - // It will continue to pull from the pattern file unless changes are made to its respective template part. useEffect( () => { - if ( selectedPattern?.blocks ) { + if ( selectedPattern?.blocks && ! innerBlocks?.length ) { // We batch updates to block list settings to avoid triggering cascading renders // for each container block included in a tree and optimize initial render. // Since the above uses microtasks, we need to use a microtask here as well, @@ -33,14 +43,79 @@ const PatternEdit = ( { attributes, clientId } ) => { // inner blocks but doesn't have blockSettings in the state. window.queueMicrotask( () => { __unstableMarkNextChangeAsNotPersistent(); + if ( syncStatus === 'partial' ) { + replaceInnerBlocks( + clientId, + selectedPattern.blocks.map( ( block ) => + cloneBlock( block ) + ) + ); + return; + } replaceBlocks( clientId, selectedPattern.blocks ); } ); } - }, [ clientId, selectedPattern?.blocks ] ); + }, [ + clientId, + selectedPattern.blocks, + replaceInnerBlocks, + __unstableMarkNextChangeAsNotPersistent, + innerBlocks, + syncStatus, + replaceBlocks, + ] ); + + useEffect( () => { + if ( syncStatus !== 'partial' ) { + return; + } + const alignments = [ 'wide', 'full' ]; + const blocks = innerBlocks; + if ( ! blocks || blocks.length === 0 ) { + return; + } + // Determine the widest setting of all the contained blocks. + const widestAlignment = blocks.reduce( ( accumulator, block ) => { + const { align } = block.attributes; + return alignments.indexOf( align ) > + alignments.indexOf( accumulator ) + ? align + : accumulator; + }, undefined ); - const props = useBlockProps(); + // Set the attribute of the Pattern block to match the widest + // alignment. + setAttributes( { + inheritedAlignment: widestAlignment ?? '', + } ); + }, [ + innerBlocks, + selectedPattern.blocks, + setAttributes, + inheritedAlignment, + syncStatus, + ] ); - return
; + const blockProps = useBlockProps( { + className: + syncStatus === 'partial' && + inheritedAlignment && + `align${ inheritedAlignment }`, + } ); + + const innerBlocksProps = useInnerBlocksProps( blockProps, { + templateLock: syncStatus === 'partial' ? 'contentOnly' : false, + } ); + + if ( syncStatus !== 'partial' ) { + return
; + } + + return ( + <> +
+ + ); }; export default PatternEdit; diff --git a/packages/block-library/src/pattern/index.js b/packages/block-library/src/pattern/index.js index e4af712da8bb29..8cedbdd94f92b0 100644 --- a/packages/block-library/src/pattern/index.js +++ b/packages/block-library/src/pattern/index.js @@ -3,13 +3,15 @@ */ import initBlock from '../utils/init-block'; import metadata from './block.json'; -import PatternEdit from './edit'; +import PatternEditV1 from './v1/edit'; +import PatternEditV2 from './edit'; +import PatternSave from './save'; const { name } = metadata; export { metadata, name }; -export const settings = { - edit: PatternEdit, -}; +export const settings = window?.__experimentalEnablePatternEnhancements + ? { edit: PatternEditV2, save: PatternSave } + : { edit: PatternEditV1 }; export const init = () => initBlock( { name, metadata, settings } ); diff --git a/packages/block-library/src/pattern/index.php b/packages/block-library/src/pattern/index.php index 32a08601ca8089..4afed4cca5c919 100644 --- a/packages/block-library/src/pattern/index.php +++ b/packages/block-library/src/pattern/index.php @@ -22,15 +22,22 @@ function register_block_core_pattern() { /** * Renders the `core/pattern` block on the server. * - * @param array $attributes Block attributes. + * @param array $attributes Block attributes. + * @param string $content The block rendered content. * * @return string Returns the output of the pattern. */ -function render_block_core_pattern( $attributes ) { +function render_block_core_pattern( $attributes, $content ) { if ( empty( $attributes['slug'] ) ) { return ''; } + $wrapper = '
%s
'; + + if ( isset( $attributes['syncStatus'] ) && 'unsynced' === $attributes['syncStatus'] ) { + return sprintf( $wrapper, $content ); + } + $slug = $attributes['slug']; $registry = WP_Block_Patterns_Registry::get_instance(); if ( ! $registry->is_registered( $slug ) ) { @@ -38,7 +45,7 @@ function render_block_core_pattern( $attributes ) { } $pattern = $registry->get_registered( $slug ); - return do_blocks( $pattern['content'] ); + return sprintf( $wrapper, do_blocks( $pattern['content'] ) ); } add_action( 'init', 'register_block_core_pattern' ); diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js new file mode 100644 index 00000000000000..3f7c4db82d60a8 --- /dev/null +++ b/packages/block-library/src/pattern/save.js @@ -0,0 +1,18 @@ +/** + * WordPress dependencies + */ +import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; + +export default function save( { + attributes: { inheritedAlignment, syncStatus }, + innerBlocks, +} ) { + if ( innerBlocks?.length === 0 || syncStatus !== 'partial' ) { + return; + } + const blockProps = useBlockProps.save( { + className: inheritedAlignment && `align${ inheritedAlignment }`, + } ); + const innerBlocksProps = useInnerBlocksProps.save( blockProps ); + return
{ innerBlocksProps.children }
; +} diff --git a/packages/block-library/src/pattern/v1/edit.js b/packages/block-library/src/pattern/v1/edit.js new file mode 100644 index 00000000000000..aa475809ccb44f --- /dev/null +++ b/packages/block-library/src/pattern/v1/edit.js @@ -0,0 +1,51 @@ +/** + * WordPress dependencies + */ +import { useSelect, useDispatch } from '@wordpress/data'; +import { useEffect } from '@wordpress/element'; +import { + store as blockEditorStore, + useBlockProps, +} from '@wordpress/block-editor'; + +const PatternEdit = ( { attributes, clientId } ) => { + const selectedPattern = useSelect( + ( select ) => + select( blockEditorStore ).__experimentalGetParsedPattern( + attributes.slug + ), + [ attributes.slug ] + ); + + const { replaceBlocks, __unstableMarkNextChangeAsNotPersistent } = + useDispatch( blockEditorStore ); + + // Run this effect when the component loads. + // This adds the Pattern's contents to the post. + // This change won't be saved. + // It will continue to pull from the pattern file unless changes are made to its respective template part. + useEffect( () => { + if ( selectedPattern?.blocks ) { + // We batch updates to block list settings to avoid triggering cascading renders + // for each container block included in a tree and optimize initial render. + // Since the above uses microtasks, we need to use a microtask here as well, + // because nested pattern blocks cannot be inserted if the parent block supports + // inner blocks but doesn't have blockSettings in the state. + window.queueMicrotask( () => { + __unstableMarkNextChangeAsNotPersistent(); + replaceBlocks( clientId, selectedPattern.blocks ); + } ); + } + }, [ + clientId, + selectedPattern?.blocks, + __unstableMarkNextChangeAsNotPersistent, + replaceBlocks, + ] ); + + const props = useBlockProps(); + + return
; +}; + +export default PatternEdit; diff --git a/test/integration/fixtures/blocks/core__pattern.json b/test/integration/fixtures/blocks/core__pattern.json index 3bb2c0891b06ec..f1101dc950e46e 100644 --- a/test/integration/fixtures/blocks/core__pattern.json +++ b/test/integration/fixtures/blocks/core__pattern.json @@ -3,7 +3,12 @@ "name": "core/pattern", "isValid": true, "attributes": { - "slug": "core/text-two-columns" + "forcedAlignment": "full", + "layout": { + "type": "constrained" + }, + "slug": "core/text-two-columns", + "syncStatus": "synced" }, "innerBlocks": [] } From f7c141827832fb2ffe2e74e492d02950395acd38 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 May 2023 15:46:50 +1200 Subject: [PATCH 02/15] Remove pattern inserter change --- .../inserter/hooks/use-patterns-state.js | 25 ++++--------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js b/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js index 2819997e196c5e..ca287b95c43b9b 100644 --- a/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js +++ b/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { useCallback } from '@wordpress/element'; -import { createBlock, cloneBlock } from '@wordpress/blocks'; +import { cloneBlock } from '@wordpress/blocks'; import { useDispatch, useSelect } from '@wordpress/data'; import { __, sprintf } from '@wordpress/i18n'; import { store as noticesStore } from '@wordpress/notices'; @@ -35,25 +35,10 @@ const usePatternsState = ( onInsert, rootClientId ) => { ); const { createSuccessNotice } = useDispatch( noticesStore ); const onClickPattern = useCallback( ( pattern, blocks ) => { - if ( window?.__experimentalEnablePatternEnhancements ) { - onInsert( - blocks - ? [ - createBlock( - 'core/pattern', - { slug: pattern.name }, - blocks.map( ( block ) => cloneBlock( block ) ) - ), - ] - : [], - pattern.name - ); - } else { - onInsert( - ( blocks ?? [] ).map( ( block ) => cloneBlock( block ) ), - pattern.name - ); - } + onInsert( + ( blocks ?? [] ).map( ( block ) => cloneBlock( block ) ), + pattern.name + ); createSuccessNotice( sprintf( /* translators: %s: block pattern title. */ From b4e01bbae2223eab418c1effc935db28fa9d8e46 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 May 2023 15:48:33 +1200 Subject: [PATCH 03/15] Remove syncStatus default as for backwards compat default should probably be undefined --- packages/block-library/src/pattern/block.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index 80ac618b068855..5feb47861c0076 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -27,8 +27,7 @@ }, "syncStatus": { "type": [ "string", "boolean" ], - "enum": [ "full", "partial", "none" ], - "default": "none" + "enum": [ "full", "partial" ] } } } From 39efc212cfa180ea62782b83100f9907f003c505 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 May 2023 17:28:37 +1200 Subject: [PATCH 04/15] Remove default align attribute --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/pattern/block.json | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 5194ca4ddd6b23..0e66c40cb2a432 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -465,7 +465,7 @@ Show a block pattern. ([Source](https://github.com/WordPress/gutenberg/tree/trun - **Name:** core/pattern - **Category:** theme - **Supports:** -- **Attributes:** forcedAlignment, layout, slug, syncStatus +- **Attributes:** inheritedAlignment, layout, slug, syncStatus ## Post Author diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index 5feb47861c0076..ffcd0c39622737 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -15,9 +15,8 @@ "slug": { "type": "string" }, - "forcedAlignment": { - "type": "string", - "default": "full" + "inheritedAlignment": { + "type": "string" }, "layout": { "type": "object", From 6016dd79254241e2f820488da004a50fbfc1d87b Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 May 2023 17:35:30 +1200 Subject: [PATCH 05/15] Fix some undefined variable errors --- packages/block-library/src/pattern/edit.js | 4 ++-- packages/block-library/src/pattern/index.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 7ee3b3d8844688..b2863326e49ff9 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -57,7 +57,7 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { } }, [ clientId, - selectedPattern.blocks, + selectedPattern?.blocks, replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent, innerBlocks, @@ -90,7 +90,7 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { } ); }, [ innerBlocks, - selectedPattern.blocks, + selectedPattern?.blocks, setAttributes, inheritedAlignment, syncStatus, diff --git a/packages/block-library/src/pattern/index.php b/packages/block-library/src/pattern/index.php index 4afed4cca5c919..1534640b2781ce 100644 --- a/packages/block-library/src/pattern/index.php +++ b/packages/block-library/src/pattern/index.php @@ -32,7 +32,8 @@ function render_block_core_pattern( $attributes, $content ) { return ''; } - $wrapper = '
%s
'; + $align_class = isset($attributes['inheritedAlignment']) ? 'class="align' . $attributes['inheritedAlignment'] . '"' : ''; + $wrapper = '
%s
'; if ( isset( $attributes['syncStatus'] ) && 'unsynced' === $attributes['syncStatus'] ) { return sprintf( $wrapper, $content ); From f7df5d944694e9072b4f648b2fa6df5675e884f1 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 12 May 2023 11:47:24 +1200 Subject: [PATCH 06/15] Changes from code review --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/pattern/block.json | 3 -- packages/block-library/src/pattern/edit.js | 48 ++----------------- packages/block-library/src/pattern/index.php | 6 +-- packages/block-library/src/pattern/save.js | 11 ++--- 5 files changed, 11 insertions(+), 59 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 0e66c40cb2a432..0b5d2cf6413ac2 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -465,7 +465,7 @@ Show a block pattern. ([Source](https://github.com/WordPress/gutenberg/tree/trun - **Name:** core/pattern - **Category:** theme - **Supports:** -- **Attributes:** inheritedAlignment, layout, slug, syncStatus +- **Attributes:** layout, slug, syncStatus ## Post Author diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index ffcd0c39622737..5796a3d72c4f67 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -15,9 +15,6 @@ "slug": { "type": "string" }, - "inheritedAlignment": { - "type": "string" - }, "layout": { "type": "object", "default": { diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index b2863326e49ff9..5072d577172b0a 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -10,8 +10,8 @@ import { useInnerBlocksProps, } from '@wordpress/block-editor'; -const PatternEdit = ( { attributes, clientId, setAttributes } ) => { - const { inheritedAlignment, slug, syncStatus } = attributes; +const PatternEdit = ( { attributes, clientId } ) => { + const { slug, syncStatus } = attributes; const { selectedPattern, innerBlocks } = useSelect( ( select ) => { return { @@ -65,43 +65,7 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { replaceBlocks, ] ); - useEffect( () => { - if ( syncStatus !== 'partial' ) { - return; - } - const alignments = [ 'wide', 'full' ]; - const blocks = innerBlocks; - if ( ! blocks || blocks.length === 0 ) { - return; - } - // Determine the widest setting of all the contained blocks. - const widestAlignment = blocks.reduce( ( accumulator, block ) => { - const { align } = block.attributes; - return alignments.indexOf( align ) > - alignments.indexOf( accumulator ) - ? align - : accumulator; - }, undefined ); - - // Set the attribute of the Pattern block to match the widest - // alignment. - setAttributes( { - inheritedAlignment: widestAlignment ?? '', - } ); - }, [ - innerBlocks, - selectedPattern?.blocks, - setAttributes, - inheritedAlignment, - syncStatus, - ] ); - - const blockProps = useBlockProps( { - className: - syncStatus === 'partial' && - inheritedAlignment && - `align${ inheritedAlignment }`, - } ); + const blockProps = useBlockProps(); const innerBlocksProps = useInnerBlocksProps( blockProps, { templateLock: syncStatus === 'partial' ? 'contentOnly' : false, @@ -111,11 +75,7 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { return
; } - return ( - <> -
- - ); + return
; }; export default PatternEdit; diff --git a/packages/block-library/src/pattern/index.php b/packages/block-library/src/pattern/index.php index 1534640b2781ce..9d5e85b30083e1 100644 --- a/packages/block-library/src/pattern/index.php +++ b/packages/block-library/src/pattern/index.php @@ -31,9 +31,9 @@ function render_block_core_pattern( $attributes, $content ) { if ( empty( $attributes['slug'] ) ) { return ''; } - - $align_class = isset($attributes['inheritedAlignment']) ? 'class="align' . $attributes['inheritedAlignment'] . '"' : ''; - $wrapper = '
%s
'; + $slug_classname = str_replace( '/', '-', $attributes['slug'] ); + $classnames = isset($attributes['className']) ? $attributes['className'] . ' ' . $slug_classname : $slug_classname; + $wrapper = '
%s
'; if ( isset( $attributes['syncStatus'] ) && 'unsynced' === $attributes['syncStatus'] ) { return sprintf( $wrapper, $content ); diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index 3f7c4db82d60a8..f0dca374bd3dc8 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -3,16 +3,11 @@ */ import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; -export default function save( { - attributes: { inheritedAlignment, syncStatus }, - innerBlocks, -} ) { - if ( innerBlocks?.length === 0 || syncStatus !== 'partial' ) { +export default function save( { attributes: { syncStatus }, innerBlocks } ) { + if ( innerBlocks?.length === 0 && syncStatus !== 'partial' ) { return; } - const blockProps = useBlockProps.save( { - className: inheritedAlignment && `align${ inheritedAlignment }`, - } ); + const blockProps = useBlockProps.save(); const innerBlocksProps = useInnerBlocksProps.save( blockProps ); return
{ innerBlocksProps.children }
; } From cc90aeee02d7690b70b3c26cec57e88f41708560 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 12 May 2023 11:56:19 +1200 Subject: [PATCH 07/15] Fix linting errors --- packages/block-library/src/pattern/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/pattern/index.php b/packages/block-library/src/pattern/index.php index 9d5e85b30083e1..50bc3c30f4c583 100644 --- a/packages/block-library/src/pattern/index.php +++ b/packages/block-library/src/pattern/index.php @@ -32,7 +32,7 @@ function render_block_core_pattern( $attributes, $content ) { return ''; } $slug_classname = str_replace( '/', '-', $attributes['slug'] ); - $classnames = isset($attributes['className']) ? $attributes['className'] . ' ' . $slug_classname : $slug_classname; + $classnames = isset( $attributes['className'] ) ? $attributes['className'] . ' ' . $slug_classname : $slug_classname; $wrapper = '
%s
'; if ( isset( $attributes['syncStatus'] ) && 'unsynced' === $attributes['syncStatus'] ) { From 6fd82a7eb1087fc682a47ec35ed64f5d6e2efdeb Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 12 May 2023 11:57:17 +1200 Subject: [PATCH 08/15] Another linting error --- packages/block-library/src/pattern/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/pattern/index.php b/packages/block-library/src/pattern/index.php index 50bc3c30f4c583..d1c636e75cf316 100644 --- a/packages/block-library/src/pattern/index.php +++ b/packages/block-library/src/pattern/index.php @@ -32,7 +32,7 @@ function render_block_core_pattern( $attributes, $content ) { return ''; } $slug_classname = str_replace( '/', '-', $attributes['slug'] ); - $classnames = isset( $attributes['className'] ) ? $attributes['className'] . ' ' . $slug_classname : $slug_classname; + $classnames = isset( $attributes['className'] ) ? $attributes['className'] . ' ' . $slug_classname : $slug_classname; $wrapper = '
%s
'; if ( isset( $attributes['syncStatus'] ) && 'unsynced' === $attributes['syncStatus'] ) { From f5423282c1c7d42bdf5b35711f5fd0f9a6844d16 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 12 May 2023 12:02:46 +1200 Subject: [PATCH 09/15] grrrrrrrrr! why is my VS Code linting not working! --- packages/block-library/src/pattern/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/pattern/index.php b/packages/block-library/src/pattern/index.php index d1c636e75cf316..72f0d4d097392e 100644 --- a/packages/block-library/src/pattern/index.php +++ b/packages/block-library/src/pattern/index.php @@ -33,7 +33,7 @@ function render_block_core_pattern( $attributes, $content ) { } $slug_classname = str_replace( '/', '-', $attributes['slug'] ); $classnames = isset( $attributes['className'] ) ? $attributes['className'] . ' ' . $slug_classname : $slug_classname; - $wrapper = '
%s
'; + $wrapper = '
%s
'; if ( isset( $attributes['syncStatus'] ) && 'unsynced' === $attributes['syncStatus'] ) { return sprintf( $wrapper, $content ); From a888f552a48f395d09faeb7ec5a4e92f36d8b9bc Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 12 May 2023 12:23:52 +1200 Subject: [PATCH 10/15] Update fixture --- test/integration/fixtures/blocks/core__pattern.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/integration/fixtures/blocks/core__pattern.json b/test/integration/fixtures/blocks/core__pattern.json index f1101dc950e46e..fa0031ebf4d87e 100644 --- a/test/integration/fixtures/blocks/core__pattern.json +++ b/test/integration/fixtures/blocks/core__pattern.json @@ -3,12 +3,10 @@ "name": "core/pattern", "isValid": true, "attributes": { - "forcedAlignment": "full", + "slug": "core/text-two-columns", "layout": { "type": "constrained" - }, - "slug": "core/text-two-columns", - "syncStatus": "synced" + } }, "innerBlocks": [] } From 31427d4550f614a467859b57dae517e6693c5d0c Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 12 May 2023 16:10:37 +1200 Subject: [PATCH 11/15] Correct esc method Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> --- packages/block-library/src/pattern/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/pattern/index.php b/packages/block-library/src/pattern/index.php index 72f0d4d097392e..4af986c423d012 100644 --- a/packages/block-library/src/pattern/index.php +++ b/packages/block-library/src/pattern/index.php @@ -33,7 +33,7 @@ function render_block_core_pattern( $attributes, $content ) { } $slug_classname = str_replace( '/', '-', $attributes['slug'] ); $classnames = isset( $attributes['className'] ) ? $attributes['className'] . ' ' . $slug_classname : $slug_classname; - $wrapper = '
%s
'; + $wrapper = '
%s
'; if ( isset( $attributes['syncStatus'] ) && 'unsynced' === $attributes['syncStatus'] ) { return sprintf( $wrapper, $content ); From e580bc3afbeb0f72e75a13f9f1b5283bea6a639a Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 12 May 2023 16:47:59 +1200 Subject: [PATCH 12/15] Change save function check back --- packages/block-library/src/pattern/save.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index f0dca374bd3dc8..3f9b3dd029eb11 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -4,7 +4,7 @@ import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; export default function save( { attributes: { syncStatus }, innerBlocks } ) { - if ( innerBlocks?.length === 0 && syncStatus !== 'partial' ) { + if ( innerBlocks?.length === 0 || syncStatus !== 'partial' ) { return; } const blockProps = useBlockProps.save(); From e286463b9868adc4a409ab209730c032c9a1d181 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Fri, 12 May 2023 13:29:10 +0800 Subject: [PATCH 13/15] Keep block fully dynamic for now by removing save.js --- packages/block-library/src/pattern/index.js | 3 +-- packages/block-library/src/pattern/save.js | 13 ------------- 2 files changed, 1 insertion(+), 15 deletions(-) delete mode 100644 packages/block-library/src/pattern/save.js diff --git a/packages/block-library/src/pattern/index.js b/packages/block-library/src/pattern/index.js index 8cedbdd94f92b0..27e74510eb5972 100644 --- a/packages/block-library/src/pattern/index.js +++ b/packages/block-library/src/pattern/index.js @@ -5,13 +5,12 @@ import initBlock from '../utils/init-block'; import metadata from './block.json'; import PatternEditV1 from './v1/edit'; import PatternEditV2 from './edit'; -import PatternSave from './save'; const { name } = metadata; export { metadata, name }; export const settings = window?.__experimentalEnablePatternEnhancements - ? { edit: PatternEditV2, save: PatternSave } + ? { edit: PatternEditV2 } : { edit: PatternEditV1 }; export const init = () => initBlock( { name, metadata, settings } ); diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js deleted file mode 100644 index 3f9b3dd029eb11..00000000000000 --- a/packages/block-library/src/pattern/save.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * WordPress dependencies - */ -import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; - -export default function save( { attributes: { syncStatus }, innerBlocks } ) { - if ( innerBlocks?.length === 0 || syncStatus !== 'partial' ) { - return; - } - const blockProps = useBlockProps.save(); - const innerBlocksProps = useInnerBlocksProps.save( blockProps ); - return
{ innerBlocksProps.children }
; -} From 538eeebed207ce0b8b2ef7e8d46c2dac354e196c Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 15 May 2023 12:04:10 +1200 Subject: [PATCH 14/15] Removed layout supports for now --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/pattern/block.json | 11 ----------- test/integration/fixtures/blocks/core__pattern.json | 5 +---- 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 0b5d2cf6413ac2..1a5888590852a8 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -465,7 +465,7 @@ Show a block pattern. ([Source](https://github.com/WordPress/gutenberg/tree/trun - **Name:** core/pattern - **Category:** theme - **Supports:** -- **Attributes:** layout, slug, syncStatus +- **Attributes:** slug, syncStatus ## Post Author diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index 5796a3d72c4f67..46913693e064ee 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -5,22 +5,11 @@ "title": "Pattern", "category": "theme", "description": "Show a block pattern.", - "supports": { - "__experimentalLayout": { - "allowEditing": false - } - }, "textdomain": "default", "attributes": { "slug": { "type": "string" }, - "layout": { - "type": "object", - "default": { - "type": "constrained" - } - }, "syncStatus": { "type": [ "string", "boolean" ], "enum": [ "full", "partial" ] diff --git a/test/integration/fixtures/blocks/core__pattern.json b/test/integration/fixtures/blocks/core__pattern.json index fa0031ebf4d87e..3bb2c0891b06ec 100644 --- a/test/integration/fixtures/blocks/core__pattern.json +++ b/test/integration/fixtures/blocks/core__pattern.json @@ -3,10 +3,7 @@ "name": "core/pattern", "isValid": true, "attributes": { - "slug": "core/text-two-columns", - "layout": { - "type": "constrained" - } + "slug": "core/text-two-columns" }, "innerBlocks": [] } From 4ac3f8e7f2437ab3a11c8d1f41fede2432293cb5 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 15 May 2023 12:07:42 +1200 Subject: [PATCH 15/15] put original supports back --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/pattern/block.json | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 1a5888590852a8..04710602d28f0b 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -464,7 +464,7 @@ Show a block pattern. ([Source](https://github.com/WordPress/gutenberg/tree/trun - **Name:** core/pattern - **Category:** theme -- **Supports:** +- **Supports:** ~~html~~, ~~inserter~~ - **Attributes:** slug, syncStatus ## Post Author diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index 46913693e064ee..82372fe1680984 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -5,6 +5,10 @@ "title": "Pattern", "category": "theme", "description": "Show a block pattern.", + "supports": { + "html": false, + "inserter": false + }, "textdomain": "default", "attributes": { "slug": {