-
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.
Showing revisions for the wp_global_styles custom post type in the styles sidebar. We're only showing max 10 to limit the payload.
- Loading branch information
Showing
5 changed files
with
259 additions
and
0 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
111 changes: 111 additions & 0 deletions
111
packages/edit-site/src/components/global-styles/screen-revisions.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,111 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { set } from 'lodash'; | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { __experimentalVStack as VStack, Button } from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useContext, useCallback, useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ScreenHeader from './header'; | ||
import Subtitle from './subtitle'; | ||
import { GlobalStylesContext } from './context'; | ||
|
||
function cloneDeep( object ) { | ||
return ! object ? {} : JSON.parse( JSON.stringify( object ) ); | ||
} | ||
|
||
function ScreenRevisions() { | ||
const { user: userConfig, setUserConfig } = | ||
useContext( GlobalStylesContext ); | ||
const { userRevisions } = useSelect( ( select ) => { | ||
const { getEditedEntityRecord } = select( coreStore ); | ||
const _globalStylesId = | ||
select( coreStore ).__experimentalGetCurrentGlobalStylesId(); | ||
|
||
// Maybe we can return the whole object from __experimentalGetCurrentGlobalStylesId | ||
// and rename it to __experimentalGetCurrentGlobalStyles, | ||
// otherwise we're grabbing this twice. | ||
const record = _globalStylesId | ||
? getEditedEntityRecord( 'root', 'globalStyles', _globalStylesId ) | ||
: undefined; | ||
|
||
return { | ||
userRevisions: record?.revisions || [], | ||
}; | ||
}, [] ); | ||
const [ currentRevision, setCurrentRevision ] = useState( | ||
userRevisions?.[ 0 ].id | ||
); | ||
const restoreRevision = useCallback( | ||
( revision ) => { | ||
const newUserConfig = cloneDeep( userConfig ); | ||
set( newUserConfig, [ 'styles' ], revision?.styles ); | ||
setUserConfig( () => newUserConfig ); | ||
setCurrentRevision( revision?.id ); | ||
}, | ||
[ userConfig ] | ||
); | ||
|
||
const hasRevisions = userRevisions.length > 0; | ||
|
||
return ( | ||
<> | ||
<ScreenHeader | ||
title={ __( 'Revisions' ) } | ||
description={ __( | ||
"Select a revision to preview it in the editor. Changes won't take effect until you've saved the template." | ||
) } | ||
/> | ||
<div className="edit-site-global-styles-screen-revisions"> | ||
<VStack spacing={ 3 }> | ||
<Subtitle>{ __( 'REVISIONS' ) }</Subtitle> | ||
{ hasRevisions | ||
? userRevisions.map( ( revision ) => ( | ||
<Button | ||
className={ classnames( | ||
'edit-site-global-styles-screen-revisions__revision-item', | ||
{ | ||
'is-current': | ||
currentRevision === revision.id, | ||
} | ||
) } | ||
variant={ | ||
currentRevision === revision.id | ||
? 'tertiary' | ||
: 'secondary' | ||
} | ||
isPressed={ | ||
currentRevision === revision.id | ||
} | ||
disabled={ currentRevision === revision.id } | ||
key={ `user-styles-revision-${ revision.id }` } | ||
onClick={ () => { | ||
restoreRevision( revision ); | ||
} } | ||
> | ||
<span className="edit-site-global-styles-screen-revisions__time-ago"> | ||
{ revision.timeAgo } | ||
</span> | ||
<span className="edit-site-global-styles-screen-revisions__date"> | ||
({ revision.dateShort }) | ||
</span> | ||
</Button> | ||
) ) | ||
: __( 'There are currently no revisions.' ) } | ||
</VStack> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export default ScreenRevisions; |
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
e32c7d4
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.
Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3737767136
📝 Reported issues:
specs/local/demo.test.js
specs/editor/blocks/classic.test.js