From 6c58262cf3e0f5b927e01877acb9e5cacd0ebc9d Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 4 Jun 2024 15:10:15 +0100 Subject: [PATCH] Batch updates when switching editor modes --- .../edit-site/src/store/private-actions.js | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/packages/edit-site/src/store/private-actions.js b/packages/edit-site/src/store/private-actions.js index 0a098d6d5efa99..bd56e30f10d11d 100644 --- a/packages/edit-site/src/store/private-actions.js +++ b/packages/edit-site/src/store/private-actions.js @@ -14,43 +14,49 @@ export const setCanvasMode = ( mode ) => ( { registry, dispatch } ) => { const switchCanvasMode = () => { - const isMediumOrBigger = - window.matchMedia( '(min-width: 782px)' ).matches; - registry.dispatch( blockEditorStore ).clearSelectedBlock(); - registry.dispatch( editorStore ).setDeviceType( 'Desktop' ); - registry - .dispatch( blockEditorStore ) - .__unstableSetEditorMode( 'edit' ); - const isPublishSidebarOpened = registry - .select( editorStore ) - .isPublishSidebarOpened(); - dispatch( { - type: 'SET_CANVAS_MODE', - mode, - } ); - const isEditMode = mode === 'edit'; - if ( isPublishSidebarOpened && ! isEditMode ) { - registry.dispatch( editorStore ).closePublishSidebar(); - } - - // Check if the block list view should be open by default. - // If `distractionFree` mode is enabled, the block list view should not be open. - // This behavior is disabled for small viewports. - if ( - isMediumOrBigger && - isEditMode && + registry.batch( () => { + const isMediumOrBigger = + window.matchMedia( '(min-width: 782px)' ).matches; + registry.dispatch( blockEditorStore ).clearSelectedBlock(); + registry.dispatch( editorStore ).setDeviceType( 'Desktop' ); registry - .select( preferencesStore ) - .get( 'core', 'showListViewByDefault' ) && - ! registry - .select( preferencesStore ) - .get( 'core', 'distractionFree' ) - ) { - registry.dispatch( editorStore ).setIsListViewOpened( true ); - } else { - registry.dispatch( editorStore ).setIsListViewOpened( false ); - } - registry.dispatch( editorStore ).setIsInserterOpened( false ); + .dispatch( blockEditorStore ) + .__unstableSetEditorMode( 'edit' ); + const isPublishSidebarOpened = registry + .select( editorStore ) + .isPublishSidebarOpened(); + dispatch( { + type: 'SET_CANVAS_MODE', + mode, + } ); + const isEditMode = mode === 'edit'; + if ( isPublishSidebarOpened && ! isEditMode ) { + registry.dispatch( editorStore ).closePublishSidebar(); + } + + // Check if the block list view should be open by default. + // If `distractionFree` mode is enabled, the block list view should not be open. + // This behavior is disabled for small viewports. + if ( + isMediumOrBigger && + isEditMode && + registry + .select( preferencesStore ) + .get( 'core', 'showListViewByDefault' ) && + ! registry + .select( preferencesStore ) + .get( 'core', 'distractionFree' ) + ) { + registry + .dispatch( editorStore ) + .setIsListViewOpened( true ); + } else { + registry + .dispatch( editorStore ) + .setIsListViewOpened( false ); + } + registry.dispatch( editorStore ).setIsInserterOpened( false ); + } ); }; if ( ! document.startViewTransition ) {