From e816e71902a70c59e16aef208a03b60562b1aa88 Mon Sep 17 00:00:00 2001 From: epiqueras Date: Sun, 15 Sep 2019 14:41:01 -0400 Subject: [PATCH] Core Data: Fix undo approach. --- package-lock.json | 1 + packages/block-editor/src/store/reducer.js | 4 +-- packages/core-data/package.json | 1 + packages/core-data/src/reducer.js | 39 ++++++++++------------ 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6997ac51ebbd9..e22853f36d83e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4885,6 +4885,7 @@ "@wordpress/api-fetch": "file:packages/api-fetch", "@wordpress/data": "file:packages/data", "@wordpress/deprecated": "file:packages/deprecated", + "@wordpress/is-shallow-equal": "file:packages/is-shallow-equal", "@wordpress/url": "file:packages/url", "equivalent-key-map": "^0.2.2", "lodash": "^4.17.14", diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index 69dcbf65d72e2..9f40684c6632d 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -381,9 +381,7 @@ function withPersistentBlockChange( reducer ) { // In comparing against the previous action, consider only those which // would have qualified as one which would have been ignored or not // have resulted in a changed state. - if ( ! isExplicitPersistentChange ) { - lastAction = action; - } + lastAction = action; return nextState; }; diff --git a/packages/core-data/package.json b/packages/core-data/package.json index bc485ead098ed..3da791a43f81c 100644 --- a/packages/core-data/package.json +++ b/packages/core-data/package.json @@ -26,6 +26,7 @@ "@wordpress/api-fetch": "file:../api-fetch", "@wordpress/data": "file:../data", "@wordpress/deprecated": "file:../deprecated", + "@wordpress/is-shallow-equal": "file:../is-shallow-equal", "@wordpress/url": "file:../url", "equivalent-key-map": "^0.2.2", "lodash": "^4.17.14", diff --git a/packages/core-data/src/reducer.js b/packages/core-data/src/reducer.js index 48c3a6da889e8..68c3ff7e51f13 100644 --- a/packages/core-data/src/reducer.js +++ b/packages/core-data/src/reducer.js @@ -7,6 +7,7 @@ import { keyBy, map, groupBy, flowRight, isEqual, get } from 'lodash'; * WordPress dependencies */ import { combineReducers } from '@wordpress/data'; +import isShallowEqual from '@wordpress/is-shallow-equal'; /** * Internal dependencies @@ -322,32 +323,26 @@ export function undo( state = UNDO_INITIAL_STATE, action ) { return nextState; } - let nextState; - if ( state.length === 0 ) { - // Create an initial entry so that we can undo to it. - nextState = [ - { - kind: action.meta.undo.kind, - name: action.meta.undo.name, - recordId: action.meta.undo.recordId, - edits: { ...state.flattenedUndo, ...action.meta.undo.edits }, - }, - ]; - nextState.offset = 0; - return nextState; - } - // Clear potential redos, because this only supports linear history. - nextState = state.slice( 0, state.offset || undefined ); + const nextState = state.slice( 0, state.offset || undefined ); nextState.offset = 0; - + nextState.pop(); nextState.push( { - kind: action.kind, - name: action.name, - recordId: action.recordId, - edits: { ...action.edits, ...state.flattenedUndo }, + kind: action.meta.undo.kind, + name: action.meta.undo.name, + recordId: action.meta.undo.recordId, + edits: { ...state.flattenedUndo, ...action.meta.undo.edits }, } ); - + const comparisonUndoEdits = Object.values( action.meta.undo.edits ).filter( ( edit ) => typeof edit !== 'function' ); + const comparisonEdits = Object.values( action.edits ).filter( ( edit ) => typeof edit !== 'function' ); + if ( ! isShallowEqual( comparisonUndoEdits, comparisonEdits ) ) { + nextState.push( { + kind: action.kind, + name: action.name, + recordId: action.recordId, + edits: action.edits, + } ); + } return nextState; }