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

Migrate post editor editorMode to use preferences store and remove localAutosaveInterval preference #39180

Merged
merged 11 commits into from
Mar 11, 2022
29 changes: 29 additions & 0 deletions lib/compat/wordpress-5.9/block-editor-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Adds settings to the block editor.
*
* @package gutenberg
*/

/**
* Extends the block editor with settings that are only in the plugin.
*
* This is a temporary solution until the Gutenberg plugin sets
* the required WordPress version to 5.9.
*
* @see https://core.trac.wordpress.org/ticket/52920
*
* @param array $settings Existing editor settings.
*
* @return array Filtered settings.
*/
function gutenberg_get_block_editor_settings_5_9( $settings ) {
$settings['__unstableEnableFullSiteEditingBlocks'] = current_theme_supports( 'block-templates' );

if ( wp_is_block_theme() ) {
$settings['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas();
}

return $settings;
}
add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings_5_9' );
2 changes: 2 additions & 0 deletions lib/compat/wordpress-6.0/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
unset( $settings['__experimentalFeatures']['spacing']['padding'] );
}

$settings['localAutosaveInterval'] = 15;

return $settings;
}

Expand Down
23 changes: 0 additions & 23 deletions lib/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,6 @@
* @package gutenberg
*/

/**
* Extends the block editor with settings that are only in the plugin.
*
* This is a temporary solution until the Gutenberg plugin sets
* the required WordPress version to 5.9.
*
* @see https://core.trac.wordpress.org/ticket/52920
*
* @param array $settings Existing editor settings.
*
* @return array Filtered settings.
*/
function gutenberg_extend_post_editor_settings( $settings ) {
$settings['__unstableEnableFullSiteEditingBlocks'] = current_theme_supports( 'block-templates' );

if ( wp_is_block_theme() ) {
$settings['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas();
}

return $settings;
}
add_filter( 'block_editor_settings_all', 'gutenberg_extend_post_editor_settings' );

/**
* Initialize a block-based editor.
*
Expand Down
5 changes: 5 additions & 0 deletions packages/data/src/plugins/persistence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ persistencePlugin.__unstableMigrate = ( pluginOptions ) => {
'core/edit-post',
'hiddenBlockTypes'
);
migrateIndividualPreferenceToPreferencesStore(
persistence,
'core/edit-post',
'editorMode'
);
migrateFeaturePreferencesToPreferencesStore(
persistence,
'core/edit-site'
Expand Down
6 changes: 3 additions & 3 deletions packages/e2e-tests/specs/editor/various/autosave.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ async function getCurrentPostId() {

async function setLocalAutosaveInterval( value ) {
return page.evaluate( ( _value ) => {
window.wp.data
.dispatch( 'core/edit-post' )
.__experimentalUpdateLocalAutosaveInterval( _value );
window.wp.data.dispatch( 'core/editor' ).updateEditorSettings( {
localAutosaveInterval: _value,
} );
}, value );
}

Expand Down
10 changes: 4 additions & 6 deletions packages/e2e-tests/specs/performance/post-editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@ describe( 'Post Editor Performance', () => {
beforeEach( async () => {
// Disable auto-save to avoid impacting the metrics.
await page.evaluate( () => {
window.wp.data
.dispatch( 'core/edit-post' )
.__experimentalUpdateLocalAutosaveInterval( 100000000000 );
window.wp.data
.dispatch( 'core/editor' )
.updateEditorSettings( { autosaveInterval: 100000000000 } );
window.wp.data.dispatch( 'core/editor' ).updateEditorSettings( {
autosaveInterval: 100000000000,
localAutosaveInterval: 100000000000,
} );
} );
} );

Expand Down
6 changes: 0 additions & 6 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function Editor( {
preferredStyleVariations,
hiddenBlockTypes,
blockTypes,
__experimentalLocalAutosaveInterval,
keepCaretInsideBlock,
isTemplateMode,
template,
Expand Down Expand Up @@ -92,9 +91,6 @@ function Editor( {
),
hiddenBlockTypes: getHiddenBlockTypes(),
blockTypes: getBlockTypes(),
__experimentalLocalAutosaveInterval: getPreference(
'localAutosaveInterval'
),
keepCaretInsideBlock: isFeatureActive( 'keepCaretInsideBlock' ),
isTemplateMode: isEditingTemplate(),
template:
Expand All @@ -121,7 +117,6 @@ function Editor( {
hasFixedToolbar,
focusMode,
hasReducedUI,
__experimentalLocalAutosaveInterval,

// This is marked as experimental to give time for the quick inserter to mature.
__experimentalSetIsInserterOpened: setIsInserterOpened,
Expand Down Expand Up @@ -156,7 +151,6 @@ function Editor( {
hiddenBlockTypes,
blockTypes,
preferredStyleVariations,
__experimentalLocalAutosaveInterval,
setIsInserterOpened,
updatePreferredStyleVariations,
keepCaretInsideBlock,
Expand Down
1 change: 1 addition & 0 deletions packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export function initializeEditor(
);

dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
editorMode: 'visual',
fixedToolbar: false,
fullscreenMode: true,
hiddenBlockTypes: [],
Expand Down
10 changes: 10 additions & 0 deletions packages/edit-post/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import '@wordpress/core-data';
import '@wordpress/format-library';
import { dispatch } from '@wordpress/data';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand All @@ -19,5 +21,13 @@ import Editor from './editor';
* @param {Object} postId ID of the post to edit (unused right now)
*/
export function initializeEditor( id, postType, postId ) {
dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
editorMode: 'visual',
fixedToolbar: false,
fullscreenMode: true,
hiddenBlockTypes: [],
welcomeGuide: true,
} );
Comment on lines +24 to +30
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see there's some defaults here - https://github.com/WordPress/gutenberg/blob/trunk/packages/react-native-bridge/common/gutenberg-web-single-block/local-storage-overrides.json.

There are some preferences that don't have matching settings in the web codebase ('pinnedPluginItems', 'isGeneralSidebarDismissed' and 'showInserterHelpPanel'), so I'm not sure if those are mobile only features, or things that were removed in the web codebase.

The web codebase also stopped using 'nux' a long time ago. Is it still in use on mobile?


return <Editor postId={ postId } postType={ postType } />;
}
23 changes: 4 additions & 19 deletions packages/edit-post/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,10 @@ export const toggleFeature = ( feature ) => ( { registry } ) =>
*
* @param {string} mode The editor mode.
*/
export const switchEditorMode = ( mode ) => ( { dispatch, registry } ) => {
dispatch( {
type: 'SWITCH_MODE',
mode,
} );
export const switchEditorMode = ( mode ) => ( { registry } ) => {
registry
.dispatch( preferencesStore )
.set( 'core/edit-post', 'editorMode', mode );

// Unselect blocks when we switch to the code editor.
if ( mode !== 'visual' ) {
Expand Down Expand Up @@ -204,20 +203,6 @@ export function updatePreferredStyleVariations( blockName, blockStyle ) {
};
}

/**
* Returns an action object used in signalling that the editor should attempt
* to locally autosave the current post every `interval` seconds.
*
* @param {number} interval The new interval, in seconds.
* @return {Object} Action object.
*/
export function __experimentalUpdateLocalAutosaveInterval( interval ) {
return {
type: 'UPDATE_LOCAL_AUTOSAVE_INTERVAL',
interval,
};
}

/**
* Update the provided block types to be visible.
*
Expand Down
2 changes: 0 additions & 2 deletions packages/edit-post/src/store/defaults.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
export const PREFERENCES_DEFAULTS = {
editorMode: 'visual',
panels: {
'post-status': {
opened: true,
},
},
hiddenBlockTypes: [],
preferredStyleVariations: {},
localAutosaveInterval: 15,
};
15 changes: 0 additions & 15 deletions packages/edit-post/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ export const preferences = flow( [

return state;
},
editorMode( state, action ) {
if ( action.type === 'SWITCH_MODE' ) {
return action.mode;
}

return state;
},
preferredStyleVariations( state, action ) {
switch ( action.type ) {
case 'UPDATE_PREFERRED_STYLE_VARIATIONS': {
Expand All @@ -102,14 +95,6 @@ export const preferences = flow( [
}
return state;
},
localAutosaveInterval( state, action ) {
switch ( action.type ) {
case 'UPDATE_LOCAL_AUTOSAVE_INTERVAL':
return action.interval;
}

return state;
},
} );

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/edit-post/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const EMPTY_ARRAY = [];
*
* @return {string} Editing mode.
*/
export function getEditorMode( state ) {
return getPreference( state, 'editorMode', 'visual' );
}
export const getEditorMode = createRegistrySelector( ( select ) => () =>
select( preferencesStore ).get( 'core/edit-post', 'editorMode' ) ?? 'visual'
Copy link
Member

Choose a reason for hiding this comment

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

Isn't the ?? unnecessary because we're using setDefaults?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it is. I added it back so that the unit tests are more logical. I suppose the unit tests could call setDefaults though. 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Nah all good that makes sense 👍

);

/**
* Returns true if the editor sidebar is opened.
Expand Down Expand Up @@ -91,7 +91,7 @@ export const getActiveGeneralSidebarName = createRegistrySelector(

// The current list of preference keys that have been migrated to the
// preferences package.
const MIGRATED_KEYS = [ 'hiddenBlockTypes' ];
const MIGRATED_KEYS = [ 'hiddenBlockTypes', 'editorMode' ];

/**
* Returns the preferences (these preferences are persisted locally).
Expand Down
27 changes: 12 additions & 15 deletions packages/edit-post/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,22 @@ describe( 'actions', () => {

describe( 'switchEditorMode', () => {
it( 'to visual', () => {
// Switch to text first, since the default is visual.
registry.dispatch( editPostStore ).switchEditorMode( 'text' );
expect( registry.select( editPostStore ).getEditorMode() ).toEqual(
'text'
);
registry.dispatch( editPostStore ).switchEditorMode( 'visual' );
expect( registry.select( editPostStore ).getEditorMode() ).toEqual(
'visual'
);
} );

it( 'to text', () => {
// It defaults to visual.
expect( registry.select( editPostStore ).getEditorMode() ).toEqual(
'visual'
);
// Add a selected client id and make sure it's there.
const clientId = 'clientId_1';
registry.dispatch( blockEditorStore ).selectionChange( clientId );
Expand All @@ -89,6 +98,9 @@ describe( 'actions', () => {
expect(
registry.select( blockEditorStore ).getSelectedBlockClientId()
).toBeNull();
expect( registry.select( editPostStore ).getEditorMode() ).toEqual(
'text'
);
} );
} );

Expand Down Expand Up @@ -196,21 +208,6 @@ describe( 'actions', () => {
} );
} );

describe( '__experimentalUpdateLocalAutosaveInterval', () => {
it( 'sets the local autosave interval', () => {
registry
.dispatch( editPostStore )
.__experimentalUpdateLocalAutosaveInterval( 42 );

// TODO - remove once `getPreference` is deprecated.
expect(
registry
.select( editPostStore )
.getPreference( 'localAutosaveInterval' )
).toBe( 42 );
} );
} );

describe( 'toggleEditorPanelEnabled', () => {
it( 'toggles panels to be enabled and not enabled', () => {
const defaultState = {
Expand Down
9 changes: 0 additions & 9 deletions packages/edit-post/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,6 @@ describe( 'state', () => {
'post-status': { opened: false },
} );
} );

it( 'should return switched mode', () => {
const state = preferences( deepFreeze( { editorMode: 'visual' } ), {
type: 'SWITCH_MODE',
mode: 'text',
} );

expect( state.editorMode ).toBe( 'text' );
} );
} );

describe( 'activeModal', () => {
Expand Down
19 changes: 0 additions & 19 deletions packages/edit-post/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import deepFreeze from 'deep-freeze';
* Internal dependencies
*/
import {
getEditorMode,
getPreference,
isEditorPanelOpened,
isModalActive,
Expand All @@ -22,24 +21,6 @@ import {
} from '../selectors';

describe( 'selectors', () => {
describe( 'getEditorMode', () => {
it( 'should return the selected editor mode', () => {
const state = {
preferences: { editorMode: 'text' },
};

expect( getEditorMode( state ) ).toEqual( 'text' );
} );

it( 'should fallback to visual if not set', () => {
const state = {
preferences: {},
};

expect( getEditorMode( state ) ).toEqual( 'visual' );
} );
} );

describe( 'getPreference', () => {
it( 'should return the preference value if set', () => {
const state = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
- warn the user upon loading a post that there is a local copy that can be loaded;
- defer to remote autosaves, if any is available.

`LocalAutosaveMonitor` observes a saving interval defined specifically for local autosaves, in contrast with remote (server-side) autosaving. See editor setting `__experimentalLocalAutosaveInterval` and setter `__experimentalUpdateLocalAutosaveInterval`.
`LocalAutosaveMonitor` observes a saving interval defined specifically for local autosaves, in contrast with remote (server-side) autosaving.

The interval used for the local autosave can be modified by updating the editor settings
```js
wp.data.dispatch( 'core/editor' ).updateEditorSettings( {
localAutosaveInterval: 100000000000,
} );
```

## Example

Expand Down
Loading