Skip to content

Commit caed8bf

Browse files
committed
Editor: withHistory: Add isIgnored callback option
1 parent 097df11 commit caed8bf

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

packages/editor/src/utils/with-history/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const DEFAULT_OPTIONS = {
1515
resetTypes: [],
1616
ignoreTypes: [],
1717
shouldOverwriteState: () => false,
18+
isIgnored: () => false,
1819
};
1920

2021
/**
@@ -26,6 +27,9 @@ const DEFAULT_OPTIONS = {
2627
* clear past.
2728
* @param {?Array} options.ignoreTypes Action types upon which to
2829
* avoid history tracking.
30+
* @param {?Function} options.isIgnored Function given action, to
31+
* return true if intended to
32+
* avoid history tracking.
2933
* @param {?Function} options.shouldOverwriteState Function receiving last and
3034
* current actions, returning
3135
* boolean indicating whether
@@ -34,13 +38,14 @@ const DEFAULT_OPTIONS = {
3438
*
3539
* @return {Function} Higher-order reducer.
3640
*/
37-
const withHistory = ( options = {} ) => ( reducer ) => {
41+
const withHistory = ( options ) => ( reducer ) => {
3842
options = { ...DEFAULT_OPTIONS, ...options };
3943

40-
// `ignoreTypes` is simply a convenience for `shouldOverwriteState`
44+
// `ignoreTypes`, `isIgnored` are conveniences for `shouldOverwriteState`
4145
options.shouldOverwriteState = overSome( [
4246
options.shouldOverwriteState,
4347
( action ) => includes( options.ignoreTypes, action.type ),
48+
( action ) => options.isIgnored( action ),
4449
] );
4550

4651
const initialState = {

packages/editor/src/utils/with-history/test/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,25 @@ describe( 'withHistory', () => {
131131
} );
132132
} );
133133

134+
it( 'should ignore history by options.isIgnored', () => {
135+
const reducer = withHistory( {
136+
isIgnored: ( action ) => action.type === 'INCREMENT',
137+
} )( counter );
138+
139+
let state;
140+
state = reducer( undefined, {} );
141+
state = reducer( state, { type: 'INCREMENT' } );
142+
state = reducer( state, { type: 'INCREMENT' } );
143+
144+
expect( state ).toEqual( {
145+
past: [],
146+
present: 2,
147+
future: [],
148+
lastAction: { type: 'INCREMENT' },
149+
shouldCreateUndoLevel: false,
150+
} );
151+
} );
152+
134153
it( 'should return same reference if state has not changed', () => {
135154
const reducer = withHistory()( counter );
136155
const original = reducer( undefined, {} );

0 commit comments

Comments
 (0)