From 2b543dc66ee8d099ec58152177a2703c8669295e Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Tue, 12 Sep 2023 14:56:56 +1000 Subject: [PATCH 1/2] Try using fallback layout instead of default in post editor --- packages/edit-post/src/components/visual-editor/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/edit-post/src/components/visual-editor/index.js b/packages/edit-post/src/components/visual-editor/index.js index a35f49c37b9c17..702d77ce6c7b6b 100644 --- a/packages/edit-post/src/components/visual-editor/index.js +++ b/packages/edit-post/src/components/visual-editor/index.js @@ -265,6 +265,9 @@ export default function VisualEditor( { styles } ) { ? postContentLayout : fallbackLayout; + const postEditorLayout = + blockListLayout?.type === 'default' ? fallbackLayout : blockListLayout; + const observeTypingRef = useTypingObserver(); const titleRef = useRef(); useEffect( () => { @@ -337,7 +340,7 @@ export default function VisualEditor( { styles } ) { /> { align && ( From fe0e7587f853721210d584075bff8cbd50c0b23e Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Tue, 12 Sep 2023 17:07:51 +1000 Subject: [PATCH 2/2] Check whether Post Content is at root level before resorting to fallback layout --- .../src/components/visual-editor/index.js | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/edit-post/src/components/visual-editor/index.js b/packages/edit-post/src/components/visual-editor/index.js index 702d77ce6c7b6b..14b6bf475045e1 100644 --- a/packages/edit-post/src/components/visual-editor/index.js +++ b/packages/edit-post/src/components/visual-editor/index.js @@ -66,6 +66,15 @@ function getPostContentAttributes( blocks ) { } } +function checkForPostContentAtRootLevel( blocks ) { + for ( let i = 0; i < blocks.length; i++ ) { + if ( blocks[ i ].name === 'core/post-content' ) { + return true; + } + } + return false; +} + export default function VisualEditor( { styles } ) { const { deviceType, @@ -221,6 +230,26 @@ export default function VisualEditor( { styles } ) { postContentAttributes, ] ); + const hasPostContentAtRootLevel = useMemo( () => { + if ( ! editedPostTemplate?.content && ! editedPostTemplate?.blocks ) { + return false; + } + // When in template editing mode, we can access the blocks directly. + if ( editedPostTemplate?.blocks ) { + return checkForPostContentAtRootLevel( editedPostTemplate?.blocks ); + } + // If there are no blocks, we have to parse the content string. + // Best double-check it's a string otherwise the parse function gets unhappy. + const parseableContent = + typeof editedPostTemplate?.content === 'string' + ? editedPostTemplate?.content + : ''; + + return ( + checkForPostContentAtRootLevel( parse( parseableContent ) ) || false + ); + }, [ editedPostTemplate?.content, editedPostTemplate?.blocks ] ); + const { layout = {}, align = '' } = newestPostContentAttributes || {}; const postContentLayoutClasses = useLayoutClasses( @@ -266,7 +295,9 @@ export default function VisualEditor( { styles } ) { : fallbackLayout; const postEditorLayout = - blockListLayout?.type === 'default' ? fallbackLayout : blockListLayout; + blockListLayout?.type === 'default' && ! hasPostContentAtRootLevel + ? fallbackLayout + : blockListLayout; const observeTypingRef = useTypingObserver(); const titleRef = useRef();