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

Stabilize defaultBlock, directInsert API's and getDirectInsertBlock selector #52083

Merged
merged 1 commit into from
Jul 13, 2023
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
23 changes: 23 additions & 0 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,29 @@ _Returns_

- `Array`: ids of top-level and descendant blocks.

### getDirectInsertBlock

Returns the block to be directly inserted by the block appender.

_Parameters_

- _state_ `Object`: Editor state.
- _rootClientId_ `?string`: Optional root client ID of block list.

_Returns_

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

_Type Definition_

- _WPDirectInsertBlock_ `Object`

_Properties_

- _name_ `string`: The type of block.
- _attributes_ `?Object`: Attributes to pass to the newly created block.
- _attributesToCopy_ `?Array<string>`: Attributes to be copied from adjecent blocks when inserted.

### getDraggedBlockClientIds

Returns the client ids of any blocks being directly dragged.
Expand Down
10 changes: 10 additions & 0 deletions packages/block-editor/src/components/inner-blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,13 @@ For example, a button block, deeply nested in several levels of block `X` that u

- **Type:** `Array`
- **Default:** - `undefined`. Determines which block types should be shown in the block inserter. For example, when inserting a block within the Navigation block we specify `core/navigation-link` and `core/navigation-link/page` as these are the most commonly used inner blocks. `prioritizedInserterBlocks` takes an array of the form {blockName}/{variationName}, where {variationName} is optional.

### `defaultBlock`

- **Type:** `Array`
- **Default:** - `undefined`. Determines which block type should be inserted by default and any attributes that should be set by default when the block is inserted. Takes an array in the form of `[ blockname, {blockAttributes} ]`.

### `directInsert`

- **Type:** `Boolean`
- **Default:** - `undefined`. Determines whether the default block should be inserted directly into the InnerBlocks area by the block appender.
4 changes: 4 additions & 0 deletions packages/block-editor/src/components/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ function UncontrolledInnerBlocks( props ) {
clientId,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
directInsert,
__experimentalDefaultBlock,
__experimentalDirectInsert,
template,
Expand All @@ -64,6 +66,8 @@ function UncontrolledInnerBlocks( props ) {
clientId,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
directInsert,
__experimentalDefaultBlock,
__experimentalDirectInsert,
templateLock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ function UncontrolledInnerBlocks( props ) {
clientId,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
directInsert,
__experimentalDefaultBlock,
__experimentalDirectInsert,
template,
Expand Down Expand Up @@ -103,6 +105,8 @@ function UncontrolledInnerBlocks( props ) {
clientId,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
directInsert,
__experimentalDefaultBlock,
__experimentalDirectInsert,
templateLock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { useLayoutEffect, useMemo } from '@wordpress/element';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
Expand All @@ -25,9 +26,13 @@ const pendingSettingsUpdates = new WeakMap();
* @param {string[]} allowedBlocks An array of block names which are permitted
* 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} __experimentalDefaultBlock The default block to insert: [ blockName, { blockAttributes } ].
* @param {?Function|boolean} __experimentalDirectInsert If a default block should be inserted directly by the
* appender.
* @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 {?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 {string} [templateLock] The template lock specified for the inner
* blocks component. (e.g. "all")
* @param {boolean} captureToolbars Whether or children toolbars should be shown
Expand All @@ -41,6 +46,8 @@ export default function useNestedSettingsUpdate(
clientId,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
directInsert,
__experimentalDefaultBlock,
__experimentalDirectInsert,
templateLock,
Expand Down Expand Up @@ -108,11 +115,29 @@ export default function useNestedSettingsUpdate(
}

if ( __experimentalDefaultBlock !== undefined ) {
newSettings.__experimentalDefaultBlock = __experimentalDefaultBlock;
deprecated( '__experimentalDefaultBlock', {
alternative: 'defaultBlock',
since: '6.3',
version: '6.4',
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey, should we follow up on these deprecations and act on them (remove or extend) as we've reached the version.

} );
newSettings.defaultBlock = __experimentalDefaultBlock;
}

if ( defaultBlock !== undefined ) {
newSettings.defaultBlock = defaultBlock;
}

if ( __experimentalDirectInsert !== undefined ) {
newSettings.__experimentalDirectInsert = __experimentalDirectInsert;
deprecated( '__experimentalDirectInsert', {
alternative: 'directInsert',
since: '6.3',
version: '6.4',
} );
newSettings.directInsert = __experimentalDirectInsert;
}

if ( directInsert !== undefined ) {
newSettings.directInsert = directInsert;
}

// Batch updates to block list settings to avoid triggering cascading renders
Expand Down Expand Up @@ -144,6 +169,8 @@ export default function useNestedSettingsUpdate(
_allowedBlocks,
_prioritizedInserterBlocks,
_templateLock,
defaultBlock,
directInsert,
__experimentalDefaultBlock,
__experimentalDirectInsert,
captureToolbars,
Expand Down
5 changes: 2 additions & 3 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export const ComposedPrivateInserter = compose( [
getBlockRootClientId,
hasInserterItems,
getAllowedBlocks,
__experimentalGetDirectInsertBlock,
getDirectInsertBlock,
getSettings,
} = select( blockEditorStore );

Expand All @@ -243,8 +243,7 @@ export const ComposedPrivateInserter = compose( [
const allowedBlocks = getAllowedBlocks( rootClientId );

const directInsertBlock =
shouldDirectInsert &&
__experimentalGetDirectInsertBlock( rootClientId );
shouldDirectInsert && getDirectInsertBlock( rootClientId );

const settings = getSettings();

Expand Down
25 changes: 22 additions & 3 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2256,15 +2256,15 @@ export const __experimentalGetAllowedBlocks = createSelector(
* @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 __experimentalGetDirectInsertBlock = createSelector(
export const getDirectInsertBlock = createSelector(
( state, rootClientId = null ) => {
if ( ! rootClientId ) {
return;
}
const defaultBlock =
state.blockListSettings[ rootClientId ]?.__experimentalDefaultBlock;
state.blockListSettings[ rootClientId ]?.defaultBlock;
const directInsert =
state.blockListSettings[ rootClientId ]?.__experimentalDirectInsert;
state.blockListSettings[ rootClientId ]?.directInsert;
if ( ! defaultBlock || ! directInsert ) {
return;
}
Expand All @@ -2281,6 +2281,25 @@ export const __experimentalGetDirectInsertBlock = createSelector(
]
);

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 ),
]
);

const checkAllowListRecursive = ( blocks, allowedBlockTypes ) => {
if ( typeof allowedBlockTypes === 'boolean' ) {
return allowedBlockTypes;
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/buttons/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ function ButtonsEdit( { attributes, className } ) {

const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: ALLOWED_BLOCKS,
__experimentalDefaultBlock: DEFAULT_BLOCK,
__experimentalDirectInsert: true,
defaultBlock: DEFAULT_BLOCK,
directInsert: true,
template: [
[
buttonBlockName,
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ export default function NavigationLinkEdit( {
},
{
allowedBlocks: ALLOWED_BLOCKS,
__experimentalDefaultBlock: DEFAULT_BLOCK,
__experimentalDirectInsert: true,
defaultBlock: DEFAULT_BLOCK,
directInsert: true,
renderAppender: false,
}
);
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/navigation-submenu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ export default function NavigationSubmenuEdit( {
getNavigationChildBlockProps( innerBlocksColors );
const innerBlocksProps = useInnerBlocksProps( navigationChildBlockProps, {
allowedBlocks,
__experimentalDefaultBlock: DEFAULT_BLOCK,
__experimentalDirectInsert: true,
defaultBlock: DEFAULT_BLOCK,
directInsert: true,

// Ensure block toolbar is not too far removed from item
// being edited.
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/navigation/edit/inner-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export default function NavigationInnerBlocks( {
onChange,
allowedBlocks: ALLOWED_BLOCKS,
prioritizedInserterBlocks: PRIORITIZED_INSERTER_BLOCKS,
__experimentalDefaultBlock: DEFAULT_BLOCK,
__experimentalDirectInsert: shouldDirectInsert,
defaultBlock: DEFAULT_BLOCK,
directInsert: shouldDirectInsert,
orientation,
templateLock,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export default function UnsavedInnerBlocks( {
{
renderAppender: hasSelection ? undefined : false,
allowedBlocks: ALLOWED_BLOCKS,
__experimentalDefaultBlock: DEFAULT_BLOCK,
__experimentalDirectInsert: shouldDirectInsert,
defaultBlock: DEFAULT_BLOCK,
directInsert: shouldDirectInsert,
}
);

Expand Down