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

Editor: Unify Header component. #61273

Merged
merged 9 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function publishPost( this: Editor ) {
.getByRole( 'button', { name: 'Save', exact: true } );
const publishButton = this.page
.getByRole( 'region', { name: 'Editor top bar' } )
.getByRole( 'button', { name: 'Publish' } );
.getByRole( 'button', { name: 'Publish', exact: true } );
const buttonToClick = ( await saveButton.isVisible() )
? saveButton
: publishButton;
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e-test-utils/src/ensure-sidebar-opened.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export async function ensureSidebarOpened() {
'.edit-post-header__settings [aria-label="Settings"][aria-expanded="false"],' +
'.edit-site-header__actions [aria-label="Settings"][aria-expanded="false"],' +
'.edit-widgets-header__actions [aria-label="Settings"][aria-expanded="false"],' +
'.edit-site-header-edit-mode__actions [aria-label="Settings"][aria-expanded="false"]'
'.edit-site-header-edit-mode__actions [aria-label="Settings"][aria-expanded="false"],' +
'.editor-header__settings [aria-label="Settings"][aria-expanded="false"]'
);

if ( toggleSidebarButton ) {
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e-test-utils/src/inserter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ async function isGlobalInserterOpen() {
'.edit-site-header [aria-label="Toggle block inserter"].is-pressed,' +
'.edit-widgets-header [aria-label="Toggle block inserter"].is-pressed,' +
'.edit-widgets-header [aria-label="Add block"].is-pressed,' +
'.edit-site-header-edit-mode__inserter-toggle.is-pressed'
'.edit-site-header-edit-mode__inserter-toggle.is-pressed,' +
'.editor-header [aria-label="Toggle block inserter"].is-pressed'
);
} );
}
Expand Down
4 changes: 1 addition & 3 deletions packages/e2e-test-utils/src/site-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ export async function visitSiteEditor( query, skipWelcomeGuide = true ) {
* Toggles the global styles sidebar (opens it if closed and closes it if open).
*/
export async function toggleGlobalStyles() {
await page.click(
'.edit-site-header-edit-mode__actions button[aria-label="Styles"]'
);
await page.click( '.editor-header__settings button[aria-label="Styles"]' );
}

/**
Expand Down
128 changes: 9 additions & 119 deletions packages/edit-post/src/components/header/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
import {
DocumentBar,
PostSavedState,
PostPreviewButton,
store as editorStore,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { useSelect } from '@wordpress/data';
import { useViewportMatch } from '@wordpress/compose';
import { __unstableMotion as motion } from '@wordpress/components';
import { store as preferencesStore } from '@wordpress/preferences';
import { useState } from '@wordpress/element';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -29,21 +14,7 @@ import MainDashboardButton from './main-dashboard-button';
import { store as editPostStore } from '../../store';
import { unlock } from '../../lock-unlock';

const {
CollapsableBlockToolbar,
DocumentTools,
PostViewLink,
PreviewDropdown,
PinnedItems,
MoreMenu,
PostPublishButtonOrToggle,
} = unlock( editorPrivateApis );

const slideY = {
hidden: { y: '-50px' },
distractionFreeInactive: { y: 0 },
hover: { y: 0, transition: { type: 'tween', delay: 0.2 } },
};
const { Header: EditorHeader } = unlock( editorPrivateApis );

const slideX = {
hidden: { x: '-100%' },
Expand All @@ -52,42 +23,17 @@ const slideX = {
};

function Header( { setEntitiesSavedStatesCallback, initialPost } ) {
const isWideViewport = useViewportMatch( 'large' );
const isLargeViewport = useViewportMatch( 'medium' );
const {
isTextEditor,
hasActiveMetaboxes,
isPublishSidebarOpened,
showIconLabels,
hasHistory,
hasFixedToolbar,
isZoomedOutView,
} = useSelect( ( select ) => {
const { get: getPreference } = select( preferencesStore );
const { getEditorMode } = select( editorStore );
const { __unstableGetEditorMode } = select( blockEditorStore );

const { hasActiveMetaboxes } = useSelect( ( select ) => {
return {
isTextEditor: getEditorMode() === 'text',
hasActiveMetaboxes: select( editPostStore ).hasMetaBoxes(),
hasHistory:
!! select( editorStore ).getEditorSettings()
.onNavigateToPreviousEntityRecord,
isPublishSidebarOpened:
select( editorStore ).isPublishSidebarOpened(),
showIconLabels: getPreference( 'core', 'showIconLabels' ),
hasFixedToolbar: getPreference( 'core', 'fixedToolbar' ),
isZoomedOutView: __unstableGetEditorMode() === 'zoom-out',
};
}, [] );

const hasTopToolbar = isLargeViewport && hasFixedToolbar;

const [ isBlockToolsCollapsed, setIsBlockToolsCollapsed ] =
useState( true );

return (
<div className="edit-post-header">
<EditorHeader
forceIsDirty={ hasActiveMetaboxes }
setEntitiesSavedStatesCallback={ setEntitiesSavedStatesCallback }
>
<MainDashboardButton.Slot>
<motion.div
variants={ slideX }
Expand All @@ -99,64 +45,8 @@ function Header( { setEntitiesSavedStatesCallback, initialPost } ) {
/>
</motion.div>
</MainDashboardButton.Slot>
<motion.div
variants={ slideY }
transition={ { type: 'tween', delay: 0.8 } }
className="edit-post-header__toolbar"
>
<DocumentTools disableBlockTools={ isTextEditor } />
{ hasTopToolbar && (
<CollapsableBlockToolbar
isCollapsed={ isBlockToolsCollapsed }
onToggle={ setIsBlockToolsCollapsed }
/>
) }
<div
className={ clsx( 'edit-post-header__center', {
'is-collapsed':
hasHistory &&
! isBlockToolsCollapsed &&
hasTopToolbar,
} ) }
>
{ hasHistory && <DocumentBar /> }
</div>
</motion.div>
<motion.div
variants={ slideY }
transition={ { type: 'tween', delay: 0.8 } }
className="edit-post-header__settings"
>
{ ! isPublishSidebarOpened && (
// This button isn't completely hidden by the publish sidebar.
// We can't hide the whole toolbar when the publish sidebar is open because
// we want to prevent mounting/unmounting the PostPublishButtonOrToggle DOM node.
// We track that DOM node to return focus to the PostPublishButtonOrToggle
// when the publish sidebar has been closed.
<PostSavedState forceIsDirty={ hasActiveMetaboxes } />
) }
<PreviewDropdown
disabled={ isZoomedOutView }
forceIsAutosaveable={ hasActiveMetaboxes }
/>
<PostPreviewButton
className="edit-post-header__post-preview-button"
forceIsAutosaveable={ hasActiveMetaboxes }
/>
<PostViewLink />
<PostPublishButtonOrToggle
forceIsDirty={ hasActiveMetaboxes }
setEntitiesSavedStatesCallback={
setEntitiesSavedStatesCallback
}
/>
{ ( isWideViewport || ! showIconLabels ) && (
<PinnedItems.Slot scope="core" />
) }
<MoreMenu />
<PostEditorMoreMenu />
</motion.div>
</div>
<PostEditorMoreMenu />
</EditorHeader>
);
}

Expand Down