diff --git a/package-lock.json b/package-lock.json index 3084fd5f03440..04d6db2bfde6f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53871,7 +53871,6 @@ "dependencies": { "@babel/runtime": "7.25.7", "@wordpress/api-fetch": "*", - "@wordpress/block-editor": "*", "@wordpress/blocks": "*", "@wordpress/compose": "*", "@wordpress/data": "*", diff --git a/packages/core-data/README.md b/packages/core-data/README.md index 9549e6742d8cd..e93ea7d4defeb 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -988,6 +988,7 @@ _Parameters_ - _name_ `string`: The entity name. - _options_ `Object`: - _options.id_ `[string]`: An entity ID to use instead of the context-provided one. +- _options.onEditEntityRecord_ `[Function]`: A callback to run before the entity record gets edited. _Returns_ diff --git a/packages/core-data/package.json b/packages/core-data/package.json index fe428aeb87288..2750a54c40f2b 100644 --- a/packages/core-data/package.json +++ b/packages/core-data/package.json @@ -34,7 +34,6 @@ "dependencies": { "@babel/runtime": "7.25.7", "@wordpress/api-fetch": "*", - "@wordpress/block-editor": "*", "@wordpress/blocks": "*", "@wordpress/compose": "*", "@wordpress/data": "*", diff --git a/packages/core-data/src/hooks/use-entity-block-editor.js b/packages/core-data/src/hooks/use-entity-block-editor.js index df213898659e7..679d1993f6618 100644 --- a/packages/core-data/src/hooks/use-entity-block-editor.js +++ b/packages/core-data/src/hooks/use-entity-block-editor.js @@ -10,7 +10,6 @@ import { parse, __unstableSerializeAndClean } from '@wordpress/blocks'; */ import { STORE_NAME } from '../name'; import useEntityId from './use-entity-id'; -import { updateFootnotesFromMeta } from '../footnotes'; const EMPTY_ARRAY = []; const parsedBlocksCache = new WeakMap(); @@ -26,14 +25,22 @@ const parsedBlocksCache = new WeakMap(); * `BlockEditorProvider` and are intended to be used with it, * or similar components or hooks. * - * @param {string} kind The entity kind. - * @param {string} name The entity name. - * @param {Object} options - * @param {string} [options.id] An entity ID to use instead of the context-provided one. + * @param {string} kind The entity kind. + * @param {string} name The entity name. + * @param {Object} options + * @param {string} [options.id] An entity ID to use instead of the context-provided one. + * @param {Function} [options.onEditEntityRecord] A callback to run before the entity record gets edited. * * @return {[unknown[], Function, Function]} The block array and setters. */ -export default function useEntityBlockEditor( kind, name, { id: _id } = {} ) { +export default function useEntityBlockEditor( + kind, + name, + { + id: _id, + onEditEntityRecord = ( blocks, meta ) => ( { blocks, meta } ), + } = {} +) { const providerId = useEntityId( kind, name ); const id = _id ?? providerId; const { getEntityRecord, getEntityRecordEdits } = useSelect( STORE_NAME ); @@ -106,7 +113,7 @@ export default function useEntityBlockEditor( kind, name, { id: _id } = {} ) { selection, content: ( { blocks: blocksForSerialization = [] } ) => __unstableSerializeAndClean( blocksForSerialization ), - ...updateFootnotesFromMeta( newBlocks, meta ), + ...onEditEntityRecord( newBlocks, meta ), }; editEntityRecord( kind, name, id, edits, { @@ -122,21 +129,24 @@ export default function useEntityBlockEditor( kind, name, { id: _id } = {} ) { meta, __unstableCreateUndoLevel, editEntityRecord, + onEditEntityRecord, ] ); const onInput = useCallback( ( newBlocks, options ) => { const { selection, ...rest } = options; - const footnotesChanges = updateFootnotesFromMeta( newBlocks, meta ); - const edits = { selection, ...footnotesChanges }; + const edits = { + selection, + ...onEditEntityRecord( newBlocks, meta ), + }; editEntityRecord( kind, name, id, edits, { isCached: true, ...rest, } ); }, - [ kind, name, id, meta, editEntityRecord ] + [ kind, name, id, meta, editEntityRecord, onEditEntityRecord ] ); return [ blocks, onInput, onChange ]; diff --git a/packages/core-data/src/footnotes/get-footnotes-order.js b/packages/editor/src/components/provider/footnotes/get-footnotes-order.js similarity index 100% rename from packages/core-data/src/footnotes/get-footnotes-order.js rename to packages/editor/src/components/provider/footnotes/get-footnotes-order.js diff --git a/packages/core-data/src/footnotes/get-rich-text-values-cached.js b/packages/editor/src/components/provider/footnotes/get-rich-text-values-cached.js similarity index 95% rename from packages/core-data/src/footnotes/get-rich-text-values-cached.js rename to packages/editor/src/components/provider/footnotes/get-rich-text-values-cached.js index a5c2d25810861..4600b29f8d439 100644 --- a/packages/core-data/src/footnotes/get-rich-text-values-cached.js +++ b/packages/editor/src/components/provider/footnotes/get-rich-text-values-cached.js @@ -6,7 +6,7 @@ import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; /** * Internal dependencies */ -import { unlock } from '../lock-unlock'; +import { unlock } from '../../../lock-unlock'; // TODO: The following line should have been: // diff --git a/packages/core-data/src/footnotes/index.js b/packages/editor/src/components/provider/footnotes/index.js similarity index 100% rename from packages/core-data/src/footnotes/index.js rename to packages/editor/src/components/provider/footnotes/index.js diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index 68d7bd1d3f4f5..ef57f6fbd3c08 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -39,6 +39,7 @@ import EditorKeyboardShortcuts from '../global-keyboard-shortcuts'; import PatternRenameModal from '../pattern-rename-modal'; import PatternDuplicateModal from '../pattern-duplicate-modal'; import TemplatePartMenuItems from '../template-part-menu-items'; +import { updateFootnotesFromMeta } from './footnotes'; const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis ); const { PatternsMenuItems } = unlock( editPatternsPrivateApis ); @@ -77,7 +78,7 @@ function useBlockEditorProps( post, template, mode ) { const [ postBlocks, onInput, onChange ] = useEntityBlockEditor( 'postType', post.type, - { id: post.id } + { id: post.id, onEditEntityRecord: updateFootnotesFromMeta } ); const [ templateBlocks, onInputTemplate, onChangeTemplate ] = useEntityBlockEditor( 'postType', template?.type, { diff --git a/packages/core-data/src/test/entity-provider.js b/packages/editor/src/components/provider/test/entity-provider.js similarity index 96% rename from packages/core-data/src/test/entity-provider.js rename to packages/editor/src/components/provider/test/entity-provider.js index 4dc0d8a51663e..0a62bf8463bb6 100644 --- a/packages/core-data/src/test/entity-provider.js +++ b/packages/editor/src/components/provider/test/entity-provider.js @@ -13,6 +13,10 @@ import { getBlockTypes, } from '@wordpress/blocks'; import { RichText, useBlockProps } from '@wordpress/block-editor'; +import { + store as coreDataStore, + useEntityBlockEditor, +} from '@wordpress/core-data'; import { createRegistry, RegistryProvider } from '@wordpress/data'; import { registerCoreBlocks } from '@wordpress/block-library'; import { unregisterFormatType } from '@wordpress/rich-text'; @@ -20,8 +24,7 @@ import { unregisterFormatType } from '@wordpress/rich-text'; /** * Internal dependencies */ -import { store as coreDataStore } from '../index'; -import useEntityBlockEditor from '../hooks/use-entity-block-editor'; +import { updateFootnotesFromMeta } from '../footnotes'; const postTypeConfig = { kind: 'postType', @@ -154,6 +157,7 @@ describe( 'useEntityBlockEditor', () => { const TestComponent = () => { [ blocks, , onChange ] = useEntityBlockEditor( 'postType', 'post', { id: 1, + onEditEntityRecord: updateFootnotesFromMeta, } ); return
; @@ -229,6 +233,7 @@ describe( 'useEntityBlockEditor', () => { const TestComponent = () => { [ blocks, , onChange ] = useEntityBlockEditor( 'postType', 'post', { id: 1, + onEditEntityRecord: updateFootnotesFromMeta, } ); return
;