Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try using fallback layout instead of default in post editor #54371

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
@@ -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(
@@ -265,6 +294,11 @@ export default function VisualEditor( { styles } ) {
? postContentLayout
: fallbackLayout;

const postEditorLayout =
blockListLayout?.type === 'default' && ! hasPostContentAtRootLevel
? fallbackLayout
: blockListLayout;

const observeTypingRef = useTypingObserver();
const titleRef = useRef();
useEffect( () => {
@@ -337,7 +371,7 @@ export default function VisualEditor( { styles } ) {
/>
<LayoutStyle
selector=".block-editor-block-list__layout.is-root-container"
layout={ blockListLayout }
layout={ postEditorLayout }
/>
{ align && (
<LayoutStyle css={ alignCSS } />