Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp-core-data should not depend on wp-block-editor #64604

Open
2 tasks done
picocodes opened this issue Aug 19, 2024 · 6 comments
Open
2 tasks done

wp-core-data should not depend on wp-block-editor #64604

picocodes opened this issue Aug 19, 2024 · 6 comments
Labels
[Package] Core data /packages/core-data [Type] Bug An existing feature does not function as intended

Comments

@picocodes
Copy link

Description

This is because wp-block-editor depends on lots of other scripts:- react, react-dom, react-jsx-runtime, wp-a11y, wp-api-fetch, wp-blob, wp-blocks, wp-commands, wp-components, wp-compose, wp-data, wp-date, wp-deprecated, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keyboard-shortcuts, wp-keycodes, wp-notices, wp-preferences, wp-primitives, wp-private-apis, wp-rich-text, wp-style-engine, wp-token-list, wp-url, wp-warning, wp-wordcount

Leading to loading dozens of scripts just to use wp-core-data.

Step-by-step reproduction instructions

Add wp-core-data as a script dependancy

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes
@picocodes picocodes added the [Type] Bug An existing feature does not function as intended label Aug 19, 2024
@up1512001 up1512001 added the [Package] Core data /packages/core-data label Aug 19, 2024
@Mamaduka
Copy link
Member

Mamaduka commented Aug 20, 2024

The same concern has been raised in the original PR - #51201 (comment).

cc @ellatrix

@Mamaduka
Copy link
Member

The behavior was reported again in #67334.

cc @jsnajdr, @youknowriad

@youknowriad
Copy link
Contributor

youknowriad commented Nov 27, 2024

Indeed, it seems like an arguable decision. If I'm reading the code properly, it seems the only reason we access it in "core-data" is because of the InnerBlocks.Content component? maybe some of this can be part of "blocks" package since this seems intrinsic to parsing/serializing blocks. cc @ellatrix

@gziolo
Copy link
Member

gziolo commented Nov 27, 2024

Re-sharing the comment from @samueljseay left in #67334 for visibility:

Recently on WooCommerce we decided to explore using entities to share some data between blocks. The data is some editable block headings we want to share between blocks.

To access this data in front-end we use the core data store, specifically we use getEditedEntityRecord. The issue with doing this, is that @wordpress/core-data depends on @wordpress/block-editor so when we do this basic query of some entity records front-end we're forced to load the entirety of the block editor.

It seems this dependency is only for unlocking a private API. The only other usage is in a test file.

I'm not sure what a good fix to this would be, but it would be hugely helpful if we could avoid depending on @wordpress/block-editor for this package.

@youknowriad
Copy link
Contributor

I think using @wordpress/core-data in the frontend is arguable though. I get that it's handy but I don't expect a huge need to handle entities / caching / edits in the frontend (maybe I'm wrong) and in that case, maybe a direct usage of api-fetch could be enough for WooCommerce's use-case.

@gziolo
Copy link
Member

gziolo commented Nov 27, 2024

I checked the codebase, and the only place where @wordpres/core-data depends on @wordpress/block editor is the following two lines:

const { getRichTextValues } = unlock( blockEditorPrivateApis );
const _content = getRichTextValues( _blocks ).join( '' ) || '';

If I'm reading the code properly, it seems the only reason we access it in "core-data" is because of the InnerBlocks.Content component? maybe some of this can be part of "blocks" package since this seems intrinsic to parsing/serializing blocks.

There is some processing of the content that collects the metadata required to update post meta for the Footnotes block. Since #43204 @wordpress/blocks depends on @wordpress/rich-text as it consumes RichTextData object when handling attribute types. That might be one of the paths forward.

More broadly, it would be great to understand why the general purpose React hook useEntityBlockEditor needs to know about footnotes in the first place? Can this handler be somehow injected? Looking at code:

const onChange = useCallback(
( newBlocks, options ) => {

const onInput = useCallback(
( newBlocks, options ) => {

and the same usage of the callback:

updateFootnotes( edits.blocks );

updateFootnotes( edits.blocks );

I'm wondering if the solution would be extending the options object with an optional callback, example for onInput:

const onInput = useCallback(
	( newBlocks, options ) => {
		const { onEdit, selection } = options;
		const edits = { blocks: newBlocks, selection };
		registry.batch( () => {
			onEdit?( newBlocks );
			editEntityRecord( kind, name, id, edits );
		} );
	},
	[ kind, name, id ]
);

where onEdit in the most simplistic case would look like:

function onEdit( blocks ) {
	updateFootnotes( blocks );
}

onInput could look very similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Core data /packages/core-data [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

5 participants