-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.js
More file actions
135 lines (118 loc) · 3.3 KB
/
index.js
File metadata and controls
135 lines (118 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* External dependencies
*/
import { overSome, includes, first, last, drop, dropRight } from 'lodash';
/**
* Default options for withHistory reducer enhancer. Refer to withHistory
* documentation for options explanation.
*
* @see withHistory
*
* @type {Object}
*/
const DEFAULT_OPTIONS = {
resetTypes: [],
ignoreTypes: [],
shouldOverwriteState: () => false,
};
/**
* Higher-order reducer creator which transforms the result of the original
* reducer into an object tracking its own history (past, present, future).
*
* @param {?Object} options Optional options.
* @param {?Array} options.resetTypes Action types upon which to
* clear past.
* @param {?Array} options.ignoreTypes Action types upon which to
* avoid history tracking.
* @param {?Function} options.shouldOverwriteState Function receiving last and
* current actions, returning
* boolean indicating whether
* present should be merged,
* rather than add undo level.
*
* @return {Function} Higher-order reducer.
*/
const withHistory = ( options = {} ) => ( reducer ) => {
options = { ...DEFAULT_OPTIONS, ...options };
// `ignoreTypes` is simply a convenience for `shouldOverwriteState`
options.shouldOverwriteState = overSome( [
options.shouldOverwriteState,
( action ) => includes( options.ignoreTypes, action.type ),
] );
const initialState = {
past: [],
present: reducer( undefined, {} ),
future: [],
lastAction: null,
shouldCreateUndoLevel: false,
};
const {
resetTypes = [],
shouldOverwriteState = () => false,
} = options;
return ( state = initialState, action ) => {
const { past, present, future, lastAction, shouldCreateUndoLevel } = state;
const previousAction = lastAction;
switch ( action.type ) {
case 'UNDO':
// Can't undo if no past.
if ( ! past.length ) {
return state;
}
return {
past: dropRight( past ),
present: last( past ),
future: [ present, ...future ],
lastAction: null,
shouldCreateUndoLevel: false,
};
case 'REDO':
// Can't redo if no future.
if ( ! future.length ) {
return state;
}
return {
past: [ ...past, present ],
present: first( future ),
future: drop( future ),
lastAction: null,
shouldCreateUndoLevel: false,
};
case 'CREATE_UNDO_LEVEL':
return {
...state,
lastAction: null,
shouldCreateUndoLevel: true,
};
}
const nextPresent = reducer( present, action );
if ( includes( resetTypes, action.type ) ) {
return {
past: [],
present: nextPresent,
future: [],
lastAction: null,
shouldCreateUndoLevel: false,
};
}
if ( present === nextPresent ) {
return state;
}
let nextPast = past;
if (
shouldCreateUndoLevel ||
! past.length ||
! shouldOverwriteState( action, previousAction )
) {
nextPast = [ ...past, present ];
}
return {
past: nextPast,
present: nextPresent,
future: [],
shouldCreateUndoLevel: false,
lastAction: action,
};
};
};
export default withHistory;