From 423d35a8fe87e8f985bb14c1f0a034e2c01cd9f6 Mon Sep 17 00:00:00 2001 From: apeatling Date: Wed, 3 Jun 2020 14:27:15 -0700 Subject: [PATCH 1/6] Add support to the post title block for text alignment and heading level. --- .../block-library/src/post-title/block.json | 9 ++++ packages/block-library/src/post-title/edit.js | 53 ++++++++++++++++++- .../block-library/src/post-title/index.php | 12 ++++- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/post-title/block.json b/packages/block-library/src/post-title/block.json index 4142f4cb1d887c..d9977626e79c19 100644 --- a/packages/block-library/src/post-title/block.json +++ b/packages/block-library/src/post-title/block.json @@ -5,6 +5,15 @@ "postId", "postType" ], + "attributes": { + "align": { + "type": "string" + }, + "level": { + "type": "number", + "default": 2 + } + }, "supports": { "html": false } diff --git a/packages/block-library/src/post-title/edit.js b/packages/block-library/src/post-title/edit.js index e06cdddee244a7..a954e93cd0a3de 100644 --- a/packages/block-library/src/post-title/edit.js +++ b/packages/block-library/src/post-title/edit.js @@ -1,10 +1,32 @@ +/** + * External dependencies + */ +import classnames from 'classnames'; + /** * WordPress dependencies */ import { useSelect } from '@wordpress/data'; +import { + AlignmentToolbar, + BlockControls, + __experimentalBlock as Block, +} from '@wordpress/block-editor'; +import { ToolbarGroup } from '@wordpress/components'; -export default function PostTitleEdit( { context } ) { +/** + * Internal dependencies + */ +import HeadingLevelDropdown from '../heading/heading-level-dropdown'; + +export default function PostTitleEdit( { + attributes, + setAttributes, + context, +} ) { + const { level, align } = attributes; const { postType, postId } = context; + const tagName = 0 === level ? 'p' : 'h' + level; const post = useSelect( ( select ) => @@ -20,5 +42,32 @@ export default function PostTitleEdit( { context } ) { return null; } - return

{ post.title }

; + return ( + <> + + + + setAttributes( { level: newLevel } ) + } + /> + + { + setAttributes( { align: nextAlign } ); + } } + /> + + + { post.title } + + + ); } diff --git a/packages/block-library/src/post-title/index.php b/packages/block-library/src/post-title/index.php index 0e30b478a39fe2..927084266b1564 100644 --- a/packages/block-library/src/post-title/index.php +++ b/packages/block-library/src/post-title/index.php @@ -19,7 +19,17 @@ function render_block_core_post_title( $attributes, $content, $block ) { return ''; } - return '

' . get_the_title( $block->context['postId'] ) . '

'; + $tag_name = 'h2'; + $class = ''; + + if ( isset( $attributes['level'] ) ) { + $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level']; + } + if ( isset( $attributes['align'] ) ) { + $class = ' class="has-text-align-' . $attributes['align'] . '"'; + } + + return sprintf( '<%1$s%2$s>%3$s', $tag_name, $class, get_the_title( $block->context['postId'] ) ); } /** From 5b11a164a4d94940251886df3ec501072c2b50a1 Mon Sep 17 00:00:00 2001 From: apeatling Date: Thu, 4 Jun 2020 11:07:48 -0700 Subject: [PATCH 2/6] Fix up classname output, and add a default post title classname. --- packages/block-library/src/post-title/index.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/post-title/index.php b/packages/block-library/src/post-title/index.php index 927084266b1564..25492fee518c40 100644 --- a/packages/block-library/src/post-title/index.php +++ b/packages/block-library/src/post-title/index.php @@ -20,16 +20,18 @@ function render_block_core_post_title( $attributes, $content, $block ) { } $tag_name = 'h2'; - $class = ''; + $align_class_name = empty( $attributes['align'] ) ? '' : ' ' . "has-text-align-{$attributes['align']}"; if ( isset( $attributes['level'] ) ) { $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level']; } - if ( isset( $attributes['align'] ) ) { - $class = ' class="has-text-align-' . $attributes['align'] . '"'; - } - return sprintf( '<%1$s%2$s>%3$s', $tag_name, $class, get_the_title( $block->context['postId'] ) ); + return sprintf( + '<%1$s class="%2$s">%3$s', + $tag_name, + 'wp-block-post-title' . esc_attr( $align_class_name ), + get_the_title( $block->context['postId'] ) + ); } /** From 3332552bb960c8017adabd3805255a3170bdad36 Mon Sep 17 00:00:00 2001 From: apeatling Date: Fri, 5 Jun 2020 10:55:31 -0700 Subject: [PATCH 3/6] Add lightBlockWrapper, and __experimentalSelector to block.json. --- packages/block-library/src/post-title/block.json | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/post-title/block.json b/packages/block-library/src/post-title/block.json index d9977626e79c19..592722c992bdfe 100644 --- a/packages/block-library/src/post-title/block.json +++ b/packages/block-library/src/post-title/block.json @@ -15,6 +15,16 @@ } }, "supports": { - "html": false + "html": false, + "lightBlockWrapper": true, + "__experimentalSelector": { + "core/post-title/h1": "h1", + "core/post-title/h2": "h2", + "core/post-title/h3": "h3", + "core/post-title/h4": "h4", + "core/post-title/h5": "h5", + "core/post-title/h6": "h6", + "core/post-title/p": "p", + } } } From dfed09226c862e109afe9acb00bab4ecd45d3838 Mon Sep 17 00:00:00 2001 From: apeatling Date: Fri, 5 Jun 2020 13:02:38 -0700 Subject: [PATCH 4/6] Fix block JSON trailing comma. --- packages/block-library/src/post-title/block.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-title/block.json b/packages/block-library/src/post-title/block.json index 592722c992bdfe..da9e1dc8c3978c 100644 --- a/packages/block-library/src/post-title/block.json +++ b/packages/block-library/src/post-title/block.json @@ -24,7 +24,7 @@ "core/post-title/h4": "h4", "core/post-title/h5": "h5", "core/post-title/h6": "h6", - "core/post-title/p": "p", + "core/post-title/p": "p" } } } From dee15ea412e2aac476499ac27893b0b67ec19abd Mon Sep 17 00:00:00 2001 From: apeatling Date: Fri, 5 Jun 2020 13:37:28 -0700 Subject: [PATCH 5/6] Fix PHP spacing lint errors. --- packages/block-library/src/post-title/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/post-title/index.php b/packages/block-library/src/post-title/index.php index 25492fee518c40..b09bb7ba93969f 100644 --- a/packages/block-library/src/post-title/index.php +++ b/packages/block-library/src/post-title/index.php @@ -19,8 +19,8 @@ function render_block_core_post_title( $attributes, $content, $block ) { return ''; } - $tag_name = 'h2'; - $align_class_name = empty( $attributes['align'] ) ? '' : ' ' . "has-text-align-{$attributes['align']}"; + $tag_name = 'h2'; + $align_class_name = empty( $attributes['align'] ) ? '' : ' ' . "has-text-align-{$attributes['align']}"; if ( isset( $attributes['level'] ) ) { $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level']; From 48899c450bfdd20fffd83ae13e9a17dacb0b1e7a Mon Sep 17 00:00:00 2001 From: apeatling Date: Mon, 8 Jun 2020 10:12:43 -0700 Subject: [PATCH 6/6] Update the core post title fixture. --- packages/e2e-tests/fixtures/blocks/core__post-title.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/e2e-tests/fixtures/blocks/core__post-title.json b/packages/e2e-tests/fixtures/blocks/core__post-title.json index a3c59c8991b37b..39a4acff96bca7 100644 --- a/packages/e2e-tests/fixtures/blocks/core__post-title.json +++ b/packages/e2e-tests/fixtures/blocks/core__post-title.json @@ -3,7 +3,9 @@ "clientId": "_clientId_0", "name": "core/post-title", "isValid": true, - "attributes": {}, + "attributes": { + "level": 2 + }, "innerBlocks": [], "originalContent": "" }