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

Show document subtext if template part child is selected #25544

Merged
merged 1 commit into from
Sep 22, 2020
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
49 changes: 42 additions & 7 deletions packages/edit-site/src/components/header/document-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,33 @@ import {
getBlockType,
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { last } from 'lodash';

function getBlockDisplayText( block ) {
return block
? getBlockLabel( getBlockType( block.name ), block.attributes )
: null;
}

function useSecondaryText() {
const selectedBlock = useSelect( ( select ) => {
return select( 'core/block-editor' ).getSelectedBlock();
const {
selectedBlock,
getBlockParentsByBlockName,
getBlockWithoutInnerBlocks,
} = useSelect( ( select ) => {
return {
selectedBlock: select( 'core/block-editor' ).getSelectedBlock(),
getBlockParentsByBlockName: select( 'core/block-editor' )
.getBlockParentsByBlockName,
getBlockWithoutInnerBlocks: select( 'core/block-editor' )
.__unstableGetBlockWithoutInnerBlocks,
};
} );

// TODO: Handle if parent is template part too.
// Check if current block is a template part:
const selectedBlockLabel =
selectedBlock?.name === 'core/template-part'
? getBlockLabel(
getBlockType( selectedBlock?.name ),
selectedBlock?.attributes
)
? getBlockDisplayText( selectedBlock )
: null;

if ( selectedBlockLabel ) {
Expand All @@ -33,6 +47,27 @@ function useSecondaryText() {
isActive: true,
};
}

// Check if an ancestor of the current block is a template part:
const templatePartParents = !! selectedBlock
? getBlockParentsByBlockName(
selectedBlock?.clientId,
'core/template-part'
)
: [];

if ( templatePartParents.length ) {
// templatePartParents is in order from top to bottom, so the closest
// parent is at the end.
const closestParent = getBlockWithoutInnerBlocks(
last( templatePartParents )
);
return {
label: getBlockDisplayText( closestParent ),
isActive: true,
};
}

return {};
}

Expand Down