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: Migrate and unify the panel preferences #57529

Merged
merged 6 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

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/edit-post/src/index.js
Expand Up @@ -57,9 +57,7 @@ export function initializeEditor(
editorMode: 'visual',
fullscreenMode: true,
hiddenBlockTypes: [],
inactivePanels: [],
isPublishSidebarEnabled: true,
openPanels: [ 'post-status' ],
preferredStyleVariations: {},
themeStyles: true,
welcomeGuide: true,
Expand All @@ -69,6 +67,8 @@ export function initializeEditor(
dispatch( preferencesStore ).setDefaults( 'core', {
allowRightClickOverrides: true,
fixedToolbar: false,
inactivePanels: [],
openPanels: [ 'post-status' ],
showBlockBreadcrumbs: true,
showIconLabels: false,
showListViewByDefault: false,
Expand Down
4 changes: 4 additions & 0 deletions packages/edit-post/src/index.native.js
Expand Up @@ -31,6 +31,10 @@ export function initializeEditor( id, postType, postId ) {
preferredStyleVariations: {},
welcomeGuide: true,
} );
dispatch( preferencesStore ).setDefaults( 'core', {
inactivePanels: [],
openPanels: [ 'post-status' ],
} );

dispatch( preferencesStore ).setDefaults( 'core', {
fixedToolbar: false,
Expand Down
7 changes: 2 additions & 5 deletions packages/edit-post/src/store/selectors.js
Expand Up @@ -186,13 +186,10 @@ export const getPreferences = createRegistrySelector( ( select ) => () => {
// the new preferences store format to old format to ensure no breaking
// changes for plugins.
const inactivePanels = select( preferencesStore ).get(
'core/edit-post',
'core',
'inactivePanels'
);
const openPanels = select( preferencesStore ).get(
'core/edit-post',
'openPanels'
);
const openPanels = select( preferencesStore ).get( 'core', 'openPanels' );
const panels = convertPanelsToOldFormat( inactivePanels, openPanels );

return {
Expand Down
@@ -0,0 +1,23 @@
/**
* WordPress dependencies
*/
import { compose, ifCondition } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { ___unstablePreferencesModalBaseOption as BaseOption } from '@wordpress/interface';
import { store as editorStore } from '@wordpress/editor';

export default compose(
withSelect( ( select, { panelName } ) => {
const { isEditorPanelEnabled, isEditorPanelRemoved } =
select( editorStore );
return {
isRemoved: isEditorPanelRemoved( panelName ),
isChecked: isEditorPanelEnabled( panelName ),
};
} ),
ifCondition( ( { isRemoved } ) => ! isRemoved ),
withDispatch( ( dispatch, { panelName } ) => ( {
onChange: () =>
dispatch( editorStore ).toggleEditorPanelEnabled( panelName ),
} ) )
)( BaseOption );
103 changes: 77 additions & 26 deletions packages/edit-site/src/components/preferences-modal/index.js
Expand Up @@ -11,12 +11,20 @@ import { useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { store as preferencesStore } from '@wordpress/preferences';
import { store as editorStore } from '@wordpress/editor';
import {
PostTaxonomies,
PostExcerptCheck,
PageAttributesCheck,
PostFeaturedImageCheck,
PostTypeSupportCheck,
store as editorStore,
} from '@wordpress/editor';

/**
* Internal dependencies
*/
import EnableFeature from './enable-feature';
import EnablePanelOption from './enable-panel-option';
import { store as editSiteStore } from '../../store';

export const PREFERENCES_MODAL_NAME = 'edit-site/preferences';
Expand Down Expand Up @@ -52,32 +60,75 @@ export default function EditSitePreferencesModal() {
name: 'general',
tabLabel: __( 'General' ),
content: (
<PreferencesModalSection title={ __( 'Interface' ) }>
<EnableFeature
scope="core"
featureName="showListViewByDefault"
help={ __(
'Opens the block list view sidebar by default.'
) }
label={ __( 'Always open list view' ) }
/>
<EnableFeature
scope="core"
featureName="showBlockBreadcrumbs"
help={ __(
'Shows block breadcrumbs at the bottom of the editor.'
) }
label={ __( 'Display block breadcrumbs' ) }
/>
<EnableFeature
scope="core"
featureName="allowRightClickOverrides"
help={ __(
'Allows contextual list view menus via right-click, overriding browser defaults.'
<>
<PreferencesModalSection title={ __( 'Interface' ) }>
<EnableFeature
scope="core"
featureName="showListViewByDefault"
help={ __(
'Opens the block list view sidebar by default.'
) }
label={ __( 'Always open list view' ) }
/>
<EnableFeature
scope="core"
featureName="showBlockBreadcrumbs"
help={ __(
'Shows block breadcrumbs at the bottom of the editor.'
) }
label={ __( 'Display block breadcrumbs' ) }
/>
<EnableFeature
scope="core"
featureName="allowRightClickOverrides"
help={ __(
'Allows contextual list view menus via right-click, overriding browser defaults.'
) }
label={ __( 'Allow right-click contextual menus' ) }
/>
</PreferencesModalSection>
<PreferencesModalSection
title={ __( 'Document settings' ) }
description={ __(
'Select what settings are shown in the document panel.'
) }
label={ __( 'Allow right-click contextual menus' ) }
/>
</PreferencesModalSection>
>
<PostTaxonomies
taxonomyWrapper={ ( content, taxonomy ) => (
<EnablePanelOption
label={ taxonomy.labels.menu_name }
panelName={ `taxonomy-panel-${ taxonomy.slug }` }
/>
) }
/>
<PostFeaturedImageCheck>
<EnablePanelOption
label={ __( 'Featured image' ) }
panelName="featured-image"
/>
</PostFeaturedImageCheck>
<PostExcerptCheck>
<EnablePanelOption
label={ __( 'Excerpt' ) }
panelName="post-excerpt"
/>
</PostExcerptCheck>
<PostTypeSupportCheck
supportKeys={ [ 'comments', 'trackbacks' ] }
>
<EnablePanelOption
label={ __( 'Discussion' ) }
panelName="discussion-panel"
/>
</PostTypeSupportCheck>
<PageAttributesCheck>
<EnablePanelOption
label={ __( 'Page attributes' ) }
panelName="page-attributes"
/>
</PageAttributesCheck>
</PreferencesModalSection>
</>
),
},
{
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-site/src/index.js
Expand Up @@ -64,7 +64,9 @@ export function initializeEditor( id, settings ) {
allowRightClickOverrides: true,
fixedToolbar: false,
focusMode: false,
inactivePanels: [],
keepCaretInsideBlock: false,
openPanels: [ 'post-status' ],
showBlockBreadcrumbs: true,
showListViewByDefault: false,
} );
Expand Down
11 changes: 5 additions & 6 deletions packages/editor/src/store/actions.js
Expand Up @@ -624,7 +624,7 @@ export const toggleEditorPanelEnabled =
const inactivePanels =
registry
.select( preferencesStore )
.get( 'core/edit-post', 'inactivePanels' ) ?? [];
.get( 'core', 'inactivePanels' ) ?? [];

const isPanelInactive = !! inactivePanels?.includes( panelName );

Expand All @@ -641,7 +641,7 @@ export const toggleEditorPanelEnabled =

registry
.dispatch( preferencesStore )
.set( 'core/edit-post', 'inactivePanels', updatedInactivePanels );
.set( 'core', 'inactivePanels', updatedInactivePanels );
};

/**
Expand All @@ -653,9 +653,8 @@ export const toggleEditorPanelOpened =
( panelName ) =>
( { registry } ) => {
const openPanels =
registry
.select( preferencesStore )
.get( 'core/edit-post', 'openPanels' ) ?? [];
registry.select( preferencesStore ).get( 'core', 'openPanels' ) ??
[];

const isPanelOpen = !! openPanels?.includes( panelName );

Expand All @@ -672,7 +671,7 @@ export const toggleEditorPanelOpened =

registry
.dispatch( preferencesStore )
.set( 'core/edit-post', 'openPanels', updatedOpenPanels );
.set( 'core', 'openPanels', updatedOpenPanels );
};

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/store/selectors.js
Expand Up @@ -1152,7 +1152,7 @@ export const isEditorPanelEnabled = createRegistrySelector(
// For backward compatibility, we check edit-post
// even though now this is in "editor" package.
const inactivePanels = select( preferencesStore ).get(
'core/edit-post',
'core',
'inactivePanels'
);
return (
Expand All @@ -1176,7 +1176,7 @@ export const isEditorPanelOpened = createRegistrySelector(
// For backward compatibility, we check edit-post
// even though now this is in "editor" package.
const openPanels = select( preferencesStore ).get(
'core/edit-post',
'core',
'openPanels'
);
return !! openPanels?.includes( panelName );
Expand Down
Expand Up @@ -10,7 +10,9 @@ const Section = ( { description, title, children } ) => (
</p>
) }
</legend>
{ children }
<div className="interface-preferences-modal__section-content">
{ children }
</div>
</fieldset>
);

Expand Down
Expand Up @@ -22,3 +22,7 @@
font-style: normal;
color: $gray-700;
}

.interface-preferences-modal__section:has(.interface-preferences-modal__section-content:empty) {
display: none;
}
Expand Up @@ -8,7 +8,9 @@ export default function convertEditorSettings( data ) {
'allowRightClickOverrides',
'fixedToolbar',
'focusMode',
'inactivePanels',
'keepCaretInsideBlock',
'openPanels',
'showBlockBreadcrumbs',
'showIconLabels',
'showListViewByDefault',
Expand Down
Expand Up @@ -44,6 +44,10 @@ describe( 'convertPreferencesPackageData', () => {
{
"core": {
"fixedToolbar": true,
"inactivePanels": [],
"openPanels": [
"post-status",
],
},
"core/customize-widgets": {
"fixedToolbar": true,
Expand All @@ -56,10 +60,6 @@ describe( 'convertPreferencesPackageData', () => {
"core/audio",
"core/cover",
],
"inactivePanels": [],
"openPanels": [
"post-status",
],
"pinnedItems": {
"my-sidebar-plugin/title-sidebar": false,
},
Expand Down