From cf812e995ecb84128437a4f2e2252632d0ceb903 Mon Sep 17 00:00:00 2001 From: epiqueras Date: Thu, 18 Jun 2020 19:20:42 -0700 Subject: [PATCH 1/6] Template Part: Improve insertion flow. --- lib/template-loader.php | 2 +- .../src/template-part/edit/index.js | 38 +++- .../src/template-part/edit/inner-blocks.js | 13 +- .../src/template-part/edit/name-panel.js | 35 +++ .../template-part/edit/placeholder/index.js | 206 ++++++------------ .../src/template-part/editor.scss | 46 +--- .../block-library/src/template-part/index.js | 2 +- .../block-library/src/template-part/index.php | 2 +- 8 files changed, 141 insertions(+), 203 deletions(-) create mode 100644 packages/block-library/src/template-part/edit/name-panel.js diff --git a/lib/template-loader.php b/lib/template-loader.php index 9718b22313093d..3d92da499586ab 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -135,7 +135,7 @@ function gutenberg_override_query_template( $template, $type, array $templates = function create_auto_draft_for_template_part_block( $block ) { $template_part_ids = array(); - if ( 'core/template-part' === $block['blockName'] ) { + if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['slug'] ) ) { if ( isset( $block['attrs']['postId'] ) ) { // Template part is customized. $template_part_id = $block['attrs']['postId']; diff --git a/packages/block-library/src/template-part/edit/index.js b/packages/block-library/src/template-part/edit/index.js index 63f44c3c5755ff..ac93f221b37e06 100644 --- a/packages/block-library/src/template-part/edit/index.js +++ b/packages/block-library/src/template-part/edit/index.js @@ -3,12 +3,12 @@ */ import { useRef, useEffect } from '@wordpress/element'; import { useSelect, useDispatch } from '@wordpress/data'; -import { EntityProvider } from '@wordpress/core-data'; /** * Internal dependencies */ import useTemplatePartPost from './use-template-part-post'; +import TemplatePartNamePanel from './name-panel'; import TemplatePartInnerBlocks from './inner-blocks'; import TemplatePartPlaceholder from './placeholder'; @@ -16,6 +16,7 @@ export default function TemplatePartEdit( { attributes: { postId: _postId, slug, theme }, setAttributes, clientId, + isSelected, } ) { const initialPostId = useRef( _postId ); const initialSlug = useRef( slug ); @@ -28,8 +29,20 @@ export default function TemplatePartEdit( { // but wait until the third inner blocks change, // because the first 2 are just the template part // content loading. - const innerBlocks = useSelect( - ( select ) => select( 'core/block-editor' ).getBlocks( clientId ), + const { innerBlocks, hasSelectedInnerBlock } = useSelect( + ( select ) => { + const { + getBlocks, + hasSelectedInnerBlock: getHasSelectedInnerBlock, + } = select( 'core/block-editor' ); + return { + innerBlocks: getBlocks( clientId ), + hasSelectedInnerBlock: getHasSelectedInnerBlock( + clientId, + true + ), + }; + }, [ clientId ] ); const { editEntityRecord } = useDispatch( 'core' ); @@ -54,13 +67,18 @@ export default function TemplatePartEdit( { if ( postId ) { // Part of a template file, post ID already resolved. return ( - - - + <> + { ( isSelected || hasSelectedInnerBlock ) && ( + + ) } + 0 } + /> + ); } if ( ! initialSlug.current && ! initialTheme.current ) { diff --git a/packages/block-library/src/template-part/edit/inner-blocks.js b/packages/block-library/src/template-part/edit/inner-blocks.js index c0624c933ab174..26e53f2b351e31 100644 --- a/packages/block-library/src/template-part/edit/inner-blocks.js +++ b/packages/block-library/src/template-part/edit/inner-blocks.js @@ -4,16 +4,25 @@ import { useEntityBlockEditor } from '@wordpress/core-data'; import { InnerBlocks } from '@wordpress/block-editor'; -export default function TemplatePartInnerBlocks() { +export default function TemplatePartInnerBlocks( { + postId: id, + hasInnerBlocks, +} ) { const [ blocks, onInput, onChange ] = useEntityBlockEditor( 'postType', - 'wp_template_part' + 'wp_template_part', + { id } ); return ( + } /> ); } diff --git a/packages/block-library/src/template-part/edit/name-panel.js b/packages/block-library/src/template-part/edit/name-panel.js new file mode 100644 index 00000000000000..def2c93364243a --- /dev/null +++ b/packages/block-library/src/template-part/edit/name-panel.js @@ -0,0 +1,35 @@ +/** + * WordPress dependencies + */ +import { useEntityProp } from '@wordpress/core-data'; +import { TextControl } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { cleanForSlug } from '@wordpress/url'; + +export default function TemplatePartNamePanel( { postId, setAttributes } ) { + const [ title, setTitle ] = useEntityProp( + 'postType', + 'wp_template_part', + 'title', + postId + ); + const [ , setSlug ] = useEntityProp( + 'postType', + 'wp_template_part', + 'slug', + postId + ); + return ( + { + setTitle( value ); + const slug = cleanForSlug( value ); + setSlug( slug ); + setAttributes( { slug } ); + } } + /> + ); +} diff --git a/packages/block-library/src/template-part/edit/placeholder/index.js b/packages/block-library/src/template-part/edit/placeholder/index.js index 1da611ae523378..91375a0a95836a 100644 --- a/packages/block-library/src/template-part/edit/placeholder/index.js +++ b/packages/block-library/src/template-part/edit/placeholder/index.js @@ -8,164 +8,82 @@ import { cleanForSlug } from '@wordpress/url'; import { Placeholder, TextControl, + Dropdown, + ButtonGroup, Button, - TabPanel, } from '@wordpress/components'; -import { layout } from '@wordpress/icons'; +import { blockDefault } from '@wordpress/icons'; /** * Internal dependencies */ -import useTemplatePartPost from '../use-template-part-post'; import TemplatePartPreviews from './template-part-previews'; -const HELP_PHRASES = { - initial: __( 'Please add a name and theme for your new Template Part.' ), - unavailable: __( - 'Name and theme combination already in use, please try another.' - ), - available: __( 'This name is available!' ), - error: __( 'Error adding template.' ), -}; - export default function TemplatePartPlaceholder( { setAttributes } ) { - const [ slug, _setSlug ] = useState( '' ); - const [ theme, _setTheme ] = useState( '' ); - const [ help, setHelp ] = useState( '' ); - - // Try to find an existing template part. - const postId = useTemplatePartPost( null, slug, theme ); - - const setSlug = useCallback( - ( nextSlug ) => { - _setSlug( nextSlug ); - if ( help ) { - setHelp( '' ); - } - }, - [ help ] - ); - - const setTheme = useCallback( - ( nextTheme ) => { - _setTheme( nextTheme ); - if ( help ) { - setHelp( '' ); - } - }, - [ help ] - ); - - const getHelpPhrase = () => { - if ( ! slug || ! theme ) { - return HELP_PHRASES.initial; - } else if ( postId ) { - return HELP_PHRASES.unavailable; - } - - return HELP_PHRASES.available; - }; - const { saveEntityRecord } = useDispatch( 'core' ); const onCreate = useCallback( async () => { - const nextAttributes = { slug, theme }; - // Create a new template part. - try { - const cleanSlug = cleanForSlug( slug ); - const templatePart = await saveEntityRecord( - 'postType', - 'wp_template_part', - { - title: cleanSlug, - status: 'publish', - slug: cleanSlug, - meta: { theme }, - } - ); - nextAttributes.postId = templatePart.id; - } catch ( err ) { - setHelp( HELP_PHRASES.error ); - } - setAttributes( nextAttributes ); - }, [ postId, slug, theme ] ); + const title = 'Untitled Section'; + const slug = cleanForSlug( title ); + const templatePart = await saveEntityRecord( + 'postType', + 'wp_template_part', + { + title, + status: 'publish', + slug, + meta: { theme: 'custom' }, + } + ); + setAttributes( { + postId: templatePart.id, + slug: templatePart.slug, + theme: templatePart.meta.theme, + } ); + }, [ setAttributes ] ); const [ filterValue, setFilterValue ] = useState( '' ); - - const createTab = ( - <> -
- - -
-
- { help || getHelpPhrase() } -
- - - ); - - const selectTab = ( - <> - - -
- -
- - ); - return ( - - - { ( tab ) => { - if ( tab.name === 'create' ) { - return createTab; - } - return selectTab; - } } - + + ( + + + + + ) } + renderContent={ () => ( + <> + +
+ +
+ + ) } + />
); } diff --git a/packages/block-library/src/template-part/editor.scss b/packages/block-library/src/template-part/editor.scss index 64fed43e2eec91..1920bcbe36b38f 100644 --- a/packages/block-library/src/template-part/editor.scss +++ b/packages/block-library/src/template-part/editor.scss @@ -1,50 +1,8 @@ -.wp-block-template-part__placeholder-tabs { - display: flex; - flex-grow: 1; - flex-direction: column; - - .components-tab-panel__tabs { - border-bottom: $border-width solid $light-gray-500; - - .components-tab-panel__tabs-item { - flex-grow: 1; - margin-bottom: -$border-width; - } - } - - .components-tab-panel__tab-content { - margin-top: $grid-unit-20; - display: flex; - flex-grow: 1; - flex-direction: column; - position: relative; - } - - .wp-block-template-part__placeholder-input-container { - display: flex; - flex-wrap: wrap; - width: 100%; - - .wp-block-template-part__placeholder-input { - margin: 5px; - } - } - - .wp-block-template-part__placeholder-help-phrase { - padding: 0 $grid-unit-15 $grid-unit-15; - } - - .wp-block-template-part__placeholder-create-button { - align-self: flex-start; - } - - .wp-block-template-part__placeholder-preview-filter-input { - width: inherit; - } +.wp-block-template-part__placeholder-preview-filter-input { + width: inherit; } - .wp-block-template-part__placeholder-preview-container { width: inherit; max-height: 600px; diff --git a/packages/block-library/src/template-part/index.js b/packages/block-library/src/template-part/index.js index af507228b2afa3..7dffabe66e18b6 100644 --- a/packages/block-library/src/template-part/index.js +++ b/packages/block-library/src/template-part/index.js @@ -18,7 +18,7 @@ const { name } = metadata; export { metadata, name }; export const settings = { - title: __( 'Template Part' ), + title: __( 'Section' ), __experimentalLabel: ( { slug } ) => startCase( slug ), edit, }; diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index bf40670885f9d1..de8de1212ca1fe 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -19,7 +19,7 @@ function render_block_core_template_part( $attributes ) { // If we have a post ID, which means this template part // is user-customized, render the corresponding post content. $content = get_post( $attributes['postId'] )->post_content; - } elseif ( wp_get_theme()->get( 'TextDomain' ) === $attributes['theme'] ) { + } elseif ( isset( $attributes['theme'] ) && wp_get_theme()->get( 'TextDomain' ) === $attributes['theme'] ) { $template_part_query = new WP_Query( array( 'post_type' => 'wp_template_part', From 1da80b6058dffb408584088d030f019643ca71f4 Mon Sep 17 00:00:00 2001 From: epiqueras Date: Fri, 19 Jun 2020 12:07:40 -0700 Subject: [PATCH 2/6] Template Part: Fix styles and remove unnecessary properties. --- packages/base-styles/_z-index.scss | 1 + .../src/template-part/edit/inner-blocks.js | 7 ++--- .../template-part/edit/placeholder/index.js | 3 ++- .../src/template-part/editor.scss | 26 ++++++++++++------- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/base-styles/_z-index.scss b/packages/base-styles/_z-index.scss index e272890ea49997..f1fe6a82f7f0c7 100644 --- a/packages/base-styles/_z-index.scss +++ b/packages/base-styles/_z-index.scss @@ -33,6 +33,7 @@ $z-layers: ( ".wp-block-cover__inner-container": 1, // InnerBlocks area inside cover image block ".wp-block-cover.has-background-dim::before": 1, // Overlay area inside block cover need to be higher than the video background. ".wp-block-cover__video-background": 0, // Video background inside cover block. + ".wp-block-template-part__placeholder-preview-filter-input": 1, // Active pill button ".components-button {:focus or .is-primary}": 1, diff --git a/packages/block-library/src/template-part/edit/inner-blocks.js b/packages/block-library/src/template-part/edit/inner-blocks.js index 26e53f2b351e31..789efb2e24689b 100644 --- a/packages/block-library/src/template-part/edit/inner-blocks.js +++ b/packages/block-library/src/template-part/edit/inner-blocks.js @@ -4,6 +4,7 @@ import { useEntityBlockEditor } from '@wordpress/core-data'; import { InnerBlocks } from '@wordpress/block-editor'; +const renderAppender = () => ; export default function TemplatePartInnerBlocks( { postId: id, hasInnerBlocks, @@ -18,11 +19,7 @@ export default function TemplatePartInnerBlocks( { value={ blocks } onInput={ onInput } onChange={ onChange } - renderAppender={ - hasInnerBlocks - ? undefined - : () => - } + renderAppender={ hasInnerBlocks ? undefined : renderAppender } /> ); } diff --git a/packages/block-library/src/template-part/edit/placeholder/index.js b/packages/block-library/src/template-part/edit/placeholder/index.js index 91375a0a95836a..2471ad1bf3f602 100644 --- a/packages/block-library/src/template-part/edit/placeholder/index.js +++ b/packages/block-library/src/template-part/edit/placeholder/index.js @@ -51,7 +51,8 @@ export default function TemplatePartPlaceholder( { setAttributes } ) { ) } > (