From 867ca865c98d4a60ee0d7db7dec0ebfed6893ecd Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 25 Jul 2023 16:46:56 +1200 Subject: [PATCH] Patterns: Allow inserting of unsynced patterns from quick inserter (#52866) --- .../block-editor/src/autocompleters/block.js | 27 ++++++++++++++----- .../src/components/block-list/block.js | 7 ++++- .../inserter/hooks/use-patterns-state.js | 2 +- .../inserter/reusable-blocks-tab.js | 5 +++- packages/block-editor/src/store/selectors.js | 14 ++-------- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/packages/block-editor/src/autocompleters/block.js b/packages/block-editor/src/autocompleters/block.js index 4d189e52b7cdd4..bc06c9de5aaaff 100644 --- a/packages/block-editor/src/autocompleters/block.js +++ b/packages/block-editor/src/autocompleters/block.js @@ -5,6 +5,7 @@ import { useSelect } from '@wordpress/data'; import { createBlock, createBlocksFromInnerBlocksTemplate, + parse, } from '@wordpress/blocks'; import { useMemo } from '@wordpress/element'; @@ -116,14 +117,28 @@ function createBlockCompleter() { return ! ( /\S/.test( before ) || /\S/.test( after ) ); }, getOptionCompletion( inserterItem ) { - const { name, initialAttributes, innerBlocks } = inserterItem; + const { + name, + initialAttributes, + innerBlocks, + syncStatus, + content, + } = inserterItem; + return { action: 'replace', - value: createBlock( - name, - initialAttributes, - createBlocksFromInnerBlocksTemplate( innerBlocks ) - ), + value: + syncStatus === 'unsynced' + ? parse( content, { + __unstableSkipMigrationLogs: true, + } ) + : createBlock( + name, + initialAttributes, + createBlocksFromInnerBlocksTemplate( + innerBlocks + ) + ), }; }, }; diff --git a/packages/block-editor/src/components/block-list/block.js b/packages/block-editor/src/components/block-list/block.js index aa1a40ef480579..a2acb3c7b53be6 100644 --- a/packages/block-editor/src/components/block-list/block.js +++ b/packages/block-editor/src/components/block-list/block.js @@ -505,9 +505,14 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => { ) { __unstableMarkLastChangeAsPersistent(); } + //Unsynced patterns are nested in an array so we need to flatten them. + const replacementBlocks = + blocks?.length === 1 && Array.isArray( blocks[ 0 ] ) + ? blocks[ 0 ] + : blocks; replaceBlocks( [ ownProps.clientId ], - blocks, + replacementBlocks, indexToSelect, initialPosition ); 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 6d6333587b7d0e..c0b8d7ff925126 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 @@ -15,7 +15,7 @@ import { store as blockEditorStore } from '../../../store'; const CUSTOM_CATEGORY = { name: 'custom', label: __( 'My patterns' ), - description: __( 'Custom patterns add by site users' ), + description: __( 'Custom patterns added by site users' ), }; /** diff --git a/packages/block-editor/src/components/inserter/reusable-blocks-tab.js b/packages/block-editor/src/components/inserter/reusable-blocks-tab.js index 08cd8d57ba0d0e..920b9f56384d4b 100644 --- a/packages/block-editor/src/components/inserter/reusable-blocks-tab.js +++ b/packages/block-editor/src/components/inserter/reusable-blocks-tab.js @@ -22,7 +22,10 @@ function ReusableBlocksList( { onHover, onInsert, rootClientId } ) { ); const filteredItems = useMemo( () => { - return items.filter( ( { category } ) => category === 'reusable' ); + return items.filter( + ( { category, syncStatus } ) => + category === 'reusable' && syncStatus !== 'unsynced' + ); }, [ items ] ); if ( filteredItems.length === 0 ) { diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index c3d8847b032397..e459e536c90914 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2023,6 +2023,7 @@ export const getInserterItems = createSelector( utility: 1, // Deprecated. frecency, content: reusableBlock.content.raw, + syncStatus: reusableBlock.wp_pattern_sync_status, }; }; @@ -2031,18 +2032,7 @@ export const getInserterItems = createSelector( 'core/block', rootClientId ) - ? getReusableBlocks( state ) - .filter( - ( reusableBlock ) => - // Reusable blocks that are fully synced should have no sync status set - // for backwards compat between patterns and old reusable blocks, but - // some in release 16.1 may have had sync status inadvertantly set to - // 'fully' if created in the site editor. - reusableBlock.wp_pattern_sync_status === 'fully' || - reusableBlock.wp_pattern_sync_status === '' || - ! reusableBlock.wp_pattern_sync_status - ) - .map( buildReusableBlockInserterItem ) + ? getReusableBlocks( state ).map( buildReusableBlockInserterItem ) : []; const buildBlockTypeInserterItem = buildBlockTypeItem( state, {