2023-09-14 08:24:46 +00:00
|
|
|
/* eslint-disable @woocommerce/dependency-group */
|
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { useEntityBlockEditor } from '@wordpress/core-data';
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { unlock } from '@wordpress/edit-site/build-module/lock-unlock';
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { store as editSiteStore } from '@wordpress/edit-site/build-module/store';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { BlockInstance } from '@wordpress/blocks';
|
|
|
|
|
2023-10-26 10:15:30 +00:00
|
|
|
export type ChangeHandler = (
|
2023-09-14 08:24:46 +00:00
|
|
|
blocks: BlockInstance[],
|
|
|
|
options: Record< string, unknown >
|
|
|
|
) => void;
|
|
|
|
|
|
|
|
// Note, must be used within BlockEditorProvider. This allows shared access of blocks currently
|
|
|
|
// being edited in the BlockEditor.
|
|
|
|
export const useEditorBlocks = (): [
|
|
|
|
BlockInstance[],
|
|
|
|
ChangeHandler,
|
|
|
|
ChangeHandler
|
|
|
|
] => {
|
|
|
|
const { templateType } = useSelect( ( select ) => {
|
|
|
|
const { getEditedPostType } = unlock( select( editSiteStore ) );
|
|
|
|
|
|
|
|
return {
|
|
|
|
templateType: getEditedPostType(),
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
// @ts-ignore Types are not up to date.
|
|
|
|
const [ blocks, onInput, onChange ]: [
|
2023-12-07 13:41:44 +00:00
|
|
|
BlockInstance[] | undefined,
|
2023-09-14 08:24:46 +00:00
|
|
|
ChangeHandler,
|
|
|
|
ChangeHandler
|
|
|
|
] = useEntityBlockEditor( 'postType', templateType );
|
|
|
|
|
2023-12-07 13:41:44 +00:00
|
|
|
return [ blocks ?? [], onInput, onChange ];
|
2023-09-14 08:24:46 +00:00
|
|
|
};
|