Skip to content
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

Move HeadinglevelDropdown to its own component #46003

Merged
merged 36 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a5fb915
Add a tag selection component for the block toolbar (replaces Heading…
carolinan Nov 23, 2022
753e2a8
Update index.php
carolinan Nov 24, 2022
939bd29
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Dec 16, 2022
165ad2f
update fixtures
carolinan Dec 16, 2022
d906d56
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Dec 30, 2022
787d080
Update CSS class, replace sprintf in the label with the tag name
carolinan Dec 30, 2022
6c62d43
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Jan 30, 2023
5691977
Add visible tag name to the drop down, add placeholder icon
carolinan Jan 30, 2023
1ba3734
Update README.md
carolinan Jan 30, 2023
a2d48fa
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Feb 8, 2023
5639693
Try to fix issue with the deprecation
carolinan Feb 8, 2023
48a7a10
Update CHANGELOG.md
carolinan Feb 8, 2023
98dbcde
Simplify and rename component
carolinan Feb 9, 2023
9a4bad3
Try reverting fixture changes
carolinan Feb 9, 2023
3724097
And again...
carolinan Feb 9, 2023
d331504
Update Query title to use the new component
carolinan Feb 9, 2023
1fba40d
Update Comments title
carolinan Feb 9, 2023
d397580
Update Site title with paragraph exception
carolinan Feb 9, 2023
dcf2f38
Update Heading block
carolinan Feb 9, 2023
62b1de0
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Feb 9, 2023
c2d4377
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Feb 10, 2023
33f32da
Update rich-text.test.js
carolinan Feb 10, 2023
a146a80
Block Heading Dropdown - Add support for mobile (#47967)
Feb 11, 2023
13476cf
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Feb 21, 2023
ed7000d
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Apr 12, 2023
b71e4df
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Apr 19, 2023
46b117a
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan May 12, 2023
a50c982
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan May 21, 2023
ec4278f
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan May 22, 2023
2716599
Text changes for a native file and e2e test spec
carolinan May 22, 2023
563f933
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan May 24, 2023
95866c5
Use the updated headingLevel icons
carolinan May 24, 2023
b674dc1
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Jun 14, 2023
b78b136
Update prop names
carolinan Jun 15, 2023
de0e2c9
Add 'options' property to type definiton
Mamaduka Jun 15, 2023
ce5c979
Merge branch 'trunk' into add/tag-selection-dropdown
carolinan Jun 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,18 @@ _Related_

- <https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#registerStore>

### TagSelectionDropdown

Dropdown for selecting a HTML tag.

_Parameters_

- _props_ `WPTagSelectionDropdownProps`: Component props.

_Returns_

- `WPComponent`: The toolbar.

### ToolSelector

Undocumented declaration.
Expand Down
52 changes: 52 additions & 0 deletions packages/block-editor/src/components/block-tag-selection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Block Tag Selection

`<TagSelectionDropdown>` Adds a dropdown with a list of selectable HTML tags to the block toolbar.
Uses `<ToolbarDropdownMenu>`.

## Usage

```jsx

import { BlockControls, TagSelectionDropdown } from '@wordpress/block-editor';

const DEFAULT_TAGS = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p' ];

const MyTagSelectionToolbar = () => (
<BlockControls group="block">
<TagSelectionDropdown
tags={ DEFAULT_TAGS }
selectedTag={ tag }
onChange={ ( newTag ) =>
setAttributes( { tag: newTag } )
}
/>
</BlockControls>
);
```

### Props

#### tags

The list of available HTML tags, passed from the block.

- Type: `Array`
- Required: no

#### selectedTag

The chosen HTML tag.

- Type: `string`
- Required: no

#### onChange

Callback to run when toolbar value is changed.

- Type: `string`
- Required: yes

## Related components

Block Editor components are components that can be used to compose the UI of your block editor. Thus, they can only be used under a [`BlockEditorProvider`](https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/provider/README.md) in the components tree.
74 changes: 74 additions & 0 deletions packages/block-editor/src/components/block-tag-selection/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* WordPress dependencies
*/
import { ToolbarDropdownMenu } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import TagIcon from './tag-icon';

// Default HTML tags
const DEFAULT_TAGS = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ];

const POPOVER_PROPS = {
className: 'block-library-heading-level-dropdown',
carolinan marked this conversation as resolved.
Show resolved Hide resolved
};

/** @typedef {import('@wordpress/element').WPComponent} WPComponent */

/**
* TagSelectionDropdown props.
*
* @typedef WPTagSelectionDropdownProps
*
* @property {Array} tags The list of available HTML tags.
* @property {string} selectedTag The chosen HTML tag.
* @property {string} onChange Callback to run when
* toolbar value is changed.
*/

/**
* Dropdown for selecting a HTML tag.
*
* @param {WPTagSelectionDropdownProps} props Component props.
*
* @return {WPComponent} The toolbar.
*/
export default function TagSelectionDropdown( {
tags,
selectedTag,
onChange,
} ) {
const tagslist = tags ? tags : DEFAULT_TAGS;

return (
<ToolbarDropdownMenu
popoverProps={ POPOVER_PROPS }
icon={ <TagIcon tag={ selectedTag } /> }
label={ __( 'Change HTML tag' ) }
controls={ tagslist.map( ( targetTag ) => {
{
const isActive = targetTag === selectedTag;

return {
icon: (
<TagIcon tag={ targetTag } isPressed={ isActive } />
),
label: sprintf(
// translators: %s: the name of the HTML tag
__( 'HTML tag: %s' ),
aristath marked this conversation as resolved.
Show resolved Hide resolved
targetTag
),
isActive,
onClick() {
onChange( targetTag );
},
role: 'menuitemradio',
};
}
} ) }
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* WordPress dependencies
*/
import { Path, SVG } from '@wordpress/components';
import { paragraph } from '@wordpress/icons';

/** @typedef {import('@wordpress/element').WPComponent} WPComponent */

/**
* TagIcon props.
*
* @typedef WPTagIconProps
*
* @property {string} tag The tag to show an icon for.
* @property {?boolean} isPressed Whether or not the icon should appear pressed; default: false.
*/

/**
* Tag icon.
*
* @param {WPTagIconProps} props Component props.
*
* @return {?WPComponent} The icon.
*/
export default function TagIcon( { tag, isPressed = false } ) {
if ( tag === 'p' ) return paragraph;
const tagToPath = {
h1: 'M9 5h2v10H9v-4H5v4H3V5h2v4h4V5zm6.6 0c-.6.9-1.5 1.7-2.6 2v1h2v7h2V5h-1.4z',
h2: 'M7 5h2v10H7v-4H3v4H1V5h2v4h4V5zm8 8c.5-.4.6-.6 1.1-1.1.4-.4.8-.8 1.2-1.3.3-.4.6-.8.9-1.3.2-.4.3-.8.3-1.3 0-.4-.1-.9-.3-1.3-.2-.4-.4-.7-.8-1-.3-.3-.7-.5-1.2-.6-.5-.2-1-.2-1.5-.2-.4 0-.7 0-1.1.1-.3.1-.7.2-1 .3-.3.1-.6.3-.9.5-.3.2-.6.4-.8.7l1.2 1.2c.3-.3.6-.5 1-.7.4-.2.7-.3 1.2-.3s.9.1 1.3.4c.3.3.5.7.5 1.1 0 .4-.1.8-.4 1.1-.3.5-.6.9-1 1.2-.4.4-1 .9-1.6 1.4-.6.5-1.4 1.1-2.2 1.6V15h8v-2H15z',
h3: 'M12.1 12.2c.4.3.8.5 1.2.7.4.2.9.3 1.4.3.5 0 1-.1 1.4-.3.3-.1.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h2v10zm8.6-7.5c-.2-.2-.5-.4-.8-.5-.6-.2-1.3-.2-1.9 0-.3.1-.6.3-.8.5l-.6.9c-.2.5-.2.9-.2 1.4.4-.3.8-.6 1.2-.8.4-.2.8-.3 1.3-.3.4 0 .8 0 1.2.2.4.1.7.3 1 .6.3.3.5.6.7.9.2.4.3.8.3 1.3s-.1.9-.3 1.4c-.2.4-.5.7-.8 1-.4.3-.8.5-1.2.6-1 .3-2 .3-3 0-.5-.2-1-.5-1.4-.9-.4-.4-.8-.9-1-1.5-.2-.6-.3-1.3-.3-2.1s.1-1.6.4-2.3c.2-.6.6-1.2 1-1.6.4-.4.9-.7 1.4-.9.6-.3 1.1-.4 1.7-.4.7 0 1.4.1 2 .3.5.2 1 .5 1.4.8 0 .1-1.3 1.4-1.3 1.4zm-2.4 5.8c.2 0 .4 0 .6-.1.2 0 .4-.1.5-.2.1-.1.3-.3.4-.5.1-.2.1-.5.1-.7 0-.4-.1-.8-.4-1.1-.3-.2-.7-.3-1.1-.3-.3 0-.7.1-1 .2-.4.2-.7.4-1 .7 0 .3.1.7.3 1 .1.2.3.4.4.6.2.1.3.3.5.3.2.1.5.2.7.1z',
};
if ( ! tagToPath.hasOwnProperty( tag ) ) {
return null;
}

return (
<SVG
width="24"
height="24"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
isPressed={ isPressed }
>
<Path d={ tagToPath[ tag ] } />
</SVG>
);
}
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export { default as BlockEdit, useBlockEditContext } from './block-edit';
export { default as BlockIcon } from './block-icon';
export { default as BlockNavigationDropdown } from './block-navigation/dropdown';
export { default as BlockStyles } from './block-styles';
export { default as TagSelectionDropdown } from './block-tag-selection';
export { default as __experimentalBlockVariationPicker } from './block-variation-picker';
export { default as __experimentalBlockPatternSetup } from './block-pattern-setup';
export { default as __experimentalBlockVariationTransforms } from './block-variation-transforms';
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/post-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"type": "string"
},
"level": {
carolinan marked this conversation as resolved.
Show resolved Hide resolved
"type": "number",
"default": 2
"type": "string",
"default": "h2"
},
"isLink": {
"type": "boolean",
Expand Down
82 changes: 81 additions & 1 deletion packages/block-library/src/post-title/deprecated.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,88 @@
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import migrateFontFamily from '../utils/migrate-font-family';

// Migrate the old level attribute (number) to the corresponding HTML tag (string).
const migrateLevel = ( attributes ) => {
const { level, ...restAttributes } = attributes;
return typeof level === 'number'
? { ...restAttributes, level: 'h' + level }
: attributes;
};

const v2 = {
attributes: {
textAlign: {
type: 'string',
},
level: {
type: 'number',
default: 2,
},
isLink: {
type: 'boolean',
default: false,
},
rel: {
type: 'string',
attribute: 'rel',
default: '',
},
linkTarget: {
type: 'string',
default: '_self',
},
},
supports: {
align: [ 'wide', 'full' ],
html: false,
color: {
gradients: true,
link: true,
__experimentalDefaultControls: {
background: true,
text: true,
link: true,
},
},
spacing: {
margin: true,
padding: true,
},
typography: {
fontSize: true,
lineHeight: true,
__experimentalFontFamily: true,
__experimentalFontWeight: true,
__experimentalFontStyle: true,
__experimentalTextTransform: true,
__experimentalTextDecoration: true,
__experimentalLetterSpacing: true,
__experimentalDefaultControls: {
fontSize: true,
fontAppearance: true,
textTransform: true,
},
},
},
save() {
return null;
},
migrate: compose( migrateFontFamily, migrateLevel ),
isEligible( attributes ) {
if ( typeof attributes.level === 'number' ) {
return true;
}
return false;
},
};

const v1 = {
attributes: {
textAlign: {
Expand Down Expand Up @@ -62,4 +142,4 @@ const v1 = {
*
* See block-deprecation.md
*/
export default [ v1 ];
export default [ v2, v1 ];
8 changes: 4 additions & 4 deletions packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
InspectorControls,
useBlockProps,
PlainText,
TagSelectionDropdown,
} from '@wordpress/block-editor';
import { ToggleControl, TextControl, PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand All @@ -20,15 +21,14 @@ import { useEntityProp } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import HeadingLevelDropdown from '../heading/heading-level-dropdown';
import { useCanEditEntity } from '../utils/hooks';

export default function PostTitleEdit( {
attributes: { level, textAlign, isLink, rel, linkTarget },
setAttributes,
context: { postType, postId, queryId },
} ) {
const TagName = 0 === level ? 'p' : 'h' + level;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that this code seems to have been adapted for allowing the post title to be a paragraph, but I don't remember this being implemented? It was only implemented for the site title.

Because of the discussions in this issue #30549
I have removed the partial support for p tag.

const TagName = level;
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const userCanEdit = useCanEditEntity( 'postType', postType, postId );
const [ rawTitle = '', setTitle, fullTitle ] = useEntityProp(
Expand Down Expand Up @@ -102,8 +102,8 @@ export default function PostTitleEdit( {
return (
<>
<BlockControls group="block">
<HeadingLevelDropdown
selectedLevel={ level }
<TagSelectionDropdown
selectedTag={ level }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/post-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function render_block_core_post_title( $attributes, $content, $block ) {
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";

if ( isset( $attributes['level'] ) ) {
$tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level'];
$tag_name = $attributes['level'];
}

if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/fixtures/blocks/core__post-title.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "core/post-title",
"isValid": true,
"attributes": {
"level": 2,
"level": "h2",
"isLink": false,
"rel": "",
"linkTarget": "_self"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- wp:post-title {"fontFamily":"cambria-georgia"} /-->
<!-- wp:post-title {"level":2,"fontFamily":"cambria-georgia"} /-->
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"name": "core/post-title",
"isValid": true,
"attributes": {
"level": 2,
"level": "h2",
"isLink": false,
"rel": "",
"linkTarget": "_self"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"name": "core/post-title",
"isValid": true,
"attributes": {
"level": 2,
"level": "h2",
"isLink": false,
"rel": "",
"linkTarget": "_self"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"name": "core/post-title",
"isValid": true,
"attributes": {
"level": 2,
"level": "h2",
"isLink": false,
"rel": "",
"linkTarget": "_self"
Expand Down