From 68a36363e538dd3ae4d1e25e387462be0de2059d Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Tue, 14 Jan 2025 15:28:02 +0100 Subject: [PATCH] Document Outline: Add check for the main element --- .../src/components/document-outline/index.js | 74 +++++++++++++++---- .../components/document-outline/style.scss | 3 +- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/packages/editor/src/components/document-outline/index.js b/packages/editor/src/components/document-outline/index.js index 6c498ccc79990..058201afefd7d 100644 --- a/packages/editor/src/components/document-outline/index.js +++ b/packages/editor/src/components/document-outline/index.js @@ -73,6 +73,15 @@ function EmptyOutlineIllustration() { ); } +const incorrectMainTag = [ +
, + + { __( + 'Your template should have exactly one main element. Adjust the HTML element in the Advanced panel of the block.' + ) } + , +]; + /** * Returns an array of heading blocks enhanced with the following properties: * level - An integer with the heading level. @@ -95,6 +104,17 @@ const computeOutlineHeadings = ( blocks = [] ) => { } ); }; +const computeOutlineMainElements = ( blocks = [] ) => { + return blocks.flatMap( ( block = {} ) => { + if ( block.attributes.tagName === 'main' ) { + return { + ...block, + }; + } + return computeOutlineMainElements( block.innerBlocks ); + } ); +}; + const isEmptyHeading = ( heading ) => ! heading.attributes.content || heading.attributes.content.trim().length === 0; @@ -129,18 +149,7 @@ export default function DocumentOutline( { const prevHeadingLevelRef = useRef( 1 ); const headings = computeOutlineHeadings( blocks ); - if ( headings.length < 1 ) { - return ( -
- -

- { __( - 'Navigate the structure of your document and address issues like empty or incorrect heading levels.' - ) } -

-
- ); - } + const mainElements = computeOutlineMainElements( blocks ); // Not great but it's the simplest way to locate the title right now. const titleNode = document.querySelector( '.editor-post-title__input' ); @@ -154,9 +163,48 @@ export default function DocumentOutline( { ); const hasMultipleH1 = countByLevel[ 1 ] > 1; + const classNames = + 'document-outline' + + ( headings.length < 1 ? ' has-no-headings' : '' ) + + ( mainElements.length === 0 ? ' has-no-main' : '' ); + return ( -
+
+ { headings.length < 1 && ( + <> + +

+ { __( + 'Navigate the structure of your document and address issues like empty or incorrect heading levels.' + ) } +

+ + ) } + { mainElements.length === 0 && ( +

+ { __( + 'The main element is missing. Select the block that contains your most important content and add the main HTML element in the Advanced panel.' + ) } +

+ ) }
    + { mainElements.map( ( item ) => { + return ( + { + selectBlock( item.clientId ); + onSelect?.(); + } } + > + { mainElements.length !== 1 && incorrectMainTag } + + ); + } ) } { hasTitle && ( svg { margin-top: $grid-unit-30 + $grid-unit-05; }