From c81de297b028c5a9c54d5f698114d3819b0cf4a7 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca <150562+mcsf@users.noreply.github.com> Date: Wed, 12 Jul 2023 16:24:53 +0100 Subject: [PATCH] Add per-block caching when extracting rich-text values --- packages/core-data/src/footnotes/index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/core-data/src/footnotes/index.js b/packages/core-data/src/footnotes/index.js index 211755f5b42aa7..7a7ce5e58e9a8c 100644 --- a/packages/core-data/src/footnotes/index.js +++ b/packages/core-data/src/footnotes/index.js @@ -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