-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Editor: Introduce the Editor component and use it in the site editor #62274
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useEditedEntityRecord from '../use-edited-entity-record'; | ||
import useTitle from '../routes/use-title'; | ||
import { POST_TYPE_LABELS, TEMPLATE_POST_TYPE } from '../../utils/constants'; | ||
|
||
function useEditorTitle() { | ||
const { | ||
record: editedPost, | ||
getTitle, | ||
isLoaded: hasLoadedPost, | ||
} = useEditedEntityRecord(); | ||
let title; | ||
if ( hasLoadedPost ) { | ||
title = sprintf( | ||
// translators: A breadcrumb trail for the Admin document title. %1$s: title of template being edited, %2$s: type of template (Template or Template Part). | ||
__( '%1$s ‹ %2$s' ), | ||
getTitle(), | ||
POST_TYPE_LABELS[ editedPost.type ] ?? | ||
POST_TYPE_LABELS[ TEMPLATE_POST_TYPE ] | ||
); | ||
} | ||
|
||
// Only announce the title once the editor is ready to prevent "Replace" | ||
// action in <URLQueryController> from double-announcing. | ||
useTitle( hasLoadedPost && title ); | ||
} | ||
|
||
export default useEditorTitle; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { Notice } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { TEMPLATE_POST_TYPE } from '../../store/constants'; | ||
import EditorInterface from '../editor-interface'; | ||
import { ExperimentalEditorProvider } from '../provider'; | ||
import Sidebar from '../sidebar'; | ||
|
||
function Editor( { | ||
postType, | ||
postId, | ||
templateId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The templateId can in theory be guessed from the postType, postId couple but it would require a lot more changes to how things work now in the site editor. So for now, this can work too. |
||
settings, | ||
children, | ||
|
||
// This could be part of the settings. | ||
onActionPerformed, | ||
|
||
// The following abstractions are not ideal but necessary | ||
// to account for site editor and post editor differences for now. | ||
className, | ||
styles, | ||
customSaveButton, | ||
forceDisableBlockTools, | ||
title, | ||
iframeProps, | ||
extraSidebarPanels, | ||
enableRegionNavigation = true, | ||
} ) { | ||
const { post, template, hasLoadedPost } = useSelect( | ||
( select ) => { | ||
const { getEntityRecord, hasFinishedResolution } = | ||
select( coreStore ); | ||
return { | ||
post: getEntityRecord( 'postType', postType, postId ), | ||
template: templateId | ||
? getEntityRecord( | ||
'postType', | ||
TEMPLATE_POST_TYPE, | ||
templateId | ||
) | ||
: undefined, | ||
hasLoadedPost: hasFinishedResolution( 'getEntityRecord', [ | ||
'postType', | ||
postType, | ||
postId, | ||
] ), | ||
}; | ||
}, | ||
[ postType, postId, templateId ] | ||
); | ||
|
||
return ( | ||
<ExperimentalEditorProvider | ||
post={ post } | ||
__unstableTemplate={ template } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I don't really think that this EditorProvider should receive the full objects, postType, postId should suffice but it's a very old component and I didn't want to touch its API. |
||
settings={ settings } | ||
useSubRegistry={ false } | ||
> | ||
{ hasLoadedPost && ! post && ( | ||
<Notice status="warning" isDismissible={ false }> | ||
{ __( | ||
"You attempted to edit an item that doesn't exist. Perhaps it was deleted?" | ||
) } | ||
</Notice> | ||
) } | ||
<EditorInterface | ||
className={ className } | ||
styles={ styles } | ||
enableRegionNavigation={ enableRegionNavigation } | ||
customSaveButton={ customSaveButton } | ||
forceDisableBlockTools={ forceDisableBlockTools } | ||
title={ title } | ||
iframeProps={ iframeProps } | ||
/> | ||
<Sidebar | ||
onActionPerformed={ onActionPerformed } | ||
extraPanels={ extraSidebarPanels } | ||
/> | ||
{ children } | ||
</ExperimentalEditorProvider> | ||
); | ||
} | ||
|
||
export default Editor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we remove this hook in favor of useEditorTitle?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really, useEditorTitle is using useTitle internally. useTitle is more generic.