Skip to content

Commit

Permalink
Write/Design tool: Persist as a user preference (#65945)
Browse files Browse the repository at this point in the history
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: oandregal <[email protected]>
Co-authored-by: mtias <[email protected]>
Co-authored-by: draganescu <[email protected]>
Co-authored-by: ndiego <[email protected]>
Co-authored-by: richtabor <[email protected]>
  • Loading branch information
7 people authored and ciampo committed Oct 14, 2024
1 parent 0803a81 commit 8a08495
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 29 deletions.
5 changes: 3 additions & 2 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 @@ -1668,7 +1669,7 @@ export const setNavigationMode =
*/
export const __unstableSetEditorMode =
( mode ) =>
( { dispatch, select } ) => {
( { dispatch, select, registry } ) => {
// When switching to zoom-out mode, we need to select the parent section
if ( mode === 'zoom-out' ) {
const firstSelectedClientId = select.getBlockSelectionStart();
Expand Down Expand Up @@ -1708,7 +1709,7 @@ export const __unstableSetEditorMode =
}
}

dispatch( { type: 'SET_EDITOR_MODE', mode } );
registry.dispatch( preferencesStore ).set( 'core', 'editorTool', mode );

if ( mode === 'navigation' ) {
speak(
Expand Down
17 changes: 0 additions & 17 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1786,22 +1786,6 @@ export const blockListSettings = ( state = {}, action ) => {
return state;
};

/**
* Reducer returning which mode is enabled.
*
* @param {string} state Current state.
* @param {Object} action Dispatched action.
*
* @return {string} Updated state.
*/
export function editorMode( state = 'edit', action ) {
if ( action.type === 'SET_EDITOR_MODE' ) {
return action.mode;
}

return state;
}

/**
* Reducer return an updated state representing the most recent block attribute
* update. The state is structured as an object where the keys represent the
Expand Down Expand Up @@ -2117,7 +2101,6 @@ const combinedReducers = combineReducers( {
preferences,
lastBlockAttributesChange,
lastFocus,
editorMode,
expandedBlock,
highlightedBlock,
lastBlockInserted,
Expand Down
11 changes: 7 additions & 4 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { symbol } from '@wordpress/icons';
import { create, remove, toHTMLString } from '@wordpress/rich-text';
import deprecated from '@wordpress/deprecated';
import { createSelector, createRegistrySelector } from '@wordpress/data';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand Down Expand Up @@ -2691,7 +2692,7 @@ export function __experimentalGetLastBlockAttributeChanges( state ) {
* @return {boolean} Is navigation mode enabled.
*/
export function isNavigationMode( state ) {
return state.editorMode === 'navigation';
return __unstableGetEditorMode( state ) === 'navigation';
}

/**
Expand All @@ -2701,9 +2702,11 @@ export function isNavigationMode( state ) {
*
* @return {string} the editor mode.
*/
export function __unstableGetEditorMode( state ) {
return state.editorMode;
}
export const __unstableGetEditorMode = createRegistrySelector(
( select ) => () => {
return select( preferencesStore ).get( 'core', 'editorTool' );
}
);

/**
* Returns whether block moving mode is enabled.
Expand Down
8 changes: 7 additions & 1 deletion packages/block-editor/src/store/test/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
isDragging,
getBlockStyles,
} from '../private-selectors';
import { getBlockEditingMode } from '../selectors';
import { getBlockEditingMode, __unstableGetEditorMode } from '../selectors';

describe( 'private selectors', () => {
describe( 'isBlockInterfaceHidden', () => {
Expand Down Expand Up @@ -125,11 +125,17 @@ describe( 'private selectors', () => {
};

const hasContentRoleAttribute = jest.fn( () => false );
const get = jest.fn( () => 'edit' );
getBlockEditingMode.registry = {
select: jest.fn( () => ( {
hasContentRoleAttribute,
} ) ),
};
__unstableGetEditorMode.registry = {
select: jest.fn( () => ( {
get,
} ) ),
};

it( 'should return false when top level block is not disabled', () => {
const state = {
Expand Down
15 changes: 13 additions & 2 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { RawHTML } from '@wordpress/element';
import { symbol } from '@wordpress/icons';
import { select, dispatch } from '@wordpress/data';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand Down Expand Up @@ -4470,7 +4471,6 @@ describe( 'getBlockEditingMode', () => {

const navigationModeStateWithRootSection = {
...baseState,
editorMode: 'navigation',
settings: {
[ sectionRootClientIdKey ]: 'ef45d5fd-5234-4fd5-ac4f-c3736c7f9337', // The group is the "main" container
},
Expand All @@ -4480,12 +4480,18 @@ describe( 'getBlockEditingMode', () => {

const fauxPrivateAPIs = {};

lock( fauxPrivateAPIs, { hasContentRoleAttribute } );
lock( fauxPrivateAPIs, {
hasContentRoleAttribute,
} );

getBlockEditingMode.registry = {
select: jest.fn( () => fauxPrivateAPIs ),
};

afterEach( () => {
dispatch( preferencesStore ).set( 'core', 'editorTool', undefined );
} );

it( 'should return default by default', () => {
expect(
getBlockEditingMode(
Expand Down Expand Up @@ -4610,6 +4616,7 @@ describe( 'getBlockEditingMode', () => {
} );

it( 'in navigation mode, the root section container is default', () => {
dispatch( preferencesStore ).set( 'core', 'editorTool', 'navigation' );
expect(
getBlockEditingMode(
navigationModeStateWithRootSection,
Expand All @@ -4619,6 +4626,7 @@ describe( 'getBlockEditingMode', () => {
} );

it( 'in navigation mode, anything outside the section container is disabled', () => {
dispatch( preferencesStore ).set( 'core', 'editorTool', 'navigation' );
expect(
getBlockEditingMode(
navigationModeStateWithRootSection,
Expand All @@ -4628,6 +4636,7 @@ describe( 'getBlockEditingMode', () => {
} );

it( 'in navigation mode, sections are contentOnly', () => {
dispatch( preferencesStore ).set( 'core', 'editorTool', 'navigation' );
expect(
getBlockEditingMode(
navigationModeStateWithRootSection,
Expand All @@ -4643,6 +4652,7 @@ describe( 'getBlockEditingMode', () => {
} );

it( 'in navigation mode, blocks with content attributes within sections are contentOnly', () => {
dispatch( preferencesStore ).set( 'core', 'editorTool', 'navigation' );
hasContentRoleAttribute.mockReturnValueOnce( true );
expect(
getBlockEditingMode(
Expand All @@ -4661,6 +4671,7 @@ describe( 'getBlockEditingMode', () => {
} );

it( 'in navigation mode, blocks without content attributes within sections are disabled', () => {
dispatch( preferencesStore ).set( 'core', 'editorTool', 'navigation' );
expect(
getBlockEditingMode(
navigationModeStateWithRootSection,
Expand Down
3 changes: 0 additions & 3 deletions packages/edit-site/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export const setCanvasMode =
registry.batch( () => {
registry.dispatch( blockEditorStore ).clearSelectedBlock();
registry.dispatch( editorStore ).setDeviceType( 'Desktop' );
registry
.dispatch( blockEditorStore )
.__unstableSetEditorMode( 'edit' );
const isPublishSidebarOpened = registry
.select( editorStore )
.isPublishSidebarOpened();
Expand Down
2 changes: 2 additions & 0 deletions packages/reusable-blocks/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {

import { store as coreStore } from '@wordpress/core-data';
import apiFetch from '@wordpress/api-fetch';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand All @@ -31,6 +32,7 @@ function createRegistryWithStores() {
registry.register( blockEditorStore );
registry.register( reusableBlocksStore );
registry.register( blocksStore );
registry.register( preferencesStore );

// Register entity here instead of mocking API handlers for loadPostTypeEntities()
registry.dispatch( coreStore ).addEntities( [
Expand Down

0 comments on commit 8a08495

Please sign in to comment.