From 0a8dfd1962938bf16dae73575907f16cd650efe3 Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 21 Dec 2022 15:36:28 +1100 Subject: [PATCH] Sending and checking for settings as well since some style variations might have different settings we'd want to reinstate. Doing a deep equality check to highlight the currently selected revision. --- ...berg-rest-global-styles-controller-6-2.php | 1 + .../global-styles/screen-revisions.js | 61 +++++++++++++------ 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/lib/compat/wordpress-6.2/class-gutenberg-rest-global-styles-controller-6-2.php b/lib/compat/wordpress-6.2/class-gutenberg-rest-global-styles-controller-6-2.php index 7c368ce602944..353d0d7a785b8 100644 --- a/lib/compat/wordpress-6.2/class-gutenberg-rest-global-styles-controller-6-2.php +++ b/lib/compat/wordpress-6.2/class-gutenberg-rest-global-styles-controller-6-2.php @@ -154,6 +154,7 @@ public function prepare_item_for_response( $post, $request ) { // phpcs:ignore V $modified_gmt = strtotime( $revision->post_modified_gmt . ' +0000' ); $user_revisions[] = array( 'styles' => ! empty( $config['styles'] ) ? $config['styles'] : new stdClass(), + 'settings' => ! empty( $config['settings'] ) ? $config['settings'] : new stdClass(), 'dateShort' => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), $modified ), /* translators: %s: Human-readable time difference. */ 'timeAgo' => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ), diff --git a/packages/edit-site/src/components/global-styles/screen-revisions.js b/packages/edit-site/src/components/global-styles/screen-revisions.js index faf1e67f16bf5..cd29a68f5c938 100644 --- a/packages/edit-site/src/components/global-styles/screen-revisions.js +++ b/packages/edit-site/src/components/global-styles/screen-revisions.js @@ -3,6 +3,7 @@ */ import { set } from 'lodash'; import classnames from 'classnames'; +import fastDeepEqual from 'fast-deep-equal/es6'; /** * WordPress dependencies @@ -20,10 +21,21 @@ import ScreenHeader from './header'; import Subtitle from './subtitle'; import { GlobalStylesContext } from './context'; +// Taken from packages/edit-site/src/hooks/push-changes-to-global-styles/index.js. +// TODO abstract function cloneDeep( object ) { return ! object ? {} : JSON.parse( JSON.stringify( object ) ); } +// Taken from packages/edit-site/src/components/global-styles/screen-style-variations.js. +// TODO abstract +function compareVariations( a, b ) { + return ( + fastDeepEqual( a.styles, b.styles ) && + fastDeepEqual( a.settings, b.settings ) + ); +} + function ScreenRevisions() { const { user: userConfig, setUserConfig } = useContext( GlobalStylesContext ); @@ -43,21 +55,33 @@ function ScreenRevisions() { userRevisions: record?.revisions || [], }; }, [] ); - const [ currentRevision, setCurrentRevision ] = useState( - userRevisions?.[ 0 ].id - ); + + const hasRevisions = userRevisions.length > 0; + const [ currentRevisionId, setCurrentRevisionId ] = useState( () => { + if ( ! hasRevisions ) { + return 0; + } + let currentRevision = userRevisions[ 0 ]; + for ( let i = 0; i < userRevisions.length; i++ ) { + if ( compareVariations( userConfig, userRevisions[ i ] ) ) { + currentRevision = userRevisions[ i ]; + break; + } + } + return currentRevision?.id; + } ); + const restoreRevision = useCallback( ( revision ) => { const newUserConfig = cloneDeep( userConfig ); set( newUserConfig, [ 'styles' ], revision?.styles ); + set( newUserConfig, [ 'settings' ], revision?.settings ); setUserConfig( () => newUserConfig ); - setCurrentRevision( revision?.id ); + setCurrentRevisionId( revision?.id ); }, [ userConfig ] ); - const hasRevisions = userRevisions.length > 0; - return ( <> { __( 'REVISIONS' ) } - { hasRevisions - ? userRevisions.map( ( revision ) => ( + { hasRevisions ? ( + userRevisions.map( ( revision ) => { + const isActive = revision?.id === currentRevisionId; + return ( - ) ) - : __( 'There are currently no revisions.' ) } + ); + } ) + ) : ( +

{ __( 'There are currently no revisions.' ) }

+ ) }