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

Site Editor: tidy up the generation of the site editor page title #48053

Merged
merged 2 commits into from
Feb 14, 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
52 changes: 21 additions & 31 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { Notice } from '@wordpress/components';
import { EntityProvider, store as coreStore } from '@wordpress/core-data';
import { EntityProvider } from '@wordpress/core-data';
import { store as preferencesStore } from '@wordpress/preferences';
import {
BlockContextProvider,
Expand All @@ -17,7 +17,7 @@ import {
store as interfaceStore,
} from '@wordpress/interface';
import { EditorNotices, EditorSnackbars } from '@wordpress/editor';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -36,6 +36,7 @@ import { GlobalStylesProvider } from '../global-styles/global-styles-provider';
import useTitle from '../routes/use-title';
import CanvasSpinner from '../canvas-spinner';
import { unlock } from '../../private-apis';
import useEditedEntityRecord from '../use-edited-entity-record';

const interfaceLabels = {
/* translators: accessibility text for the editor content landmark region. */
Expand All @@ -50,11 +51,15 @@ const interfaceLabels = {

export default function Editor() {
const {
editedPostId,
editedPostType,
editedPost,
record: editedPost,
getTitle,
isLoaded: hasLoadedPost,
} = useEditedEntityRecord();

const { id: editedPostId, type: editedPostType } = editedPost;

const {
context,
hasLoadedPost,
editorMode,
canvasMode,
blockEditorMode,
Expand All @@ -64,36 +69,19 @@ export default function Editor() {
showIconLabels,
} = useSelect( ( select ) => {
const {
getEditedPostType,
getEditedPostId,
getEditedPostContext,
getEditorMode,
getCanvasMode,
isInserterOpened,
isListViewOpened,
} = unlock( select( editSiteStore ) );
const { hasFinishedResolution, getEntityRecord } = select( coreStore );
const { __unstableGetEditorMode } = select( blockEditorStore );
const { getActiveComplementaryArea } = select( interfaceStore );
const postType = getEditedPostType();
const postId = getEditedPostId();

// The currently selected entity to display.
// Typically template or template part in the site editor.
return {
editedPostId: postId,
editedPostType: postType,
editedPost: postId
? getEntityRecord( 'postType', postType, postId )
: null,
context: getEditedPostContext(),
hasLoadedPost: postId
? hasFinishedResolution( 'getEntityRecord', [
'postType',
postType,
postId,
] )
: false,
editorMode: getEditorMode(),
canvasMode: getCanvasMode(),
blockEditorMode: __unstableGetEditorMode(),
Expand Down Expand Up @@ -135,26 +123,28 @@ export default function Editor() {
} ),
],
} ),
[ context ]
[ context, setEditedPostContext ]
);
const isReady = editedPostType !== undefined && editedPostId !== undefined;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't seem to add anything over and above isLoaded from useEditedEntityRecord. In my testing editedPostType and editedPostId where always defined when isLoaded === true , but I may have missed an edge case where this isn't the case.

let title;
if ( isReady && editedPost ) {
if ( hasLoadedPost ) {
const type =
editedPostType === 'wp_template'
? __( 'Template' )
: __( 'Template Part' );
title = `${ editedPost.title?.rendered } ‹ ${ type } ‹ ${ __(
'Editor (beta)'
) }`;
title = sprintf(
// translators: A breadcrumb trail in browser tab. %1$s: title of template being edited, %2$s: type of template (Template or Template Part).
__( '%1$s ‹ %2$s ‹ Editor' ),
getTitle(),
type
);
}

// Only announce the title once the editor is ready to prevent "Replace"
// action in <URlQueryController> from double-announcing.
useTitle( isReady && title );
useTitle( hasLoadedPost && title );

if ( ! isReady ) {
if ( ! hasLoadedPost ) {
return <CanvasSpinner />;
}

Expand Down