Skip to content

Commit

Permalink
Fix multi-entity multi-property undo redo (#50911)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored May 29, 2023
1 parent dd99224 commit 41907dc
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 188 deletions.
4 changes: 4 additions & 0 deletions docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ _Returns_

### getRedoEdit

> **Deprecated** since 6.3
Returns the next edit from the current undo offset for the entity records edits history, if any.

_Parameters_
Expand Down Expand Up @@ -401,6 +403,8 @@ _Returns_

### getUndoEdit

> **Deprecated** since 6.3
Returns the previous edit from the current undo offset for the entity records edits history, if any.

_Parameters_
Expand Down
4 changes: 4 additions & 0 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ _Returns_

### getRedoEdit

> **Deprecated** since 6.3
Returns the next edit from the current undo offset for the entity records edits history, if any.

_Parameters_
Expand Down Expand Up @@ -578,6 +580,8 @@ _Returns_

### getUndoEdit

> **Deprecated** since 6.3
Returns the previous edit from the current undo offset for the entity records edits history, if any.

_Parameters_
Expand Down
17 changes: 9 additions & 8 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { receiveItems, removeItems, receiveQueriedItems } from './queried-data';
import { getOrLoadEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
import { createBatch } from './batch';
import { STORE_NAME } from './name';
import { getUndoEdits, getRedoEdits } from './private-selectors';

/**
* Returns an action object used in signalling that authors have been received.
Expand Down Expand Up @@ -406,14 +407,14 @@ export const editEntityRecord =
export const undo =
() =>
( { select, dispatch } ) => {
const undoEdit = select.getUndoEdit();
// Todo: we shouldn't have to pass "root" here.
const undoEdit = select( ( state ) => getUndoEdits( state.root ) );
if ( ! undoEdit ) {
return;
}
dispatch( {
type: 'EDIT_ENTITY_RECORD',
...undoEdit,
meta: { isUndo: true },
type: 'UNDO',
stackedEdits: undoEdit,
} );
};

Expand All @@ -424,14 +425,14 @@ export const undo =
export const redo =
() =>
( { select, dispatch } ) => {
const redoEdit = select.getRedoEdit();
// Todo: we shouldn't have to pass "root" here.
const redoEdit = select( ( state ) => getRedoEdits( state.root ) );
if ( ! redoEdit ) {
return;
}
dispatch( {
type: 'EDIT_ENTITY_RECORD',
...redoEdit,
meta: { isRedo: true },
type: 'REDO',
stackedEdits: redoEdit,
} );
};

Expand Down
1 change: 0 additions & 1 deletion packages/core-data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ const storeConfig = () => ( {
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
*/
export const store = createReduxStore( STORE_NAME, storeConfig() );

register( store );

export { default as EntityProvider } from './entity-provider';
Expand Down
30 changes: 30 additions & 0 deletions packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Internal dependencies
*/
import type { State, UndoEdit } from './selectors';

type Optional< T > = T | undefined;

/**
* Returns the previous edit from the current undo offset
* for the entity records edits history, if any.
*
* @param state State tree.
*
* @return The edit.
*/
export function getUndoEdits( state: State ): Optional< UndoEdit[] > {
return state.undo.list[ state.undo.list.length - 1 + state.undo.offset ];
}

/**
* Returns the next edit from the current undo offset
* for the entity records edits history, if any.
*
* @param state State tree.
*
* @return The edit.
*/
export function getRedoEdits( state: State ): Optional< UndoEdit[] > {
return state.undo.list[ state.undo.list.length + state.undo.offset ];
}
Loading

0 comments on commit 41907dc

Please sign in to comment.