Skip to content

Commit

Permalink
Lay foundation for persisting mode
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Sep 3, 2024
1 parent 8ef39a7 commit 8dd5c44
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/block-editor/src/components/tool-selector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { forwardRef } from '@wordpress/element';
import { forwardRef, useEffect } from '@wordpress/element';
import { Icon, edit as editIcon, brush as brushIcon } from '@wordpress/icons';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand Down Expand Up @@ -51,6 +52,18 @@ function ToolSelector( props, ref ) {
// Usage
const modeIcon = modeIcons[ mode ] || modeIcons.default;

const { get: getPreference } = useSelect( ( select ) =>
select( preferencesStore )
);

// Todo - avoid effect.
useEffect( () => {
const editorMode = getPreference( 'core', '__experimentalEditorMode' );
if ( editorMode === 'simple' ) {
__unstableSetEditorMode( editorMode );
}
}, [ __unstableSetEditorMode, getPreference ] );

return (
<Dropdown
renderToggle={ ( { isOpen, onToggle } ) => (
Expand Down
9 changes: 9 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { __, _n, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { create, insert, remove, toHTMLString } from '@wordpress/rich-text';
import deprecated from '@wordpress/deprecated';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand Down Expand Up @@ -1672,6 +1673,8 @@ export const setNavigationMode =
export const __unstableSetEditorMode =
( mode ) =>
( { dispatch, select, registry } ) => {
const { set: setPreference } = registry.dispatch( preferencesStore );

const { [ sectionRootClientIdKey ]: sectionRootClientId } = registry
.select( STORE_NAME )
.getSettings();
Expand Down Expand Up @@ -1721,6 +1724,9 @@ export const __unstableSetEditorMode =
sectionClientIds.forEach( ( clientId ) => {
dispatch.setBlockEditingMode( clientId, 'contentOnly' );
} );

// Set the preference
setPreference( 'core', '__experimentalEditorMode', 'simple' );
} else {
dispatch.updateBlockAttributes( sectionClientIds, {
templateLock: null,
Expand All @@ -1729,6 +1735,9 @@ export const __unstableSetEditorMode =
sectionClientIds.forEach( ( clientId ) => {
dispatch.unsetBlockEditingMode( clientId );
} );

// Remove the preference
setPreference( 'core', '__experimentalEditorMode', null );
}

dispatch( { type: 'SET_EDITOR_MODE', mode } );
Expand Down

0 comments on commit 8dd5c44

Please sign in to comment.