Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi-entity multi-property undo redo #50911

Merged
merged 9 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Optional< T > = T | undefined;
* @return The edit.
*/
export function getUndoEdits( state: State ): Optional< any > {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know the return value is an array so Optional< unknown[] > should be better.

return state.undo.list[ state.undo.list.length - 2 + state.undo.offset ];
return state.undo.list[ state.undo.list.length - 1 + state.undo.offset ];
}

/**
Expand Down
107 changes: 55 additions & 52 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,19 @@ const withMultiEntityRecordEdits = ( reducer ) => ( state, action ) => {
const { stackedEdits } = action;

let newState = state;
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
stackedEdits.forEach( ( { kind, name, recordId, edits } ) => {
newState = reducer( newState, {
type: 'EDIT_ENTITY_RECORD',
kind,
name,
recordId,
edits,
} );
} );
stackedEdits.forEach(
( { kind, name, recordId, property, from, to } ) => {
newState = reducer( newState, {
type: 'EDIT_ENTITY_RECORD',
kind,
name,
recordId,
edits: {
[ property ]: action.type === 'UNDO' ? from : to,
},
} );
}
);
return newState;
}

Expand Down Expand Up @@ -479,7 +483,7 @@ export function undo( state = UNDO_INITIAL_STATE, action ) {
nextState = omitPendingRedos( nextState );
let previousUndoState = nextState.list.pop();
currentState.cache.forEach( ( edit ) => {
previousUndoState = appendEditsToStack( previousUndoState, edit );
previousUndoState = appendEditToStack( previousUndoState, edit );
} );
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
nextState.list.push( previousUndoState );

Expand All @@ -489,30 +493,31 @@ export function undo( state = UNDO_INITIAL_STATE, action ) {
};
};

const appendEditsToStack = (
const appendEditToStack = (
stack = [],
{ kind, name, recordId, edits }
{ kind, name, recordId, property, from, to }
) => {
const existingEditIndex = stack?.findIndex(
( { kind: k, name: n, recordId: r } ) => {
return k === kind && n === name && r === recordId;
( { kind: k, name: n, recordId: r, property: p } ) => {
return (
k === kind && n === name && r === recordId && p === property
);
}
);

const nextStack = stack.filter(
( _, index ) => index !== existingEditIndex
);
nextStack.push( {
kind,
name,
recordId,
edits: {
...( existingEditIndex !== -1
? stack[ existingEditIndex ].edits
: {} ),
...edits,
},
} );
const nextStack = [ ...stack ];
if ( existingEditIndex !== -1 ) {
// If the edit is already in the stack leave the initial "from" value.
nextStack[ existingEditIndex ].to = to;
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
} else {
nextStack.push( {
kind,
name,
recordId,
property,
from,
to,
} );
}
return nextStack;
};

Expand All @@ -529,37 +534,41 @@ export function undo( state = UNDO_INITIAL_STATE, action ) {
};
}

case 'EDIT_ENTITY_RECORD':
// When undo is not specified,
// It's unclear whether we should ignore the undo entirely
// or consider it a transient (cached) edit.
if ( ! action?.meta?.undo ) {
case 'EDIT_ENTITY_RECORD': {
if ( ! action.meta.undo ) {
return state;
}

const isCachedChange = Object.keys( action.edits ).every(
( key ) => action.transientEdits[ key ]
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
);

const edits = Object.keys( action.edits ).map( ( key ) => {
return {
kind: action.kind,
name: action.name,
recordId: action.recordId,
property: key,
from: action.meta.undo.edits[ key ],
to: action.edits[ key ],
};
} );

if ( isCachedChange ) {
let newCache = state.cache;
edits.forEach(
( edit ) =>
( newCache = appendEditToStack( newCache, edit ) )
);
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
return {
...state,
cache: appendEditsToStack( state.cache, action ),
cache: newCache,
};
}

let nextState = omitPendingRedos( state );
nextState = appendCachedEditsToLastUndo( nextState );
nextState = { ...nextState, list: [ ...nextState.list ] };
const previousUndoState = nextState.list.pop();
nextState.list.push(
appendEditsToStack( previousUndoState, {
kind: action.kind,
name: action.name,
recordId: action.recordId,
edits: action.meta.undo.edits,
} )
);
// When an edit is a function it's an optimization to avoid running some expensive operation.
// We can't rely on the function references being the same so we opt out of comparing them here.
const comparisonUndoEdits = Object.values(
Expand All @@ -569,17 +578,11 @@ export function undo( state = UNDO_INITIAL_STATE, action ) {
( edit ) => typeof edit !== 'function'
);
if ( ! isShallowEqual( comparisonUndoEdits, comparisonEdits ) ) {
nextState.list.push( [
{
kind: action.kind,
name: action.name,
recordId: action.recordId,
edits: action.edits,
},
] );
nextState.list.push( edits );
}

return nextState;
}
}

return state;
Expand Down
Loading