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

Extract undo/redo as a separate package #54292

Merged
merged 10 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,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().record();
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
};

/**
* 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
Loading