Skip to content
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
58 changes: 38 additions & 20 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import clsx from 'clsx';
import { speak } from '@wordpress/a11y';
import { __, _x, sprintf } from '@wordpress/i18n';
import { Dropdown, Button } from '@wordpress/components';
import { useDispatch, useRegistry, useSelect } from '@wordpress/data';
import { createBlock, store as blocksStore } from '@wordpress/blocks';
import { useDispatch, useSelect } from '@wordpress/data';
import {
createBlock,
store as blocksStore,
__experimentalGetBlockLabel as getBlockLabel,
} from '@wordpress/blocks';
import { forwardRef } from '@wordpress/element';
import { plus } from '@wordpress/icons';

Expand Down Expand Up @@ -156,7 +160,6 @@ const UnforwardedInserter = (
const defaultBlockType = directInsertBlock
? getBlockType( directInsertBlock.name )
: null;

return {
hasItems: hasInserterItems( _targetRootClientId ),
hasSingleBlockType: _hasSingleBlockType,
Expand All @@ -173,8 +176,16 @@ const UnforwardedInserter = (
[ rootClientId, clientId, shouldDirectInsert ]
);

const registry = useRegistry();
const { insertBlock } = useDispatch( blockEditorStore );
const {
getBlock,
getBlockIndex,
getBlockOrder,
getBlockRootClientId,
getBlockSelectionEnd,
getPreviousBlockClientId,
} = useSelect( blockEditorStore );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any reason for switching to static useSelect instead of continuing to use useRegistry?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not really, I prompted codex with "perhaps use the useSelect shorthand", I didn't actually spot that there were existing useRegistry calls in the file, but it replaced them 😊

I do actually prefer this though, it feels more idiomatic / higher level than useRegistry.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Definitely, not a blocker 👍

const { getActiveBlockVariation, getBlockType } = useSelect( blocksStore );

// The global inserter (no isAppender, no rootClientId, no clientId) should
// always render, even with no items.
Expand All @@ -190,9 +201,6 @@ const UnforwardedInserter = (
return {};
}

const { getBlock, getPreviousBlockClientId } =
registry.select( blockEditorStore );

// Find the adjacent block of the same type whose attributes
// should be copied: previous sibling when inserting next to
// an existing block, otherwise the last child of the root.
Expand Down Expand Up @@ -225,13 +233,6 @@ const UnforwardedInserter = (
}

function getInsertionIndex() {
const {
getBlockIndex,
getBlockSelectionEnd,
getBlockOrder,
getBlockRootClientId,
} = registry.select( blockEditorStore );

// If the clientId is defined, we insert at the position of the block.
if ( clientId ) {
return getBlockIndex( clientId );
Expand Down Expand Up @@ -272,12 +273,29 @@ const UnforwardedInserter = (

onSelectOrClose?.( newBlock );

const message = sprintf(
// translators: %s: the name of the block that has been added
__( '%s block added' ),
allowedBlockType.title
);
speak( message );
const blockTypeToInsert = getBlockType( blockName );
let blockLabelToInsert;
if ( blockTypeToInsert ) {
blockLabelToInsert = getBlockLabel(
blockTypeToInsert,
newBlock.attributes
);

if ( blockLabelToInsert === blockTypeToInsert.title ) {
blockLabelToInsert =
getActiveBlockVariation( blockName, newBlock.attributes )
?.title || blockLabelToInsert;
}
}

if ( blockLabelToInsert ) {
const message = sprintf(
// translators: %s: the name of the block that has been added
__( '%s block added' ),
blockLabelToInsert
);
speak( message );
}
}

function renderToggle( { onToggle: dropdownOnToggle, isOpen } ) {
Expand Down
Loading