Skip to content

Commit

Permalink
Add per-block caching when extracting rich-text values
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Jul 12, 2023
1 parent 60d9dbc commit c81de29
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/core-data/src/footnotes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ const { getRichTextValues } = unlock( blockEditorPrivateApis );

let oldFootnotes = {};

const cache = new WeakMap();

function getRichTextValuesCached( block ) {
if ( ! cache.has( block ) ) {
const values = getRichTextValues( [ block ] );
cache.set( block, values );
}
return cache.get( block );
}

export function updateFootnotesFromMeta( blocks, meta ) {
const output = { blocks };
if ( ! meta ) return output;

// If meta.footnotes is empty, it means the meta is not registered.
if ( meta.footnotes === undefined ) return output;

const _content = getRichTextValues( blocks ).join( '' ) || '';
const _content = blocks.map( getRichTextValuesCached ).join( '' );

const newOrder = [];

// This can be avoided when
Expand Down

0 comments on commit c81de29

Please sign in to comment.