Skip to content

Commit

Permalink
Core Data: Fix undo approach.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Sep 16, 2019
1 parent dd7782a commit a40ce33
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 17 additions & 22 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit a40ce33

Please sign in to comment.