From f20c8478ee51f530f403adbcb78c52dca0865272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Thu, 22 Jun 2023 19:34:06 +0300 Subject: [PATCH] Register meta --- lib/client-assets.php | 22 +++++++ packages/core-data/src/entity-provider.js | 76 +++++++++++------------ 2 files changed, 60 insertions(+), 38 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 9757e4b7ff24a..57a762455558d 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -569,3 +569,25 @@ function gutenberg_register_vendor_scripts( $scripts ) { // Enqueue stored styles. add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' ); add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 ); + +/** + * Registers the `core/footnotes` block on the server. + */ +function register_block_core_footnotes() { + register_post_meta( + 'post', + 'footnotes', + array( + 'show_in_rest' => true, + 'single' => true, + 'type' => 'string', + ) + ); + register_block_type_from_metadata( + __DIR__ . '/footnotes', + array( + 'render_callback' => 'render_block_core_footnotes', + ) + ); +} +add_action( 'init', 'register_block_core_footnotes' ); diff --git a/packages/core-data/src/entity-provider.js b/packages/core-data/src/entity-provider.js index 04bb4c21433e3..96402a5dae57a 100644 --- a/packages/core-data/src/entity-provider.js +++ b/packages/core-data/src/entity-provider.js @@ -21,7 +21,7 @@ import { unlock } from './private-apis'; const EMPTY_ARRAY = []; -let oldFootnotes = {}; +const oldFootnotes = {}; /** * Internal dependencies @@ -198,49 +198,49 @@ export function useEntityBlockEditor( kind, name, { id: _id } = {} ) { const { getRichTextValues } = unlock( blockEditorPrivateApis ); const _content = getRichTextValues( _blocks ).join( '' ) || ''; - const newOrder = []; + // const newOrder = []; - // This can be avoided when - // https://github.com/WordPress/gutenberg/pull/43204 lands. We can then - // get the order directly from the rich text values. - if ( _content.indexOf( 'data-fn' ) !== -1 ) { - const regex = /data-fn="([^"]+)"/g; - let match; - while ( ( match = regex.exec( _content ) ) !== null ) { - newOrder.push( match[ 1 ] ); - } - } + // // This can be avoided when + // // https://github.com/WordPress/gutenberg/pull/43204 lands. We can then + // // get the order directly from the rich text values. + // if ( _content.indexOf( 'data-fn' ) !== -1 ) { + // const regex = /data-fn="([^"]+)"/g; + // let match; + // while ( ( match = regex.exec( _content ) ) !== null ) { + // newOrder.push( match[ 1 ] ); + // } + // } - const footnotes = meta.footnotes - ? JSON.parse( meta.footnotes ) - : []; - const currentOrder = footnotes.map( ( fn ) => fn.id ); + // const footnotes = meta.footnotes + // ? JSON.parse( meta.footnotes ) + // : []; + // const currentOrder = footnotes.map( ( fn ) => fn.id ); - if ( currentOrder.join( '' ) === newOrder.join( '' ) ) return; + // if ( currentOrder.join( '' ) === newOrder.join( '' ) ) return; - const newFootnotes = newOrder.map( - ( fnId ) => - footnotes.find( ( fn ) => fn.id === fnId ) || - oldFootnotes[ fnId ] || { - id: fnId, - content: '', - } - ); + // const newFootnotes = newOrder.map( + // ( fnId ) => + // footnotes.find( ( fn ) => fn.id === fnId ) || + // oldFootnotes[ fnId ] || { + // id: fnId, + // content: '', + // } + // ); - oldFootnotes = { - ...oldFootnotes, - ...footnotes.reduce( ( acc, fn ) => { - if ( ! newOrder.includes( fn.id ) ) { - acc[ fn.id ] = fn; - } - return acc; - }, {} ), - }; + // oldFootnotes = { + // ...oldFootnotes, + // ...footnotes.reduce( ( acc, fn ) => { + // if ( ! newOrder.includes( fn.id ) ) { + // acc[ fn.id ] = fn; + // } + // return acc; + // }, {} ), + // }; - updateMeta( { - ...meta, - footnotes: JSON.stringify( newFootnotes ), - } ); + // updateMeta( { + // ...meta, + // footnotes: JSON.stringify( newFootnotes ), + // } ); }, [ meta, updateMeta ] );