From 86a396aaa9bf39a6b16806efa19cc8efa431136b Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 24 Oct 2024 17:45:30 +0300 Subject: [PATCH 1/3] Site editor: Fix save shortcut --- .../components/keyboard-shortcuts/global.js | 32 ++++++++----------- .../edit-site/src/components/layout/index.js | 2 +- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/edit-site/src/components/keyboard-shortcuts/global.js b/packages/edit-site/src/components/keyboard-shortcuts/global.js index 8ab9723e42ea65..c46f3c5b7115eb 100644 --- a/packages/edit-site/src/components/keyboard-shortcuts/global.js +++ b/packages/edit-site/src/components/keyboard-shortcuts/global.js @@ -5,44 +5,40 @@ import { useShortcut } from '@wordpress/keyboard-shortcuts'; import { useDispatch, useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; import { store as editorStore } from '@wordpress/editor'; -import { privateApis as routerPrivateApis } from '@wordpress/router'; /** * Internal dependencies */ import { store as editSiteStore } from '../../store'; -import { unlock } from '../../lock-unlock'; - -const { useHistory } = unlock( routerPrivateApis ); +/** + * Register the save keyboard shortcut in view mode. + * + * @return {null} Returns null. + */ function KeyboardShortcutsGlobal() { const { __experimentalGetDirtyEntityRecords, isSavingEntityRecord } = useSelect( coreStore ); - const { hasNonPostEntityChanges } = useSelect( editorStore ); + const { hasNonPostEntityChanges, isPostSavingLocked } = + useSelect( editorStore ); + const { savePost } = useDispatch( editorStore ); const { setIsSaveViewOpened } = useDispatch( editSiteStore ); - const history = useHistory(); useShortcut( 'core/edit-site/save', ( event ) => { event.preventDefault(); - const dirtyEntityRecords = __experimentalGetDirtyEntityRecords(); const hasDirtyEntities = !! dirtyEntityRecords.length; const isSaving = dirtyEntityRecords.some( ( record ) => isSavingEntityRecord( record.kind, record.name, record.key ) ); - const _hasNonPostEntityChanges = hasNonPostEntityChanges(); - const isViewMode = - history.getLocationWithParams().params.canvas === 'view'; - if ( - ( ! hasDirtyEntities || ! _hasNonPostEntityChanges || isSaving ) && - ! isViewMode - ) { + if ( ! hasDirtyEntities || isSaving ) { return; } - // At this point, we know that there are dirty entities, other than - // the edited post, and we're not in the process of saving, so open - // save view. - setIsSaveViewOpened( true ); + if ( hasNonPostEntityChanges() ) { + setIsSaveViewOpened( true ); + } else if ( ! isPostSavingLocked() ) { + savePost(); + } } ); return null; diff --git a/packages/edit-site/src/components/layout/index.js b/packages/edit-site/src/components/layout/index.js index 12799f46fbe348..596fc2420c48da 100644 --- a/packages/edit-site/src/components/layout/index.js +++ b/packages/edit-site/src/components/layout/index.js @@ -81,7 +81,7 @@ export default function Layout( { route } ) { return ( <> - + { canvas === 'view' && }
Date: Thu, 24 Oct 2024 19:21:39 +0300 Subject: [PATCH 2/3] rename to SaveKeyboardShortcut --- .../components/keyboard-shortcuts/register.js | 27 ----------------- .../edit-site/src/components/layout/index.js | 6 ++-- .../index.js} | 30 ++++++++++++++++--- 3 files changed, 28 insertions(+), 35 deletions(-) delete mode 100644 packages/edit-site/src/components/keyboard-shortcuts/register.js rename packages/edit-site/src/components/{keyboard-shortcuts/global.js => save-keyboard-shortcut/index.js} (64%) diff --git a/packages/edit-site/src/components/keyboard-shortcuts/register.js b/packages/edit-site/src/components/keyboard-shortcuts/register.js deleted file mode 100644 index 8e491ab61c36f7..00000000000000 --- a/packages/edit-site/src/components/keyboard-shortcuts/register.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * WordPress dependencies - */ -import { useEffect } from '@wordpress/element'; -import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts'; -import { useDispatch } from '@wordpress/data'; -import { __ } from '@wordpress/i18n'; - -function KeyboardShortcutsRegister() { - // Registering the shortcuts. - const { registerShortcut } = useDispatch( keyboardShortcutsStore ); - useEffect( () => { - registerShortcut( { - name: 'core/edit-site/save', - category: 'global', - description: __( 'Save your changes.' ), - keyCombination: { - modifier: 'primary', - character: 's', - }, - } ); - }, [ registerShortcut ] ); - - return null; -} - -export default KeyboardShortcutsRegister; diff --git a/packages/edit-site/src/components/layout/index.js b/packages/edit-site/src/components/layout/index.js index 596fc2420c48da..4ca39c1af7c51a 100644 --- a/packages/edit-site/src/components/layout/index.js +++ b/packages/edit-site/src/components/layout/index.js @@ -35,8 +35,7 @@ import ErrorBoundary from '../error-boundary'; import { default as SiteHub, SiteHubMobile } from '../site-hub'; import ResizableFrame from '../resizable-frame'; import { unlock } from '../../lock-unlock'; -import KeyboardShortcutsRegister from '../keyboard-shortcuts/register'; -import KeyboardShortcutsGlobal from '../keyboard-shortcuts/global'; +import SaveKeyboardShortcut from '../save-keyboard-shortcut'; import { useIsSiteEditorLoading } from './hooks'; import useMovingAnimation from './animation'; import SidebarContent from '../sidebar'; @@ -81,8 +80,7 @@ export default function Layout( { route } ) { return ( <> - { canvas === 'view' && } - + { canvas === 'view' && }
{ + registerShortcut( { + name: shortCutName, + category: 'global', + description: __( 'Save your changes.' ), + keyCombination: { + modifier: 'primary', + character: 's', + }, + } ); + return () => { + unregisterShortcut( shortCutName ); + }; + }, [ registerShortcut, unregisterShortcut ] ); useShortcut( 'core/edit-site/save', ( event ) => { event.preventDefault(); @@ -43,5 +67,3 @@ function KeyboardShortcutsGlobal() { return null; } - -export default KeyboardShortcutsGlobal; From 2c57e6d81e1e67a0a2e59b8ad035c4f489b951a4 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca <150562+mcsf@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:14:07 +0000 Subject: [PATCH 3/3] Rename internal variable --- .../src/components/save-keyboard-shortcut/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/edit-site/src/components/save-keyboard-shortcut/index.js b/packages/edit-site/src/components/save-keyboard-shortcut/index.js index eee71179b2017a..a1dccbd2a6b483 100644 --- a/packages/edit-site/src/components/save-keyboard-shortcut/index.js +++ b/packages/edit-site/src/components/save-keyboard-shortcut/index.js @@ -16,7 +16,7 @@ import { store as editorStore } from '@wordpress/editor'; */ import { store as editSiteStore } from '../../store'; -const shortCutName = 'core/edit-site/save'; +const shortcutName = 'core/edit-site/save'; /** * Register the save keyboard shortcut in view mode. @@ -35,7 +35,7 @@ export default function SaveKeyboardShortcut() { ); useEffect( () => { registerShortcut( { - name: shortCutName, + name: shortcutName, category: 'global', description: __( 'Save your changes.' ), keyCombination: { @@ -44,7 +44,7 @@ export default function SaveKeyboardShortcut() { }, } ); return () => { - unregisterShortcut( shortCutName ); + unregisterShortcut( shortcutName ); }; }, [ registerShortcut, unregisterShortcut ] );