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

getDirectInsertBlock: Remove 'directInsert' as a callback handler #59172

Merged
merged 3 commits into from
Feb 26, 2024
Merged
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
2 changes: 1 addition & 1 deletion docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ _Parameters_

_Returns_

- `?WPDirectInsertBlock`: The block type to be directly inserted.
- `WPDirectInsertBlock|undefined`: The block type to be directly inserted.

_Type Definition_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ function useShallowMemo( value ) {
* in inner blocks.
* @param {string[]} prioritizedInserterBlocks Block names and/or block variations to be prioritized in the inserter, in the format {blockName}/{variationName}.
* @param {?WPDirectInsertBlock} defaultBlock The default block to insert: [ blockName, { blockAttributes } ].
* @param {?Function|boolean} directInsert If a default block should be inserted directly by the appender.
* @param {?boolean} directInsert If a default block should be inserted directly by the appender.
*
* @param {?WPDirectInsertBlock} __experimentalDefaultBlock A deprecated prop for the default block to insert: [ blockName, { blockAttributes } ]. Use `defaultBlock` instead.
*
* @param {?Function|boolean} __experimentalDirectInsert A deprecated prop for whether a default block should be inserted directly by the appender. Use `directInsert` instead.
* @param {?boolean} __experimentalDirectInsert A deprecated prop for whether a default block should be inserted directly by the appender. Use `directInsert` instead.
*
* @param {string} [templateLock] The template lock specified for the inner
* blocks component. (e.g. "all")
Expand Down Expand Up @@ -138,6 +138,16 @@ export default function useNestedSettingsUpdate(
newSettings.directInsert = directInsert;
}

if (
newSettings.directInsert !== undefined &&
typeof newSettings.directInsert !== 'boolean'
) {
deprecated( 'Using `Function` as a `directInsert` argument', {
alternative: '`boolean` values',
since: '6.5',
} );
}

// Batch updates to block list settings to avoid triggering cascading renders
// for each container block included in a tree and optimize initial render.
// To avoid triggering updateBlockListSettings for each container block
Expand Down
71 changes: 28 additions & 43 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2231,56 +2231,41 @@ export const __experimentalGetAllowedBlocks = createSelector(
* @param {Object} state Editor state.
* @param {?string} rootClientId Optional root client ID of block list.
*
* @return {?WPDirectInsertBlock} The block type to be directly inserted.
* @return {WPDirectInsertBlock|undefined} The block type to be directly inserted.
*
* @typedef {Object} WPDirectInsertBlock
* @property {string} name The type of block.
* @property {?Object} attributes Attributes to pass to the newly created block.
* @property {?Array<string>} attributesToCopy Attributes to be copied from adjecent blocks when inserted.
*/
export const getDirectInsertBlock = createSelector(
( state, rootClientId = null ) => {
if ( ! rootClientId ) {
return;
}
const defaultBlock =
state.blockListSettings[ rootClientId ]?.defaultBlock;
const directInsert =
state.blockListSettings[ rootClientId ]?.directInsert;
if ( ! defaultBlock || ! directInsert ) {
return;
}
if ( typeof directInsert === 'function' ) {
return directInsert( getBlock( state, rootClientId ) )
? defaultBlock
: null;
}
return defaultBlock;
},
( state, rootClientId ) => [
state.blockListSettings[ rootClientId ],
state.blocks.tree.get( rootClientId ),
]
);
export function getDirectInsertBlock( state, rootClientId = null ) {
if ( ! rootClientId ) {
return;
}
const { defaultBlock, directInsert } =
state.blockListSettings[ rootClientId ] ?? {};
if ( ! defaultBlock || ! directInsert ) {
return;
}

export const __experimentalGetDirectInsertBlock = createSelector(
( state, rootClientId = null ) => {
deprecated(
'wp.data.select( "core/block-editor" ).__experimentalGetDirectInsertBlock',
{
alternative:
'wp.data.select( "core/block-editor" ).getDirectInsertBlock',
since: '6.3',
version: '6.4',
}
);
return getDirectInsertBlock( state, rootClientId );
},
( state, rootClientId ) => [
state.blockListSettings[ rootClientId ],
state.blocks.tree.get( rootClientId ),
]
);
return defaultBlock;
}

export function __experimentalGetDirectInsertBlock(
state,
rootClientId = null
) {
deprecated(
'wp.data.select( "core/block-editor" ).__experimentalGetDirectInsertBlock',
{
alternative:
'wp.data.select( "core/block-editor" ).getDirectInsertBlock',
since: '6.3',
version: '6.4',
}
);
return getDirectInsertBlock( state, rootClientId );
}

export const __experimentalGetParsedPattern = createRegistrySelector(
( select ) =>
Expand Down
Loading