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

Site editor: Fix opening of save panel when using the save shortcut #59647

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading