2021-09-21 10:18:27 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-12-14 15:54:59 +00:00
|
|
|
import { useRef, useEffect } from '@wordpress/element';
|
|
|
|
import { useRegistry, dispatch } from '@wordpress/data';
|
2021-09-21 10:18:27 +00:00
|
|
|
import {
|
|
|
|
createBlock,
|
|
|
|
getBlockType,
|
2021-11-22 12:45:48 +00:00
|
|
|
createBlocksFromInnerBlocksTemplate,
|
2023-03-30 14:15:00 +00:00
|
|
|
TemplateArray,
|
2021-09-21 10:18:27 +00:00
|
|
|
} from '@wordpress/blocks';
|
2023-03-30 14:15:00 +00:00
|
|
|
import { useEditorContext } from '@woocommerce/base-context';
|
2021-09-21 10:18:27 +00:00
|
|
|
|
2021-10-15 09:48:57 +00:00
|
|
|
/**
|
2023-03-30 14:15:00 +00:00
|
|
|
* Internal dependencies
|
2022-12-14 15:54:59 +00:00
|
|
|
*/
|
2023-03-30 14:15:00 +00:00
|
|
|
import { getMissingBlocks, findBlockPosition } from './utils';
|
2022-12-14 15:54:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook to ensure FORCED blocks are rendered in the correct place.
|
2021-10-15 09:48:57 +00:00
|
|
|
*/
|
2021-09-21 10:18:27 +00:00
|
|
|
export const useForcedLayout = ( {
|
|
|
|
clientId,
|
2021-10-15 09:48:57 +00:00
|
|
|
registeredBlocks,
|
|
|
|
defaultTemplate = [],
|
2021-09-21 10:18:27 +00:00
|
|
|
}: {
|
2021-10-15 09:48:57 +00:00
|
|
|
// Client ID of the parent block.
|
2021-09-21 10:18:27 +00:00
|
|
|
clientId: string;
|
2021-10-15 09:48:57 +00:00
|
|
|
// An array of registered blocks that may be forced in this particular layout.
|
|
|
|
registeredBlocks: Array< string >;
|
|
|
|
// The default template for the inner blocks in this layout.
|
|
|
|
defaultTemplate: TemplateArray;
|
2022-12-14 15:54:59 +00:00
|
|
|
} ) => {
|
2021-10-15 09:48:57 +00:00
|
|
|
const currentRegisteredBlocks = useRef( registeredBlocks );
|
|
|
|
const currentDefaultTemplate = useRef( defaultTemplate );
|
2022-12-14 15:54:59 +00:00
|
|
|
const registry = useRegistry();
|
2023-03-30 14:15:00 +00:00
|
|
|
const { isPreview } = useEditorContext();
|
|
|
|
|
2022-12-14 15:54:59 +00:00
|
|
|
useEffect( () => {
|
2023-01-27 10:45:49 +00:00
|
|
|
let templateSynced = false;
|
2023-03-30 14:15:00 +00:00
|
|
|
|
|
|
|
if ( isPreview ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:54:59 +00:00
|
|
|
const { replaceInnerBlocks } = dispatch( 'core/block-editor' );
|
2023-03-30 14:15:00 +00:00
|
|
|
|
2022-12-14 15:54:59 +00:00
|
|
|
return registry.subscribe( () => {
|
|
|
|
const innerBlocks = registry
|
|
|
|
.select( 'core/block-editor' )
|
|
|
|
.getBlocks( clientId );
|
2021-10-15 09:48:57 +00:00
|
|
|
|
2022-12-14 15:54:59 +00:00
|
|
|
// If there are NO inner blocks, sync with the given template.
|
|
|
|
if (
|
|
|
|
innerBlocks.length === 0 &&
|
2023-01-27 10:45:49 +00:00
|
|
|
currentDefaultTemplate.current.length > 0 &&
|
|
|
|
! templateSynced
|
2022-12-14 15:54:59 +00:00
|
|
|
) {
|
|
|
|
const nextBlocks = createBlocksFromInnerBlocksTemplate(
|
|
|
|
currentDefaultTemplate.current
|
|
|
|
);
|
2023-01-27 10:45:49 +00:00
|
|
|
if ( nextBlocks.length !== 0 ) {
|
|
|
|
templateSynced = true;
|
2022-12-14 15:54:59 +00:00
|
|
|
replaceInnerBlocks( clientId, nextBlocks );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-10-15 09:48:57 +00:00
|
|
|
|
2022-12-14 15:54:59 +00:00
|
|
|
const registeredBlockTypes = currentRegisteredBlocks.current.map(
|
|
|
|
( blockName: string ) => getBlockType( blockName )
|
|
|
|
);
|
2021-10-15 09:48:57 +00:00
|
|
|
|
2022-12-14 15:54:59 +00:00
|
|
|
const missingBlocks = getMissingBlocks(
|
|
|
|
innerBlocks,
|
|
|
|
registeredBlockTypes
|
2021-10-15 09:48:57 +00:00
|
|
|
);
|
|
|
|
|
2022-12-14 15:54:59 +00:00
|
|
|
if ( missingBlocks.length === 0 ) {
|
2021-10-15 09:48:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:54:59 +00:00
|
|
|
// Initially set as -1, so we can skip checking the position multiple times. Later on in the map callback,
|
|
|
|
// we check where the forced blocks should be inserted. This gets set to >= 0 if we find a missing block,
|
|
|
|
// so we know we can skip calculating it.
|
|
|
|
let insertAtPosition = -1;
|
|
|
|
const blockConfig = missingBlocks.map( ( block ) => {
|
|
|
|
const defaultTemplatePosition =
|
|
|
|
currentDefaultTemplate.current.findIndex(
|
|
|
|
( [ blockName ] ) => blockName === block.name
|
2021-10-15 09:48:57 +00:00
|
|
|
);
|
2022-12-14 15:54:59 +00:00
|
|
|
const createdBlock = createBlock( block.name );
|
|
|
|
|
|
|
|
// As mentioned above, if this is not -1, this is the first time we're calculating the position, if it's
|
|
|
|
// already been calculated we can skip doing so.
|
|
|
|
if ( insertAtPosition === -1 ) {
|
|
|
|
insertAtPosition = findBlockPosition( {
|
|
|
|
defaultTemplatePosition,
|
|
|
|
innerBlocks,
|
|
|
|
currentDefaultTemplate,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return createdBlock;
|
|
|
|
} );
|
|
|
|
|
|
|
|
registry.batch( () => {
|
|
|
|
registry
|
|
|
|
.dispatch( 'core/block-editor' )
|
|
|
|
.insertBlocks( blockConfig, insertAtPosition, clientId );
|
|
|
|
} );
|
2023-01-27 10:45:49 +00:00
|
|
|
}, 'core/block-editor' );
|
2023-03-30 14:15:00 +00:00
|
|
|
}, [ clientId, isPreview, registry ] );
|
2021-09-21 10:18:27 +00:00
|
|
|
};
|