Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patterns: Allow inserting of unsynced patterns from quick inserter #52866

Merged
merged 7 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions packages/block-editor/src/autocompleters/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useSelect } from '@wordpress/data';
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
parse,
} from '@wordpress/blocks';
import { useMemo } from '@wordpress/element';

Expand Down Expand Up @@ -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
)
),
};
},
};
Expand Down
7 changes: 6 additions & 1 deletion packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,14 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, registry ) => {
) {
__unstableMarkLastChangeAsPersistent();
}
//Unsynced patterns are nested in an array so we need to flatten them.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially flattening this in the autocomplete component, but decided there were more potential backwards compat issues modifying that public API.

It seems like this replaceBlocks API is currently only ever expecting an array with block objects rather than an array with a nested array of blocks, so this seems like a safe extension, but open to thoughts about whether this is the best approach.

const replacementBlocks =
blocks?.length === 1 && Array.isArray( blocks[ 0 ] )
? blocks[ 0 ]
: blocks;
replaceBlocks(
[ ownProps.clientId ],
blocks,
replacementBlocks,
indexToSelect,
initialPosition
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
14 changes: 2 additions & 12 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,7 @@ export const getInserterItems = createSelector(
utility: 1, // Deprecated.
frecency,
content: reusableBlock.content.raw,
syncStatus: reusableBlock.wp_pattern_sync_status,
};
};

Expand All @@ -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, {
Expand Down
Loading