Skip to content

Commit

Permalink
Deprecate getUndoEdit and getRedoEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 25, 2023
1 parent 7ae67d8 commit 18733ea
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 12 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
6 changes: 3 additions & 3 deletions package-lock.json

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

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
7 changes: 5 additions & 2 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,7 +407,8 @@ 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;
}
Expand All @@ -423,7 +425,8 @@ 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;
}
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 } 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< any > {
return state.undo.list[ state.undo.list.length - 2 + 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< any > {
return state.undo.list[ state.undo.list.length + state.undo.offset ];
}
23 changes: 17 additions & 6 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
setNestedValue,
} from './utils';
import type * as ET from './entity-types';
import { getUndoEdits, getRedoEdits } from './private-selectors';

// This is an incomplete, high-level approximation of the State type.
// It makes the selectors slightly more safe, but is intended to evolve
Expand Down Expand Up @@ -884,28 +885,38 @@ function getCurrentUndoOffset( state: State ): number {
* Returns the previous edit from the current undo offset
* for the entity records edits history, if any.
*
* @param state State tree.
* @deprecated since 6.3
*
* @param state State tree.
*
* @return The edit.
*/
export function getUndoEdit( state: State ): Optional< any > {
deprecated( "select( 'core' ).getUndoEdit()", {
since: '6.3',
} );
return state.undo.list[
state.undo.list.length - 2 + getCurrentUndoOffset( state )
];
]?.[ 0 ];
}

/**
* Returns the next edit from the current undo offset
* for the entity records edits history, if any.
*
* @param state State tree.
* @deprecated since 6.3
*
* @param state State tree.
*
* @return The edit.
*/
export function getRedoEdit( state: State ): Optional< any > {
deprecated( "select( 'core' ).getRedoEdit()", {
since: '6.3',
} );
return state.undo.list[
state.undo.list.length + getCurrentUndoOffset( state )
];
]?.[ 0 ];
}

/**
Expand All @@ -917,7 +928,7 @@ export function getRedoEdit( state: State ): Optional< any > {
* @return Whether there is a previous edit or not.
*/
export function hasUndo( state: State ): boolean {
return Boolean( getUndoEdit( state ) );
return Boolean( getUndoEdits( state ) );
}

/**
Expand All @@ -929,7 +940,7 @@ export function hasUndo( state: State ): boolean {
* @return Whether there is a next edit or not.
*/
export function hasRedo( state: State ): boolean {
return Boolean( getRedoEdit( state ) );
return Boolean( getRedoEdits( state ) );
}

/**
Expand Down

0 comments on commit 18733ea

Please sign in to comment.