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

Relax directInsert #54409

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,4 @@ For example, a button block, deeply nested in several levels of block `X` that u
### `directInsert`

- **Type:** `Boolean`
- **Default:** - `undefined`. Determines whether the default block should be inserted directly into the InnerBlocks area by the block appender.
- **Default:** - `undefined`. Determines whether the default block is inserted directly into the InnerBlocks area by the block appender. Direct insertion is automatic if `defaultBlock` is defined or `allowedBlocks` specifies a single block type that has no variations. This setting can be used to override the automated behavior.
95 changes: 50 additions & 45 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,14 @@ class PrivateInserter extends Component {
const {
position,
hasSingleBlockType,
directInsert,
directInsertBlock,
insertOnlyAllowedBlock,
__experimentalIsQuick: isQuick,
onSelectOrClose,
} = this.props;

if ( hasSingleBlockType || directInsertBlock ) {
if ( hasSingleBlockType || ( directInsertBlock && directInsert ) ) {
return this.renderToggle( { onToggle: insertOnlyAllowedBlock } );
}

Expand All @@ -225,51 +226,55 @@ class PrivateInserter extends Component {
}

export const ComposedPrivateInserter = compose( [
withSelect(
( select, { clientId, rootClientId, shouldDirectInsert = true } ) => {
const {
getBlockRootClientId,
hasInserterItems,
getAllowedBlocks,
getDirectInsertBlock,
getSettings,
} = select( blockEditorStore );

const { getBlockVariations } = select( blocksStore );

rootClientId =
rootClientId || getBlockRootClientId( clientId ) || undefined;

const allowedBlocks = getAllowedBlocks( rootClientId );

const directInsertBlock =
shouldDirectInsert && getDirectInsertBlock( rootClientId );

const settings = getSettings();

const hasSingleBlockType =
allowedBlocks?.length === 1 &&
getBlockVariations( allowedBlocks[ 0 ].name, 'inserter' )
?.length === 0;

let allowedBlockType = false;
if ( hasSingleBlockType ) {
allowedBlockType = allowedBlocks[ 0 ];
}

return {
hasItems: hasInserterItems( rootClientId ),
hasSingleBlockType,
blockTitle: allowedBlockType ? allowedBlockType.title : '',
allowedBlockType,
directInsertBlock,
rootClientId,
prioritizePatterns:
settings.__experimentalPreferPatternsOnRoot &&
! rootClientId,
};
withSelect( ( select, { clientId, rootClientId, shouldDirectInsert } ) => {
const {
getBlock,
getBlockRootClientId,
hasInserterItems,
getAllowedBlocks,
getSettings,
getBlockListSettings,
} = select( blockEditorStore );

const { getBlockVariations } = select( blocksStore );

rootClientId =
rootClientId || getBlockRootClientId( clientId ) || undefined;

const allowedBlocks = getAllowedBlocks( rootClientId );

const { defaultBlock, directInsert } =
getBlockListSettings( rootClientId ) ?? {};

const resolvedDirectInsert =
shouldDirectInsert ?? typeof directInsert === 'function'
? directInsert( getBlock( rootClientId ) )
: directInsert;

const settings = getSettings();

const hasSingleBlockType =
allowedBlocks?.length === 1 &&
getBlockVariations( allowedBlocks[ 0 ].name, 'inserter' )
?.length === 0;

let allowedBlockType = false;
if ( hasSingleBlockType ) {
allowedBlockType = allowedBlocks[ 0 ];
}
),

return {
hasItems: hasInserterItems( rootClientId ),
hasSingleBlockType,
blockTitle: allowedBlockType ? allowedBlockType.title : '',
allowedBlockType,
directInsert: resolvedDirectInsert,
directInsertBlock: defaultBlock,
rootClientId,
prioritizePatterns:
settings.__experimentalPreferPatternsOnRoot && ! rootClientId,
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
return {
insertOnlyAllowedBlock() {
Expand Down
15 changes: 2 additions & 13 deletions packages/block-library/src/buttons/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { store as blocksStore } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -43,26 +42,16 @@ function ButtonsEdit( { attributes, className } ) {
'has-custom-font-size': fontSize || style?.typography?.fontSize,
} ),
} );
const { preferredStyle, hasButtonVariations } = useSelect( ( select ) => {
const preferredStyle = useSelect( ( select ) => {
const preferredStyleVariations =
select( blockEditorStore ).getSettings()
.__experimentalPreferredStyleVariations;
const buttonVariations = select( blocksStore ).getBlockVariations(
buttonBlockName,
'inserter'
);
return {
preferredStyle:
preferredStyleVariations?.value?.[ buttonBlockName ],
hasButtonVariations: buttonVariations.length > 0,
};
return preferredStyleVariations?.value?.[ buttonBlockName ];
}, [] );

const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: ALLOWED_BLOCKS,
defaultBlock: DEFAULT_BLOCK,
// This check should be handled by the `Inserter` internally to be consistent across all blocks that use it.
directInsert: ! hasButtonVariations,
template: [
[
buttonBlockName,
Expand Down
Loading