Skip to content

Commit

Permalink
Extract undo/redo as a separate package (#54292)
Browse files Browse the repository at this point in the history
Co-authored-by: Ella <4710635+ellatrix@users.noreply.github.com>
  • Loading branch information
youknowriad and ellatrix authored Sep 11, 2023
1 parent bb4ed88 commit d9208b8
Show file tree
Hide file tree
Showing 24 changed files with 651 additions and 525 deletions.
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,12 @@
"markdown_source": "../packages/token-list/README.md",
"parent": "packages"
},
{
"title": "@wordpress/undo-manager",
"slug": "packages-undo-manager",
"markdown_source": "../packages/undo-manager/README.md",
"parent": "packages"
},
{
"title": "@wordpress/url",
"slug": "packages-url",
Expand Down
2 changes: 1 addition & 1 deletion docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ _Usage_

_Parameters_

- _state_ `State`: Editor state.
- _state_ Editor state.

_Returns_

Expand Down
26 changes: 26 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"@wordpress/style-engine": "file:packages/style-engine",
"@wordpress/sync": "file:packages/sync",
"@wordpress/token-list": "file:packages/token-list",
"@wordpress/undo-manager": "file:packages/undo-manager",
"@wordpress/url": "file:packages/url",
"@wordpress/viewport": "file:packages/viewport",
"@wordpress/warning": "file:packages/warning",
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ _Usage_

_Parameters_

- _state_ `State`: Editor state.
- _state_ Editor state.

_Returns_

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 @@ -43,6 +43,7 @@
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/private-apis": "file:../private-apis",
"@wordpress/sync": "file:../sync",
"@wordpress/undo-manager": "file:../undo-manager",
"@wordpress/url": "file:../url",
"change-case": "^4.1.2",
"equivalent-key-map": "^0.2.2",
Expand Down
49 changes: 31 additions & 18 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,20 +391,29 @@ export const editEntityRecord =
edit.edits
);
} else {
if ( ! options.undoIgnore ) {
select.getUndoManager().addRecord(
[
{
id: { kind, name, recordId },
changes: Object.keys( edits ).reduce(
( acc, key ) => {
acc[ key ] = {
from: editedRecord[ key ],
to: edits[ key ],
};
return acc;
},
{}
),
},
],
options.isCached
);
}
dispatch( {
type: 'EDIT_ENTITY_RECORD',
...edit,
meta: {
undo: ! options.undoIgnore && {
...edit,
// Send the current values for things like the first undo stack entry.
edits: Object.keys( edits ).reduce( ( acc, key ) => {
acc[ key ] = editedRecord[ key ];
return acc;
}, {} ),
isCached: options.isCached,
},
},
} );
}
};
Expand All @@ -416,13 +425,14 @@ export const editEntityRecord =
export const undo =
() =>
( { select, dispatch } ) => {
const undoEdit = select.getUndoEdits();
const undoEdit = select.getUndoManager().getUndoRecord();
if ( ! undoEdit ) {
return;
}
select.getUndoManager().undo();
dispatch( {
type: 'UNDO',
stackedEdits: undoEdit,
record: undoEdit,
} );
};

Expand All @@ -433,13 +443,14 @@ export const undo =
export const redo =
() =>
( { select, dispatch } ) => {
const redoEdit = select.getRedoEdits();
const redoEdit = select.getUndoManager().getRedoRecord();
if ( ! redoEdit ) {
return;
}
select.getUndoManager().redo();
dispatch( {
type: 'REDO',
stackedEdits: redoEdit,
record: redoEdit,
} );
};

Expand All @@ -448,9 +459,11 @@ export const redo =
*
* @return {Object} Action object.
*/
export function __unstableCreateUndoLevel() {
return { type: 'CREATE_UNDO_LEVEL' };
}
export const __unstableCreateUndoLevel =
() =>
( { select } ) => {
select.getUndoManager().addRecord();
};

/**
* Action triggered to save an entity record.
Expand Down
21 changes: 4 additions & 17 deletions packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
* Internal dependencies
*/
import type { State, UndoEdit } from './selectors';
import type { State } from './selectors';

type Optional< T > = T | undefined;
type EntityRecordKey = string | number;

/**
Expand All @@ -12,22 +11,10 @@ type EntityRecordKey = string | number;
*
* @param state State tree.
*
* @return The edit.
* @return The undo manager.
*/
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 ];
export function getUndoManager( state: State ) {
return state.undoManager;
}

/**
Expand Down
Loading

0 comments on commit d9208b8

Please sign in to comment.