Skip to content

Commit

Permalink
Site editor: Fix opening of save panel when using the save shortcut (
Browse files Browse the repository at this point in the history
…#59647)

Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: jasmussen <joen@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: SaxonF <saxonafletcher@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
Co-authored-by: colorful-tones <colorful-tones@git.wordpress.org>
Co-authored-by: sunil25393 <sunil25393@git.wordpress.org>
  • Loading branch information
8 people committed Mar 8, 2024
1 parent c08b5d7 commit ed7edbc
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions packages/edit-site/src/components/keyboard-shortcuts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,41 @@
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';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';

function KeyboardShortcutsGlobal() {
const { __experimentalGetDirtyEntityRecords, isSavingEntityRecord } =
useSelect( coreStore );
const { hasNonPostEntityChanges } = useSelect( editorStore );
const { getCanvasMode } = unlock( useSelect( editSiteStore ) );
const { setIsSaveViewOpened } = useDispatch( editSiteStore );

useShortcut( 'core/edit-site/save', ( event ) => {
event.preventDefault();

const dirtyEntityRecords = __experimentalGetDirtyEntityRecords();
const isDirty = !! dirtyEntityRecords.length;
const hasDirtyEntities = !! dirtyEntityRecords.length;
const isSaving = dirtyEntityRecords.some( ( record ) =>
isSavingEntityRecord( record.kind, record.name, record.key )
);

if ( ! isSaving && isDirty ) {
setIsSaveViewOpened( true );
const _hasNonPostEntityChanges = hasNonPostEntityChanges();
const isViewMode = getCanvasMode() === 'view';
if (
( ! hasDirtyEntities || ! _hasNonPostEntityChanges || isSaving ) &&
! isViewMode
) {
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 );
} );

return null;
Expand Down

0 comments on commit ed7edbc

Please sign in to comment.