From 988ba5d9dd756f1306b9319a026174280006b857 Mon Sep 17 00:00:00 2001 From: Enrique Piqueras Date: Mon, 24 Feb 2020 09:03:15 -0800 Subject: [PATCH] Block Library: Allow changing Site Title block heading level. (#20361) * Core Data: Fix null property access. * Edit Site: Provide site entity. * Block Library: Allow changing Site Title block heading level. * E2E Tests: Update fixtures. --- .../block-library/src/site-title/block.json | 8 +++- packages/block-library/src/site-title/edit.js | 19 ---------- .../src/site-title/edit/index.js | 38 +++++++++++++++++++ .../src/site-title/edit/level-icon.js | 27 +++++++++++++ .../src/site-title/edit/level-toolbar.js | 38 +++++++++++++++++++ .../block-library/src/site-title/index.php | 10 ++++- packages/core-data/src/entity-provider.js | 15 +++----- .../fixtures/blocks/core__site-title.json | 4 +- .../edit-site/src/components/editor/index.js | 26 +++++++------ 9 files changed, 141 insertions(+), 44 deletions(-) delete mode 100644 packages/block-library/src/site-title/edit.js create mode 100644 packages/block-library/src/site-title/edit/index.js create mode 100644 packages/block-library/src/site-title/edit/level-icon.js create mode 100644 packages/block-library/src/site-title/edit/level-toolbar.js diff --git a/packages/block-library/src/site-title/block.json b/packages/block-library/src/site-title/block.json index a853cde2517b5..905d2256ab6f3 100644 --- a/packages/block-library/src/site-title/block.json +++ b/packages/block-library/src/site-title/block.json @@ -1,4 +1,10 @@ { "name": "core/site-title", - "category": "layout" + "category": "layout", + "attributes": { + "level": { + "type": "number", + "default": 1 + } + } } diff --git a/packages/block-library/src/site-title/edit.js b/packages/block-library/src/site-title/edit.js deleted file mode 100644 index 5a3bb5a0bc264..0000000000000 --- a/packages/block-library/src/site-title/edit.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * WordPress dependencies - */ -import { useEntityProp } from '@wordpress/core-data'; -import { __ } from '@wordpress/i18n'; -import { RichText } from '@wordpress/block-editor'; - -export default function SiteTitleEdit() { - const [ title, setTitle ] = useEntityProp( 'root', 'site', 'title' ); - return ( - - ); -} diff --git a/packages/block-library/src/site-title/edit/index.js b/packages/block-library/src/site-title/edit/index.js new file mode 100644 index 0000000000000..a44629f7edf21 --- /dev/null +++ b/packages/block-library/src/site-title/edit/index.js @@ -0,0 +1,38 @@ +/** + * WordPress dependencies + */ +import { useEntityProp } from '@wordpress/core-data'; +import { BlockControls, RichText } from '@wordpress/block-editor'; +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import LevelToolbar from './level-toolbar'; + +export default function SiteTitleEdit( { + attributes: { level }, + setAttributes, +} ) { + const [ title, setTitle ] = useEntityProp( 'root', 'site', 'title' ); + const tagName = level === 0 ? 'p' : `h${ level }`; + return ( + <> + + + setAttributes( { level: newLevel } ) + } + /> + + + + ); +} diff --git a/packages/block-library/src/site-title/edit/level-icon.js b/packages/block-library/src/site-title/edit/level-icon.js new file mode 100644 index 0000000000000..76d27dda75ea6 --- /dev/null +++ b/packages/block-library/src/site-title/edit/level-icon.js @@ -0,0 +1,27 @@ +/** + * WordPress dependencies + */ +import { SVG, Path } from '@wordpress/components'; + +export default function LevelIcon( { level, isPressed = false } ) { + const levelToPath = { + 0: 'M11 5v7H9.5C7.6 12 6 10.4 6 8.5S7.6 5 9.5 5H11m8-2H9.5C6.5 3 4 5.5 4 8.5S6.5 14 9.5 14H11v7h2V5h2v16h2V5h2V3z', + 1: 'M9 5h2v10H9v-4H5v4H3V5h2v4h4V5zm6.6 0c-.6.9-1.5 1.7-2.6 2v1h2v7h2V5h-1.4z', + 2: '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.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 1.1-.4.3-.8.5-1.3.7-.5.2-1 .3-1.5.3-.8 0-1.6-.1-2.3-.4-.6-.2-1.1-.6-1.6-1-.1-.1 1-1.5 1-1.5zM9 15H7v-4H3v4H1V5h2v4h4V5h2v10z', + 6: 'M9 15H7v-4H3v4H1V5h2v4h4V5h2v10zm8.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', + }; + return ( + + + + ); +} diff --git a/packages/block-library/src/site-title/edit/level-toolbar.js b/packages/block-library/src/site-title/edit/level-toolbar.js new file mode 100644 index 0000000000000..a2edc900fbc43 --- /dev/null +++ b/packages/block-library/src/site-title/edit/level-toolbar.js @@ -0,0 +1,38 @@ +/** + * WordPress dependencies + */ +import { ToolbarGroup } from '@wordpress/components'; +import { __, sprintf } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import LevelIcon from './level-icon'; + +export default function LevelToolbar( { level, onChange } ) { + return ( + } + controls={ [ 0, 1, 2, 3, 4, 5, 6 ].map( ( currentLevel ) => { + const isActive = currentLevel === level; + return { + icon: ( + + ), + title: + currentLevel === 0 + ? __( 'Paragraph' ) + : // translators: %s: heading level e.g: "1", "2", "3" + sprintf( __( 'Heading %d' ), currentLevel ), + isActive, + onClick: () => onChange( currentLevel ), + }; + } ) } + label={ __( 'Change heading level' ) } + /> + ); +} diff --git a/packages/block-library/src/site-title/index.php b/packages/block-library/src/site-title/index.php index 6e5b5786436f0..eddf094f0300f 100644 --- a/packages/block-library/src/site-title/index.php +++ b/packages/block-library/src/site-title/index.php @@ -8,10 +8,16 @@ /** * Renders the `core/site-title` block on the server. * + * @param array $attributes The block attributes. + * * @return string The render. */ -function render_block_core_site_title() { - return sprintf( '

%s

', get_bloginfo( 'name' ) ); +function render_block_core_site_title( $attributes ) { + $tag_name = 'h1'; + if ( isset( $attributes['level'] ) ) { + $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level']; + } + return sprintf( '<%1$s>%2$s', $tag_name, get_bloginfo( 'name' ) ); } /** diff --git a/packages/core-data/src/entity-provider.js b/packages/core-data/src/entity-provider.js index 3a73e5a79c280..d2d0f23569ca4 100644 --- a/packages/core-data/src/entity-provider.js +++ b/packages/core-data/src/entity-provider.js @@ -84,23 +84,20 @@ export function useEntityId( kind, type ) { */ export function useEntityProp( kind, type, prop ) { const id = useEntityId( kind, type ); - const { value, fullValue } = useSelect( ( select ) => { const { getEntityRecord, getEditedEntityRecord } = select( 'core' ); const entity = getEntityRecord( kind, type, id ); // Trigger resolver. const editedEntity = getEditedEntityRecord( kind, type, id ); - return ( - entity && - editedEntity && { - value: editedEntity[ prop ], - fullValue: entity[ prop ], - } - ); + return entity && editedEntity + ? { + value: editedEntity[ prop ], + fullValue: entity[ prop ], + } + : {}; }, [ kind, type, id, prop ] ); - const { editEntityRecord } = useDispatch( 'core' ); const setValue = useCallback( ( newValue ) => { diff --git a/packages/e2e-tests/fixtures/blocks/core__site-title.json b/packages/e2e-tests/fixtures/blocks/core__site-title.json index 6070316cf4179..32dc5aa4633e1 100644 --- a/packages/e2e-tests/fixtures/blocks/core__site-title.json +++ b/packages/e2e-tests/fixtures/blocks/core__site-title.json @@ -3,7 +3,9 @@ "clientId": "_clientId_0", "name": "core/site-title", "isValid": true, - "attributes": {}, + "attributes": { + "level": 1 + }, "innerBlocks": [], "originalContent": "" } diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js index 4757ade7f6229..1900823137c2e 100644 --- a/packages/edit-site/src/components/editor/index.js +++ b/packages/edit-site/src/components/editor/index.js @@ -47,18 +47,20 @@ function Editor( { settings: _settings } ) { return template ? ( - - - -
- - - - + + + + +
+ + + + +