Skip to content

Commit 41907dc

Browse files
authored
Fix multi-entity multi-property undo redo (#50911)
1 parent dd99224 commit 41907dc

File tree

10 files changed

+336
-188
lines changed

10 files changed

+336
-188
lines changed

docs/reference-guides/data/data-core.md

+4
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ _Returns_
358358

359359
### getRedoEdit
360360

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

363365
_Parameters_
@@ -401,6 +403,8 @@ _Returns_
401403

402404
### getUndoEdit
403405

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

406410
_Parameters_

packages/core-data/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ _Returns_
535535

536536
### getRedoEdit
537537

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

540542
_Parameters_
@@ -578,6 +580,8 @@ _Returns_
578580

579581
### getUndoEdit
580582

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

583587
_Parameters_

packages/core-data/src/actions.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { receiveItems, removeItems, receiveQueriedItems } from './queried-data';
1818
import { getOrLoadEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
1919
import { createBatch } from './batch';
2020
import { STORE_NAME } from './name';
21+
import { getUndoEdits, getRedoEdits } from './private-selectors';
2122

2223
/**
2324
* Returns an action object used in signalling that authors have been received.
@@ -406,14 +407,14 @@ export const editEntityRecord =
406407
export const undo =
407408
() =>
408409
( { select, dispatch } ) => {
409-
const undoEdit = select.getUndoEdit();
410+
// Todo: we shouldn't have to pass "root" here.
411+
const undoEdit = select( ( state ) => getUndoEdits( state.root ) );
410412
if ( ! undoEdit ) {
411413
return;
412414
}
413415
dispatch( {
414-
type: 'EDIT_ENTITY_RECORD',
415-
...undoEdit,
416-
meta: { isUndo: true },
416+
type: 'UNDO',
417+
stackedEdits: undoEdit,
417418
} );
418419
};
419420

@@ -424,14 +425,14 @@ export const undo =
424425
export const redo =
425426
() =>
426427
( { select, dispatch } ) => {
427-
const redoEdit = select.getRedoEdit();
428+
// Todo: we shouldn't have to pass "root" here.
429+
const redoEdit = select( ( state ) => getRedoEdits( state.root ) );
428430
if ( ! redoEdit ) {
429431
return;
430432
}
431433
dispatch( {
432-
type: 'EDIT_ENTITY_RECORD',
433-
...redoEdit,
434-
meta: { isRedo: true },
434+
type: 'REDO',
435+
stackedEdits: redoEdit,
435436
} );
436437
};
437438

packages/core-data/src/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const storeConfig = () => ( {
6262
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
6363
*/
6464
export const store = createReduxStore( STORE_NAME, storeConfig() );
65-
6665
register( store );
6766

6867
export { default as EntityProvider } from './entity-provider';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import type { State, UndoEdit } from './selectors';
5+
6+
type Optional< T > = T | undefined;
7+
8+
/**
9+
* Returns the previous edit from the current undo offset
10+
* for the entity records edits history, if any.
11+
*
12+
* @param state State tree.
13+
*
14+
* @return The edit.
15+
*/
16+
export function getUndoEdits( state: State ): Optional< UndoEdit[] > {
17+
return state.undo.list[ state.undo.list.length - 1 + state.undo.offset ];
18+
}
19+
20+
/**
21+
* Returns the next edit from the current undo offset
22+
* for the entity records edits history, if any.
23+
*
24+
* @param state State tree.
25+
*
26+
* @return The edit.
27+
*/
28+
export function getRedoEdits( state: State ): Optional< UndoEdit[] > {
29+
return state.undo.list[ state.undo.list.length + state.undo.offset ];
30+
}

0 commit comments

Comments
 (0)