-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Display heading levels dynamically in Heading Options when Heading block is selected #14689
Changes from 4 commits
6791ea6
a75eef2
590592c
d0ab84a
e5d5256
cf484c4
9e71339
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
|
@@ -10,33 +15,37 @@ import { __ } from '@wordpress/i18n'; | |
import { Fragment } from '@wordpress/element'; | ||
import { PanelBody } from '@wordpress/components'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { withSelect } from '@wordpress/data'; | ||
import { | ||
RichText, | ||
BlockControls, | ||
InspectorControls, | ||
AlignmentToolbar, | ||
} from '@wordpress/block-editor'; | ||
|
||
export default function HeadingEdit( { | ||
function HeadingEdit( { | ||
attributes, | ||
setAttributes, | ||
mergeBlocks, | ||
insertBlocksAfter, | ||
onReplace, | ||
className, | ||
levelChoices, | ||
} ) { | ||
const { align, content, level, placeholder } = attributes; | ||
const tagName = 'h' + level; | ||
|
||
const BlockControlsLevelsRange = ( levelChoices.length <= 3 ) ? | ||
levelChoices : | ||
levelChoices.slice( 1, 4 ); // | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I'm following what the logic is here as far as the second, third, fourth, and fifth level choice options being significant. I sense you'd considered to add an inline comment but forgot to complete it? Taking a guess with the default options, is it mostly about limiting to a select few common options, and omitting https://lodash.com/docs/4.17.11#take This also seems like it would help avoid having to test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aduth What I'm trying to do here is to replicate the previous behavior in the BlockControls options (not the sidebar). Right now, it displays only 3 headings: H2, H3 and H4. So if the levels list contains 3 or less options, it will display all of them, otherwise, slice the array and display 3 elements starting by the second one. I think using Lodash for that would be an overkill for such a simple operation. |
||
return ( | ||
<Fragment> | ||
<BlockControls> | ||
<HeadingToolbar minLevel={ 2 } maxLevel={ 5 } selectedLevel={ level } onChange={ ( newLevel ) => setAttributes( { level: newLevel } ) } /> | ||
<HeadingToolbar levelsRange={ BlockControlsLevelsRange } selectedLevel={ level } onChange={ ( newLevel ) => setAttributes( { level: newLevel } ) } /> | ||
</BlockControls> | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Heading Settings' ) }> | ||
<p>{ __( 'Level' ) }</p> | ||
<HeadingToolbar minLevel={ 1 } maxLevel={ 7 } selectedLevel={ level } onChange={ ( newLevel ) => setAttributes( { level: newLevel } ) } /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did the default max level change from 7 to 6? And if so, why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment here: https://github.com/WordPress/gutenberg/pull/14689/files#r271809189 |
||
<HeadingToolbar levelsRange={ levelChoices } selectedLevel={ level } onChange={ ( newLevel ) => setAttributes( { level: newLevel } ) } /> | ||
<p>{ __( 'Text Alignment' ) }</p> | ||
<AlignmentToolbar | ||
value={ align } | ||
|
@@ -72,3 +81,17 @@ export default function HeadingEdit( { | |
</Fragment> | ||
); | ||
} | ||
|
||
export default withSelect( ( select ) => { | ||
// Parse the h1,h2,h3... choices to level numbers and pass it as a prop. | ||
const levelChoices = get( | ||
select( 'core/blocks' ).getBlockType( 'core/heading' ), | ||
[ 'attributes', 'level', 'enum' ], | ||
[] | ||
igmoweb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
|
||
return { | ||
levelChoices, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation will result in poor performance, at least when using the fallback empty array value, due to a combination of factors that (a) As long as a block types const DEFAULT_LEVEL_CHOICES = [ 1, 2, 3, 4, 5, 6, 7 ]; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Great point. It's fixed now. Just a side comment: The previous |
||
}; | ||
} )( HeadingEdit ); | ||
|
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.
This should be camel-cased with an initial lower-case letter.
https://make.wordpress.org/core/handbook/best-practices/coding-standards/javascript/#naming-conventions
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.
Fixed