Skip to content

Commit

Permalink
Editor: Unify the Editor Mode preference. (#57642)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Jan 12, 2024
1 parent 7fce392 commit f89e323
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export function initializeEditor(
const root = createRoot( target );

dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
editorMode: 'visual',
fullscreenMode: true,
hiddenBlockTypes: [],
isPublishSidebarEnabled: true,
Expand All @@ -65,6 +64,7 @@ export function initializeEditor(

dispatch( preferencesStore ).setDefaults( 'core', {
allowRightClickOverrides: true,
editorMode: 'visual',
fixedToolbar: false,
inactivePanels: [],
openPanels: [ 'post-status' ],
Expand Down
4 changes: 1 addition & 3 deletions packages/edit-post/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ export const toggleFeature =
export const switchEditorMode =
( mode ) =>
( { dispatch, registry } ) => {
registry
.dispatch( preferencesStore )
.set( 'core/edit-post', 'editorMode', mode );
registry.dispatch( preferencesStore ).set( 'core', 'editorMode', mode );

// Unselect blocks when we switch to the code editor.
if ( mode !== 'visual' ) {
Expand Down
19 changes: 16 additions & 3 deletions packages/edit-post/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const EMPTY_OBJECT = {};
*/
export const getEditorMode = createRegistrySelector(
( select ) => () =>
select( preferencesStore ).get( 'core/edit-post', 'editorMode' ) ??
'visual'
select( preferencesStore ).get( 'core', 'editorMode' ) ?? 'visual'
);

/**
Expand Down Expand Up @@ -167,7 +166,6 @@ export const getPreferences = createRegistrySelector( ( select ) => () => {
// editor preferences.
const preferences = [
'hiddenBlockTypes',
'editorMode',
'preferredStyleVariations',
].reduce( ( accumulatedPrefs, preferenceKey ) => {
const value = select( preferencesStore ).get(
Expand All @@ -180,6 +178,20 @@ export const getPreferences = createRegistrySelector( ( select ) => () => {
[ preferenceKey ]: value,
};
}, {} );
const corePreferences = [ 'editorMode' ].reduce(
( accumulatedPrefs, preferenceKey ) => {
const value = select( preferencesStore ).get(
'core',
preferenceKey
);

return {
...accumulatedPrefs,
[ preferenceKey ]: value,
};
},
{}
);

// Panels were a preference, but the data structure changed when the state
// was migrated to the preferences store. They need to be converted from
Expand All @@ -194,6 +206,7 @@ export const getPreferences = createRegistrySelector( ( select ) => () => {

return {
...preferences,
...corePreferences,
panels,
};
} );
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export function initializeEditor( id, settings ) {
// We dispatch actions and update the store synchronously before rendering
// so that we won't trigger unnecessary re-renders with useEffect.
dispatch( preferencesStore ).setDefaults( 'core/edit-site', {
editorMode: 'visual',
welcomeGuide: true,
welcomeGuideStyles: true,
welcomeGuidePage: true,
Expand All @@ -62,6 +61,7 @@ export function initializeEditor( id, settings ) {
dispatch( preferencesStore ).setDefaults( 'core', {
allowRightClickOverrides: true,
distractionFree: false,
editorMode: 'visual',
fixedToolbar: false,
focusMode: false,
inactivePanels: [],
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ export const switchEditorMode =
( { dispatch, registry } ) => {
registry
.dispatch( 'core/preferences' )
.set( 'core/edit-site', 'editorMode', mode );
.set( 'core', 'editorMode', mode );

// Unselect blocks when we switch to a non visual mode.
if ( mode !== 'visual' ) {
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export const getCurrentTemplateTemplateParts = createRegistrySelector(
* @return {string} Editing mode.
*/
export const getEditorMode = createRegistrySelector( ( select ) => () => {
return select( preferencesStore ).get( 'core/edit-site', 'editorMode' );
return select( preferencesStore ).get( 'core', 'editorMode' );
} );

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function convertEditorSettings( data ) {
const settingsToMoveToCore = [
'allowRightClickOverrides',
'distractionFree',
'editorMode',
'fixedToolbar',
'focusMode',
'inactivePanels',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe( 'convertPreferencesPackageData', () => {
.toMatchInlineSnapshot( `
{
"core": {
"editorMode": "visual",
"fixedToolbar": true,
"inactivePanels": [],
"openPanels": [
Expand All @@ -54,7 +55,6 @@ describe( 'convertPreferencesPackageData', () => {
"welcomeGuide": false,
},
"core/edit-post": {
"editorMode": "visual",
"fullscreenMode": false,
"hiddenBlockTypes": [
"core/audio",
Expand Down

0 comments on commit f89e323

Please sign in to comment.