Skip to content

Commit

Permalink
Core Data: Remove dependencies on the block editor package
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Dec 6, 2024
1 parent 32cbb04 commit 8764ccf
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 16 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_

Expand Down
1 change: 0 additions & 1 deletion packages/core-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"dependencies": {
"@babel/runtime": "7.25.7",
"@wordpress/api-fetch": "*",
"@wordpress/block-editor": "*",
"@wordpress/blocks": "*",
"@wordpress/compose": "*",
"@wordpress/data": "*",
Expand Down
30 changes: 20 additions & 10 deletions packages/core-data/src/hooks/use-entity-block-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 );
Expand Down Expand Up @@ -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, {
Expand All @@ -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 ];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ 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';

/**
* Internal dependencies
*/
import { store as coreDataStore } from '../index';
import useEntityBlockEditor from '../hooks/use-entity-block-editor';
import { updateFootnotesFromMeta } from '../footnotes';

const postTypeConfig = {
kind: 'postType',
Expand Down Expand Up @@ -154,6 +157,7 @@ describe( 'useEntityBlockEditor', () => {
const TestComponent = () => {
[ blocks, , onChange ] = useEntityBlockEditor( 'postType', 'post', {
id: 1,
onEditEntityRecord: updateFootnotesFromMeta,
} );

return <div />;
Expand Down Expand Up @@ -229,6 +233,7 @@ describe( 'useEntityBlockEditor', () => {
const TestComponent = () => {
[ blocks, , onChange ] = useEntityBlockEditor( 'postType', 'post', {
id: 1,
onEditEntityRecord: updateFootnotesFromMeta,
} );

return <div />;
Expand Down

0 comments on commit 8764ccf

Please sign in to comment.