-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explore Multi-Entity Saving (#18029)
* Core Data: Add selector for getting all entity changes. * Editor: Add global changes save button. * Editor: Implement pre-pre-publish-flow global save flow. * Post Publish Button: Change dashicon to dot pseudo element. * Post Publish Button: Also add trailing ellipsis when there are global changes. * Entities Saved States: Remove changed properties text. * Entities Saved States: Use the correct label. * Post Publish Button: Review suggestions. * Package: Update lockfile. * Core Data: Improve `getEntityRecordChangesByRecord` types and docs. * Entities Saved States: Add inline comments and fix post saving race condition. * Core Data: Test `getEntityRecordChangesByRecord`. * Editor: Test `hasNonPostEntityChanges`. * Entities Saved States: Fix cancel close error. * Post Publish Button: Enable buttons when there are non post entity changes.
- Loading branch information
Showing
16 changed files
with
491 additions
and
16 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
104 changes: 104 additions & 0 deletions
104
packages/editor/src/components/entities-saved-states/index.js
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { startCase } from 'lodash'; | ||
import EquivalentKeyMap from 'equivalent-key-map'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { CheckboxControl, Modal, Button } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useState } from '@wordpress/element'; | ||
|
||
const EntitiesSavedStatesCheckbox = ( { | ||
id, | ||
name, | ||
changes: { rawRecord }, | ||
checked, | ||
setCheckedById, | ||
} ) => ( | ||
<CheckboxControl | ||
label={ `${ startCase( name ) }: "${ rawRecord.title || | ||
rawRecord.name || | ||
__( 'Untitled' ) }"` } | ||
checked={ checked } | ||
onChange={ ( nextChecked ) => setCheckedById( id, nextChecked ) } | ||
/> | ||
); | ||
|
||
export default function EntitiesSavedStates( { | ||
isOpen, | ||
onRequestClose, | ||
ignoredForSave = new EquivalentKeyMap(), | ||
} ) { | ||
const entityRecordChangesByRecord = useSelect( ( select ) => | ||
select( 'core' ).getEntityRecordChangesByRecord() | ||
); | ||
const { saveEditedEntityRecord } = useDispatch( 'core' ); | ||
|
||
const [ checkedById, _setCheckedById ] = useState( () => new EquivalentKeyMap() ); | ||
const setCheckedById = ( id, checked ) => | ||
_setCheckedById( ( prevCheckedById ) => { | ||
const nextCheckedById = new EquivalentKeyMap( prevCheckedById ); | ||
if ( checked ) { | ||
nextCheckedById.set( id, true ); | ||
} else { | ||
nextCheckedById.delete( id ); | ||
} | ||
return nextCheckedById; | ||
} ); | ||
const saveCheckedEntities = () => { | ||
checkedById.forEach( ( _checked, id ) => { | ||
if ( ! ignoredForSave.has( id ) ) { | ||
saveEditedEntityRecord( | ||
...id.filter( ( s, i ) => i !== id.length - 1 || s !== 'undefined' ) | ||
); | ||
} | ||
} ); | ||
onRequestClose( checkedById ); | ||
}; | ||
return ( | ||
isOpen && ( | ||
<Modal | ||
title={ __( 'What do you want to save?' ) } | ||
onRequestClose={ () => onRequestClose() } | ||
contentLabel={ __( 'Select items to save.' ) } | ||
> | ||
{ Object.keys( entityRecordChangesByRecord ).map( ( changedKind ) => | ||
Object.keys( entityRecordChangesByRecord[ changedKind ] ).map( | ||
( changedName ) => | ||
Object.keys( | ||
entityRecordChangesByRecord[ changedKind ][ changedName ] | ||
).map( ( changedKey ) => { | ||
const id = [ changedKind, changedName, changedKey ]; | ||
return ( | ||
<EntitiesSavedStatesCheckbox | ||
key={ id.join( ' | ' ) } | ||
id={ id } | ||
name={ changedName } | ||
changes={ | ||
entityRecordChangesByRecord[ changedKind ][ changedName ][ | ||
changedKey | ||
] | ||
} | ||
checked={ checkedById.get( id ) } | ||
setCheckedById={ setCheckedById } | ||
/> | ||
); | ||
} ) | ||
) | ||
) } | ||
<Button | ||
isPrimary | ||
disabled={ checkedById.size === 0 } | ||
onClick={ saveCheckedEntities } | ||
className="editor-entities-saved-states__save-button" | ||
> | ||
{ __( 'Save' ) } | ||
</Button> | ||
</Modal> | ||
) | ||
); | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/editor/src/components/entities-saved-states/style.scss
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.editor-entities-saved-states__save-button { | ||
display: block; | ||
margin-left: auto; | ||
margin-right: 0; | ||
} |
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
Oops, something went wrong.