Skip to content

Commit

Permalink
ToC block: re-enable + use static markup and only support core Headin…
Browse files Browse the repository at this point in the history
…g and Page Break blocks (#29739)

Re-enables the Table of Contents block, which was first merged over a year ago in #21234. The block is using static markup and only supports the core Heading and Page Break blocks, using a much cleaner and performant implementation. It is also using <ol> for the lists.

Co-authored-by: Miguel Fonseca <miguelcsf@gmail.com>
  • Loading branch information
ZebulanStanphill and mcsf committed May 6, 2022
1 parent b53edba commit c4c3173
Show file tree
Hide file tree
Showing 26 changed files with 416 additions and 585 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Expand Up @@ -20,6 +20,7 @@
/packages/block-library/src/page-list @tellthemachines
/packages/block-library/src/comment-template @michalczaplinski
/packages/block-library/src/comments @michalczaplinski
/packages/block-library/src/table-of-contents @ZebulanStanphill

# Duotone
/lib/block-supports/duotone.php @ajlende
Expand Down
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Expand Up @@ -798,7 +798,7 @@ Summarize your post with a list of headings. Add HTML anchors to Heading blocks
- **Name:** core/table-of-contents
- **Category:** layout
- **Supports:** ~~html~~
- **Attributes:** onlyIncludeCurrentPage
- **Attributes:** headings, onlyIncludeCurrentPage

## Tag Cloud

Expand Down
3 changes: 1 addition & 2 deletions lib/blocks.php
Expand Up @@ -38,7 +38,7 @@ function gutenberg_reregister_core_block_types() {
'social-links',
'spacer',
'table',
// 'table-of-contents',
'table-of-contents',
'text-columns',
'verse',
'video',
Expand Down Expand Up @@ -107,7 +107,6 @@ function gutenberg_reregister_core_block_types() {
'site-logo.php' => 'core/site-logo',
'site-tagline.php' => 'core/site-tagline',
'site-title.php' => 'core/site-title',
// 'table-of-contents.php' => 'core/table-of-contents',
'tag-cloud.php' => 'core/tag-cloud',
'template-part.php' => 'core/template-part',
'term-description.php' => 'core/term-description',
Expand Down
7 changes: 5 additions & 2 deletions packages/block-library/src/heading/edit.js
Expand Up @@ -42,10 +42,13 @@ function HeadingEdit( {
} );

const { canGenerateAnchors } = useSelect( ( select ) => {
const settings = select( blockEditorStore ).getSettings();
const { getGlobalBlockCount, getSettings } = select( blockEditorStore );
const settings = getSettings();

return {
canGenerateAnchors: !! settings.generateAnchors,
canGenerateAnchors:
!! settings.generateAnchors ||
getGlobalBlockCount( 'core/table-of-contents' ) > 0,
};
}, [] );

Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/index.js
Expand Up @@ -99,6 +99,7 @@ import * as socialLink from './social-link';
import * as socialLinks from './social-links';
import * as spacer from './spacer';
import * as table from './table';
import * as tableOfContents from './table-of-contents';
import * as tagCloud from './tag-cloud';
import * as templatePart from './template-part';
import * as termDescription from './term-description';
Expand Down Expand Up @@ -175,7 +176,6 @@ export const __experimentalGetCoreBlocks = () => [
socialLinks,
spacer,
table,
// tableOfContents,
tagCloud,
textColumns,
verse,
Expand Down Expand Up @@ -269,6 +269,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = process.env
[
// Experimental blocks.
postAuthorName,
tableOfContents,
...( window.__experimentalEnableListBlockV2
? [ listItem ]
: [] ),
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/list/transforms.js
Expand Up @@ -208,6 +208,13 @@ const transforms = {
} );
},
},
{
type: 'block',
blocks: [ 'core/table-of-contents' ],
transform: () => {
return createBlock( 'core/table-of-contents' );
},
},
],
};

Expand Down
10 changes: 8 additions & 2 deletions packages/block-library/src/table-of-contents/block.json
Expand Up @@ -8,13 +8,19 @@
"keywords": [ "document outline", "summary" ],
"textdomain": "default",
"attributes": {
"headings": {
"type": "array",
"items": {
"type": "object"
}
},
"onlyIncludeCurrentPage": {
"type": "boolean",
"default": false
}
},
"usesContext": [ "postId" ],
"supports": {
"html": false
}
},
"example": {}
}

0 comments on commit c4c3173

Please sign in to comment.