Skip to content

Commit

Permalink
Table of Contents: Try maintaining block example attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Sep 23, 2024
1 parent da16125 commit d01d240
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/block-library/src/table-of-contents/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function TableOfContentsEdit( {
clientId,
setAttributes,
} ) {
useObserveHeadings( clientId );
useObserveHeadings( clientId, headings );

const blockProps = useBlockProps();
const instanceId = useInstanceId(
Expand Down
28 changes: 20 additions & 8 deletions packages/block-library/src/table-of-contents/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import fastDeepEqual from 'fast-deep-equal/es6';
/**
* WordPress dependencies
*/
import { useRegistry } from '@wordpress/data';
import { store as blocksStore } from '@wordpress/blocks';
import { useRegistry, useSelect } from '@wordpress/data';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
import { useEffect } from '@wordpress/element';
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
Expand Down Expand Up @@ -144,13 +145,24 @@ function observeCallback( select, dispatch, clientId ) {
}
}

export function useObserveHeadings( clientId ) {
export function useObserveHeadings( clientId, headings ) {
const registry = useRegistry();
const blockType = useSelect(
( select ) =>
select( blocksStore ).getBlockType( 'core/table-of-contents' ),
[]
);
const isExample =
JSON.stringify( blockType.example.attributes.headings ) ===
JSON.stringify( headings );

useEffect( () => {
// Todo: Limit subscription to block editor store when data no longer depends on `getPermalink`.
// See: https://github.com/WordPress/gutenberg/pull/45513
return registry.subscribe( () =>
observeCallback( registry.select, registry.dispatch, clientId )
);
}, [ registry, clientId ] );
if ( ! isExample ) {
// Todo: Limit subscription to block editor store when data no longer depends on `getPermalink`.
// See: https://github.com/WordPress/gutenberg/pull/45513
return registry.subscribe( () =>
observeCallback( registry.select, registry.dispatch, clientId )
);
}
}, [ registry, clientId, isExample ] );
}
22 changes: 22 additions & 0 deletions packages/block-library/src/table-of-contents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ export const settings = {
icon,
edit,
save,
example: {
attributes: {
headings: [
{
content: 'Heading',
level: 2,
},
{
content: 'Subheading',
level: 3,
},
{
content: 'Heading',
level: 2,
},
{
content: 'Subheading',
level: 3,
},
],
},
},
};

export const init = () => initBlock( { name, metadata, settings } );

0 comments on commit d01d240

Please sign in to comment.