/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __, isRTL } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import {
Button,
VisuallyHidden,
__experimentalText as Text,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { BlockIcon } from '@wordpress/block-editor';
import { store as commandsStore } from '@wordpress/commands';
import {
chevronLeftSmall,
chevronRightSmall,
page as pageIcon,
navigation as navigationIcon,
symbol,
} from '@wordpress/icons';
import { displayShortcut } from '@wordpress/keycodes';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { useRef, useState, useEffect } from '@wordpress/element';
/**
* Internal dependencies
*/
import useEditedEntityRecord from '../../use-edited-entity-record';
import { store as editSiteStore } from '../../../store';
import {
TEMPLATE_POST_TYPE,
NAVIGATION_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
PATTERN_TYPES,
PATTERN_SYNC_TYPES,
} from '../../../utils/constants';
const typeLabels = {
[ PATTERN_TYPES.user ]: __( 'Editing pattern:' ),
[ NAVIGATION_POST_TYPE ]: __( 'Editing navigation menu:' ),
[ TEMPLATE_POST_TYPE ]: __( 'Editing template:' ),
[ TEMPLATE_PART_POST_TYPE ]: __( 'Editing template part:' ),
};
export default function DocumentActions() {
const isPage = useSelect(
( select ) => select( editSiteStore ).isPage(),
[]
);
return isPage ? : ;
}
function PageDocumentActions() {
const { isEditingPage, hasResolved, isFound, title } = useSelect(
( select ) => {
const { getEditedPostContext } = select( editSiteStore );
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
const { getRenderingMode } = select( editorStore );
const context = getEditedPostContext();
const queryArgs = [ 'postType', context.postType, context.postId ];
const page = getEditedEntityRecord( ...queryArgs );
return {
isEditingPage:
!! context.postId && getRenderingMode() !== 'template-only',
hasResolved: hasFinishedResolution(
'getEditedEntityRecord',
queryArgs
),
isFound: !! page,
title: page?.title,
};
},
[]
);
const { setRenderingMode } = useDispatch( editorStore );
const [ isAnimated, setIsAnimated ] = useState( false );
const isLoading = useRef( true );
useEffect( () => {
if ( ! isLoading.current ) {
setIsAnimated( true );
}
isLoading.current = false;
}, [ isEditingPage ] );
if ( ! hasResolved ) {
return null;
}
if ( ! isFound ) {
return (
{ __( 'Document not found' ) }
);
}
return isEditingPage ? (
{ title }
) : (
setRenderingMode( 'template-locked' ) }
/>
);
}
function TemplateDocumentActions( { className, onBack } ) {
const { isLoaded, record, getTitle, icon } = useEditedEntityRecord();
if ( ! isLoaded ) {
return null;
}
if ( ! record ) {
return (
{ __( 'Document not found' ) }
);
}
let typeIcon = icon;
if ( record.type === NAVIGATION_POST_TYPE ) {
typeIcon = navigationIcon;
} else if ( record.type === PATTERN_TYPES.user ) {
typeIcon = symbol;
}
return (
{ typeLabels[ record.type ] ??
typeLabels[ TEMPLATE_POST_TYPE ] }
{ getTitle() }
);
}
function BaseDocumentActions( { className, icon, children, onBack } ) {
const { open: openCommandCenter } = useDispatch( commandsStore );
return (
{ onBack && (
) }
);
}