Skip to content

Commit

Permalink
createBlock doesn't need to be sync after all, if block attributes ar…
Browse files Browse the repository at this point in the history
…e bootstrapped
  • Loading branch information
jsnajdr committed Aug 17, 2023
1 parent b6057a4 commit 4a22a8f
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 61 deletions.
4 changes: 2 additions & 2 deletions packages/block-editor/src/autocompleters/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ function createBlockCompleter() {
? await parse( content, {
__unstableSkipMigrationLogs: true,
} )
: await createBlock(
: createBlock(
name,
initialAttributes,
await createBlocksFromInnerBlocksTemplate(
createBlocksFromInnerBlocksTemplate(
innerBlocks
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getBlockFromExample,
store as blocksStore,
} from '@wordpress/blocks';
import { useEffect, useState } from '@wordpress/element';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -23,19 +23,14 @@ import { store as blockEditorStore } from '../../store';
* @return {WPBlock} A generic block ready for styles preview.
*/
function useGenericPreviewBlock( block, type ) {
const [ example, setExample ] = useState( null );
useEffect( () => {
return useMemo( () => {
if ( type && type.blockName && type.example ) {
getBlockFromExample( type.blockName, type.example ).then(
setExample
);
return getBlockFromExample( type.blockName, type.example );
}
if ( block ) {
setExample( cloneBlock( block ) );
return cloneBlock( block );
}
}, [ type, type?.example ? block?.name : block ] );

return example;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const useBlockTypesState = ( rootClientId, onInsert ) => {
? await parse( item.content, {
__unstableSkipMigrationLogs: true,
} )
: await createBlock(
: createBlock(
item.name,
item.initialAttributes,
createBlocksFromInnerBlocksTemplate(
Expand Down
4 changes: 1 addition & 3 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
getBlockType,
getDefaultBlockName,
hasBlockSupport,
loadBlockType,
switchToBlockType,
synchronizeBlocksWithTemplate,
getBlockSupport,
Expand Down Expand Up @@ -1359,14 +1358,13 @@ export function selectionChange(
*/
export const insertDefaultBlock =
( attributes, rootClientId, index ) =>
async ( { dispatch } ) => {
( { dispatch } ) => {
// Abort if there is no default block type (if it has been unregistered).
const defaultBlockName = getDefaultBlockName();
if ( ! defaultBlockName ) {
return;
}

await loadBlockType( defaultBlockName );
const block = createBlock( defaultBlockName, attributes );

return dispatch.insertBlock( block, index, rootClientId );
Expand Down
47 changes: 22 additions & 25 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { createBlock, loadBlockType } from '@wordpress/blocks';
import { createBlock } from '@wordpress/blocks';
import {
InspectorControls,
BlockControls,
Expand Down Expand Up @@ -113,32 +113,29 @@ function BlockContent( {
}
}

async function getBlockList( parentId, pagesByParentId ) {
function getBlockList( parentId, pagesByParentId ) {
const childPages = pagesByParentId.get( parentId );

if ( ! childPages?.length ) {
return [];
}

return Promise.all(
childPages.map( async ( page ) => {
const hasChildren = pagesByParentId.has( page.id );
const pageProps = {
id: page.id,
label:
// translators: displayed when a page has an empty title.
page.title?.rendered?.trim() !== ''
? page.title?.rendered
: __( '(no title)' ),
title: page.title?.rendered,
link: page.url,
hasChildren,
};
const children = await getBlockList( page.id, pagesByParentId );
await loadBlockType( 'core/page-list-item' );
return createBlock( 'core/page-list-item', pageProps, children );
} )
);
return childPages.map( ( page ) => {
const hasChildren = pagesByParentId.has( page.id );
const pageProps = {
id: page.id,
label:
// translators: displayed when a page has an empty title.
page.title?.rendered?.trim() !== ''
? page.title?.rendered
: __( '(no title)' ),
title: page.title?.rendered,
link: page.url,
hasChildren,
};
const children = getBlockList( page.id, pagesByParentId );
return createBlock( 'core/page-list-item', pageProps, children );
} );
}

export default function PageListEdit( {
Expand Down Expand Up @@ -236,10 +233,10 @@ export default function PageListEdit( {
[ pagesByParentId ]
);

const [ blockList, setBlockList ] = useState( null );
useEffect( () => {
getBlockList( parentPageID, pagesByParentId ).then( setBlockList );
}, [ pagesByParentId, parentPageID ] );
const blockList = useMemo(
() => getBlockList( parentPageID, pagesByParentId ),
[ parentPageID, pagesByParentId ]
);

const {
isNested,
Expand Down
11 changes: 4 additions & 7 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,16 +589,13 @@ export async function switchToBlockType( blocks, name ) {
*
* @return {Object} block.
*/
export const getBlockFromExample = async ( name, example ) => {
export function getBlockFromExample( name, example ) {
let innerBlocks = [];
if ( example.innerBlocks ) {
innerBlocks = await Promise.all(
example.innerBlocks.map( ( innerBlock ) =>
getBlockFromExample( innerBlock.name, innerBlock )
)
innerBlocks = example.innerBlocks.map( ( innerBlock ) =>
getBlockFromExample( innerBlock.name, innerBlock )
);
}

await loadBlockType( name );
return createBlock( name, example.attributes, innerBlocks );
};
}
2 changes: 0 additions & 2 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ export {
setGroupingBlockName,
getGroupingBlockName,
getBlockType,
loadBlockType,
getBlockTypes,
loadBlockTypes,
getBlockSupport,
hasBlockSupport,
getBlockVariations,
Expand Down
4 changes: 4 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ export function getBlockType( name ) {
return select( blocksStore ).getBlockType( name );
}

export function getBootstrappedBlockType( name ) {
return select( blocksStore ).getBootstrappedBlockType( name );
}

export function loadBlockType( name ) {
return resolveSelect( blocksStore ).getBlockType( name );
}
Expand Down
12 changes: 8 additions & 4 deletions packages/blocks/src/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
* Internal dependencies
*/
import { BLOCK_ICON_DEFAULT } from './constants';
import { getBlockType, getDefaultBlockName } from './registration';
import {
getBlockType,
getBootstrappedBlockType,
getDefaultBlockName,
} from './registration';
import { createBlock } from './factory';

extend( [ namesPlugin, a11yPlugin ] );
Expand Down Expand Up @@ -45,9 +49,9 @@ export function isUnmodifiedBlock( block ) {
}

const newBlock = isUnmodifiedBlock[ block.name ];
const blockType = getBlockType( block.name );
const blockType = getBootstrappedBlockType( block.name );

return Object.keys( blockType?.attributes ?? {} ).every(
return Object.keys( blockType.attributes ?? {} ).every(
( key ) => newBlock.attributes[ key ] === block.attributes[ key ]
);
}
Expand Down Expand Up @@ -253,7 +257,7 @@ export function getAccessibleBlockLabel(
*/
export function __experimentalSanitizeBlockAttributes( name, attributes ) {
// Get the type definition associated with a registered block.
const blockType = getBlockType( name );
const blockType = getBootstrappedBlockType( name );

if ( undefined === blockType ) {
throw new Error( `Block type '${ name }' is not registered.` );
Expand Down
5 changes: 2 additions & 3 deletions packages/e2e-tests/specs/performance/post-editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ async function loadHtmlIntoTheBlockEditor( html ) {
}

async function load1000Paragraphs() {
await page.evaluate( async () => {
const { loadBlockType, createBlock } = window.wp.blocks;
await page.evaluate( () => {
const { createBlock } = window.wp.blocks;
const { dispatch } = window.wp.data;
await loadBlockType?.( 'core/paragraph' );
const blocks = Array.from( { length: 1000 } ).map( () =>
createBlock( 'core/paragraph' )
);
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/style-book/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { __, sprintf } from '@wordpress/i18n';
import {
getCategories,
getBlockTypes,
getBootstrappedBlockTypes,
getBlockFromExample,
createBlock,
} from '@wordpress/blocks';
Expand Down Expand Up @@ -144,7 +144,7 @@ function getExamples() {
],
};

const otherExamples = getBlockTypes()
const otherExamples = getBootstrappedBlockTypes()
.filter( ( blockType ) => {
const { name, example, supports } = blockType;
return (
Expand Down
5 changes: 2 additions & 3 deletions test/performance/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,9 @@ export async function loadBlocksFromHtml( page, filepath ) {
}

export async function load1000Paragraphs( page ) {
await page.evaluate( async () => {
const { loadBlockType, createBlock } = window.wp.blocks;
await page.evaluate( () => {
const { createBlock } = window.wp.blocks;
const { dispatch } = window.wp.data;
await loadBlockType?.( 'core/paragraph' );
const blocks = Array.from( { length: 1000 } ).map( () =>
createBlock( 'core/paragraph' )
);
Expand Down

0 comments on commit 4a22a8f

Please sign in to comment.