Skip to content

Commit

Permalink
Prefix meta key and add helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Sep 12, 2023
1 parent 2e549dc commit 96cfeb5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
7 changes: 2 additions & 5 deletions packages/block-library/src/table-of-contents/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { __ } from '@wordpress/i18n';
import icon from './icon';
import TableOfContentsList from './list';
import { linearToNestedHeadingList } from './utils';
import { useObserveHeadings } from './hooks';
import { getHeadingsFromMeta, useObserveHeadings } from './hooks';

/** @typedef {import('./utils').HeadingData} HeadingData */

Expand Down Expand Up @@ -69,10 +69,7 @@ export default function TableOfContentsEdit( {
const { replaceBlocks } = useDispatch( blockEditorStore );

const [ meta ] = useEntityProp( 'postType', postType, 'meta', postId );
const headings = meta?.table_of_contents
? JSON.parse( meta.table_of_contents )
: [];

const headings = getHeadingsFromMeta( meta );
const headingTree = linearToNestedHeadingList( headings );

const toolbarControls = canInsertList && (
Expand Down
16 changes: 12 additions & 4 deletions packages/block-library/src/table-of-contents/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
import { useEffect } from '@wordpress/element';
import { store as blockEditorStore } from '@wordpress/block-editor';

export const META_KEY = 'core_table_of_contents';

export function getHeadingsFromMeta( meta ) {
if ( ! meta?.[ META_KEY ] ) {
return [];
}

return JSON.parse( meta[ META_KEY ] );
}

function getLatestHeadings( select, clientId ) {
const {
getBlockAttributes,
Expand Down Expand Up @@ -112,9 +122,7 @@ function observeCallback( select, dispatch, context ) {
const { editEntityRecord } = dispatch( coreStore );

const meta = getEditedEntityRecord( 'postType', postType, postId ).meta;
const storedHeadings = meta?.table_of_contents
? JSON.parse( meta.table_of_contents )
: [];
const storedHeadings = getHeadingsFromMeta( meta );

const headings = getLatestHeadings( select, clientId );
if ( ! fastDeepEqual( headings, storedHeadings ) ) {
Expand All @@ -125,7 +133,7 @@ function observeCallback( select, dispatch, context ) {
{
meta: {
...meta,
table_of_contents: JSON.stringify( headings ),
[ META_KEY ]: JSON.stringify( headings ),
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/table-of-contents/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function register_block_core_table_of_contents() {

register_post_meta(
$post_type,
'table_of_contents',
'core_table_of_contents',
array(
'show_in_rest' => true,
'single' => true,
Expand Down Expand Up @@ -162,7 +162,7 @@ function render_block_core_table_of_contents( $attributes, $content, $block ) {
return '';
}

$headings = get_post_meta( $block->context['postId'], 'table_of_contents', true );
$headings = get_post_meta( $block->context['postId'], 'core_table_of_contents', true );

if ( ! $headings ) {
return '';
Expand Down
2 changes: 1 addition & 1 deletion phpunit/blocks/render-block-table-of-contents-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function wpSetUpBeforeClass() {

add_post_meta(
self::$page->ID,
'table_of_contents',
'core_table_of_contents',
'[{"content":"Heading text","level":2,"link":"#heading-text"},{"content":"A sub-heading","level":3,"link":"#a-sub-heading"}]',
true
);
Expand Down

0 comments on commit 96cfeb5

Please sign in to comment.