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

Fix: Show paragraph block variations in rich text inserter #66318

Merged
Merged
Changes from all 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
23 changes: 16 additions & 7 deletions packages/block-editor/src/autocompleters/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createBlock,
createBlocksFromInnerBlocksTemplate,
parse,
store as blocksStore,
} from '@wordpress/blocks';
import { useMemo } from '@wordpress/element';

Expand Down Expand Up @@ -36,22 +37,30 @@ function createBlockCompleter() {
triggerPrefix: '/',

useItems( filterValue ) {
const { rootClientId, selectedBlockName, prioritizedBlocks } =
const { rootClientId, selectedBlockId, prioritizedBlocks } =
useSelect( ( select ) => {
const {
getSelectedBlockClientId,
getBlockName,
getBlock,
getBlockListSettings,
getBlockRootClientId,
} = select( blockEditorStore );
const { getActiveBlockVariation } = select( blocksStore );
const selectedBlockClientId = getSelectedBlockClientId();
const { name: blockName, attributes } = getBlock(
selectedBlockClientId
);
const activeBlockVariation = getActiveBlockVariation(
blockName,
attributes
);
const _rootClientId = getBlockRootClientId(
selectedBlockClientId
);
return {
selectedBlockName: selectedBlockClientId
? getBlockName( selectedBlockClientId )
: null,
selectedBlockId: activeBlockVariation
? `${ blockName }/${ activeBlockVariation.name }`
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 am not sure if there is a better way to get the block id of the block variation.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's the way to go. See existing code as the reference:

const getItemFromVariation = ( state, item ) => ( variation ) => {
const variationId = `${ item.id }/${ variation.name }`;

: blockName,
rootClientId: _rootClientId,
prioritizedBlocks:
getBlockListSettings( _rootClientId )
Expand All @@ -78,11 +87,11 @@ function createBlockCompleter() {
);

return initialFilteredItems
.filter( ( item ) => item.name !== selectedBlockName )
.filter( ( item ) => item.id !== selectedBlockId )
.slice( 0, SHOWN_BLOCK_TYPES );
}, [
filterValue,
selectedBlockName,
selectedBlockId,
items,
categories,
collections,
Expand Down
Loading