Skip to content

Commit

Permalink
Patterns: Allow inserting of unsynced patterns from quick inserter (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
glendaviesnz committed Jul 25, 2023
1 parent b1278c6 commit 867ca86
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
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.
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 @@ -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' ),

This comment has been minimized.

Copy link
@richtabor

richtabor Aug 10, 2023

Member

Missing a terminal here.

};

/**
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

1 comment on commit 867ca86

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 867ca86.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5652748040
📝 Reported issues:

Please sign in to comment.