Skip to content

Commit 1291719

Browse files
gmovrgmovrswissspidyMamadukamrfoxtalbot
authored
Feature: Adding support for more granular controls over the ToC block (#69063)
Unlinked contributors: jodamo5. Co-authored-by: gmovr <etobiesen@git.wordpress.org> Co-authored-by: swissspidy <swissspidy@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: mrfoxtalbot <mrfoxtalbot@git.wordpress.org> Co-authored-by: juanmaguitar <juanmaguitar@git.wordpress.org> Co-authored-by: JosVelasco <josvelasco@git.wordpress.org> Co-authored-by: jasmussen <joen@git.wordpress.org>
1 parent bdb1679 commit 1291719

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

docs/reference-guides/core-blocks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ Summarize your post with a list of headings. Add HTML anchors to Heading blocks
914914
- **Experimental:** true
915915
- **Category:** design
916916
- **Supports:** color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
917-
- **Attributes:** headings, onlyIncludeCurrentPage
917+
- **Attributes:** headings, maxLevel, onlyIncludeCurrentPage
918918

919919
## Tag Cloud
920920

packages/block-library/src/table-of-contents/block.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"onlyIncludeCurrentPage": {
2020
"type": "boolean",
2121
"default": false
22+
},
23+
"maxLevel": {
24+
"type": "number"
2225
}
2326
},
2427
"supports": {

packages/block-library/src/table-of-contents/edit.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { createBlock } from '@wordpress/blocks';
1212
import {
1313
Placeholder,
1414
ToggleControl,
15+
SelectControl,
1516
ToolbarButton,
1617
ToolbarGroup,
1718
__experimentalToolsPanel as ToolsPanel,
@@ -39,15 +40,16 @@ import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
3940
*
4041
* @param {Object} props The props.
4142
* @param {Object} props.attributes The block attributes.
42-
* @param {HeadingData[]} props.attributes.headings A list of data for each heading in the post.
43+
* @param {HeadingData[]} props.attributes.headings The list of data for each heading in the post.
4344
* @param {boolean} props.attributes.onlyIncludeCurrentPage Whether to only include headings from the current page (if the post is paginated).
44-
* @param {string} props.clientId
45-
* @param {(attributes: Object) => void} props.setAttributes
45+
* @param {number|undefined} props.attributes.maxLevel The maximum heading level to include, or null to include all levels.
46+
* @param {string} props.clientId The client id.
47+
* @param {(attributes: Object) => void} props.setAttributes The set attributes function.
4648
*
4749
* @return {Component} The component.
4850
*/
4951
export default function TableOfContentsEdit( {
50-
attributes: { headings = [], onlyIncludeCurrentPage },
52+
attributes: { headings = [], onlyIncludeCurrentPage, maxLevel },
5153
clientId,
5254
setAttributes,
5355
} ) {
@@ -115,6 +117,7 @@ export default function TableOfContentsEdit( {
115117
resetAll={ () => {
116118
setAttributes( {
117119
onlyIncludeCurrentPage: false,
120+
maxLevel: undefined,
118121
} );
119122
} }
120123
dropdownMenuProps={ dropdownMenuProps }
@@ -145,6 +148,44 @@ export default function TableOfContentsEdit( {
145148
}
146149
/>
147150
</ToolsPanelItem>
151+
<ToolsPanelItem
152+
hasValue={ () => !! maxLevel }
153+
label={ __( 'Limit heading levels' ) }
154+
onDeselect={ () =>
155+
setAttributes( { maxLevel: undefined } )
156+
}
157+
isShownByDefault
158+
>
159+
<SelectControl
160+
__nextHasNoMarginBottom
161+
__next40pxDefaultSize
162+
label={ __( 'Include headings down to level' ) }
163+
value={ maxLevel || '' }
164+
options={ [
165+
{ value: '', label: __( 'All levels' ) },
166+
{ value: '1', label: __( 'Heading 1' ) },
167+
{ value: '2', label: __( 'Heading 2' ) },
168+
{ value: '3', label: __( 'Heading 3' ) },
169+
{ value: '4', label: __( 'Heading 4' ) },
170+
{ value: '5', label: __( 'Heading 5' ) },
171+
{ value: '6', label: __( 'Heading 6' ) },
172+
] }
173+
onChange={ ( value ) =>
174+
setAttributes( {
175+
maxLevel: value ? parseInt( value ) : undefined,
176+
} )
177+
}
178+
help={
179+
maxLevel
180+
? __(
181+
'Including all heading levels in the table of contents.'
182+
)
183+
: __(
184+
'Only include headings up to and including this level.'
185+
)
186+
}
187+
/>
188+
</ToolsPanelItem>
148189
</ToolsPanel>
149190
</InspectorControls>
150191
);

packages/block-library/src/table-of-contents/hooks.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ function getLatestHeadings( select, clientId ) {
2929
const permalink = select( 'core/editor' ).getPermalink() ?? null;
3030

3131
const isPaginated = getBlocksByName( 'core/nextpage' ).length !== 0;
32-
const { onlyIncludeCurrentPage } = getBlockAttributes( clientId ) ?? {};
32+
const { onlyIncludeCurrentPage, maxLevel } =
33+
getBlockAttributes( clientId ) ?? {};
3334

3435
// Get post-content block client ID.
3536
const [ postContentClientId = '' ] = getBlocksByName( 'core/post-content' );
@@ -100,6 +101,11 @@ function getLatestHeadings( select, clientId ) {
100101
if ( blockName === 'core/heading' ) {
101102
const headingAttributes = getBlockAttributes( blockClientId );
102103

104+
// Skip headings that are deeper than maxLevel
105+
if ( maxLevel && headingAttributes.level > maxLevel ) {
106+
continue;
107+
}
108+
103109
const canBeLinked =
104110
typeof headingPageLink === 'string' &&
105111
typeof headingAttributes.anchor === 'string' &&

0 commit comments

Comments
 (0)