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 the top toolbar preference #57531

Merged
merged 2 commits into from Jan 5, 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
Expand Up @@ -15,6 +15,6 @@ export async function setIsFixedToolbar( this: Editor, isFixed: boolean ) {
await this.page.evaluate( ( _isFixed ) => {
window.wp.data
.dispatch( 'core/preferences' )
.set( 'core/edit-post', 'fixedToolbar', _isFixed );
.set( 'core', 'fixedToolbar', _isFixed );
}, isFixed );
}
2 changes: 1 addition & 1 deletion packages/edit-post/src/components/header/index.js
Expand Up @@ -82,7 +82,7 @@ function Header( { setEntitiesSavedStatesCallback } ) {
select( editorStore ).getRenderingMode() === 'template-only',
isPublishSidebarOpened:
select( editPostStore ).isPublishSidebarOpened(),
hasFixedToolbar: getPreference( 'core/edit-post', 'fixedToolbar' ),
hasFixedToolbar: getPreference( 'core', 'fixedToolbar' ),
showIconLabels: getPreference( 'core', 'showIconLabels' ),
};
}, [] );
Expand Down
Expand Up @@ -27,7 +27,7 @@ function WritingMenu() {

const toggleDistractionFree = () => {
registry.batch( () => {
setPreference( 'core/edit-post', 'fixedToolbar', true );
setPreference( 'core', 'fixedToolbar', true );
setIsInserterOpened( false );
setIsListViewOpened( false );
closeGeneralSidebar();
Expand All @@ -46,7 +46,7 @@ function WritingMenu() {
return (
<MenuGroup label={ _x( 'View', 'noun' ) }>
<PreferenceToggleMenuItem
scope="core/edit-post"
scope="core"
name="fixedToolbar"
onToggle={ turnOffDistractionFree }
label={ __( 'Top toolbar' ) }
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-post/src/components/preferences-modal/index.js
Expand Up @@ -70,7 +70,7 @@ export default function EditPostPreferencesModal() {
const { set: setPreference } = useDispatch( preferencesStore );

const toggleDistractionFree = () => {
setPreference( 'core/edit-post', 'fixedToolbar', true );
setPreference( 'core', 'fixedToolbar', true );
setIsInserterOpened( false );
setIsListViewOpened( false );
closeGeneralSidebar();
Expand Down Expand Up @@ -186,6 +186,7 @@ export default function EditPostPreferencesModal() {
) }
>
<EnableFeature
scope="core"
featureName="fixedToolbar"
onToggle={ turnOffDistractionFree }
help={ __(
Expand Down
9 changes: 1 addition & 8 deletions packages/edit-post/src/editor.js
Expand Up @@ -14,7 +14,6 @@ import { SlotFillProvider } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { store as preferencesStore } from '@wordpress/preferences';
import { CommandMenu } from '@wordpress/commands';
import { useViewportMatch } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -34,14 +33,12 @@ function Editor( {
initialEdits,
...props
} ) {
const isLargeViewport = useViewportMatch( 'medium' );
Copy link
Contributor

@ntsekouras ntsekouras Jan 4, 2024

Choose a reason for hiding this comment

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

We don't need this check any more? I think we do and now in smaller viewports display both toolbars.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch. The later ! isLargeViewport check does toggle the fixed toolbar setting on for mobile devices. So we should find a way to preserve it or architect it differently.

const { currentPost, getPostLinkProps, goBack } = usePostHistory(
initialPostId,
initialPostType
);

const {
hasFixedToolbar,
isDistractionFree,
hasInlineToolbar,
post,
Expand Down Expand Up @@ -88,8 +85,6 @@ function Editor( {
getPostType( currentPost.postType )?.viewable ?? false;
const canEditTemplate = canUser( 'create', 'templates' );
return {
hasFixedToolbar:
isFeatureActive( 'fixedToolbar' ) || ! isLargeViewport,
isDistractionFree: isFeatureActive( 'distractionFree' ),
hasInlineToolbar: isFeatureActive( 'inlineToolbar' ),
preferredStyleVariations: select( preferencesStore ).get(
Expand All @@ -105,7 +100,7 @@ function Editor( {
post: postObject,
};
},
[ currentPost.postType, currentPost.postId, isLargeViewport ]
[ currentPost.postType, currentPost.postId ]
);

const { updatePreferredStyleVariations } = useDispatch( editPostStore );
Expand All @@ -119,7 +114,6 @@ function Editor( {
value: preferredStyleVariations,
onChange: updatePreferredStyleVariations,
},
hasFixedToolbar,
isDistractionFree,
hasInlineToolbar,

Expand All @@ -146,7 +140,6 @@ function Editor( {
return result;
}, [
settings,
hasFixedToolbar,
hasInlineToolbar,
isDistractionFree,
hiddenBlockTypes,
Expand Down
14 changes: 2 additions & 12 deletions packages/edit-post/src/editor.native.js
Expand Up @@ -47,16 +47,10 @@ class Editor extends Component {
this.setTitleRef = this.setTitleRef.bind( this );
}

getEditorSettings(
settings,
hasFixedToolbar,
hiddenBlockTypes,
blockTypes
) {
getEditorSettings( settings, hiddenBlockTypes, blockTypes ) {
settings = {
...settings,
isRTL: I18nManager.isRTL,
hasFixedToolbar,
};

// Omit hidden block types if exists and non-empty.
Expand Down Expand Up @@ -132,7 +126,6 @@ class Editor extends Component {
render() {
const {
settings,
hasFixedToolbar,
initialEdits,
hiddenBlockTypes,
blockTypes,
Expand All @@ -146,7 +139,6 @@ class Editor extends Component {

const editorSettings = this.getEditorSettings(
settings,
hasFixedToolbar,
hiddenBlockTypes,
blockTypes
);
Expand Down Expand Up @@ -188,12 +180,10 @@ class Editor extends Component {

export default compose( [
withSelect( ( select ) => {
const { isFeatureActive, getEditorMode, getHiddenBlockTypes } =
select( editPostStore );
const { getEditorMode, getHiddenBlockTypes } = select( editPostStore );
const { getBlockTypes } = select( blocksStore );

return {
hasFixedToolbar: isFeatureActive( 'fixedToolbar' ),
mode: getEditorMode(),
hiddenBlockTypes: getHiddenBlockTypes(),
blockTypes: getBlockTypes(),
Expand Down
Expand Up @@ -134,7 +134,7 @@ export default function useCommonCommands() {
name: 'core/toggle-top-toolbar',
label: __( 'Toggle top toolbar' ),
callback: ( { close } ) => {
toggle( 'core/edit-post', 'fixedToolbar' );
toggle( 'core', 'fixedToolbar' );
if ( isDistractionFree ) {
toggleDistractionFree();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-post/src/index.js
Expand Up @@ -55,7 +55,6 @@ export function initializeEditor(

dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
editorMode: 'visual',
fixedToolbar: false,
fullscreenMode: true,
hiddenBlockTypes: [],
inactivePanels: [],
Expand All @@ -69,6 +68,7 @@ export function initializeEditor(

dispatch( preferencesStore ).setDefaults( 'core', {
allowRightClickOverrides: true,
fixedToolbar: false,
showBlockBreadcrumbs: true,
showIconLabels: false,
showListViewByDefault: false,
Expand Down
5 changes: 4 additions & 1 deletion packages/edit-post/src/index.native.js
Expand Up @@ -23,7 +23,6 @@ import Editor from './editor';
export function initializeEditor( id, postType, postId ) {
dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
editorMode: 'visual',
fixedToolbar: false,
fullscreenMode: true,
hiddenBlockTypes: [],
inactivePanels: [],
Expand All @@ -33,5 +32,9 @@ export function initializeEditor( id, postType, postId ) {
welcomeGuide: true,
} );

dispatch( preferencesStore ).setDefaults( 'core', {
fixedToolbar: false,
} );

return <Editor postId={ postId } postType={ postType } />;
}
2 changes: 1 addition & 1 deletion packages/edit-post/src/store/actions.js
Expand Up @@ -585,7 +585,7 @@ export const toggleDistractionFree =
registry.batch( () => {
registry
.dispatch( preferencesStore )
.set( 'core/edit-post', 'fixedToolbar', true );
.set( 'core', 'fixedToolbar', true );
registry.dispatch( editorStore ).setIsInserterOpened( false );
registry.dispatch( editorStore ).setIsListViewOpened( false );
dispatch.closeGeneralSidebar();
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-post/src/store/test/actions.js
Expand Up @@ -259,7 +259,7 @@ describe( 'actions', () => {
// Enable everything that shouldn't be enabled in distraction free mode.
registry
.dispatch( preferencesStore )
.set( 'core/edit-post', 'fixedToolbar', true );
.set( 'core', 'fixedToolbar', true );
registry.dispatch( editorStore ).setIsListViewOpened( true );
registry
.dispatch( editPostStore )
Expand All @@ -269,7 +269,7 @@ describe( 'actions', () => {
expect(
registry
.select( preferencesStore )
.get( 'core/edit-post', 'fixedToolbar' )
.get( 'core', 'fixedToolbar' )
).toBe( true );
expect( registry.select( editorStore ).isListViewOpened() ).toBe(
false
Expand Down
Expand Up @@ -95,7 +95,6 @@ export function useSpecificEditorSettings() {
const {
templateSlug,
isDistractionFree,
hasFixedToolbar,
canvasMode,
settings,
postWithTemplate,
Expand Down Expand Up @@ -124,9 +123,6 @@ export function useSpecificEditorSettings() {
'core/edit-site',
'distractionFree'
),
hasFixedToolbar:
!! getPreference( 'core/edit-site', 'fixedToolbar' ) ||
! isLargeViewport,
canvasMode: getCanvasMode(),
settings: getSettings(),
postWithTemplate: _context?.postId,
Expand All @@ -144,7 +140,6 @@ export function useSpecificEditorSettings() {
supportsTemplateMode: true,
focusMode: canvasMode !== 'view',
isDistractionFree,
hasFixedToolbar,
defaultRenderingMode,
getPostLinkProps,
// I wonder if they should be set in the post editor too
Expand All @@ -155,7 +150,6 @@ export function useSpecificEditorSettings() {
settings,
canvasMode,
isDistractionFree,
hasFixedToolbar,
defaultRenderingMode,
getPostLinkProps,
archiveLabels.archiveTypeLabel,
Expand Down
5 changes: 1 addition & 4 deletions packages/edit-site/src/components/header-edit-mode/index.js
Expand Up @@ -70,10 +70,7 @@ export default function HeaderEditMode() {
editorCanvasView: unlock(
select( editSiteStore )
).getEditorCanvasContainerView(),
hasFixedToolbar: getPreference(
editSiteStore.name,
'fixedToolbar'
),
hasFixedToolbar: getPreference( 'core', 'fixedToolbar' ),
isDistractionFree: getPreference(
editSiteStore.name,
'distractionFree'
Expand Down
Expand Up @@ -46,7 +46,7 @@ export default function MoreMenu( { showIconLabels } ) {

const toggleDistractionFree = () => {
registry.batch( () => {
setPreference( 'core/edit-site', 'fixedToolbar', true );
setPreference( 'core', 'fixedToolbar', true );
setIsInserterOpened( false );
setIsListViewOpened( false );
closeGeneralSidebar();
Expand All @@ -69,7 +69,7 @@ export default function MoreMenu( { showIconLabels } ) {
<>
<MenuGroup label={ _x( 'View', 'noun' ) }>
<PreferenceToggleMenuItem
scope="core/edit-site"
scope="core"
name="fixedToolbar"
onToggle={ turnOffDistractionFree }
label={ __( 'Top toolbar' ) }
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/layout/index.js
Expand Up @@ -98,7 +98,7 @@ export default function Layout() {
'core/edit-site/next-region'
),
hasFixedToolbar: select( preferencesStore ).get(
'core/edit-site',
'core',
'fixedToolbar'
),
isDistractionFree: select( preferencesStore ).get(
Expand Down
Expand Up @@ -36,7 +36,7 @@ export default function EditSitePreferencesModal() {
const { set: setPreference } = useDispatch( preferencesStore );
const toggleDistractionFree = () => {
registry.batch( () => {
setPreference( 'core/edit-site', 'fixedToolbar', true );
setPreference( 'core', 'fixedToolbar', true );
setIsInserterOpened( false );
setIsListViewOpened( false );
closeGeneralSidebar();
Expand Down
Expand Up @@ -291,7 +291,7 @@ function useEditUICommands() {
name: 'core/toggle-top-toolbar',
label: __( 'Toggle top toolbar' ),
callback: ( { close } ) => {
toggle( 'core/edit-site', 'fixedToolbar' );
toggle( 'core', 'fixedToolbar' );
if ( isDistractionFree ) {
toggleDistractionFree();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/index.js
Expand Up @@ -53,7 +53,6 @@ export function initializeEditor( id, settings ) {
// so that we won't trigger unnecessary re-renders with useEffect.
dispatch( preferencesStore ).setDefaults( 'core/edit-site', {
editorMode: 'visual',
fixedToolbar: false,
distractionFree: false,
welcomeGuide: true,
welcomeGuideStyles: true,
Expand All @@ -63,6 +62,7 @@ export function initializeEditor( id, settings ) {

dispatch( preferencesStore ).setDefaults( 'core', {
allowRightClickOverrides: true,
fixedToolbar: false,
focusMode: false,
keepCaretInsideBlock: false,
showBlockBreadcrumbs: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/store/actions.js
Expand Up @@ -607,7 +607,7 @@ export const toggleDistractionFree =
registry.batch( () => {
registry
.dispatch( preferencesStore )
.set( 'core/edit-site', 'fixedToolbar', true );
.set( 'core', 'fixedToolbar', true );
registry.dispatch( editorStore ).setIsInserterOpened( false );
registry.dispatch( editorStore ).setIsListViewOpened( false );
dispatch.closeGeneralSidebar();
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/store/test/actions.js
Expand Up @@ -120,7 +120,7 @@ describe( 'actions', () => {
// Enable everything that shouldn't be enabled in distraction free mode.
registry
.dispatch( preferencesStore )
.set( 'core/edit-site', 'fixedToolbar', true );
.set( 'core', 'fixedToolbar', true );
registry.dispatch( editorStore ).setIsListViewOpened( true );
registry
.dispatch( editSiteStore )
Expand All @@ -130,7 +130,7 @@ describe( 'actions', () => {
expect(
registry
.select( preferencesStore )
.get( 'core/edit-site', 'fixedToolbar' )
.get( 'core', 'fixedToolbar' )
).toBe( true );
expect( registry.select( editorStore ).isListViewOpened() ).toBe(
false
Expand Down