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

Site editor: avoid double post content parse (alternative) #58146

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/core-data/src/entity-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export function useEntityProp( kind, name, prop, _id ) {
return [ value, setValue, fullValue ];
}

const parsedBlocksCache = new WeakMap();

/**
* Hook that returns block content getters and setters for
* the nearest provided entity of the specified type.
Expand All @@ -153,6 +155,7 @@ export function useEntityProp( kind, name, prop, _id ) {
export function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
const providerId = useEntityId( kind, name );
const id = _id ?? providerId;
const { getEntityRecord } = useSelect( STORE_NAME );
const { content, editedBlocks, meta } = useSelect(
( select ) => {
if ( ! id ) {
Expand Down Expand Up @@ -180,10 +183,20 @@ export function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
return editedBlocks;
}

return content && typeof content !== 'function'
? parse( content )
: EMPTY_ARRAY;
}, [ id, editedBlocks, content ] );
if ( ! content || typeof content === 'function' ) {
return EMPTY_ARRAY;
}

const entityRecord = getEntityRecord( kind, name, id );
let _blocks = parsedBlocksCache.get( entityRecord );

if ( ! _blocks ) {
_blocks = parse( content );
parsedBlocksCache.set( entityRecord, _blocks );
}

return _blocks;
}, [ kind, name, id, editedBlocks, content, getEntityRecord ] );

const updateFootnotes = useCallback(
( _blocks ) => updateFootnotesFromMeta( _blocks, meta ),
Expand Down
Loading