-
Notifications
You must be signed in to change notification settings - Fork 8
Add H1-H6 heading level selection controls to Dynamic Table of Contents block #101
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
Open
jbouganim
wants to merge
2
commits into
a8cteam51:trunk
Choose a base branch
from
jbouganim:toc/updates
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| = 0.2.0 - Add toggles to preselect heading tags for the table of contents. |
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
37,230 changes: 18,615 additions & 18,615 deletions
37,230
dynamic-table-of-contents/package-lock.json
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,20 +1,20 @@ | ||
| { | ||
| "name": "dynamic-table-of-contents", | ||
| "version": "0.1.9", | ||
| "description": "Creates a table of contents that's dynamically (PHP) rendered.", | ||
| "author": "The WordPress Contributors", | ||
| "license": "GPL-2.0-or-later", | ||
| "main": "build/index.js", | ||
| "scripts": { | ||
| "build": "wp-scripts build src/index.js src/view.js --webpack-copy-php", | ||
| "format": "wp-scripts format", | ||
| "lint:css": "wp-scripts lint-style", | ||
| "lint:js": "wp-scripts lint-js", | ||
| "packages-update": "wp-scripts packages-update", | ||
| "plugin-zip": "wp-scripts plugin-zip", | ||
| "start": "wp-scripts start src/index.js src/view.js --webpack-copy-php" | ||
| }, | ||
| "devDependencies": { | ||
| "@wordpress/scripts": "^26.18.0" | ||
| } | ||
| } | ||
| "name": "dynamic-table-of-contents", | ||
| "version": "0.1.9", | ||
| "description": "Creates a table of contents that's dynamically (PHP) rendered.", | ||
| "author": "The WordPress Contributors", | ||
| "license": "GPL-2.0-or-later", | ||
| "main": "build/index.js", | ||
| "scripts": { | ||
| "build": "wp-scripts build src/index.js src/view.js --webpack-copy-php", | ||
| "format": "wp-scripts format", | ||
| "lint:css": "wp-scripts lint-style", | ||
| "lint:js": "wp-scripts lint-js", | ||
| "packages-update": "wp-scripts packages-update", | ||
| "plugin-zip": "wp-scripts plugin-zip", | ||
| "start": "wp-scripts start src/index.js src/view.js --webpack-copy-php" | ||
| }, | ||
| "devDependencies": { | ||
| "@wordpress/scripts": "^26.18.0" | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,35 +1,135 @@ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useBlockProps, RichText } from '@wordpress/block-editor'; | ||
| import { | ||
| useBlockProps, | ||
| RichText, | ||
| InspectorControls, | ||
| } from '@wordpress/block-editor'; | ||
| import { PanelBody, ToggleControl } from '@wordpress/components'; | ||
|
|
||
| /** | ||
| * The edit function describes the structure of your block in the context of the | ||
| * editor. This represents what the editor will render when the block is used. | ||
| * | ||
| * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit | ||
| * | ||
| * @param {Object} root0 The component props. | ||
| * @param {Object} root0.attributes The block attributes. | ||
| * @param {Function} root0.setAttributes Function to update block attributes. | ||
| * @return {Element} Element to render. | ||
| */ | ||
| export default function Edit({ attributes, setAttributes }) { | ||
| export default function Edit( { attributes, setAttributes } ) { | ||
| const { | ||
| title, | ||
| includeH1, | ||
| includeH2, | ||
| includeH3, | ||
| includeH4, | ||
| includeH5, | ||
| includeH6, | ||
| } = attributes; | ||
|
|
||
| return ( | ||
| <div {...useBlockProps()}> | ||
| <RichText | ||
| tagName="h2" | ||
| placeholder={__('Table of Contents Title', 'dynamic-table-of-contents')} | ||
| value={attributes.title} | ||
| onChange={(title) => setAttributes({ title })} | ||
| className="table-of-contents-title" | ||
| /> | ||
| <ul className="table-of-contents-list"> | ||
| <li className="table-of-contents-list-item"> | ||
| <a href="#" className='active'>This is an example.</a> | ||
| </li> | ||
| <li className="table-of-contents-list-item"> | ||
| <a href="#">Of the block appearance.</a> | ||
| </li> | ||
| <li className="table-of-contents-list-item"> | ||
| <a href="#">When used in your post.</a> | ||
| </li> | ||
| </ul> | ||
| </div> | ||
| <> | ||
| <InspectorControls> | ||
| <PanelBody | ||
| title={ __( | ||
| 'Heading Levels', | ||
| 'dynamic-table-of-contents' | ||
| ) } | ||
| initialOpen={ true } | ||
| > | ||
| <ToggleControl | ||
| label={ __( | ||
| 'Include H1 headings', | ||
| 'dynamic-table-of-contents' | ||
| ) } | ||
| checked={ includeH1 } | ||
| onChange={ ( newIncludeH1 ) => | ||
| setAttributes( { includeH1: newIncludeH1 } ) | ||
| } | ||
| /> | ||
| <ToggleControl | ||
| label={ __( | ||
| 'Include H2 headings', | ||
| 'dynamic-table-of-contents' | ||
| ) } | ||
| checked={ includeH2 } | ||
| onChange={ ( newIncludeH2 ) => | ||
| setAttributes( { includeH2: newIncludeH2 } ) | ||
| } | ||
| /> | ||
| <ToggleControl | ||
| label={ __( | ||
| 'Include H3 headings', | ||
| 'dynamic-table-of-contents' | ||
| ) } | ||
| checked={ includeH3 } | ||
| onChange={ ( newIncludeH3 ) => | ||
| setAttributes( { includeH3: newIncludeH3 } ) | ||
| } | ||
| /> | ||
| <ToggleControl | ||
| label={ __( | ||
| 'Include H4 headings', | ||
| 'dynamic-table-of-contents' | ||
| ) } | ||
| checked={ includeH4 } | ||
| onChange={ ( newIncludeH4 ) => | ||
| setAttributes( { includeH4: newIncludeH4 } ) | ||
| } | ||
| /> | ||
| <ToggleControl | ||
| label={ __( | ||
| 'Include H5 headings', | ||
| 'dynamic-table-of-contents' | ||
| ) } | ||
| checked={ includeH5 } | ||
| onChange={ ( newIncludeH5 ) => | ||
| setAttributes( { includeH5: newIncludeH5 } ) | ||
| } | ||
| /> | ||
| <ToggleControl | ||
| label={ __( | ||
| 'Include H6 headings', | ||
| 'dynamic-table-of-contents' | ||
| ) } | ||
| checked={ includeH6 } | ||
| onChange={ ( newIncludeH6 ) => | ||
| setAttributes( { includeH6: newIncludeH6 } ) | ||
| } | ||
| /> | ||
| </PanelBody> | ||
| </InspectorControls> | ||
| <div { ...useBlockProps() }> | ||
| <RichText | ||
| tagName="h2" | ||
| placeholder={ __( | ||
| 'Table of Contents Title', | ||
| 'dynamic-table-of-contents' | ||
| ) } | ||
| value={ title } | ||
| onChange={ ( newTitle ) => | ||
| setAttributes( { title: newTitle } ) | ||
| } | ||
| className="table-of-contents-title" | ||
| /> | ||
| <ul className="table-of-contents-list"> | ||
| <li className="table-of-contents-list-item"> | ||
| { /* eslint-disable-next-line jsx-a11y/anchor-is-valid -- Not a valid link, just preview. */ } | ||
| <a href="#" className="active"> | ||
| This is an example. | ||
| </a> | ||
| </li> | ||
| <li className="table-of-contents-list-item"> | ||
| { /* eslint-disable-next-line jsx-a11y/anchor-is-valid -- Not a valid link, just preview. */ } | ||
| <a href="#">Of the block appearance.</a> | ||
| </li> | ||
| <li className="table-of-contents-list-item"> | ||
| { /* eslint-disable-next-line jsx-a11y/anchor-is-valid -- Not a valid link, just preview. */ } | ||
| <a href="#">When used in your post.</a> | ||
| </li> | ||
| </ul> | ||
| </div> | ||
| </> | ||
| ); | ||
| } |
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
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.
All-levels-off edge case currently falls back to “all levels”.
When all toggles are off,
$heading_levelsbecomes empty, resulting indata-heading-levels="". In view.js, an empty string is falsy and defaults to all levels—contrary to the PR objective (“edge case: all levels toggled off (empty list)”). We need to distinguish:Set the data attribute only when any include* attribute exists; leave it absent for legacy. Keep it present (possibly empty) for new blocks. Pair with the view.js change below.
Apply:
🤖 Prompt for AI Agents