From dca3ed4338fa01ed60efb38d612e3ced44ef6eb6 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Mon, 15 May 2023 13:17:02 +1000 Subject: [PATCH 1/5] Add paragraph prompt to post content when empty --- .../block-library/src/post-content/edit.js | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index 8385447581969c..08411be35e44d1 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -8,9 +8,10 @@ import { __experimentalRecursionProvider as RecursionProvider, __experimentalUseHasRecursion as useHasRecursion, Warning, + store as blockEditorStore, } from '@wordpress/block-editor'; import { useEntityProp, useEntityBlockEditor } from '@wordpress/core-data'; - +import { useSelect } from '@wordpress/data'; /** * Internal dependencies */ @@ -36,7 +37,7 @@ function ReadOnlyContent( { userCanEdit, postType, postId } ) { ); } -function EditableContent( { context = {} } ) { +function EditableContent( { context = {}, clientId } ) { const { postType, postId } = context; const [ blocks, onInput, onChange ] = useEntityBlockEditor( @@ -45,12 +46,29 @@ function EditableContent( { context = {} } ) { { id: postId } ); + const hasInnerBlocks = useSelect( + ( select ) => + select( blockEditorStore ).getBlock( clientId ).innerBlocks.length > + 0, + [ clientId ] + ); + + const initialInnerBlocks = [ + [ + 'core/paragraph', + { + placeholder: __( 'Type / to add blocks to your page' ), + }, + ], + ]; + const props = useInnerBlocksProps( useBlockProps( { className: 'entry-content' } ), { value: blocks, onInput, onChange, + template: ! hasInnerBlocks ? initialInnerBlocks : undefined, } ); return
; @@ -114,6 +132,7 @@ function RecursionError() { export default function PostContentEdit( { context, attributes, + clientId, __unstableLayoutClassNames: layoutClassNames, } ) { const { postId: contextPostId, postType: contextPostType } = context; @@ -127,7 +146,11 @@ export default function PostContentEdit( { return ( { contextPostId && contextPostType ? ( - + ) : ( ) } From b75d458be8ecad6301c0c403c0840a19b49f3406 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Tue, 16 May 2023 10:13:30 +1000 Subject: [PATCH 2/5] Remove placeholder and check if getBlock exists --- packages/block-library/src/post-content/edit.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index 08411be35e44d1..ba4bedffa04fd8 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -48,19 +48,12 @@ function EditableContent( { context = {}, clientId } ) { const hasInnerBlocks = useSelect( ( select ) => - select( blockEditorStore ).getBlock( clientId ).innerBlocks.length > - 0, + select( blockEditorStore ).getBlock( clientId )?.innerBlocks + .length > 0, [ clientId ] ); - const initialInnerBlocks = [ - [ - 'core/paragraph', - { - placeholder: __( 'Type / to add blocks to your page' ), - }, - ], - ]; + const initialInnerBlocks = [ [ 'core/paragraph' ] ]; const props = useInnerBlocksProps( useBlockProps( { className: 'entry-content' } ), From 11c292ba2b8a98b26d3fed1073b49aa72856a82f Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Wed, 17 May 2023 13:49:17 +1000 Subject: [PATCH 3/5] Fix inner blocks empty in draft mode --- .../block-library/src/post-content/edit.js | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index ba4bedffa04fd8..3dd0d9ea60db3f 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -8,9 +8,12 @@ import { __experimentalRecursionProvider as RecursionProvider, __experimentalUseHasRecursion as useHasRecursion, 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 } from '@wordpress/data'; /** * Internal dependencies @@ -37,7 +40,7 @@ function ReadOnlyContent( { userCanEdit, postType, postId } ) { ); } -function EditableContent( { context = {}, clientId } ) { +function EditableContent( { context = {} } ) { const { postType, postId } = context; const [ blocks, onInput, onChange ] = useEntityBlockEditor( @@ -46,13 +49,19 @@ function EditableContent( { context = {}, clientId } ) { { id: postId } ); - const hasInnerBlocks = useSelect( - ( select ) => - select( blockEditorStore ).getBlock( clientId )?.innerBlocks - .length > 0, - [ clientId ] + const entityRecord = useSelect( + ( select ) => { + return select( coreStore ).getEntityRecord( + 'postType', + postType, + postId + ); + }, + [ postType, postId ] ); + const hasInnerBlocks = !! entityRecord?.content?.raw; + const initialInnerBlocks = [ [ 'core/paragraph' ] ]; const props = useInnerBlocksProps( @@ -125,7 +134,6 @@ function RecursionError() { export default function PostContentEdit( { context, attributes, - clientId, __unstableLayoutClassNames: layoutClassNames, } ) { const { postId: contextPostId, postType: contextPostType } = context; @@ -139,11 +147,7 @@ export default function PostContentEdit( { return ( { contextPostId && contextPostType ? ( - + ) : ( ) } From 13fc935594c0bd07e21a77afe194ab53099ef61f Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Thu, 18 May 2023 16:30:50 +1000 Subject: [PATCH 4/5] Fix paragraph not appearing --- packages/block-library/src/post-content/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index 3dd0d9ea60db3f..ed5cfe767070f6 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -67,7 +67,7 @@ function EditableContent( { context = {} } ) { const props = useInnerBlocksProps( useBlockProps( { className: 'entry-content' } ), { - value: blocks, + value: hasInnerBlocks ? blocks : undefined, onInput, onChange, template: ! hasInnerBlocks ? initialInnerBlocks : undefined, From e4c0f1d10a7d9864654236b1299ab6f99937ebf0 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Wed, 24 May 2023 12:22:36 +1000 Subject: [PATCH 5/5] Revert checking inner blocks to set value --- packages/block-library/src/post-content/edit.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index ed5cfe767070f6..193c524dfe200b 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -60,14 +60,14 @@ function EditableContent( { context = {} } ) { [ postType, postId ] ); - const hasInnerBlocks = !! entityRecord?.content?.raw; + const hasInnerBlocks = !! entityRecord?.content?.raw || blocks?.length; const initialInnerBlocks = [ [ 'core/paragraph' ] ]; const props = useInnerBlocksProps( useBlockProps( { className: 'entry-content' } ), { - value: hasInnerBlocks ? blocks : undefined, + value: blocks, onInput, onChange, template: ! hasInnerBlocks ? initialInnerBlocks : undefined,