From 202975ea70f7c7683d8d9590f52da4742c94c5df Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 21 Apr 2023 10:25:20 +1200 Subject: [PATCH 01/20] Initial attempt to leave a pattern block wrapped around a pattern --- packages/block-library/src/pattern/edit.js | 39 ++++++++++++++------- packages/block-library/src/pattern/index.js | 2 ++ packages/block-library/src/pattern/save.js | 12 +++++++ 3 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 packages/block-library/src/pattern/save.js diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index be0b778eb4ae19..56343e5e053825 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -6,18 +6,25 @@ 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 { selectedPattern, innerBlocks } = useSelect( + ( select ) => { + return { + selectedPattern: select( + blockEditorStore + ).__experimentalGetParsedPattern( attributes.slug ), + innerBlocks: + select( blockEditorStore ).getBlock( clientId ) + ?.innerBlocks, + }; + }, + [ attributes.slug, clientId ] ); - const { replaceBlocks, __unstableMarkNextChangeAsNotPersistent } = + const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); // Run this effect when the component loads. @@ -25,7 +32,7 @@ const PatternEdit = ( { attributes, clientId } ) => { // 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 +40,20 @@ const PatternEdit = ( { attributes, clientId } ) => { // inner blocks but doesn't have blockSettings in the state. window.queueMicrotask( () => { __unstableMarkNextChangeAsNotPersistent(); - replaceBlocks( clientId, selectedPattern.blocks ); + replaceInnerBlocks( clientId, selectedPattern.blocks ); } ); } - }, [ clientId, selectedPattern?.blocks ] ); + }, [ + clientId, + selectedPattern?.blocks, + replaceInnerBlocks, + __unstableMarkNextChangeAsNotPersistent, + innerBlocks, + ] ); - const props = useBlockProps(); - - return
; + const blockProps = useBlockProps(); + const innerBlocksProps = useInnerBlocksProps( blockProps, {} ); + return <> { innerBlocksProps.children } ; }; export default PatternEdit; diff --git a/packages/block-library/src/pattern/index.js b/packages/block-library/src/pattern/index.js index e4af712da8bb29..4a7f933d5052d5 100644 --- a/packages/block-library/src/pattern/index.js +++ b/packages/block-library/src/pattern/index.js @@ -4,12 +4,14 @@ import initBlock from '../utils/init-block'; import metadata from './block.json'; import PatternEdit from './edit'; +import PatternSave from './save'; const { name } = metadata; export { metadata, name }; export const settings = { edit: PatternEdit, + save: PatternSave, }; 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 new file mode 100644 index 00000000000000..59d73888ecd5e6 --- /dev/null +++ b/packages/block-library/src/pattern/save.js @@ -0,0 +1,12 @@ +/** + * WordPress dependencies + */ +import { InnerBlocks } from '@wordpress/block-editor'; + +export default function save() { + return ( + <> + + + ); +} From f5e64f8a8a9dea3ab16388ceabf841a88409966e Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 21 Apr 2023 12:30:57 +1200 Subject: [PATCH 02/20] Add a wrapper div --- packages/block-library/src/pattern/edit.js | 3 +-- packages/block-library/src/pattern/save.js | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 56343e5e053825..74da44584efd3d 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -23,7 +23,6 @@ const PatternEdit = ( { attributes, clientId } ) => { }, [ attributes.slug, clientId ] ); - const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); @@ -53,7 +52,7 @@ const PatternEdit = ( { attributes, clientId } ) => { const blockProps = useBlockProps(); const innerBlocksProps = useInnerBlocksProps( blockProps, {} ); - return <> { innerBlocksProps.children } ; + return
{ innerBlocksProps.children }
; }; export default PatternEdit; diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index 59d73888ecd5e6..000acdcd4a6055 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -1,12 +1,12 @@ /** * WordPress dependencies */ -import { InnerBlocks } from '@wordpress/block-editor'; +import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; export default function save() { return ( - <> +
- +
); } From 65a852a06e4f05762e2d98b8f177121524547a72 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 21 Apr 2023 16:47:41 +1200 Subject: [PATCH 03/20] Add rough prototype of sync versus unsynced --- packages/block-library/src/pattern/block.json | 4 ++++ packages/block-library/src/pattern/edit.js | 17 +++++++++++++++-- packages/block-library/src/pattern/save.js | 13 ++++++++----- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index da023142403c87..3068c9ec71a6e2 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -13,6 +13,10 @@ "attributes": { "slug": { "type": "string" + }, + "unsynced": { + "type": "boolean", + "default": false } } } diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 74da44584efd3d..e740bcb4afa868 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -7,9 +7,11 @@ import { store as blockEditorStore, useBlockProps, useInnerBlocksProps, + BlockControls, } from '@wordpress/block-editor'; +import { ToolbarButton } from '@wordpress/components'; -const PatternEdit = ( { attributes, clientId } ) => { +const PatternEdit = ( { attributes, clientId, setAttributes } ) => { const { selectedPattern, innerBlocks } = useSelect( ( select ) => { return { @@ -52,7 +54,18 @@ const PatternEdit = ( { attributes, clientId } ) => { const blockProps = useBlockProps(); const innerBlocksProps = useInnerBlocksProps( blockProps, {} ); - return
{ innerBlocksProps.children }
; + return ( + <> +
+ + setAttributes( { unsynced: true } ) } + > + Unsync + + + + ); }; export default PatternEdit; diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index 000acdcd4a6055..a08121117ce09c 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -1,12 +1,15 @@ /** * WordPress dependencies */ -import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; +import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; -export default function save() { +export default function save( { attributes } ) { + if ( ! attributes.unsynced ) { + return null; + } + const blockProps = useBlockProps.save(); + const innerBlocksProps = useInnerBlocksProps.save( blockProps ); return ( -
- -
+
{ innerBlocksProps.children }
); } From 0da8ebacb355aefc6f97d64eaaef3cfd3cbdfc75 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 May 2023 12:24:06 +1200 Subject: [PATCH 04/20] add pattern experiment --- docs/reference-guides/core-blocks.md | 2 +- lib/experimental/editor-settings.php | 3 ++ lib/experiments-page.php | 12 +++++ packages/block-library/src/pattern/index.js | 10 ++-- packages/block-library/src/pattern/v1/edit.js | 51 +++++++++++++++++++ 5 files changed, 72 insertions(+), 6 deletions(-) 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..3e29e64f8fc2e3 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:** ~~html~~, ~~inserter~~ -- **Attributes:** slug +- **Attributes:** slug, unsynced ## 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-library/src/pattern/index.js b/packages/block-library/src/pattern/index.js index 4a7f933d5052d5..8cedbdd94f92b0 100644 --- a/packages/block-library/src/pattern/index.js +++ b/packages/block-library/src/pattern/index.js @@ -3,15 +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, - save: PatternSave, -}; +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/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; From 6b60cd4b306878e7b99460efafd6e48b191b31d0 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 26 Apr 2023 10:47:25 +1200 Subject: [PATCH 05/20] Add default layout attributes and change sync status to enum --- packages/block-library/src/pattern/block.json | 21 ++++++++++++++----- packages/block-library/src/pattern/edit.js | 4 +++- packages/block-library/src/pattern/save.js | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index 3068c9ec71a6e2..08efa709146120 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -6,17 +6,28 @@ "category": "theme", "description": "Show a block pattern.", "supports": { - "html": false, - "inserter": false + "align": [ "wide", "full" ], + "__experimentalLayout": true }, "textdomain": "default", "attributes": { "slug": { "type": "string" }, - "unsynced": { - "type": "boolean", - "default": false + "align": { + "type": "string", + "default": "full" + }, + "layout": { + "type": "object", + "default": { + "type": "constrained" + } + }, + "syncStatus": { + "type": [ "string", "boolean" ], + "enum": [ "synced", "unsynced" ], + "default": "synced" } } } diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index e740bcb4afa868..7cb6b2b0f5b1b6 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -59,7 +59,9 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => {
setAttributes( { unsynced: true } ) } + onClick={ () => + setAttributes( { syncStatus: 'unsynced' } ) + } > Unsync diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index a08121117ce09c..6461785a9be9e5 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 } ) { - if ( ! attributes.unsynced ) { + if ( attributes.syncStatus === 'synced' ) { return null; } const blockProps = useBlockProps.save(); From 03b49e7c5f6d99bd2f79af4de1ee1dc30d86c110 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 27 Apr 2023 15:22:09 +1200 Subject: [PATCH 06/20] Remove wrapper div from frontend --- packages/block-library/src/pattern/block.json | 4 +++- packages/block-library/src/pattern/save.js | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index 08efa709146120..12eea9aed09aa2 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -7,7 +7,9 @@ "description": "Show a block pattern.", "supports": { "align": [ "wide", "full" ], - "__experimentalLayout": true + "__experimentalLayout": { + "allowEditing": false + } }, "textdomain": "default", "attributes": { diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index 6461785a9be9e5..05c3328904c45b 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -9,7 +9,5 @@ export default function save( { attributes } ) { } const blockProps = useBlockProps.save(); const innerBlocksProps = useInnerBlocksProps.save( blockProps ); - return ( -
{ innerBlocksProps.children }
- ); + return <>{ innerBlocksProps.children }; } From ea0b808e4cca426cb36e56dba41864b6675f8245 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 27 Apr 2023 13:48:05 +1000 Subject: [PATCH 07/20] Pattern Enhancements: Render unsynced pattern content (#50114) --- docs/reference-guides/core-blocks.md | 4 ++-- packages/block-library/src/pattern/index.php | 9 +++++++-- packages/block-library/src/pattern/save.js | 1 + test/integration/fixtures/blocks/core__pattern.json | 7 ++++++- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 3e29e64f8fc2e3..eb875bc73006b0 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, unsynced +- **Supports:** align (full, wide) +- **Attributes:** align, layout, slug, syncStatus ## Post Author diff --git a/packages/block-library/src/pattern/index.php b/packages/block-library/src/pattern/index.php index 32a08601ca8089..b60df3d43fdcea 100644 --- a/packages/block-library/src/pattern/index.php +++ b/packages/block-library/src/pattern/index.php @@ -22,15 +22,20 @@ 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 ''; } + if ( isset( $attributes['syncStatus'] ) && 'unsynced' === $attributes['syncStatus'] ) { + return $content; + } + $slug = $attributes['slug']; $registry = WP_Block_Patterns_Registry::get_instance(); if ( ! $registry->is_registered( $slug ) ) { diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index 05c3328904c45b..a58adc56f80067 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -7,6 +7,7 @@ export default function save( { attributes } ) { if ( attributes.syncStatus === 'synced' ) { return null; } + const blockProps = useBlockProps.save(); const innerBlocksProps = useInnerBlocksProps.save( blockProps ); return <>{ innerBlocksProps.children }; diff --git a/test/integration/fixtures/blocks/core__pattern.json b/test/integration/fixtures/blocks/core__pattern.json index 3bb2c0891b06ec..a92b57163082f0 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" + "align": "full", + "layout": { + "type": "constrained" + }, + "slug": "core/text-two-columns", + "syncStatus": "synced" }, "innerBlocks": [] } From de7183d5b94a466ab3a1c1c18b565663e82a707d Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 27 Apr 2023 20:18:39 +1200 Subject: [PATCH 08/20] Use a fixed custom align attrib so block toolbar align button doesn't appear on pattern block (#50118) --------- Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> --- docs/reference-guides/core-blocks.md | 4 ++-- packages/block-library/src/pattern/block.json | 3 +-- packages/block-library/src/pattern/edit.js | 14 +++++++++----- .../integration/fixtures/blocks/core__pattern.json | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index eb875bc73006b0..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:** align (full, wide) -- **Attributes:** align, layout, slug, syncStatus +- **Supports:** +- **Attributes:** forcedAlignment, layout, slug, syncStatus ## Post Author diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index 12eea9aed09aa2..ff006d50b52a43 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -6,7 +6,6 @@ "category": "theme", "description": "Show a block pattern.", "supports": { - "align": [ "wide", "full" ], "__experimentalLayout": { "allowEditing": false } @@ -16,7 +15,7 @@ "slug": { "type": "string" }, - "align": { + "forcedAlignment": { "type": "string", "default": "full" }, diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 7cb6b2b0f5b1b6..8cbabc5dd3684f 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -12,18 +12,20 @@ import { import { ToolbarButton } from '@wordpress/components'; const PatternEdit = ( { attributes, clientId, setAttributes } ) => { + const { forcedAlignment, slug } = attributes; const { selectedPattern, innerBlocks } = useSelect( ( select ) => { return { - selectedPattern: select( - blockEditorStore - ).__experimentalGetParsedPattern( attributes.slug ), + selectedPattern: + select( blockEditorStore ).__experimentalGetParsedPattern( + slug + ), innerBlocks: select( blockEditorStore ).getBlock( clientId ) ?.innerBlocks, }; }, - [ attributes.slug, clientId ] + [ slug, clientId ] ); const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); @@ -52,7 +54,9 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { innerBlocks, ] ); - const blockProps = useBlockProps(); + const blockProps = useBlockProps( { + className: forcedAlignment && `align${ forcedAlignment }`, + } ); const innerBlocksProps = useInnerBlocksProps( blockProps, {} ); return ( <> diff --git a/test/integration/fixtures/blocks/core__pattern.json b/test/integration/fixtures/blocks/core__pattern.json index a92b57163082f0..f1101dc950e46e 100644 --- a/test/integration/fixtures/blocks/core__pattern.json +++ b/test/integration/fixtures/blocks/core__pattern.json @@ -3,7 +3,7 @@ "name": "core/pattern", "isValid": true, "attributes": { - "align": "full", + "forcedAlignment": "full", "layout": { "type": "constrained" }, From a81ed2dfd8c9084182d6b77d7d6cea56a399ad5d Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 1 May 2023 10:17:39 +1200 Subject: [PATCH 09/20] Fix issue with duplicated block toolbar and inspector controls by cloning the inner blocks --- packages/block-library/src/pattern/edit.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 8cbabc5dd3684f..0547236ba81ee5 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -1,6 +1,7 @@ /** * WordPress dependencies */ +import { cloneBlock } from '@wordpress/blocks'; import { useSelect, useDispatch } from '@wordpress/data'; import { useEffect } from '@wordpress/element'; import { @@ -43,7 +44,12 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { // inner blocks but doesn't have blockSettings in the state. window.queueMicrotask( () => { __unstableMarkNextChangeAsNotPersistent(); - replaceInnerBlocks( clientId, selectedPattern.blocks ); + replaceInnerBlocks( + clientId, + selectedPattern.blocks.map( ( block ) => + cloneBlock( block ) + ) + ); } ); } }, [ From 18852ff28356f3fb5d003c5e77d61b4c9f7d66b8 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 1 May 2023 17:26:13 +1200 Subject: [PATCH 10/20] Add wrapper to frontend and add pattern slug to use as selector --- packages/block-library/src/pattern/edit.js | 2 +- packages/block-library/src/pattern/index.php | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 0547236ba81ee5..60c6346acf4c10 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -66,7 +66,7 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { const innerBlocksProps = useInnerBlocksProps( blockProps, {} ); return ( <> -
+
diff --git a/packages/block-library/src/pattern/index.php b/packages/block-library/src/pattern/index.php index b60df3d43fdcea..4afed4cca5c919 100644 --- a/packages/block-library/src/pattern/index.php +++ b/packages/block-library/src/pattern/index.php @@ -32,8 +32,10 @@ function render_block_core_pattern( $attributes, $content ) { return ''; } + $wrapper = '
%s
'; + if ( isset( $attributes['syncStatus'] ) && 'unsynced' === $attributes['syncStatus'] ) { - return $content; + return sprintf( $wrapper, $content ); } $slug = $attributes['slug']; @@ -43,7 +45,7 @@ function render_block_core_pattern( $attributes, $content ) { } $pattern = $registry->get_registered( $slug ); - return do_blocks( $pattern['content'] ); + return sprintf( $wrapper, do_blocks( $pattern['content'] ) ); } add_action( 'init', 'register_block_core_pattern' ); From 0fe7893f37a974be06e4b6c4189012a5a93420c0 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 2 May 2023 13:03:12 +1200 Subject: [PATCH 11/20] Add ability to sync and unsync --- packages/block-library/src/pattern/edit.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 60c6346acf4c10..b655c082fc9d5a 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -11,9 +11,10 @@ import { BlockControls, } from '@wordpress/block-editor'; import { ToolbarButton } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; const PatternEdit = ( { attributes, clientId, setAttributes } ) => { - const { forcedAlignment, slug } = attributes; + const { forcedAlignment, slug, syncStatus } = attributes; const { selectedPattern, innerBlocks } = useSelect( ( select ) => { return { @@ -70,10 +71,17 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { - setAttributes( { syncStatus: 'unsynced' } ) + setAttributes( { + syncStatus: + syncStatus === 'unsynced' + ? 'synced' + : 'unsynced', + } ) } > - Unsync + { syncStatus === 'unsynced' + ? __( 'Sync' ) + : __( 'Unsync' ) } From 7ebe8a05a3acb432e8afde08bfed45245de503bd Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 2 May 2023 14:18:46 +1200 Subject: [PATCH 12/20] Make reverting to `Sync` revert to saved pattern blocks --- packages/block-library/src/pattern/edit.js | 26 +++++++++++++--------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index b655c082fc9d5a..db7da2ffa5577f 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -65,23 +65,27 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { className: forcedAlignment && `align${ forcedAlignment }`, } ); const innerBlocksProps = useInnerBlocksProps( blockProps, {} ); + + const handleSync = () => { + if ( syncStatus === 'synced' ) { + setAttributes( { syncStatus: 'unsynced' } ); + } else { + setAttributes( { syncStatus: 'synced' } ); + replaceInnerBlocks( + clientId, + selectedPattern.blocks.map( ( block ) => cloneBlock( block ) ) + ); + } + }; + return ( <>
- - setAttributes( { - syncStatus: - syncStatus === 'unsynced' - ? 'synced' - : 'unsynced', - } ) - } - > + { syncStatus === 'unsynced' ? __( 'Sync' ) - : __( 'Unsync' ) } + : __( 'Desync' ) } From 618edc3788b7c9017670eaf0f6e77f1503cf4a3f Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 3 May 2023 12:47:46 +1200 Subject: [PATCH 13/20] Change default syncStatus to unsynced for barckwards compat --- packages/block-library/src/pattern/block.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index ff006d50b52a43..369024d10c49c0 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -28,7 +28,7 @@ "syncStatus": { "type": [ "string", "boolean" ], "enum": [ "synced", "unsynced" ], - "default": "synced" + "default": "unsynced" } } } From 8e690a7ddb98f90821519bc4bed3510ade0dc403 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 3 May 2023 15:18:36 +1200 Subject: [PATCH 14/20] Get widest align from child blocks (#50230) --- packages/block-library/src/pattern/edit.js | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index db7da2ffa5577f..01beff026c6ee8 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -61,6 +61,35 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { innerBlocks, ] ); + useEffect( () => { + const alignments = [ 'wide', 'full' ]; + const blocks = + syncStatus === 'synced' ? selectedPattern?.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( { + forcedAlignment: widestAlignment ?? '', + } ); + }, [ + innerBlocks, + selectedPattern?.blocks, + setAttributes, + syncStatus, + forcedAlignment, + ] ); + const blockProps = useBlockProps( { className: forcedAlignment && `align${ forcedAlignment }`, } ); From 604e67a812ddacdd26b30b6add356adc67cd4b54 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 3 May 2023 15:36:10 +1200 Subject: [PATCH 15/20] Switch to using template lock to denote different edit statuses of pattern --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/pattern/block.json | 6 ++-- packages/block-library/src/pattern/edit.js | 34 ++++++++----------- packages/block-library/src/pattern/save.js | 6 +--- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 5194ca4ddd6b23..ae6b50e5102e0c 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:** forcedAlignment, layout, slug, templateLock ## Post Author diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index 369024d10c49c0..b07aea19a9cc89 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -25,10 +25,10 @@ "type": "constrained" } }, - "syncStatus": { + "templateLock": { "type": [ "string", "boolean" ], - "enum": [ "synced", "unsynced" ], - "default": "unsynced" + "enum": [ "contentOnly", false ], + "default": "contentOnly" } } } diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 01beff026c6ee8..af4111f07fa611 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -14,7 +14,7 @@ import { ToolbarButton } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; const PatternEdit = ( { attributes, clientId, setAttributes } ) => { - const { forcedAlignment, slug, syncStatus } = attributes; + const { inheritedAlignment, slug, templateLock } = attributes; const { selectedPattern, innerBlocks } = useSelect( ( select ) => { return { @@ -34,8 +34,6 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { // 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 && ! innerBlocks?.length ) { // We batch updates to block list settings to avoid triggering cascading renders @@ -63,8 +61,7 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { useEffect( () => { const alignments = [ 'wide', 'full' ]; - const blocks = - syncStatus === 'synced' ? selectedPattern?.blocks : innerBlocks; + const blocks = innerBlocks; if ( ! blocks || blocks.length === 0 ) { return; } @@ -80,30 +77,27 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { // Set the attribute of the Pattern block to match the widest // alignment. setAttributes( { - forcedAlignment: widestAlignment ?? '', + inheritedAlignment: widestAlignment ?? '', } ); }, [ innerBlocks, selectedPattern?.blocks, setAttributes, - syncStatus, - forcedAlignment, + inheritedAlignment, ] ); const blockProps = useBlockProps( { - className: forcedAlignment && `align${ forcedAlignment }`, + className: inheritedAlignment && `align${ inheritedAlignment }`, + } ); + const innerBlocksProps = useInnerBlocksProps( blockProps, { + templateLock: templateLock === 'contentOnly' ? 'contentOnly' : false, } ); - const innerBlocksProps = useInnerBlocksProps( blockProps, {} ); const handleSync = () => { - if ( syncStatus === 'synced' ) { - setAttributes( { syncStatus: 'unsynced' } ); + if ( templateLock === false ) { + setAttributes( { templateLock: 'contentOnly' } ); } else { - setAttributes( { syncStatus: 'synced' } ); - replaceInnerBlocks( - clientId, - selectedPattern.blocks.map( ( block ) => cloneBlock( block ) ) - ); + setAttributes( { templateLock: false } ); } }; @@ -112,9 +106,9 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => {
- { syncStatus === 'unsynced' - ? __( 'Sync' ) - : __( 'Desync' ) } + { templateLock === false + ? __( 'Edit content only' ) + : __( 'Edit all' ) } diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index a58adc56f80067..3f8d2d34d39398 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -3,11 +3,7 @@ */ import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; -export default function save( { attributes } ) { - if ( attributes.syncStatus === 'synced' ) { - return null; - } - +export default function save() { const blockProps = useBlockProps.save(); const innerBlocksProps = useInnerBlocksProps.save( blockProps ); return <>{ innerBlocksProps.children }; From 7c5f361cc846f7bbf8619146ac10f5bd9d0f169e Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 3 May 2023 15:44:56 +1200 Subject: [PATCH 16/20] Add wrapper to frontend --- packages/block-library/src/pattern/edit.js | 2 +- packages/block-library/src/pattern/save.js | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index af4111f07fa611..3bd59f98721364 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -103,7 +103,7 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { return ( <> -
+
{ templateLock === false diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index 3f8d2d34d39398..887dfba4d6b7d2 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -3,8 +3,10 @@ */ import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; -export default function save() { - const blockProps = useBlockProps.save(); +export default function save( { attributes: { inheritedAlignment } } ) { + const blockProps = useBlockProps.save( { + className: inheritedAlignment && `align${ inheritedAlignment }`, + } ); const innerBlocksProps = useInnerBlocksProps.save( blockProps ); - return <>{ innerBlocksProps.children }; + return
{ innerBlocksProps.children }
; } From 94d77801f2781e2fe14f51b23977cd12c0785eb9 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Wed, 3 May 2023 11:25:40 +0800 Subject: [PATCH 17/20] Wrap patterns in the inserter with the pattern block --- .../inserter/hooks/use-patterns-state.js | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 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 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. */ From 177b85a29f31b95f1cab0a166d936d3fd2a36f5c Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 3 May 2023 16:12:27 +1200 Subject: [PATCH 18/20] Fix issue with block invalidation on first load --- packages/block-library/src/pattern/save.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index 887dfba4d6b7d2..da8e6a103aa325 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -3,7 +3,13 @@ */ import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; -export default function save( { attributes: { inheritedAlignment } } ) { +export default function save( { + attributes: { inheritedAlignment }, + innerBlocks, +} ) { + if ( innerBlocks?.length === 0 ) { + return; + } const blockProps = useBlockProps.save( { className: inheritedAlignment && `align${ inheritedAlignment }`, } ); From e70dc514d04465fb7366082b29ccca689ab59c78 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 May 2023 13:45:00 +1200 Subject: [PATCH 19/20] Switch to only adding a wrapper and contentOnly if syncStatus === partial --- packages/block-library/src/pattern/block.json | 6 +- packages/block-library/src/pattern/edit.js | 60 ++++++++++--------- packages/block-library/src/pattern/save.js | 4 +- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index b07aea19a9cc89..80ac618b068855 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -25,10 +25,10 @@ "type": "constrained" } }, - "templateLock": { + "syncStatus": { "type": [ "string", "boolean" ], - "enum": [ "contentOnly", false ], - "default": "contentOnly" + "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 3bd59f98721364..1c5821fff06bd9 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -14,7 +14,7 @@ import { ToolbarButton } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; const PatternEdit = ( { attributes, clientId, setAttributes } ) => { - const { inheritedAlignment, slug, templateLock } = attributes; + const { inheritedAlignment, slug, syncStatus } = attributes; const { selectedPattern, innerBlocks } = useSelect( ( select ) => { return { @@ -29,8 +29,11 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { }, [ slug, clientId ] ); - const { replaceInnerBlocks, __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. @@ -43,23 +46,32 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { // inner blocks but doesn't have blockSettings in the state. window.queueMicrotask( () => { __unstableMarkNextChangeAsNotPersistent(); - replaceInnerBlocks( - clientId, - selectedPattern.blocks.map( ( block ) => - cloneBlock( block ) - ) - ); + if ( syncStatus === 'partial' ) { + replaceInnerBlocks( + clientId, + selectedPattern.blocks.map( ( block ) => + cloneBlock( block ) + ) + ); + return; + } + replaceBlocks( clientId, selectedPattern.blocks ); } ); } }, [ clientId, - selectedPattern?.blocks, + selectedPattern.blocks, replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent, innerBlocks, + syncStatus, + replaceBlocks, ] ); useEffect( () => { + if ( syncStatus !== 'partial' ) { + return; + } const alignments = [ 'wide', 'full' ]; const blocks = innerBlocks; if ( ! blocks || blocks.length === 0 ) { @@ -81,36 +93,30 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { } ); }, [ innerBlocks, - selectedPattern?.blocks, + selectedPattern.blocks, setAttributes, inheritedAlignment, + syncStatus, ] ); const blockProps = useBlockProps( { - className: inheritedAlignment && `align${ inheritedAlignment }`, + className: + syncStatus === 'partial' && + inheritedAlignment && + `align${ inheritedAlignment }`, } ); + const innerBlocksProps = useInnerBlocksProps( blockProps, { - templateLock: templateLock === 'contentOnly' ? 'contentOnly' : false, + templateLock: syncStatus === 'partial' ? 'contentOnly' : false, } ); - const handleSync = () => { - if ( templateLock === false ) { - setAttributes( { templateLock: 'contentOnly' } ); - } else { - setAttributes( { templateLock: false } ); - } - }; + if ( syncStatus !== 'partial' ) { + return
; + } return ( <>
- - - { templateLock === false - ? __( 'Edit content only' ) - : __( 'Edit all' ) } - - ); }; diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index da8e6a103aa325..3f7c4db82d60a8 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -4,10 +4,10 @@ import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; export default function save( { - attributes: { inheritedAlignment }, + attributes: { inheritedAlignment, syncStatus }, innerBlocks, } ) { - if ( innerBlocks?.length === 0 ) { + if ( innerBlocks?.length === 0 || syncStatus !== 'partial' ) { return; } const blockProps = useBlockProps.save( { From 86afac24c63d35f1ed6dea1a537ed055eaeecc34 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Thu, 11 May 2023 15:12:40 +1200 Subject: [PATCH 20/20] Revert "Switch to only adding a wrapper and contentOnly if syncStatus === partial" This reverts commit e70dc514d04465fb7366082b29ccca689ab59c78. --- packages/block-library/src/pattern/block.json | 6 +- packages/block-library/src/pattern/edit.js | 60 +++++++++---------- packages/block-library/src/pattern/save.js | 4 +- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index 80ac618b068855..b07aea19a9cc89 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -25,10 +25,10 @@ "type": "constrained" } }, - "syncStatus": { + "templateLock": { "type": [ "string", "boolean" ], - "enum": [ "full", "partial", "none" ], - "default": "none" + "enum": [ "contentOnly", false ], + "default": "contentOnly" } } } diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 1c5821fff06bd9..3bd59f98721364 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -14,7 +14,7 @@ import { ToolbarButton } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; const PatternEdit = ( { attributes, clientId, setAttributes } ) => { - const { inheritedAlignment, slug, syncStatus } = attributes; + const { inheritedAlignment, slug, templateLock } = attributes; const { selectedPattern, innerBlocks } = useSelect( ( select ) => { return { @@ -29,11 +29,8 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { }, [ slug, clientId ] ); - const { - replaceBlocks, - replaceInnerBlocks, - __unstableMarkNextChangeAsNotPersistent, - } = useDispatch( blockEditorStore ); + const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } = + useDispatch( blockEditorStore ); // Run this effect when the component loads. // This adds the Pattern's contents to the post. @@ -46,32 +43,23 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { // 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 ); + replaceInnerBlocks( + clientId, + selectedPattern.blocks.map( ( block ) => + cloneBlock( block ) + ) + ); } ); } }, [ clientId, - selectedPattern.blocks, + selectedPattern?.blocks, replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent, innerBlocks, - syncStatus, - replaceBlocks, ] ); useEffect( () => { - if ( syncStatus !== 'partial' ) { - return; - } const alignments = [ 'wide', 'full' ]; const blocks = innerBlocks; if ( ! blocks || blocks.length === 0 ) { @@ -93,30 +81,36 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => { } ); }, [ innerBlocks, - selectedPattern.blocks, + selectedPattern?.blocks, setAttributes, inheritedAlignment, - syncStatus, ] ); const blockProps = useBlockProps( { - className: - syncStatus === 'partial' && - inheritedAlignment && - `align${ inheritedAlignment }`, + className: inheritedAlignment && `align${ inheritedAlignment }`, } ); - const innerBlocksProps = useInnerBlocksProps( blockProps, { - templateLock: syncStatus === 'partial' ? 'contentOnly' : false, + templateLock: templateLock === 'contentOnly' ? 'contentOnly' : false, } ); - if ( syncStatus !== 'partial' ) { - return
; - } + const handleSync = () => { + if ( templateLock === false ) { + setAttributes( { templateLock: 'contentOnly' } ); + } else { + setAttributes( { templateLock: false } ); + } + }; return ( <>
+ + + { templateLock === false + ? __( 'Edit content only' ) + : __( 'Edit all' ) } + + ); }; diff --git a/packages/block-library/src/pattern/save.js b/packages/block-library/src/pattern/save.js index 3f7c4db82d60a8..da8e6a103aa325 100644 --- a/packages/block-library/src/pattern/save.js +++ b/packages/block-library/src/pattern/save.js @@ -4,10 +4,10 @@ import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; export default function save( { - attributes: { inheritedAlignment, syncStatus }, + attributes: { inheritedAlignment }, innerBlocks, } ) { - if ( innerBlocks?.length === 0 || syncStatus !== 'partial' ) { + if ( innerBlocks?.length === 0 ) { return; } const blockProps = useBlockProps.save( {