Skip to content

Commit b8808b7

Browse files
mikachanMaggieCabrera
andauthored
Table of Contents: Update toolbar controls (WordPress#71587)
* Make list type control consistent with List block * Update test fixtures Co-authored-by: mikachan <[email protected]> Co-authored-by: MaggieCabrera <[email protected]>
1 parent bc8fd77 commit b8808b7

File tree

7 files changed

+81
-29
lines changed

7 files changed

+81
-29
lines changed

docs/reference-guides/core-blocks.md

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

963963
## Tag Cloud
964964

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
},
2323
"maxLevel": {
2424
"type": "number"
25+
},
26+
"ordered": {
27+
"type": "boolean",
28+
"default": true
2529
}
2630
},
2731
"supports": {

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

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ import {
2020
} from '@wordpress/components';
2121
import { useDispatch, useSelect } from '@wordpress/data';
2222
import { renderToString } from '@wordpress/element';
23-
import { __ } from '@wordpress/i18n';
23+
import { __, isRTL } from '@wordpress/i18n';
2424
import { useInstanceId } from '@wordpress/compose';
2525
import { store as noticeStore } from '@wordpress/notices';
26-
import { tableOfContents as icon } from '@wordpress/icons';
26+
import {
27+
tableOfContents as icon,
28+
formatListBullets,
29+
formatListBulletsRTL,
30+
formatListNumbered,
31+
formatListNumberedRTL,
32+
} from '@wordpress/icons';
2733

2834
/**
2935
* Internal dependencies
@@ -43,13 +49,19 @@ import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
4349
* @param {HeadingData[]} props.attributes.headings The list of data for each heading in the post.
4450
* @param {boolean} props.attributes.onlyIncludeCurrentPage Whether to only include headings from the current page (if the post is paginated).
4551
* @param {number|undefined} props.attributes.maxLevel The maximum heading level to include, or null to include all levels.
52+
* @param {boolean} props.attributes.ordered Whether to display as an ordered list (true) or unordered list (false).
4653
* @param {string} props.clientId The client id.
4754
* @param {(attributes: Object) => void} props.setAttributes The set attributes function.
4855
*
4956
* @return {Component} The component.
5057
*/
5158
export default function TableOfContentsEdit( {
52-
attributes: { headings = [], onlyIncludeCurrentPage, maxLevel },
59+
attributes: {
60+
headings = [],
61+
onlyIncludeCurrentPage,
62+
maxLevel,
63+
ordered = true,
64+
},
5365
clientId,
5466
setAttributes,
5567
} ) {
@@ -86,27 +98,48 @@ export default function TableOfContentsEdit( {
8698
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
8799
const headingTree = linearToNestedHeadingList( headings );
88100

89-
const toolbarControls = canInsertList && (
101+
const toolbarControls = (
90102
<BlockControls>
91103
<ToolbarGroup>
92104
<ToolbarButton
93-
onClick={ () =>
94-
replaceBlocks(
95-
clientId,
96-
createBlock( 'core/list', {
97-
ordered: true,
98-
values: renderToString(
99-
<TableOfContentsList
100-
nestedHeadingList={ headingTree }
101-
/>
102-
),
103-
} )
104-
)
105+
icon={ isRTL() ? formatListBulletsRTL : formatListBullets }
106+
title={ __( 'Unordered' ) }
107+
description={ __( 'Convert to unordered list' ) }
108+
onClick={ () => setAttributes( { ordered: false } ) }
109+
isActive={ ordered === false }
110+
/>
111+
<ToolbarButton
112+
icon={
113+
isRTL() ? formatListNumberedRTL : formatListNumbered
105114
}
106-
>
107-
{ __( 'Convert to static list' ) }
108-
</ToolbarButton>
115+
title={ __( 'Ordered' ) }
116+
description={ __( 'Convert to ordered list' ) }
117+
onClick={ () => setAttributes( { ordered: true } ) }
118+
isActive={ ordered === true }
119+
/>
109120
</ToolbarGroup>
121+
{ canInsertList && (
122+
<ToolbarGroup>
123+
<ToolbarButton
124+
onClick={ () =>
125+
replaceBlocks(
126+
clientId,
127+
createBlock( 'core/list', {
128+
ordered,
129+
values: renderToString(
130+
<TableOfContentsList
131+
nestedHeadingList={ headingTree }
132+
ordered={ ordered }
133+
/>
134+
),
135+
} )
136+
)
137+
}
138+
>
139+
{ __( 'Convert to static list' ) }
140+
</ToolbarButton>
141+
</ToolbarGroup>
142+
) }
110143
</BlockControls>
111144
);
112145

@@ -118,6 +151,7 @@ export default function TableOfContentsEdit( {
118151
setAttributes( {
119152
onlyIncludeCurrentPage: false,
120153
maxLevel: undefined,
154+
ordered: true,
121155
} );
122156
} }
123157
dropdownMenuProps={ dropdownMenuProps }
@@ -210,16 +244,19 @@ export default function TableOfContentsEdit( {
210244
);
211245
}
212246

247+
const ListTag = ordered ? 'ol' : 'ul';
248+
213249
return (
214250
<>
215251
<nav { ...blockProps }>
216-
<ol>
252+
<ListTag>
217253
<TableOfContentsList
218254
nestedHeadingList={ headingTree }
219255
disableLinkActivation
220256
onClick={ showRedirectionPreventedNotice }
257+
ordered={ ordered }
221258
/>
222-
</ol>
259+
</ListTag>
223260
</nav>
224261
{ toolbarControls }
225262
{ inspectorControls }

packages/block-library/src/table-of-contents/list.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ export default function TableOfContentsList( {
1414
nestedHeadingList,
1515
disableLinkActivation,
1616
onClick,
17+
ordered = true,
1718
}: {
1819
nestedHeadingList: NestedHeadingData[];
1920
disableLinkActivation?: boolean;
2021
onClick?: ( event: MouseEvent< HTMLAnchorElement > ) => void;
22+
ordered?: boolean;
2123
} ): ReactElement {
2224
return (
2325
<>
@@ -42,11 +44,13 @@ export default function TableOfContentsList( {
4244
<span className={ ENTRY_CLASS_NAME }>{ content }</span>
4345
);
4446

47+
const NestedListTag = ordered ? 'ol' : 'ul';
48+
4549
return (
4650
<li key={ index }>
4751
{ entry }
4852
{ node.children ? (
49-
<ol>
53+
<NestedListTag>
5054
<TableOfContentsList
5155
nestedHeadingList={ node.children }
5256
disableLinkActivation={
@@ -58,8 +62,9 @@ export default function TableOfContentsList( {
5862
? onClick
5963
: undefined
6064
}
65+
ordered={ ordered }
6166
/>
62-
</ol>
67+
</NestedListTag>
6368
) : null }
6469
</li>
6570
);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ import { useBlockProps } from '@wordpress/block-editor';
99
import TableOfContentsList from './list';
1010
import { linearToNestedHeadingList } from './utils';
1111

12-
export default function save( { attributes: { headings = [] } } ) {
12+
export default function save( {
13+
attributes: { headings = [], ordered = true },
14+
} ) {
1315
if ( headings.length === 0 ) {
1416
return null;
1517
}
18+
const ListTag = ordered ? 'ol' : 'ul';
1619
return (
1720
<nav { ...useBlockProps.save() }>
18-
<ol>
21+
<ListTag>
1922
<TableOfContentsList
2023
nestedHeadingList={ linearToNestedHeadingList( headings ) }
24+
ordered={ ordered }
2125
/>
22-
</ol>
26+
</ListTag>
2327
</nav>
2428
);
2529
}

test/integration/fixtures/blocks/core__table-of-contents.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"link": "#heading-id-2"
1616
}
1717
],
18-
"onlyIncludeCurrentPage": false
18+
"onlyIncludeCurrentPage": false,
19+
"ordered": true
1920
},
2021
"innerBlocks": []
2122
}

test/integration/fixtures/blocks/core__table-of-contents__empty.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"isValid": true,
55
"attributes": {
66
"headings": [],
7-
"onlyIncludeCurrentPage": false
7+
"onlyIncludeCurrentPage": false,
8+
"ordered": true
89
},
910
"innerBlocks": []
1011
}

0 commit comments

Comments
 (0)