From 720b3c6fe4bb152233d9fdf8cbbbeb989bad2fc8 Mon Sep 17 00:00:00 2001 From: ramon Date: Fri, 5 Jan 2024 15:52:26 +1100 Subject: [PATCH 1/9] Initial commit - initial paragraph block can be selected. --- .../block-library/src/post-content/edit.js | 115 +++++++++++++----- 1 file changed, 83 insertions(+), 32 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index ae3bb938c90eee..640987dbb0ab2d 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -8,13 +8,19 @@ import { __experimentalRecursionProvider as RecursionProvider, __experimentalUseHasRecursion as useHasRecursion, Warning, + store as blockEditorStore, } from '@wordpress/block-editor'; import { useEntityProp, useEntityBlockEditor, store as coreStore, } from '@wordpress/core-data'; -import { useSelect } from '@wordpress/data'; +import { useSelect, useDispatch } from '@wordpress/data'; +import { Placeholder, Button } from '@wordpress/components'; +import { postContent as icon } from '@wordpress/icons'; +import { useState } from '@wordpress/element'; +import { createBlock } from '@wordpress/blocks'; + /** * Internal dependencies */ @@ -45,9 +51,60 @@ function ReadOnlyContent( { ); } -function EditableContent( { context = {} } ) { - const { postType, postId } = context; +function EmptyContentPlaceholder( { context, onClose } ) { + const { postType } = context; + const label = + 'page' === postType + ? __( 'This page’s content is empty' ) + : __( 'This post’s content is empty' ); + return ( + + + + + + ); +} + +function PostContentPlaceholder( { layoutClassNames } ) { + const blockProps = useBlockProps( { className: layoutClassNames } ); + return ( +
+

+ { __( + 'This is the Content block, it will display all the blocks in any single post or page.' + ) } +

+

+ { __( + 'That might be a simple arrangement like consecutive paragraphs in a blog post, or a more elaborate composition that includes image galleries, videos, tables, columns, and any other block types.' + ) } +

+

+ { __( + 'If there are any Custom Post Types registered at your site, the Content block can display the contents of those entries as well.' + ) } +

+
+ ); +} +function EditableContent( { context = {}, clientId } ) { + const { postType, postId } = context; + const { selectBlock, insertBlock } = useDispatch( blockEditorStore ); const [ blocks, onInput, onChange ] = useEntityBlockEditor( 'postType', postType, @@ -66,19 +123,34 @@ function EditableContent( { context = {} } ) { ); const hasInnerBlocks = !! entityRecord?.content?.raw || blocks?.length; + const [ hasPlaceholder, setHasPlaceholder ] = useState( ! hasInnerBlocks ); - const initialInnerBlocks = [ [ 'core/paragraph' ] ]; - - const props = useInnerBlocksProps( + const { children, ...props } = useInnerBlocksProps( useBlockProps( { className: 'entry-content' } ), { value: blocks, onInput, onChange, - template: ! hasInnerBlocks ? initialInnerBlocks : undefined, } ); - return
; + const onClose = async () => { + await setHasPlaceholder( false ); + const initialBlock = createBlock( 'core/paragraph' ); + insertBlock( initialBlock, 0, clientId ); + selectBlock( initialBlock.clientId ); + }; + + return ( +
+ { children } + { hasPlaceholder && ( + + ) } +
+ ); } function Content( props ) { @@ -104,29 +176,6 @@ function Content( props ) { ); } -function Placeholder( { layoutClassNames } ) { - const blockProps = useBlockProps( { className: layoutClassNames } ); - return ( -
-

- { __( - 'This is the Content block, it will display all the blocks in any single post or page.' - ) } -

-

- { __( - 'That might be a simple arrangement like consecutive paragraphs in a blog post, or a more elaborate composition that includes image galleries, videos, tables, columns, and any other block types.' - ) } -

-

- { __( - 'If there are any Custom Post Types registered at your site, the Content block can display the contents of those entries as well.' - ) } -

-
- ); -} - function RecursionError() { const blockProps = useBlockProps(); return ( @@ -139,6 +188,7 @@ function RecursionError() { } export default function PostContentEdit( { + clientId, context, __unstableLayoutClassNames: layoutClassNames, } ) { @@ -153,11 +203,12 @@ export default function PostContentEdit( { { contextPostId && contextPostType ? ( ) : ( - + ) } ); From f1eb82ac3ddc7f50f6becf2b010be06cc9eef21c Mon Sep 17 00:00:00 2001 From: ramon Date: Fri, 5 Jan 2024 16:52:28 +1100 Subject: [PATCH 2/9] Hacking to get patterns tab open. Need to head check this --- .../src/components/inserter/library.js | 2 + .../block-library/src/post-content/edit.js | 38 ++++++++++++------- .../src/components/inserter-sidebar/index.js | 21 ++++++---- .../editor/src/store/private-selectors.js | 16 +++++++- 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/packages/block-editor/src/components/inserter/library.js b/packages/block-editor/src/components/inserter/library.js index 7d462b343c310d..9715f9334102e2 100644 --- a/packages/block-editor/src/components/inserter/library.js +++ b/packages/block-editor/src/components/inserter/library.js @@ -21,6 +21,7 @@ function InserterLibrary( showMostUsedBlocks = false, __experimentalInsertionIndex, __experimentalFilterValue, + __experimentalInitialCategory, onSelect = noop, shouldFocusBlock = false, }, @@ -46,6 +47,7 @@ function InserterLibrary( isAppender={ isAppender } showInserterHelpPanel={ showInserterHelpPanel } showMostUsedBlocks={ showMostUsedBlocks } + __experimentalInitialCategory={ __experimentalInitialCategory } __experimentalInsertionIndex={ __experimentalInsertionIndex } __experimentalFilterValue={ __experimentalFilterValue } shouldFocusBlock={ shouldFocusBlock } diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index 640987dbb0ab2d..5415bcb18c5ca1 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -51,7 +51,7 @@ function ReadOnlyContent( { ); } -function EmptyContentPlaceholder( { context, onClose } ) { +function EmptyContentPlaceholder( { context, onClose, openInserter } ) { const { postType } = context; const label = 'page' === postType @@ -65,10 +65,7 @@ function EmptyContentPlaceholder( { context, onClose } ) { 'Add your first block or pattern to get started.' ) } > - @@ -111,13 +108,18 @@ function EditableContent( { context = {}, clientId } ) { { id: postId } ); - const entityRecord = useSelect( + const { entityRecord, setInserterIsOpened } = useSelect( ( select ) => { - return select( coreStore ).getEntityRecord( - 'postType', - postType, - postId - ); + return { + entityRecord: select( coreStore ).getEntityRecord( + 'postType', + postType, + postId + ), + setInserterIsOpened: + select( blockEditorStore ).getSettings() + .__experimentalSetIsInserterOpened, + }; }, [ postType, postId ] ); @@ -133,13 +135,22 @@ function EditableContent( { context = {}, clientId } ) { onChange, } ); - const onClose = async () => { - await setHasPlaceholder( false ); + const onClose = () => { + setHasPlaceholder( false ); const initialBlock = createBlock( 'core/paragraph' ); insertBlock( initialBlock, 0, clientId ); selectBlock( initialBlock.clientId ); }; + const openInserter = () => { + setHasPlaceholder( false ); + setInserterIsOpened( { + initialCategory: 'patterns', + rootClientId: clientId, + insertionIndex: 0, + } ); + }; + return (
{ children } @@ -147,6 +158,7 @@ function EditableContent( { context = {}, clientId } ) { ) }
diff --git a/packages/editor/src/components/inserter-sidebar/index.js b/packages/editor/src/components/inserter-sidebar/index.js index 7db4335309935a..15e9230a61d0ac 100644 --- a/packages/editor/src/components/inserter-sidebar/index.js +++ b/packages/editor/src/components/inserter-sidebar/index.js @@ -20,14 +20,18 @@ import { unlock } from '../../lock-unlock'; import { store as editorStore } from '../../store'; export default function InserterSidebar() { - const { insertionPoint, showMostUsedBlocks } = useSelect( ( select ) => { - const { getInsertionPoint } = unlock( select( editorStore ) ); - const { get } = select( preferencesStore ); - return { - insertionPoint: getInsertionPoint(), - showMostUsedBlocks: get( 'core', 'mostUsedBlocks' ), - }; - }, [] ); + const { insertionPoint, inserterInitialCategory, showMostUsedBlocks } = + useSelect( ( select ) => { + const { getInsertionPoint, getInserterInitialCategory } = unlock( + select( editorStore ) + ); + const { get } = select( preferencesStore ); + return { + insertionPoint: getInsertionPoint(), + inserterInitialCategory: getInserterInitialCategory(), + showMostUsedBlocks: get( 'core', 'mostUsedBlocks' ), + }; + }, [] ); const { setIsInserterOpened } = useDispatch( editorStore ); const isMobileViewport = useViewportMatch( 'medium', '<' ); @@ -64,6 +68,7 @@ export default function InserterSidebar() { __experimentalInsertionIndex={ insertionPoint.insertionIndex } + __experimentalInitialCategory={ inserterInitialCategory } __experimentalFilterValue={ insertionPoint.filterValue } ref={ libraryRef } /> diff --git a/packages/editor/src/store/private-selectors.js b/packages/editor/src/store/private-selectors.js index e276859f884038..6afa4c96a91170 100644 --- a/packages/editor/src/store/private-selectors.js +++ b/packages/editor/src/store/private-selectors.js @@ -25,7 +25,11 @@ const EMPTY_INSERTION_POINT = { export const getInsertionPoint = createRegistrySelector( ( select ) => ( state ) => { if ( typeof state.blockInserterPanel === 'object' ) { - return state.blockInserterPanel; + return { + rootClientId: state.blockInserterPanel?.rootClientId, + insertionIndex: state.blockInserterPanel?.insertionIndex, + filterValue: state.blockInserterPanel?.filterValue, + }; } if ( getRenderingMode( state ) === 'template-locked' ) { @@ -46,6 +50,16 @@ export const getInsertionPoint = createRegistrySelector( } ); +export const getInserterInitialCategory = createRegistrySelector( + () => ( state ) => { + if ( typeof state.blockInserterPanel === 'object' ) { + return state.blockInserterPanel?.initialCategory; + } + + return null; + } +); + export function getListViewToggleRef( state ) { return state.listViewToggleRef; } From a13743bd8c8a29f256273760d9ebb24f07a3f636 Mon Sep 17 00:00:00 2001 From: ramon Date: Fri, 5 Jan 2024 16:53:03 +1100 Subject: [PATCH 3/9] Hacking to get patterns tab open. Need to head check this --- packages/block-editor/src/components/inserter/menu.js | 7 +++++-- packages/block-editor/src/components/inserter/tabs.js | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/block-editor/src/components/inserter/menu.js b/packages/block-editor/src/components/inserter/menu.js index 6a38e52cbffba1..8e93ed59836ee1 100644 --- a/packages/block-editor/src/components/inserter/menu.js +++ b/packages/block-editor/src/components/inserter/menu.js @@ -45,6 +45,7 @@ function InserterMenu( showMostUsedBlocks, __experimentalFilterValue = '', shouldFocusBlock = true, + __experimentalInitialCategory, }, ref ) { @@ -56,8 +57,9 @@ function InserterMenu( const [ patternFilter, setPatternFilter ] = useState( 'all' ); const [ selectedMediaCategory, setSelectedMediaCategory ] = useState( null ); - const [ selectedTab, setSelectedTab ] = useState( null ); - + const [ selectedTab, setSelectedTab ] = useState( + __experimentalInitialCategory || null + ); const [ destinationRootClientId, onInsertBlocks, onToggleInsertionPoint ] = useInsertionPoint( { rootClientId, @@ -259,6 +261,7 @@ function InserterMenu( showMedia={ showMedia } onSelect={ handleSetSelectedTab } tabsContents={ inserterTabsContents } + initialTabId={ __experimentalInitialCategory } /> ) } { ! delayedFilterValue && ! showAsTabs && ( diff --git a/packages/block-editor/src/components/inserter/tabs.js b/packages/block-editor/src/components/inserter/tabs.js index 4795c3ce4fdc24..af86d7dc69a8db 100644 --- a/packages/block-editor/src/components/inserter/tabs.js +++ b/packages/block-editor/src/components/inserter/tabs.js @@ -33,6 +33,7 @@ function InserterTabs( { showMedia = false, onSelect, tabsContents, + initialTabId, } ) { const tabs = [ blocksTab, @@ -42,7 +43,7 @@ function InserterTabs( { return (
- + { tabs.map( ( tab ) => ( From 320d4544ca0c62502b389cd0026d0322d7dd971f Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 9 Jan 2024 14:20:39 +1100 Subject: [PATCH 4/9] Adding styles Adding hook to set placeholder --- packages/block-library/src/editor.scss | 1 + .../block-library/src/post-content/edit.js | 43 +++++++++++-------- .../src/post-content/editor.scss | 26 +++++++++++ 3 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 packages/block-library/src/post-content/editor.scss diff --git a/packages/block-library/src/editor.scss b/packages/block-library/src/editor.scss index e3642868034a5a..e2a9262362706a 100644 --- a/packages/block-library/src/editor.scss +++ b/packages/block-library/src/editor.scss @@ -32,6 +32,7 @@ @import "./nextpage/editor.scss"; @import "./page-list/editor.scss"; @import "./paragraph/editor.scss"; +@import "./post-content/editor.scss"; @import "./post-excerpt/editor.scss"; @import "./pullquote/editor.scss"; @import "./rss/editor.scss"; diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index 5415bcb18c5ca1..8657a1a9c9c213 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -1,3 +1,8 @@ +/** + * External dependencies + */ +import classnames from 'classnames'; + /** * WordPress dependencies */ @@ -18,7 +23,7 @@ import { import { useSelect, useDispatch } from '@wordpress/data'; import { Placeholder, Button } from '@wordpress/components'; import { postContent as icon } from '@wordpress/icons'; -import { useState } from '@wordpress/element'; +import { useState, useEffect } from '@wordpress/element'; import { createBlock } from '@wordpress/blocks'; /** @@ -80,21 +85,14 @@ function PostContentPlaceholder( { layoutClassNames } ) { const blockProps = useBlockProps( { className: layoutClassNames } ); return (
-

- { __( - 'This is the Content block, it will display all the blocks in any single post or page.' - ) } -

-

- { __( - 'That might be a simple arrangement like consecutive paragraphs in a blog post, or a more elaborate composition that includes image galleries, videos, tables, columns, and any other block types.' - ) } -

-

- { __( - 'If there are any Custom Post Types registered at your site, the Content block can display the contents of those entries as well.' - ) } -

+ +

+ { __( 'This block will be replaced with your content.' ) } +

+
); } @@ -128,13 +126,23 @@ function EditableContent( { context = {}, clientId } ) { const [ hasPlaceholder, setHasPlaceholder ] = useState( ! hasInnerBlocks ); const { children, ...props } = useInnerBlocksProps( - useBlockProps( { className: 'entry-content' } ), + useBlockProps( { + className: classnames( 'entry-content', { + 'wp-block-post-content__placeholder': hasPlaceholder, + } ), + } ), { value: blocks, onInput, onChange, } ); + + useEffect( + () => setHasPlaceholder( ! hasInnerBlocks ), + [ hasInnerBlocks ] + ); + const onClose = () => { setHasPlaceholder( false ); const initialBlock = createBlock( 'core/paragraph' ); @@ -143,7 +151,6 @@ function EditableContent( { context = {}, clientId } ) { }; const openInserter = () => { - setHasPlaceholder( false ); setInserterIsOpened( { initialCategory: 'patterns', rootClientId: clientId, diff --git a/packages/block-library/src/post-content/editor.scss b/packages/block-library/src/post-content/editor.scss new file mode 100644 index 00000000000000..4c4ecba63d90f2 --- /dev/null +++ b/packages/block-library/src/post-content/editor.scss @@ -0,0 +1,26 @@ +.wp-block-post-content__placeholder { + .components-placeholder { + min-height: auto; + box-shadow: none; + padding: $grid-unit-30; + } +} + +.wp-block-post-content__content-placeholder { + &.has-illustration::before { + border: 1px solid currentColor; + background: none; + } + .components-placeholder__illustration { + opacity: 0.1; + } + .components-placeholder__fieldset { + font-size: inherit; + font-family: inherit; + align-self: center; + p { + font-size: inherit; + font-family: inherit; + } + } +} From 456088192a4e1005b2d9180863509b44b9f37fe3 Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 9 Jan 2024 15:50:55 +1100 Subject: [PATCH 5/9] Remove internal state, derive from props. --- packages/block-library/src/post-content/edit.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index 8657a1a9c9c213..ac0cd745611c54 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -23,7 +23,6 @@ import { import { useSelect, useDispatch } from '@wordpress/data'; import { Placeholder, Button } from '@wordpress/components'; import { postContent as icon } from '@wordpress/icons'; -import { useState, useEffect } from '@wordpress/element'; import { createBlock } from '@wordpress/blocks'; /** @@ -87,7 +86,7 @@ function PostContentPlaceholder( { layoutClassNames } ) {

{ __( 'This block will be replaced with your content.' ) } @@ -123,12 +122,11 @@ function EditableContent( { context = {}, clientId } ) { ); const hasInnerBlocks = !! entityRecord?.content?.raw || blocks?.length; - const [ hasPlaceholder, setHasPlaceholder ] = useState( ! hasInnerBlocks ); const { children, ...props } = useInnerBlocksProps( useBlockProps( { className: classnames( 'entry-content', { - 'wp-block-post-content__placeholder': hasPlaceholder, + 'wp-block-post-content__placeholder': ! hasInnerBlocks, } ), } ), { @@ -138,13 +136,7 @@ function EditableContent( { context = {}, clientId } ) { } ); - useEffect( - () => setHasPlaceholder( ! hasInnerBlocks ), - [ hasInnerBlocks ] - ); - const onClose = () => { - setHasPlaceholder( false ); const initialBlock = createBlock( 'core/paragraph' ); insertBlock( initialBlock, 0, clientId ); selectBlock( initialBlock.clientId ); @@ -161,7 +153,7 @@ function EditableContent( { context = {}, clientId } ) { return (

{ children } - { hasPlaceholder && ( + { ! hasInnerBlocks && ( Date: Tue, 9 Jan 2024 16:15:35 +1100 Subject: [PATCH 6/9] Update docs Only use initialTabId if a corresponding tab exists --- .../data/data-core-customize-widgets.md | 1 + .../data/data-core-edit-widgets.md | 1 + .../reference-guides/data/data-core-editor.md | 1 + .../src/components/inserter/tabs.js | 4 ++++ .../customize-widgets/src/store/actions.js | 14 ++++++++------ packages/edit-widgets/src/store/actions.js | 14 ++++++++------ packages/editor/src/store/actions.js | 14 ++++++++------ .../editor/src/store/private-selectors.js | 19 ++++++++++++------- 8 files changed, 43 insertions(+), 25 deletions(-) diff --git a/docs/reference-guides/data/data-core-customize-widgets.md b/docs/reference-guides/data/data-core-customize-widgets.md index 8796f4cfea5ddf..5856803d830574 100644 --- a/docs/reference-guides/data/data-core-customize-widgets.md +++ b/docs/reference-guides/data/data-core-customize-widgets.md @@ -78,6 +78,7 @@ _Parameters_ - _value_ `boolean|Object`: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object. - _value.rootClientId_ `string`: The root client ID to insert at. - _value.insertionIndex_ `number`: The index to insert at. +- _value.initialCategory_ `string`: The tab category to display first when the block editor inserter is opened. A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. _Returns_ diff --git a/docs/reference-guides/data/data-core-edit-widgets.md b/docs/reference-guides/data/data-core-edit-widgets.md index 2dde89bf69ad73..f936a40f11dfe5 100644 --- a/docs/reference-guides/data/data-core-edit-widgets.md +++ b/docs/reference-guides/data/data-core-edit-widgets.md @@ -215,6 +215,7 @@ _Parameters_ - _value_ `boolean|Object`: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object. - _value.rootClientId_ `string`: The root client ID to insert at. - _value.insertionIndex_ `number`: The index to insert at. +- _value.initialCategory_ `string`: The tab category to display first when the block editor inserter is opened. A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. _Returns_ diff --git a/docs/reference-guides/data/data-core-editor.md b/docs/reference-guides/data/data-core-editor.md index be2276bbf03641..b45111e47d5ded 100644 --- a/docs/reference-guides/data/data-core-editor.md +++ b/docs/reference-guides/data/data-core-editor.md @@ -1382,6 +1382,7 @@ _Parameters_ - _value_ `boolean|Object`: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object. - _value.rootClientId_ `string`: The root client ID to insert at. - _value.insertionIndex_ `number`: The index to insert at. +- _value.initialCategory_ `string`: The tab category to display first when the block editor inserter is opened. A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. _Returns_ diff --git a/packages/block-editor/src/components/inserter/tabs.js b/packages/block-editor/src/components/inserter/tabs.js index af86d7dc69a8db..1ea917596711ac 100644 --- a/packages/block-editor/src/components/inserter/tabs.js +++ b/packages/block-editor/src/components/inserter/tabs.js @@ -41,6 +41,10 @@ function InserterTabs( { showMedia && mediaTab, ].filter( Boolean ); + initialTabId = !! tabs.find( ( { name } ) => initialTabId === name ) + ? initialTabId + : 'blocks'; + return (
diff --git a/packages/customize-widgets/src/store/actions.js b/packages/customize-widgets/src/store/actions.js index 8656ef8854c9f5..eccd1a85272b5a 100644 --- a/packages/customize-widgets/src/store/actions.js +++ b/packages/customize-widgets/src/store/actions.js @@ -1,12 +1,14 @@ /** * Returns an action object used to open/close the inserter. * - * @param {boolean|Object} value Whether the inserter should be - * opened (true) or closed (false). - * To specify an insertion point, - * use an object. - * @param {string} value.rootClientId The root client ID to insert at. - * @param {number} value.insertionIndex The index to insert at. + * @param {boolean|Object} value Whether the inserter should be + * opened (true) or closed (false). + * To specify an insertion point, + * use an object. + * @param {string} value.rootClientId The root client ID to insert at. + * @param {number} value.insertionIndex The index to insert at. + * @param {string} value.initialCategory The tab category to display first when the block editor inserter is opened. + * A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. * * @example * ```js diff --git a/packages/edit-widgets/src/store/actions.js b/packages/edit-widgets/src/store/actions.js index 8124ace66bdb3a..bac053e9a90b4c 100644 --- a/packages/edit-widgets/src/store/actions.js +++ b/packages/edit-widgets/src/store/actions.js @@ -342,12 +342,14 @@ export function setIsWidgetAreaOpen( clientId, isOpen ) { /** * Returns an action object used to open/close the inserter. * - * @param {boolean|Object} value Whether the inserter should be - * opened (true) or closed (false). - * To specify an insertion point, - * use an object. - * @param {string} value.rootClientId The root client ID to insert at. - * @param {number} value.insertionIndex The index to insert at. + * @param {boolean|Object} value Whether the inserter should be + * opened (true) or closed (false). + * To specify an insertion point, + * use an object. + * @param {string} value.rootClientId The root client ID to insert at. + * @param {number} value.insertionIndex The index to insert at. + * @param {string} value.initialCategory The tab category to display first when the block editor inserter is opened. + * A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. * * @return {Object} Action object. */ diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index a0330321bac8f7..a7b3fc9562f297 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -691,12 +691,14 @@ export function removeEditorPanel( panelName ) { /** * Returns an action object used to open/close the inserter. * - * @param {boolean|Object} value Whether the inserter should be - * opened (true) or closed (false). - * To specify an insertion point, - * use an object. - * @param {string} value.rootClientId The root client ID to insert at. - * @param {number} value.insertionIndex The index to insert at. + * @param {boolean|Object} value Whether the inserter should be + * opened (true) or closed (false). + * To specify an insertion point, + * use an object. + * @param {string} value.rootClientId The root client ID to insert at. + * @param {number} value.insertionIndex The index to insert at. + * @param {string} value.initialCategory The tab category to display first when the block editor inserter is opened. + * A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. * * @return {Object} Action object. */ diff --git a/packages/editor/src/store/private-selectors.js b/packages/editor/src/store/private-selectors.js index 6afa4c96a91170..83aa093baa03fa 100644 --- a/packages/editor/src/store/private-selectors.js +++ b/packages/editor/src/store/private-selectors.js @@ -50,14 +50,19 @@ export const getInsertionPoint = createRegistrySelector( } ); +/** + * Get the initial category for the inserter. + * A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. + * + * @param {Object} state Global application state. + * + * @return {string} The initial tab category to open when the inserter is opened. + */ export const getInserterInitialCategory = createRegistrySelector( - () => ( state ) => { - if ( typeof state.blockInserterPanel === 'object' ) { - return state.blockInserterPanel?.initialCategory; - } - - return null; - } + () => ( state ) => + typeof state.blockInserterPanel === 'object' + ? state.blockInserterPanel?.initialCategory + : null ); export function getListViewToggleRef( state ) { From d609677796ad7cd23c91a0f0c4bffb487d707817 Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 9 Jan 2024 16:31:23 +1100 Subject: [PATCH 7/9] Show the placeholder when the block has no blocks in the editor. --- .../block-library/src/post-content/edit.js | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index ac0cd745611c54..d1de2d87b5dbbb 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -15,11 +15,7 @@ import { Warning, store as blockEditorStore, } from '@wordpress/block-editor'; -import { - useEntityProp, - useEntityBlockEditor, - store as coreStore, -} from '@wordpress/core-data'; +import { useEntityProp, useEntityBlockEditor } from '@wordpress/core-data'; import { useSelect, useDispatch } from '@wordpress/data'; import { Placeholder, Button } from '@wordpress/components'; import { postContent as icon } from '@wordpress/icons'; @@ -105,23 +101,14 @@ function EditableContent( { context = {}, clientId } ) { { id: postId } ); - const { entityRecord, setInserterIsOpened } = useSelect( - ( select ) => { - return { - entityRecord: select( coreStore ).getEntityRecord( - 'postType', - postType, - postId - ), - setInserterIsOpened: - select( blockEditorStore ).getSettings() - .__experimentalSetIsInserterOpened, - }; - }, - [ postType, postId ] + const setInserterIsOpened = useSelect( + ( select ) => + select( blockEditorStore ).getSettings() + .__experimentalSetIsInserterOpened, + [] ); - const hasInnerBlocks = !! entityRecord?.content?.raw || blocks?.length; + const hasInnerBlocks = !! blocks?.length; const { children, ...props } = useInnerBlocksProps( useBlockProps( { From f995eb3d662a446f1f860ab09f6c52e6282e2db5 Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 10 Jan 2024 16:07:16 +1100 Subject: [PATCH 8/9] Reverting hasInnerBlocks change to punt to another PR. It will required E2E test changes and more manual testing in post and site editors. --- .../block-library/src/post-content/edit.js | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index d1de2d87b5dbbb..ac0cd745611c54 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -15,7 +15,11 @@ import { Warning, store as blockEditorStore, } from '@wordpress/block-editor'; -import { useEntityProp, useEntityBlockEditor } from '@wordpress/core-data'; +import { + useEntityProp, + useEntityBlockEditor, + store as coreStore, +} from '@wordpress/core-data'; import { useSelect, useDispatch } from '@wordpress/data'; import { Placeholder, Button } from '@wordpress/components'; import { postContent as icon } from '@wordpress/icons'; @@ -101,14 +105,23 @@ function EditableContent( { context = {}, clientId } ) { { id: postId } ); - const setInserterIsOpened = useSelect( - ( select ) => - select( blockEditorStore ).getSettings() - .__experimentalSetIsInserterOpened, - [] + const { entityRecord, setInserterIsOpened } = useSelect( + ( select ) => { + return { + entityRecord: select( coreStore ).getEntityRecord( + 'postType', + postType, + postId + ), + setInserterIsOpened: + select( blockEditorStore ).getSettings() + .__experimentalSetIsInserterOpened, + }; + }, + [ postType, postId ] ); - const hasInnerBlocks = !! blocks?.length; + const hasInnerBlocks = !! entityRecord?.content?.raw || blocks?.length; const { children, ...props } = useInnerBlocksProps( useBlockProps( { From f7f0a929118a5e64e465733731cfce44eadcc409 Mon Sep 17 00:00:00 2001 From: ramon Date: Fri, 12 Jan 2024 14:00:29 +1100 Subject: [PATCH 9/9] updates nomenclature "category" > "tab" update e2e tests remove `_experimental` Flag for initialTab prop --- .../data/data-core-customize-widgets.md | 2 +- .../data/data-core-edit-widgets.md | 2 +- .../reference-guides/data/data-core-editor.md | 2 +- .../src/components/inserter/library.js | 4 +-- .../src/components/inserter/menu.js | 6 ++--- .../block-library/src/post-content/edit.js | 27 +++++-------------- .../customize-widgets/src/store/actions.js | 16 +++++------ packages/edit-widgets/src/store/actions.js | 16 +++++------ .../src/components/inserter-sidebar/index.js | 8 +++--- packages/editor/src/store/actions.js | 16 +++++------ .../editor/src/store/private-selectors.js | 8 +++--- test/e2e/specs/site-editor/pages.spec.js | 8 +++--- 12 files changed, 50 insertions(+), 65 deletions(-) diff --git a/docs/reference-guides/data/data-core-customize-widgets.md b/docs/reference-guides/data/data-core-customize-widgets.md index 5856803d830574..b0c0fc8818f327 100644 --- a/docs/reference-guides/data/data-core-customize-widgets.md +++ b/docs/reference-guides/data/data-core-customize-widgets.md @@ -78,7 +78,7 @@ _Parameters_ - _value_ `boolean|Object`: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object. - _value.rootClientId_ `string`: The root client ID to insert at. - _value.insertionIndex_ `number`: The index to insert at. -- _value.initialCategory_ `string`: The tab category to display first when the block editor inserter is opened. A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. +- _value.initialTab_ `string`: The id of the tab to display first when the block editor inserter is opened. A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js. _Returns_ diff --git a/docs/reference-guides/data/data-core-edit-widgets.md b/docs/reference-guides/data/data-core-edit-widgets.md index f936a40f11dfe5..f5268963882bab 100644 --- a/docs/reference-guides/data/data-core-edit-widgets.md +++ b/docs/reference-guides/data/data-core-edit-widgets.md @@ -215,7 +215,7 @@ _Parameters_ - _value_ `boolean|Object`: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object. - _value.rootClientId_ `string`: The root client ID to insert at. - _value.insertionIndex_ `number`: The index to insert at. -- _value.initialCategory_ `string`: The tab category to display first when the block editor inserter is opened. A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. +- _value.initialTab_ `string`: The id of the tab to display first when the block editor inserter is opened. A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js. _Returns_ diff --git a/docs/reference-guides/data/data-core-editor.md b/docs/reference-guides/data/data-core-editor.md index b45111e47d5ded..51126696942fad 100644 --- a/docs/reference-guides/data/data-core-editor.md +++ b/docs/reference-guides/data/data-core-editor.md @@ -1382,7 +1382,7 @@ _Parameters_ - _value_ `boolean|Object`: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object. - _value.rootClientId_ `string`: The root client ID to insert at. - _value.insertionIndex_ `number`: The index to insert at. -- _value.initialCategory_ `string`: The tab category to display first when the block editor inserter is opened. A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. +- _value.initialTab_ `string`: The id of the tab to display first when the block editor inserter is opened. A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js. _Returns_ diff --git a/packages/block-editor/src/components/inserter/library.js b/packages/block-editor/src/components/inserter/library.js index 9715f9334102e2..870bb7b91b29c3 100644 --- a/packages/block-editor/src/components/inserter/library.js +++ b/packages/block-editor/src/components/inserter/library.js @@ -21,7 +21,7 @@ function InserterLibrary( showMostUsedBlocks = false, __experimentalInsertionIndex, __experimentalFilterValue, - __experimentalInitialCategory, + initialInserterTab, onSelect = noop, shouldFocusBlock = false, }, @@ -47,7 +47,7 @@ function InserterLibrary( isAppender={ isAppender } showInserterHelpPanel={ showInserterHelpPanel } showMostUsedBlocks={ showMostUsedBlocks } - __experimentalInitialCategory={ __experimentalInitialCategory } + initialInserterTab={ initialInserterTab } __experimentalInsertionIndex={ __experimentalInsertionIndex } __experimentalFilterValue={ __experimentalFilterValue } shouldFocusBlock={ shouldFocusBlock } diff --git a/packages/block-editor/src/components/inserter/menu.js b/packages/block-editor/src/components/inserter/menu.js index 8e93ed59836ee1..4527318813a3da 100644 --- a/packages/block-editor/src/components/inserter/menu.js +++ b/packages/block-editor/src/components/inserter/menu.js @@ -45,7 +45,7 @@ function InserterMenu( showMostUsedBlocks, __experimentalFilterValue = '', shouldFocusBlock = true, - __experimentalInitialCategory, + initialInserterTab, }, ref ) { @@ -58,7 +58,7 @@ function InserterMenu( const [ selectedMediaCategory, setSelectedMediaCategory ] = useState( null ); const [ selectedTab, setSelectedTab ] = useState( - __experimentalInitialCategory || null + initialInserterTab || null ); const [ destinationRootClientId, onInsertBlocks, onToggleInsertionPoint ] = useInsertionPoint( { @@ -261,7 +261,7 @@ function InserterMenu( showMedia={ showMedia } onSelect={ handleSetSelectedTab } tabsContents={ inserterTabsContents } - initialTabId={ __experimentalInitialCategory } + initialTabId={ initialInserterTab } /> ) } { ! delayedFilterValue && ! showAsTabs && ( diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index ac0cd745611c54..ec491210b38270 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -15,11 +15,7 @@ import { Warning, store as blockEditorStore, } from '@wordpress/block-editor'; -import { - useEntityProp, - useEntityBlockEditor, - store as coreStore, -} from '@wordpress/core-data'; +import { useEntityProp, useEntityBlockEditor } from '@wordpress/core-data'; import { useSelect, useDispatch } from '@wordpress/data'; import { Placeholder, Button } from '@wordpress/components'; import { postContent as icon } from '@wordpress/icons'; @@ -105,23 +101,14 @@ function EditableContent( { context = {}, clientId } ) { { id: postId } ); - const { entityRecord, setInserterIsOpened } = useSelect( - ( select ) => { - return { - entityRecord: select( coreStore ).getEntityRecord( - 'postType', - postType, - postId - ), - setInserterIsOpened: - select( blockEditorStore ).getSettings() - .__experimentalSetIsInserterOpened, - }; - }, + const setInserterIsOpened = useSelect( + ( select ) => + select( blockEditorStore ).getSettings() + .__experimentalSetIsInserterOpened, [ postType, postId ] ); - const hasInnerBlocks = !! entityRecord?.content?.raw || blocks?.length; + const hasInnerBlocks = blocks?.length; const { children, ...props } = useInnerBlocksProps( useBlockProps( { @@ -144,7 +131,7 @@ function EditableContent( { context = {}, clientId } ) { const openInserter = () => { setInserterIsOpened( { - initialCategory: 'patterns', + initialTab: 'patterns', rootClientId: clientId, insertionIndex: 0, } ); diff --git a/packages/customize-widgets/src/store/actions.js b/packages/customize-widgets/src/store/actions.js index eccd1a85272b5a..528f2c395d57e6 100644 --- a/packages/customize-widgets/src/store/actions.js +++ b/packages/customize-widgets/src/store/actions.js @@ -1,14 +1,14 @@ /** * Returns an action object used to open/close the inserter. * - * @param {boolean|Object} value Whether the inserter should be - * opened (true) or closed (false). - * To specify an insertion point, - * use an object. - * @param {string} value.rootClientId The root client ID to insert at. - * @param {number} value.insertionIndex The index to insert at. - * @param {string} value.initialCategory The tab category to display first when the block editor inserter is opened. - * A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. + * @param {boolean|Object} value Whether the inserter should be + * opened (true) or closed (false). + * To specify an insertion point, + * use an object. + * @param {string} value.rootClientId The root client ID to insert at. + * @param {number} value.insertionIndex The index to insert at. + * @param {string} value.initialTab The id of the tab to display first when the block editor inserter is opened. + * A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js. * * @example * ```js diff --git a/packages/edit-widgets/src/store/actions.js b/packages/edit-widgets/src/store/actions.js index bac053e9a90b4c..40d48bd85fab25 100644 --- a/packages/edit-widgets/src/store/actions.js +++ b/packages/edit-widgets/src/store/actions.js @@ -342,14 +342,14 @@ export function setIsWidgetAreaOpen( clientId, isOpen ) { /** * Returns an action object used to open/close the inserter. * - * @param {boolean|Object} value Whether the inserter should be - * opened (true) or closed (false). - * To specify an insertion point, - * use an object. - * @param {string} value.rootClientId The root client ID to insert at. - * @param {number} value.insertionIndex The index to insert at. - * @param {string} value.initialCategory The tab category to display first when the block editor inserter is opened. - * A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. + * @param {boolean|Object} value Whether the inserter should be + * opened (true) or closed (false). + * To specify an insertion point, + * use an object. + * @param {string} value.rootClientId The root client ID to insert at. + * @param {number} value.insertionIndex The index to insert at. + * @param {string} value.initialTab The id of the tab to display first when the block editor inserter is opened. + * A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js. * * @return {Object} Action object. */ diff --git a/packages/editor/src/components/inserter-sidebar/index.js b/packages/editor/src/components/inserter-sidebar/index.js index 15e9230a61d0ac..783294dddee7d1 100644 --- a/packages/editor/src/components/inserter-sidebar/index.js +++ b/packages/editor/src/components/inserter-sidebar/index.js @@ -20,15 +20,15 @@ import { unlock } from '../../lock-unlock'; import { store as editorStore } from '../../store'; export default function InserterSidebar() { - const { insertionPoint, inserterInitialCategory, showMostUsedBlocks } = + const { insertionPoint, initialInserterTab, showMostUsedBlocks } = useSelect( ( select ) => { - const { getInsertionPoint, getInserterInitialCategory } = unlock( + const { getInsertionPoint, getInserterInitialTab } = unlock( select( editorStore ) ); const { get } = select( preferencesStore ); return { insertionPoint: getInsertionPoint(), - inserterInitialCategory: getInserterInitialCategory(), + initialInserterTab: getInserterInitialTab(), showMostUsedBlocks: get( 'core', 'mostUsedBlocks' ), }; }, [] ); @@ -68,7 +68,7 @@ export default function InserterSidebar() { __experimentalInsertionIndex={ insertionPoint.insertionIndex } - __experimentalInitialCategory={ inserterInitialCategory } + initialInserterTab={ initialInserterTab } __experimentalFilterValue={ insertionPoint.filterValue } ref={ libraryRef } /> diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index a7b3fc9562f297..9f0bf4700c4715 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -691,14 +691,14 @@ export function removeEditorPanel( panelName ) { /** * Returns an action object used to open/close the inserter. * - * @param {boolean|Object} value Whether the inserter should be - * opened (true) or closed (false). - * To specify an insertion point, - * use an object. - * @param {string} value.rootClientId The root client ID to insert at. - * @param {number} value.insertionIndex The index to insert at. - * @param {string} value.initialCategory The tab category to display first when the block editor inserter is opened. - * A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. + * @param {boolean|Object} value Whether the inserter should be + * opened (true) or closed (false). + * To specify an insertion point, + * use an object. + * @param {string} value.rootClientId The root client ID to insert at. + * @param {number} value.insertionIndex The index to insert at. + * @param {string} value.initialTab The id of the tab to display first when the block editor inserter is opened. + * A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js. * * @return {Object} Action object. */ diff --git a/packages/editor/src/store/private-selectors.js b/packages/editor/src/store/private-selectors.js index 83aa093baa03fa..f3dd3f4404e24e 100644 --- a/packages/editor/src/store/private-selectors.js +++ b/packages/editor/src/store/private-selectors.js @@ -51,17 +51,17 @@ export const getInsertionPoint = createRegistrySelector( ); /** - * Get the initial category for the inserter. - * A category corresponds to one of the tab categories defined in packages/block-editor/src/components/inserter/tabs.js. + * Get the initial tab id for the inserter. + * A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js. * * @param {Object} state Global application state. * * @return {string} The initial tab category to open when the inserter is opened. */ -export const getInserterInitialCategory = createRegistrySelector( +export const getInserterInitialTab = createRegistrySelector( () => ( state ) => typeof state.blockInserterPanel === 'object' - ? state.blockInserterPanel?.initialCategory + ? state.blockInserterPanel?.initialTab : null ); diff --git a/test/e2e/specs/site-editor/pages.spec.js b/test/e2e/specs/site-editor/pages.spec.js index a04359730421b5..e3b12ca7126174 100644 --- a/test/e2e/specs/site-editor/pages.spec.js +++ b/test/e2e/specs/site-editor/pages.spec.js @@ -23,8 +23,8 @@ async function addPageContent( editor, page ) { .getByRole( 'document', { name: 'Block: Content', } ) - .getByRole( 'document', { - name: 'Empty block; start writing or type forward slash to choose a block', + .getByRole( 'button', { + name: 'Start blank', } ) .click(); @@ -124,9 +124,7 @@ test.describe( 'Pages', () => { editor.canvas.getByRole( 'document', { name: 'Block: Content', } ) - ).toContainText( - 'This is the Content block, it will display all the blocks in any single post or page.' - ); + ).toContainText( 'This block will be replaced with your content.' ); await expect( page.locator( 'role=button[name="Dismiss this notice"i] >> text="Editing template. Changes made here affect all posts and pages that use the template."'