-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add/table of contents #15426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add/table of contents #15426
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5c910e8
added table of contents block
ashwin-pc d6050ad
fix block warning
ashwin-pc 225990f
Added toc fixture
ashwin-pc b73a652
removed the outline for link and added block transform
ashwin-pc 75c2e6f
added table of contents block
ashwin-pc 273d31f
fix block warning
ashwin-pc ffc8791
Added toc fixture
ashwin-pc 4ab2946
removed the outline for link and added block transform
ashwin-pc de0f085
Merge branch 'add/table-of-contents' of https://github.com/ashwin-pc/…
ashwin-pc 4331062
updated to incorporate requested changes
ashwin-pc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { RichText } from '@wordpress/editor'; | ||
|
|
||
| export default function ListLevel( props ) { | ||
| const { edit, attributes, setAttributes } = props; | ||
| let childnodes = null; | ||
|
|
||
| if ( props.children ) { | ||
| childnodes = props.children.map( function( childnode ) { | ||
| const link = getLinkElement( childnode, props ); | ||
|
|
||
| return ( | ||
| <li key={ childnode.block.anchor }> | ||
| { link } | ||
| { childnode.children ? <ListLevel | ||
| edit={ edit } | ||
| attributes={ attributes } | ||
| setAttributes={ setAttributes } | ||
| > | ||
| { childnode.children } | ||
| </ListLevel> : null } | ||
| </li> | ||
| ); | ||
| } ); | ||
|
|
||
| return ( | ||
| <ul> | ||
| { childnodes } | ||
| </ul> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function getLinkElement( childnode, props ) { | ||
| const { edit, attributes, setAttributes } = props; | ||
| const { headings, autosync } = attributes; | ||
|
|
||
| const updateHeading = ( content ) => { | ||
| headings[ childnode.index ].content = content; | ||
| setAttributes( { headings } ); | ||
| }; | ||
|
|
||
| if ( autosync ) { | ||
| return <a href={ childnode.block.anchor } data-level={ childnode.block.level }>{ childnode.block.content }</a>; | ||
| } | ||
|
|
||
| if ( edit ) { | ||
| return ( | ||
| <RichText | ||
| tagName="a" | ||
| href={ childnode.block.anchor } | ||
| data-level={ childnode.block.level } | ||
| onChange={ ( content ) => updateHeading( content ) } | ||
| value={ childnode.block.content } | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <RichText.Content | ||
| tagName="a" | ||
| href={ childnode.block.anchor } | ||
| data-level={ childnode.block.level } | ||
| value={ childnode.block.content } | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "name": "core/table-of-contents", | ||
| "category": "common" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import * as Utils from './utils'; | ||
| import ListLevel from './ListLevel'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Component } from '@wordpress/element'; | ||
| import { subscribe } from '@wordpress/data'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { | ||
| IconButton, | ||
| Toolbar, | ||
| PanelBody, | ||
| ToggleControl, | ||
| } from '@wordpress/components'; | ||
| import { | ||
| BlockControls, | ||
| InspectorControls, | ||
| } from '@wordpress/editor'; | ||
|
|
||
| class TOCEdit extends Component { | ||
| constructor() { | ||
| super( ...arguments ); | ||
|
|
||
| this.state = { | ||
| wpDataUnsubscribe: null, | ||
| }; | ||
|
|
||
| this.toggleAttribute = this.toggleAttribute.bind( this ); | ||
| this.refresh = this.refresh.bind( this ); | ||
| } | ||
|
|
||
| toggleAttribute( propName ) { | ||
| const value = this.props.attributes[ propName ]; | ||
| const { setAttributes } = this.props; | ||
|
|
||
| setAttributes( { [ propName ]: ! value } ); | ||
| } | ||
|
|
||
| refresh() { | ||
| const { setAttributes } = this.props; | ||
| const headings = Utils.getPageHeadings(); | ||
| setAttributes( { headings } ); | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| const { attributes, setAttributes } = this.props; | ||
| const headings = attributes.headings || []; | ||
| const wpDataUnsubscribe = subscribe( () => { | ||
| const pageHeadings = Utils.getPageHeadings(); | ||
| this.setState( { pageHeadings } ); | ||
| } ); | ||
|
|
||
| setAttributes( { headings } ); | ||
| this.setState( { wpDataUnsubscribe } ); | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| this.state.wpDataUnsubscribe(); | ||
| } | ||
|
|
||
| componentDidUpdate( prevProps, prevState ) { | ||
| const { attributes, setAttributes } = this.props; | ||
| const pageHeadings = Utils.getPageHeadings(); | ||
| if ( JSON.stringify( pageHeadings ) !== JSON.stringify( prevState.pageHeadings ) ) { | ||
| this.setState( { pageHeadings } ); | ||
| if ( attributes.autosync ) { | ||
| setAttributes( { headings: pageHeadings } ); // this is displayed on the page | ||
| } | ||
| } | ||
| } | ||
|
|
||
| render() { | ||
| const { attributes, setAttributes } = this.props; | ||
| const { autosync } = attributes; | ||
| const headings = attributes.headings || []; | ||
| if ( headings.length === 0 ) { | ||
| return ( <p>{ __( 'Start adding headings to generate Table of Contents' ) }</p> ); | ||
| } | ||
|
|
||
| Utils.updateHeadingBlockAnchors(); | ||
|
|
||
| return ( | ||
| <div className={ this.props.className }> | ||
| { ! autosync && | ||
| <BlockControls> | ||
| <Toolbar> | ||
| <IconButton | ||
| label={ __( 'Update' ) } | ||
| aria-pressed={ this.state.isEditing } | ||
| onClick={ this.refresh } | ||
| icon="update" | ||
| /> | ||
| </Toolbar> | ||
| </BlockControls> | ||
| } | ||
| { | ||
| <InspectorControls> | ||
| <PanelBody title={ __( 'Table of Contents Settings' ) }> | ||
| <ToggleControl | ||
| label={ __( 'Auto Sync' ) } | ||
| checked={ autosync } | ||
| onChange={ () => { | ||
| if ( ! autosync ) { | ||
| this.refresh(); | ||
| } | ||
| this.toggleAttribute( 'autosync' ); | ||
| } } | ||
| /> | ||
| </PanelBody> | ||
| </InspectorControls> | ||
| } | ||
| <ListLevel | ||
| edit={ true } | ||
| attributes={ attributes } | ||
| setAttributes={ setAttributes } | ||
| > | ||
| { Utils.linearToNestedList( headings ) } | ||
| </ListLevel> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default TOCEdit; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| .wp-block-table-of-contents { | ||
| .editor-block-list__block & ul { | ||
| padding-left: 1.3em; | ||
|
|
||
| a { | ||
| display: block; | ||
|
|
||
| &:focus { | ||
| box-shadow: none; | ||
| } | ||
| } | ||
|
|
||
| ul { | ||
| margin-bottom: 0; | ||
| } | ||
| } | ||
|
|
||
| p { | ||
| opacity: 0.5; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import edit from './edit'; | ||
| import metadata from './block.json'; | ||
| import save from './save'; | ||
|
|
||
| const { name } = metadata; | ||
|
|
||
| export { metadata, name }; | ||
|
|
||
| export const settings = { | ||
| title: __( 'Table of Contents' ), | ||
| description: __( 'Add a list of internal links allowing your readers to quickly navigate around.' ), | ||
| icon: 'list-view', | ||
| category: 'layout', | ||
| attributes: { | ||
| headings: { | ||
| source: 'query', | ||
| selector: 'a', | ||
| query: { | ||
| content: { source: 'text' }, | ||
| anchor: { source: 'attribute', attribute: 'href' }, | ||
| level: { source: 'attribute', attribute: 'data-level' }, | ||
| }, | ||
| }, | ||
| autosync: { | ||
| type: 'boolean', | ||
| default: true, | ||
| }, | ||
| }, | ||
| edit, | ||
| save, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import * as Utils from './utils'; | ||
| import ListLevel from './ListLevel'; | ||
|
|
||
| export default function save( props ) { | ||
| const { attributes, setAttributes } = props; | ||
| const headings = attributes.headings; | ||
|
|
||
| if ( headings.length === 0 ) { | ||
| return null; | ||
| } | ||
|
|
||
| Utils.updateHeadingBlockAnchors(); | ||
| return ( | ||
| <nav className={ props.className }> | ||
| <ListLevel | ||
| edit={ false } | ||
| attributes={ attributes } | ||
| setAttributes={ setAttributes } | ||
| > | ||
| { Utils.linearToNestedList( headings ) } | ||
| </ListLevel> | ||
| </nav> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest using
withSelectto get the page headings instead of doing a manual subscribe here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@swissspidy Thanks for the suggestion. I will definitely look into that.