valtio-history 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.d.ts +7 -14
- package/src/index.js +12 -17
- package/src/index.js.map +1 -1
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -14,11 +14,12 @@ export type HistoryNode<T> = {
|
|
|
14
14
|
*/
|
|
15
15
|
updatedAt?: Date;
|
|
16
16
|
};
|
|
17
|
+
declare const EMPTY_WIP: unique symbol;
|
|
17
18
|
export type History<T> = {
|
|
18
19
|
/**
|
|
19
20
|
* field for holding sandbox changes; used to avoid infinite loops
|
|
20
21
|
*/
|
|
21
|
-
wip
|
|
22
|
+
wip: Snapshot<T> | typeof EMPTY_WIP;
|
|
22
23
|
/**
|
|
23
24
|
* the nodes of the history for each change
|
|
24
25
|
*/
|
|
@@ -42,15 +43,6 @@ export type HistoryOptions = {
|
|
|
42
43
|
* - history.index: the history index of the current snapshot<br>
|
|
43
44
|
* - history.nodes: the nodes of the history for each change<br>
|
|
44
45
|
* - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
|
|
45
|
-
* - canUndo: a function to return true if undo is available <br>
|
|
46
|
-
* - undo: a function to go back history <br>
|
|
47
|
-
* - canRedo: a function to return true if redo is available <br>
|
|
48
|
-
* - redo: a function to go forward history <br>
|
|
49
|
-
* - saveHistory: a function to save history <br>
|
|
50
|
-
* - getCurrentChangeDate: gets the date of the current change <br>
|
|
51
|
-
* - remove: a function to remove a specified history index <br>
|
|
52
|
-
* - replace: a function to replace a snapshot at a specified history index <br>
|
|
53
|
-
* - getNode: a function to get the node at a specified history index <br>
|
|
54
46
|
*
|
|
55
47
|
* <br>
|
|
56
48
|
* Notes: <br>
|
|
@@ -88,20 +80,20 @@ export declare function proxyWithHistory<V>(initialValue: V, options?: HistoryOp
|
|
|
88
80
|
*/
|
|
89
81
|
getCurrentChangeDate: () => Date | undefined;
|
|
90
82
|
/**
|
|
91
|
-
* get the date when
|
|
83
|
+
* get the date when the current node was entered into history.
|
|
92
84
|
*
|
|
93
|
-
* @
|
|
85
|
+
* @type {Date}
|
|
94
86
|
*/
|
|
95
87
|
readonly currentChangeDate: Date | undefined;
|
|
96
88
|
/**
|
|
97
89
|
* the current history node index.
|
|
98
90
|
*
|
|
99
|
-
* @
|
|
91
|
+
* @type {number}
|
|
100
92
|
*/
|
|
101
93
|
readonly currentIndex: number;
|
|
102
94
|
/**the total number of the history nodes.
|
|
103
95
|
*
|
|
104
|
-
* @
|
|
96
|
+
* @type {number}
|
|
105
97
|
*/
|
|
106
98
|
readonly historyNodeCount: number;
|
|
107
99
|
/**
|
|
@@ -230,3 +222,4 @@ export declare function proxyWithHistory<V>(initialValue: V, options?: HistoryOp
|
|
|
230
222
|
*/
|
|
231
223
|
replace: (index: number, value: Snapshot<V>) => void;
|
|
232
224
|
};
|
|
225
|
+
export {};
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { unstable_buildProxyFunction as buildProxyFunction, proxy, ref, snapshot, subscribe, } from 'valtio/vanilla';
|
|
2
|
+
const EMPTY_WIP = Symbol('valtio-history-wip-empty');
|
|
2
3
|
const isObject = (value) => !!value && typeof value === 'object';
|
|
3
4
|
let refSet;
|
|
4
5
|
const deepClone = (value) => {
|
|
@@ -45,15 +46,6 @@ const normalizeOptions = (options) => {
|
|
|
45
46
|
* - history.index: the history index of the current snapshot<br>
|
|
46
47
|
* - history.nodes: the nodes of the history for each change<br>
|
|
47
48
|
* - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
|
|
48
|
-
* - canUndo: a function to return true if undo is available <br>
|
|
49
|
-
* - undo: a function to go back history <br>
|
|
50
|
-
* - canRedo: a function to return true if redo is available <br>
|
|
51
|
-
* - redo: a function to go forward history <br>
|
|
52
|
-
* - saveHistory: a function to save history <br>
|
|
53
|
-
* - getCurrentChangeDate: gets the date of the current change <br>
|
|
54
|
-
* - remove: a function to remove a specified history index <br>
|
|
55
|
-
* - replace: a function to replace a snapshot at a specified history index <br>
|
|
56
|
-
* - getNode: a function to get the node at a specified history index <br>
|
|
57
49
|
*
|
|
58
50
|
* <br>
|
|
59
51
|
* Notes: <br>
|
|
@@ -83,7 +75,7 @@ export function proxyWithHistory(initialValue, options) {
|
|
|
83
75
|
* - history.wip: field for holding sandbox changes; used to avoid infinite loops<br>
|
|
84
76
|
*/
|
|
85
77
|
history: ref({
|
|
86
|
-
wip:
|
|
78
|
+
wip: EMPTY_WIP,
|
|
87
79
|
nodes: [],
|
|
88
80
|
index: -1,
|
|
89
81
|
}),
|
|
@@ -98,9 +90,9 @@ export function proxyWithHistory(initialValue, options) {
|
|
|
98
90
|
return node?.createdAt;
|
|
99
91
|
},
|
|
100
92
|
/**
|
|
101
|
-
* get the date when
|
|
93
|
+
* get the date when the current node was entered into history.
|
|
102
94
|
*
|
|
103
|
-
* @
|
|
95
|
+
* @type {Date}
|
|
104
96
|
*/
|
|
105
97
|
get currentChangeDate() {
|
|
106
98
|
const node = this.history.nodes[this.history.index];
|
|
@@ -109,14 +101,14 @@ export function proxyWithHistory(initialValue, options) {
|
|
|
109
101
|
/**
|
|
110
102
|
* the current history node index.
|
|
111
103
|
*
|
|
112
|
-
* @
|
|
104
|
+
* @type {number}
|
|
113
105
|
*/
|
|
114
106
|
get currentIndex() {
|
|
115
107
|
return this.history.index;
|
|
116
108
|
},
|
|
117
109
|
/**the total number of the history nodes.
|
|
118
110
|
*
|
|
119
|
-
* @
|
|
111
|
+
* @type {number}
|
|
120
112
|
*/
|
|
121
113
|
get historyNodeCount() {
|
|
122
114
|
return this.history.nodes.length;
|
|
@@ -176,7 +168,8 @@ export function proxyWithHistory(initialValue, options) {
|
|
|
176
168
|
*/
|
|
177
169
|
undo: () => {
|
|
178
170
|
if (proxyObject.canUndo()) {
|
|
179
|
-
proxyObject.history.wip =
|
|
171
|
+
proxyObject.history.wip =
|
|
172
|
+
proxyObject.clone(proxyObject.history.nodes[--proxyObject.history.index]?.snapshot) ?? EMPTY_WIP;
|
|
180
173
|
proxyObject.value = proxyObject.history.wip;
|
|
181
174
|
}
|
|
182
175
|
},
|
|
@@ -192,7 +185,8 @@ export function proxyWithHistory(initialValue, options) {
|
|
|
192
185
|
*/
|
|
193
186
|
redo: () => {
|
|
194
187
|
if (proxyObject.canRedo()) {
|
|
195
|
-
proxyObject.history.wip =
|
|
188
|
+
proxyObject.history.wip =
|
|
189
|
+
proxyObject.clone(proxyObject.history.nodes[++proxyObject.history.index]?.snapshot) ?? EMPTY_WIP;
|
|
196
190
|
proxyObject.value = proxyObject.history.wip;
|
|
197
191
|
}
|
|
198
192
|
},
|
|
@@ -243,7 +237,8 @@ export function proxyWithHistory(initialValue, options) {
|
|
|
243
237
|
if (isCurrentIndex) {
|
|
244
238
|
const resolvedIndex = isLastIndex ? index - 1 : index + 1;
|
|
245
239
|
const resolvedNode = proxyObject.history.nodes[resolvedIndex];
|
|
246
|
-
proxyObject.history.wip =
|
|
240
|
+
proxyObject.history.wip =
|
|
241
|
+
proxyObject.clone(resolvedNode?.snapshot) ?? EMPTY_WIP;
|
|
247
242
|
proxyObject.value = proxyObject.history.wip;
|
|
248
243
|
if (isLastIndex)
|
|
249
244
|
proxyObject.history.index--;
|
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/history-utility/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,IAAI,kBAAkB,EACjD,KAAK,EACL,GAAG,EACH,QAAQ,EACR,SAAS,GACV,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/history-utility/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,IAAI,kBAAkB,EACjD,KAAK,EACL,GAAG,EACH,QAAQ,EACR,SAAS,GACV,MAAM,gBAAgB,CAAC;AAmBxB,MAAM,SAAS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