Skip to content

Commit

Permalink
Register meta
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jun 22, 2023
1 parent 1f71d81 commit f20c847
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 38 deletions.
22 changes: 22 additions & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
76 changes: 38 additions & 38 deletions packages/core-data/src/entity-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { unlock } from './private-apis';

const EMPTY_ARRAY = [];

let oldFootnotes = {};
const oldFootnotes = {};

/**
* Internal dependencies
Expand Down Expand Up @@ -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 ]
);
Expand Down

0 comments on commit f20c847

Please sign in to comment.