-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Directly save if only changing current context #50567
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
55f6a40
Directly save if only changing current context
SaxonF 476b4e3
change save button label
SaxonF 2db3dcf
save button label updating
SaxonF e9e1547
Merge branch 'trunk' into update/save-hub-behaviour
SaxonF 0c25a0d
update to check postId for save hub
SaxonF c052b14
refactored save hub logic
SaxonF e147c67
Merge branch 'trunk' into update/save-hub-behaviour
SaxonF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,165 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { __experimentalHStack as HStack } from '@wordpress/components'; | ||
import { sprintf, _n } from '@wordpress/i18n'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { Button, __experimentalHStack as HStack } from '@wordpress/components'; | ||
import { __, sprintf, _n } from '@wordpress/i18n'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
import { check } from '@wordpress/icons'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import SaveButton from '../save-button'; | ||
import { isPreviewingTheme } from '../../utils/is-previewing-theme'; | ||
import { unlock } from '../../private-apis'; | ||
|
||
const { useLocation } = unlock( routerPrivateApis ); | ||
|
||
const PUBLISH_ON_SAVE_ENTITIES = [ | ||
{ | ||
kind: 'postType', | ||
name: 'wp_navigation', | ||
}, | ||
]; | ||
|
||
export default function SaveHub() { | ||
const { countUnsavedChanges, isDirty, isSaving } = useSelect( | ||
( select ) => { | ||
const { | ||
__experimentalGetDirtyEntityRecords, | ||
isSavingEntityRecord, | ||
} = select( coreStore ); | ||
const dirtyEntityRecords = __experimentalGetDirtyEntityRecords(); | ||
return { | ||
isDirty: dirtyEntityRecords.length > 0, | ||
isSaving: dirtyEntityRecords.some( ( record ) => | ||
isSavingEntityRecord( record.kind, record.name, record.key ) | ||
), | ||
countUnsavedChanges: dirtyEntityRecords.length, | ||
}; | ||
}, | ||
[] | ||
); | ||
const { params } = useLocation(); | ||
|
||
const { __unstableMarkLastChangeAsPersistent } = | ||
useDispatch( blockEditorStore ); | ||
|
||
const { createSuccessNotice, createErrorNotice } = | ||
useDispatch( noticesStore ); | ||
|
||
const { dirtyCurrentEntity, countUnsavedChanges, isDirty, isSaving } = | ||
useSelect( | ||
( select ) => { | ||
const { | ||
__experimentalGetDirtyEntityRecords, | ||
isSavingEntityRecord, | ||
} = select( coreStore ); | ||
const dirtyEntityRecords = | ||
__experimentalGetDirtyEntityRecords(); | ||
let calcDirtyCurrentEntity = null; | ||
|
||
if ( dirtyEntityRecords.length === 1 ) { | ||
// if we are on global styles | ||
if ( params.path?.includes( 'wp_global_styles' ) ) { | ||
calcDirtyCurrentEntity = dirtyEntityRecords.find( | ||
( record ) => record.name === 'globalStyles' | ||
); | ||
} | ||
// if we are on pages | ||
else if ( params.postId ) { | ||
calcDirtyCurrentEntity = dirtyEntityRecords.find( | ||
( record ) => | ||
record.name === params.postType && | ||
String( record.key ) === params.postId | ||
); | ||
} | ||
} | ||
|
||
return { | ||
dirtyCurrentEntity: calcDirtyCurrentEntity, | ||
isDirty: dirtyEntityRecords.length > 0, | ||
isSaving: dirtyEntityRecords.some( ( record ) => | ||
isSavingEntityRecord( | ||
record.kind, | ||
record.name, | ||
record.key | ||
) | ||
), | ||
countUnsavedChanges: dirtyEntityRecords.length, | ||
}; | ||
}, | ||
[ params.path, params.postType, params.postId ] | ||
); | ||
|
||
const { | ||
editEntityRecord, | ||
saveEditedEntityRecord, | ||
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits, | ||
} = useDispatch( coreStore ); | ||
|
||
const disabled = isSaving || ( ! isDirty && ! isPreviewingTheme() ); | ||
|
||
// if we have only one unsaved change and it matches current context, we can show a more specific label | ||
let label = dirtyCurrentEntity | ||
? __( 'Save' ) | ||
: sprintf( | ||
// translators: %d: number of unsaved changes (number). | ||
_n( | ||
'Review %d change…', | ||
'Review %d changes…', | ||
countUnsavedChanges | ||
), | ||
countUnsavedChanges | ||
); | ||
|
||
if ( isSaving ) { | ||
label = __( 'Saving' ); | ||
} | ||
Comment on lines
+103
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does but we also need it for the direct saving button |
||
|
||
const saveCurrentEntity = async () => { | ||
if ( ! dirtyCurrentEntity ) return; | ||
|
||
const { kind, name, key, property } = dirtyCurrentEntity; | ||
|
||
try { | ||
if ( 'root' === dirtyCurrentEntity.kind && 'site' === name ) { | ||
await saveSpecifiedEntityEdits( 'root', 'site', undefined, [ | ||
property, | ||
] ); | ||
} else { | ||
if ( | ||
PUBLISH_ON_SAVE_ENTITIES.some( | ||
( typeToPublish ) => | ||
typeToPublish.kind === kind && | ||
typeToPublish.name === name | ||
) | ||
) { | ||
editEntityRecord( kind, name, key, { status: 'publish' } ); | ||
} | ||
|
||
await saveEditedEntityRecord( kind, name, key ); | ||
} | ||
|
||
__unstableMarkLastChangeAsPersistent(); | ||
|
||
createSuccessNotice( __( 'Site updated.' ), { | ||
type: 'snackbar', | ||
} ); | ||
} catch ( error ) { | ||
createErrorNotice( `${ __( 'Saving failed.' ) } ${ error }` ); | ||
} | ||
}; | ||
|
||
return ( | ||
<HStack className="edit-site-save-hub" alignment="right" spacing={ 4 }> | ||
{ isDirty && ( | ||
<span> | ||
{ sprintf( | ||
// translators: %d: number of unsaved changes (number). | ||
_n( | ||
'%d unsaved change', | ||
'%d unsaved changes', | ||
countUnsavedChanges | ||
), | ||
countUnsavedChanges | ||
) } | ||
</span> | ||
{ dirtyCurrentEntity ? ( | ||
<Button | ||
variant="primary" | ||
onClick={ saveCurrentEntity } | ||
isBusy={ isSaving } | ||
disabled={ isSaving } | ||
aria-disabled={ isSaving } | ||
className="edit-site-save-hub__button" | ||
> | ||
{ label } | ||
</Button> | ||
) : ( | ||
<SaveButton | ||
className="edit-site-save-hub__button" | ||
variant={ disabled ? null : 'primary' } | ||
showTooltip={ false } | ||
icon={ disabled && ! isSaving ? check : null } | ||
defaultLabel={ label } | ||
/> | ||
) } | ||
<SaveButton | ||
className="edit-site-save-hub__button" | ||
variant={ disabled ? null : 'primary' } | ||
showTooltip={ false } | ||
icon={ disabled ? check : null } | ||
/> | ||
</HStack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a huge deal since
SaveButton
isn't a component that we export to third parties but I think the semantics arounddefaultLabel
are a little confusing. (When will it be shown?) If possible I think it'd be better to have alabel
prop that allows callers to override the label altogether. That way it's clearer that whenlabel
is set then that's what will appear.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@noisysocks main reason I called it
defaultLabel
was that the button label changes depending on saving state. If we called it label we'd have to also provide labels for each state (e.g. disabled, saving etc)