Skip to content

Commit

Permalink
getDirectInsertBlock: Remove 'directInsert' as a callback handler (#5…
Browse files Browse the repository at this point in the history
…9172)

* getDirectInsertBlock: Remove 'directInsert' as a callback handler
* No need to memoize selectors
* Add deprecation notice

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: noisysocks <[email protected]>
Co-authored-by: jsnajdr <[email protected]>
  • Loading branch information
4 people authored Feb 26, 2024
1 parent a5721d9 commit 7808dc7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 46 deletions.
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

0 comments on commit 7808dc7

Please sign in to comment.