@cfp-studio/iframe 18.0.0 → 18.2.0
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/README.md +37 -0
- package/dist/index.d.mts +168 -149
- package/dist/index.mjs +671 -893
- package/package.json +12 -6
- package/preview/__previewjs_internal__/index.ts +2 -3
- package/preview/__previewjs_internal__/links.ts +0 -1
- package/preview/__previewjs_internal__/logs.ts +0 -4
- package/preview/__previewjs_internal__/overlay.ts +187 -0
- package/preview/__previewjs_internal__/run-renderer.ts +10 -4
- package/preview/__previewjs_internal__/runtime-bridge.ts +424 -0
- package/preview/__previewjs_internal__/vscode-control.ts +75 -50
- package/preview/index.html +17 -5
- package/dist/index.d.ts +0 -268
package/dist/index.mjs
CHANGED
|
@@ -1,991 +1,769 @@
|
|
|
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
|
-
throw new Error("Unhandled discriminated union member: ".concat(JSON.stringify(value)));
|
|
41
|
-
}
|
|
42
|
-
assertNever$1.default = assertNever;
|
|
43
|
-
|
|
44
|
-
// src/utils/env.ts
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
3
|
+
//#endregion
|
|
4
|
+
//#region ../node_modules/.pnpm/immer@10.1.1/node_modules/immer/dist/immer.mjs
|
|
5
|
+
var import_assert_never = (/* @__PURE__ */ __commonJSMin(((exports) => {
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.assertNever = assertNever;
|
|
8
|
+
/**
|
|
9
|
+
* Helper function for exhaustive checks of discriminated unions.
|
|
10
|
+
* https://basarat.gitbooks.io/typescript/docs/types/discriminated-unions.html
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* type A = {type: 'a'};
|
|
15
|
+
* type B = {type: 'b'};
|
|
16
|
+
* type Union = A | B;
|
|
17
|
+
*
|
|
18
|
+
* function doSomething(arg: Union) {
|
|
19
|
+
* if (arg.type === 'a') {
|
|
20
|
+
* return something;
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* if (arg.type === 'b') {
|
|
24
|
+
* return somethingElse;
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* // TS will error if there are other types in the union
|
|
28
|
+
* // Will throw an Error when called at runtime.
|
|
29
|
+
* // Use `assertNever(arg, true)` instead to fail silently.
|
|
30
|
+
* return assertNever(arg);
|
|
31
|
+
* }
|
|
32
|
+
*/
|
|
33
|
+
function assertNever(value, errorMessageOrNoThrow) {
|
|
34
|
+
if (typeof errorMessageOrNoThrow === "string") throw new Error(errorMessageOrNoThrow);
|
|
35
|
+
if (typeof errorMessageOrNoThrow === "function") throw new Error(errorMessageOrNoThrow(value));
|
|
36
|
+
if (errorMessageOrNoThrow) return value;
|
|
37
|
+
throw new Error("Unhandled discriminated union member: ".concat(JSON.stringify(value)));
|
|
38
|
+
}
|
|
39
|
+
})))();
|
|
45
40
|
var NOTHING = Symbol.for("immer-nothing");
|
|
46
41
|
var DRAFTABLE = Symbol.for("immer-draftable");
|
|
47
42
|
var DRAFT_STATE = Symbol.for("immer-state");
|
|
48
|
-
|
|
49
|
-
// src/utils/errors.ts
|
|
50
43
|
var errors = process.env.NODE_ENV !== "production" ? [
|
|
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
|
-
// Note: if more errors are added, the errorOffset in Patches.ts should be increased
|
|
79
|
-
// See Patches.ts for additional errors
|
|
44
|
+
function(plugin) {
|
|
45
|
+
return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`;
|
|
46
|
+
},
|
|
47
|
+
function(thing) {
|
|
48
|
+
return `produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${thing}'`;
|
|
49
|
+
},
|
|
50
|
+
"This object has been frozen and should not be mutated",
|
|
51
|
+
function(data) {
|
|
52
|
+
return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + data;
|
|
53
|
+
},
|
|
54
|
+
"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
|
|
55
|
+
"Immer forbids circular references",
|
|
56
|
+
"The first or second argument to `produce` must be a function",
|
|
57
|
+
"The third argument to `produce` must be a function or undefined",
|
|
58
|
+
"First argument to `createDraft` must be a plain object, an array, or an immerable object",
|
|
59
|
+
"First argument to `finishDraft` must be a draft returned by `createDraft`",
|
|
60
|
+
function(thing) {
|
|
61
|
+
return `'current' expects a draft, got: ${thing}`;
|
|
62
|
+
},
|
|
63
|
+
"Object.defineProperty() cannot be used on an Immer draft",
|
|
64
|
+
"Object.setPrototypeOf() cannot be used on an Immer draft",
|
|
65
|
+
"Immer only supports deleting array indices",
|
|
66
|
+
"Immer only supports setting array indices and the 'length' property",
|
|
67
|
+
function(thing) {
|
|
68
|
+
return `'original' expects a draft, got: ${thing}`;
|
|
69
|
+
}
|
|
80
70
|
] : [];
|
|
81
71
|
function die(error, ...args) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// src/utils/common.ts
|
|
72
|
+
if (process.env.NODE_ENV !== "production") {
|
|
73
|
+
const e = errors[error];
|
|
74
|
+
const msg = typeof e === "function" ? e.apply(null, args) : e;
|
|
75
|
+
throw new Error(`[Immer] ${msg}`);
|
|
76
|
+
}
|
|
77
|
+
throw new Error(`[Immer] minified error nr: ${error}. Full error at: https://bit.ly/3cXEKWf`);
|
|
78
|
+
}
|
|
93
79
|
var getPrototypeOf = Object.getPrototypeOf;
|
|
94
80
|
function isDraft(value) {
|
|
95
|
-
|
|
81
|
+
return !!value && !!value[DRAFT_STATE];
|
|
96
82
|
}
|
|
97
83
|
function isDraftable(value) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!value.constructor?.[DRAFTABLE] || isMap(value) || isSet(value);
|
|
84
|
+
if (!value) return false;
|
|
85
|
+
return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!value.constructor?.[DRAFTABLE] || isMap(value) || isSet(value);
|
|
101
86
|
}
|
|
102
87
|
var objectCtorString = Object.prototype.constructor.toString();
|
|
103
88
|
function isPlainObject(value) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|
111
|
-
if (Ctor === Object)
|
|
112
|
-
return true;
|
|
113
|
-
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|
89
|
+
if (!value || typeof value !== "object") return false;
|
|
90
|
+
const proto = getPrototypeOf(value);
|
|
91
|
+
if (proto === null) return true;
|
|
92
|
+
const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|
93
|
+
if (Ctor === Object) return true;
|
|
94
|
+
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
|
|
114
95
|
}
|
|
115
96
|
function each(obj, iter) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} else {
|
|
121
|
-
obj.forEach((entry, index) => iter(index, entry, obj));
|
|
122
|
-
}
|
|
97
|
+
if (getArchtype(obj) === 0) Reflect.ownKeys(obj).forEach((key) => {
|
|
98
|
+
iter(key, obj[key], obj);
|
|
99
|
+
});
|
|
100
|
+
else obj.forEach((entry, index) => iter(index, entry, obj));
|
|
123
101
|
}
|
|
124
102
|
function getArchtype(thing) {
|
|
125
|
-
|
|
126
|
-
|
|
103
|
+
const state = thing[DRAFT_STATE];
|
|
104
|
+
return state ? state.type_ : Array.isArray(thing) ? 1 : isMap(thing) ? 2 : isSet(thing) ? 3 : 0;
|
|
127
105
|
}
|
|
128
106
|
function has(thing, prop) {
|
|
129
|
-
|
|
107
|
+
return getArchtype(thing) === 2 ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
|
|
130
108
|
}
|
|
131
109
|
function set(thing, propOrOldValue, value) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
thing.add(value);
|
|
137
|
-
} else
|
|
138
|
-
thing[propOrOldValue] = value;
|
|
110
|
+
const t = getArchtype(thing);
|
|
111
|
+
if (t === 2) thing.set(propOrOldValue, value);
|
|
112
|
+
else if (t === 3) thing.add(value);
|
|
113
|
+
else thing[propOrOldValue] = value;
|
|
139
114
|
}
|
|
140
115
|
function is(x, y) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
} else {
|
|
144
|
-
return x !== x && y !== y;
|
|
145
|
-
}
|
|
116
|
+
if (x === y) return x !== 0 || 1 / x === 1 / y;
|
|
117
|
+
else return x !== x && y !== y;
|
|
146
118
|
}
|
|
147
119
|
function isMap(target) {
|
|
148
|
-
|
|
120
|
+
return target instanceof Map;
|
|
149
121
|
}
|
|
150
122
|
function isSet(target) {
|
|
151
|
-
|
|
123
|
+
return target instanceof Set;
|
|
152
124
|
}
|
|
153
125
|
function latest(state) {
|
|
154
|
-
|
|
126
|
+
return state.copy_ || state.base_;
|
|
155
127
|
}
|
|
156
128
|
function shallowCopy(base, strict) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return Object.create(getPrototypeOf(base), descriptors);
|
|
187
|
-
} else {
|
|
188
|
-
const proto = getPrototypeOf(base);
|
|
189
|
-
if (proto !== null && isPlain) {
|
|
190
|
-
return { ...base };
|
|
191
|
-
}
|
|
192
|
-
const obj = Object.create(proto);
|
|
193
|
-
return Object.assign(obj, base);
|
|
194
|
-
}
|
|
129
|
+
if (isMap(base)) return new Map(base);
|
|
130
|
+
if (isSet(base)) return new Set(base);
|
|
131
|
+
if (Array.isArray(base)) return Array.prototype.slice.call(base);
|
|
132
|
+
const isPlain = isPlainObject(base);
|
|
133
|
+
if (strict === true || strict === "class_only" && !isPlain) {
|
|
134
|
+
const descriptors = Object.getOwnPropertyDescriptors(base);
|
|
135
|
+
delete descriptors[DRAFT_STATE];
|
|
136
|
+
let keys = Reflect.ownKeys(descriptors);
|
|
137
|
+
for (let i = 0; i < keys.length; i++) {
|
|
138
|
+
const key = keys[i];
|
|
139
|
+
const desc = descriptors[key];
|
|
140
|
+
if (desc.writable === false) {
|
|
141
|
+
desc.writable = true;
|
|
142
|
+
desc.configurable = true;
|
|
143
|
+
}
|
|
144
|
+
if (desc.get || desc.set) descriptors[key] = {
|
|
145
|
+
configurable: true,
|
|
146
|
+
writable: true,
|
|
147
|
+
enumerable: desc.enumerable,
|
|
148
|
+
value: base[key]
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
return Object.create(getPrototypeOf(base), descriptors);
|
|
152
|
+
} else {
|
|
153
|
+
const proto = getPrototypeOf(base);
|
|
154
|
+
if (proto !== null && isPlain) return { ...base };
|
|
155
|
+
const obj = Object.create(proto);
|
|
156
|
+
return Object.assign(obj, base);
|
|
157
|
+
}
|
|
195
158
|
}
|
|
196
159
|
function freeze(obj, deep = false) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
Object.freeze(obj);
|
|
203
|
-
if (deep)
|
|
204
|
-
Object.entries(obj).forEach(([key, value]) => freeze(value, true));
|
|
205
|
-
return obj;
|
|
160
|
+
if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj;
|
|
161
|
+
if (getArchtype(obj) > 1) obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections;
|
|
162
|
+
Object.freeze(obj);
|
|
163
|
+
if (deep) Object.entries(obj).forEach(([key, value]) => freeze(value, true));
|
|
164
|
+
return obj;
|
|
206
165
|
}
|
|
207
166
|
function dontMutateFrozenCollections() {
|
|
208
|
-
|
|
167
|
+
die(2);
|
|
209
168
|
}
|
|
210
169
|
function isFrozen(obj) {
|
|
211
|
-
|
|
170
|
+
return Object.isFrozen(obj);
|
|
212
171
|
}
|
|
213
|
-
|
|
214
|
-
// src/utils/plugins.ts
|
|
215
172
|
var plugins = {};
|
|
216
173
|
function getPlugin(pluginKey) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return plugin;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// src/core/scope.ts
|
|
174
|
+
const plugin = plugins[pluginKey];
|
|
175
|
+
if (!plugin) die(0, pluginKey);
|
|
176
|
+
return plugin;
|
|
177
|
+
}
|
|
225
178
|
var currentScope;
|
|
226
179
|
function getCurrentScope() {
|
|
227
|
-
|
|
180
|
+
return currentScope;
|
|
228
181
|
}
|
|
229
182
|
function createScope(parent_, immer_) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
unfinalizedDrafts_: 0
|
|
238
|
-
};
|
|
183
|
+
return {
|
|
184
|
+
drafts_: [],
|
|
185
|
+
parent_,
|
|
186
|
+
immer_,
|
|
187
|
+
canAutoFreeze_: true,
|
|
188
|
+
unfinalizedDrafts_: 0
|
|
189
|
+
};
|
|
239
190
|
}
|
|
240
191
|
function usePatchesInScope(scope, patchListener) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
192
|
+
if (patchListener) {
|
|
193
|
+
getPlugin("Patches");
|
|
194
|
+
scope.patches_ = [];
|
|
195
|
+
scope.inversePatches_ = [];
|
|
196
|
+
scope.patchListener_ = patchListener;
|
|
197
|
+
}
|
|
247
198
|
}
|
|
248
199
|
function revokeScope(scope) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
200
|
+
leaveScope(scope);
|
|
201
|
+
scope.drafts_.forEach(revokeDraft);
|
|
202
|
+
scope.drafts_ = null;
|
|
252
203
|
}
|
|
253
204
|
function leaveScope(scope) {
|
|
254
|
-
|
|
255
|
-
currentScope = scope.parent_;
|
|
256
|
-
}
|
|
205
|
+
if (scope === currentScope) currentScope = scope.parent_;
|
|
257
206
|
}
|
|
258
207
|
function enterScope(immer2) {
|
|
259
|
-
|
|
208
|
+
return currentScope = createScope(currentScope, immer2);
|
|
260
209
|
}
|
|
261
210
|
function revokeDraft(draft) {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
state.revoked_ = true;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// src/core/finalize.ts
|
|
211
|
+
const state = draft[DRAFT_STATE];
|
|
212
|
+
if (state.type_ === 0 || state.type_ === 1) state.revoke_();
|
|
213
|
+
else state.revoked_ = true;
|
|
214
|
+
}
|
|
270
215
|
function processResult(result, scope) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
result,
|
|
288
|
-
scope.patches_,
|
|
289
|
-
scope.inversePatches_
|
|
290
|
-
);
|
|
291
|
-
}
|
|
292
|
-
} else {
|
|
293
|
-
result = finalize(scope, baseDraft, []);
|
|
294
|
-
}
|
|
295
|
-
revokeScope(scope);
|
|
296
|
-
if (scope.patches_) {
|
|
297
|
-
scope.patchListener_(scope.patches_, scope.inversePatches_);
|
|
298
|
-
}
|
|
299
|
-
return result !== NOTHING ? result : void 0;
|
|
216
|
+
scope.unfinalizedDrafts_ = scope.drafts_.length;
|
|
217
|
+
const baseDraft = scope.drafts_[0];
|
|
218
|
+
if (result !== void 0 && result !== baseDraft) {
|
|
219
|
+
if (baseDraft[DRAFT_STATE].modified_) {
|
|
220
|
+
revokeScope(scope);
|
|
221
|
+
die(4);
|
|
222
|
+
}
|
|
223
|
+
if (isDraftable(result)) {
|
|
224
|
+
result = finalize(scope, result);
|
|
225
|
+
if (!scope.parent_) maybeFreeze(scope, result);
|
|
226
|
+
}
|
|
227
|
+
if (scope.patches_) getPlugin("Patches").generateReplacementPatches_(baseDraft[DRAFT_STATE].base_, result, scope.patches_, scope.inversePatches_);
|
|
228
|
+
} else result = finalize(scope, baseDraft, []);
|
|
229
|
+
revokeScope(scope);
|
|
230
|
+
if (scope.patches_) scope.patchListener_(scope.patches_, scope.inversePatches_);
|
|
231
|
+
return result !== NOTHING ? result : void 0;
|
|
300
232
|
}
|
|
301
233
|
function finalize(rootScope, value, path) {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
each(
|
|
330
|
-
resultEach,
|
|
331
|
-
(key, childValue) => finalizeProperty(rootScope, state, result, key, childValue, path, isSet2)
|
|
332
|
-
);
|
|
333
|
-
maybeFreeze(rootScope, result, false);
|
|
334
|
-
if (path && rootScope.patches_) {
|
|
335
|
-
getPlugin("Patches").generatePatches_(
|
|
336
|
-
state,
|
|
337
|
-
path,
|
|
338
|
-
rootScope.patches_,
|
|
339
|
-
rootScope.inversePatches_
|
|
340
|
-
);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
return state.copy_;
|
|
234
|
+
if (isFrozen(value)) return value;
|
|
235
|
+
const state = value[DRAFT_STATE];
|
|
236
|
+
if (!state) {
|
|
237
|
+
each(value, (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path));
|
|
238
|
+
return value;
|
|
239
|
+
}
|
|
240
|
+
if (state.scope_ !== rootScope) return value;
|
|
241
|
+
if (!state.modified_) {
|
|
242
|
+
maybeFreeze(rootScope, state.base_, true);
|
|
243
|
+
return state.base_;
|
|
244
|
+
}
|
|
245
|
+
if (!state.finalized_) {
|
|
246
|
+
state.finalized_ = true;
|
|
247
|
+
state.scope_.unfinalizedDrafts_--;
|
|
248
|
+
const result = state.copy_;
|
|
249
|
+
let resultEach = result;
|
|
250
|
+
let isSet2 = false;
|
|
251
|
+
if (state.type_ === 3) {
|
|
252
|
+
resultEach = new Set(result);
|
|
253
|
+
result.clear();
|
|
254
|
+
isSet2 = true;
|
|
255
|
+
}
|
|
256
|
+
each(resultEach, (key, childValue) => finalizeProperty(rootScope, state, result, key, childValue, path, isSet2));
|
|
257
|
+
maybeFreeze(rootScope, result, false);
|
|
258
|
+
if (path && rootScope.patches_) getPlugin("Patches").generatePatches_(state, path, rootScope.patches_, rootScope.inversePatches_);
|
|
259
|
+
}
|
|
260
|
+
return state.copy_;
|
|
344
261
|
}
|
|
345
262
|
function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath, targetIsSet) {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
targetObject.add(childValue);
|
|
359
|
-
}
|
|
360
|
-
if (isDraftable(childValue) && !isFrozen(childValue)) {
|
|
361
|
-
if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
|
|
362
|
-
return;
|
|
363
|
-
}
|
|
364
|
-
finalize(rootScope, childValue);
|
|
365
|
-
if ((!parentState || !parentState.scope_.parent_) && typeof prop !== "symbol" && Object.prototype.propertyIsEnumerable.call(targetObject, prop))
|
|
366
|
-
maybeFreeze(rootScope, childValue);
|
|
367
|
-
}
|
|
263
|
+
if (process.env.NODE_ENV !== "production" && childValue === targetObject) die(5);
|
|
264
|
+
if (isDraft(childValue)) {
|
|
265
|
+
const res = finalize(rootScope, childValue, rootPath && parentState && parentState.type_ !== 3 && !has(parentState.assigned_, prop) ? rootPath.concat(prop) : void 0);
|
|
266
|
+
set(targetObject, prop, res);
|
|
267
|
+
if (isDraft(res)) rootScope.canAutoFreeze_ = false;
|
|
268
|
+
else return;
|
|
269
|
+
} else if (targetIsSet) targetObject.add(childValue);
|
|
270
|
+
if (isDraftable(childValue) && !isFrozen(childValue)) {
|
|
271
|
+
if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) return;
|
|
272
|
+
finalize(rootScope, childValue);
|
|
273
|
+
if ((!parentState || !parentState.scope_.parent_) && typeof prop !== "symbol" && Object.prototype.propertyIsEnumerable.call(targetObject, prop)) maybeFreeze(rootScope, childValue);
|
|
274
|
+
}
|
|
368
275
|
}
|
|
369
276
|
function maybeFreeze(scope, value, deep = false) {
|
|
370
|
-
|
|
371
|
-
freeze(value, deep);
|
|
372
|
-
}
|
|
277
|
+
if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) freeze(value, deep);
|
|
373
278
|
}
|
|
374
|
-
|
|
375
|
-
// src/core/proxy.ts
|
|
376
279
|
function createProxyProxy(base, parent) {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
let target = state;
|
|
402
|
-
let traps = objectTraps;
|
|
403
|
-
if (isArray) {
|
|
404
|
-
target = [state];
|
|
405
|
-
traps = arrayTraps;
|
|
406
|
-
}
|
|
407
|
-
const { revoke, proxy } = Proxy.revocable(target, traps);
|
|
408
|
-
state.draft_ = proxy;
|
|
409
|
-
state.revoke_ = revoke;
|
|
410
|
-
return proxy;
|
|
280
|
+
const isArray = Array.isArray(base);
|
|
281
|
+
const state = {
|
|
282
|
+
type_: isArray ? 1 : 0,
|
|
283
|
+
scope_: parent ? parent.scope_ : getCurrentScope(),
|
|
284
|
+
modified_: false,
|
|
285
|
+
finalized_: false,
|
|
286
|
+
assigned_: {},
|
|
287
|
+
parent_: parent,
|
|
288
|
+
base_: base,
|
|
289
|
+
draft_: null,
|
|
290
|
+
copy_: null,
|
|
291
|
+
revoke_: null,
|
|
292
|
+
isManual_: false
|
|
293
|
+
};
|
|
294
|
+
let target = state;
|
|
295
|
+
let traps = objectTraps;
|
|
296
|
+
if (isArray) {
|
|
297
|
+
target = [state];
|
|
298
|
+
traps = arrayTraps;
|
|
299
|
+
}
|
|
300
|
+
const { revoke, proxy } = Proxy.revocable(target, traps);
|
|
301
|
+
state.draft_ = proxy;
|
|
302
|
+
state.revoke_ = revoke;
|
|
303
|
+
return proxy;
|
|
411
304
|
}
|
|
412
305
|
var objectTraps = {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
return {
|
|
484
|
-
writable: true,
|
|
485
|
-
configurable: state.type_ !== 1 /* Array */ || prop !== "length",
|
|
486
|
-
enumerable: desc.enumerable,
|
|
487
|
-
value: owner[prop]
|
|
488
|
-
};
|
|
489
|
-
},
|
|
490
|
-
defineProperty() {
|
|
491
|
-
die(11);
|
|
492
|
-
},
|
|
493
|
-
getPrototypeOf(state) {
|
|
494
|
-
return getPrototypeOf(state.base_);
|
|
495
|
-
},
|
|
496
|
-
setPrototypeOf() {
|
|
497
|
-
die(12);
|
|
498
|
-
}
|
|
306
|
+
get(state, prop) {
|
|
307
|
+
if (prop === DRAFT_STATE) return state;
|
|
308
|
+
const source = latest(state);
|
|
309
|
+
if (!has(source, prop)) return readPropFromProto(state, source, prop);
|
|
310
|
+
const value = source[prop];
|
|
311
|
+
if (state.finalized_ || !isDraftable(value)) return value;
|
|
312
|
+
if (value === peek(state.base_, prop)) {
|
|
313
|
+
prepareCopy(state);
|
|
314
|
+
return state.copy_[prop] = createProxy(value, state);
|
|
315
|
+
}
|
|
316
|
+
return value;
|
|
317
|
+
},
|
|
318
|
+
has(state, prop) {
|
|
319
|
+
return prop in latest(state);
|
|
320
|
+
},
|
|
321
|
+
ownKeys(state) {
|
|
322
|
+
return Reflect.ownKeys(latest(state));
|
|
323
|
+
},
|
|
324
|
+
set(state, prop, value) {
|
|
325
|
+
const desc = getDescriptorFromProto(latest(state), prop);
|
|
326
|
+
if (desc?.set) {
|
|
327
|
+
desc.set.call(state.draft_, value);
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
if (!state.modified_) {
|
|
331
|
+
const current2 = peek(latest(state), prop);
|
|
332
|
+
const currentState = current2?.[DRAFT_STATE];
|
|
333
|
+
if (currentState && currentState.base_ === value) {
|
|
334
|
+
state.copy_[prop] = value;
|
|
335
|
+
state.assigned_[prop] = false;
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
if (is(value, current2) && (value !== void 0 || has(state.base_, prop))) return true;
|
|
339
|
+
prepareCopy(state);
|
|
340
|
+
markChanged(state);
|
|
341
|
+
}
|
|
342
|
+
if (state.copy_[prop] === value && (value !== void 0 || prop in state.copy_) || Number.isNaN(value) && Number.isNaN(state.copy_[prop])) return true;
|
|
343
|
+
state.copy_[prop] = value;
|
|
344
|
+
state.assigned_[prop] = true;
|
|
345
|
+
return true;
|
|
346
|
+
},
|
|
347
|
+
deleteProperty(state, prop) {
|
|
348
|
+
if (peek(state.base_, prop) !== void 0 || prop in state.base_) {
|
|
349
|
+
state.assigned_[prop] = false;
|
|
350
|
+
prepareCopy(state);
|
|
351
|
+
markChanged(state);
|
|
352
|
+
} else delete state.assigned_[prop];
|
|
353
|
+
if (state.copy_) delete state.copy_[prop];
|
|
354
|
+
return true;
|
|
355
|
+
},
|
|
356
|
+
getOwnPropertyDescriptor(state, prop) {
|
|
357
|
+
const owner = latest(state);
|
|
358
|
+
const desc = Reflect.getOwnPropertyDescriptor(owner, prop);
|
|
359
|
+
if (!desc) return desc;
|
|
360
|
+
return {
|
|
361
|
+
writable: true,
|
|
362
|
+
configurable: state.type_ !== 1 || prop !== "length",
|
|
363
|
+
enumerable: desc.enumerable,
|
|
364
|
+
value: owner[prop]
|
|
365
|
+
};
|
|
366
|
+
},
|
|
367
|
+
defineProperty() {
|
|
368
|
+
die(11);
|
|
369
|
+
},
|
|
370
|
+
getPrototypeOf(state) {
|
|
371
|
+
return getPrototypeOf(state.base_);
|
|
372
|
+
},
|
|
373
|
+
setPrototypeOf() {
|
|
374
|
+
die(12);
|
|
375
|
+
}
|
|
499
376
|
};
|
|
500
377
|
var arrayTraps = {};
|
|
501
378
|
each(objectTraps, (key, fn) => {
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
379
|
+
arrayTraps[key] = function() {
|
|
380
|
+
arguments[0] = arguments[0][0];
|
|
381
|
+
return fn.apply(this, arguments);
|
|
382
|
+
};
|
|
506
383
|
});
|
|
507
384
|
arrayTraps.deleteProperty = function(state, prop) {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
return arrayTraps.set.call(this, state, prop, void 0);
|
|
385
|
+
if (process.env.NODE_ENV !== "production" && isNaN(parseInt(prop))) die(13);
|
|
386
|
+
return arrayTraps.set.call(this, state, prop, void 0);
|
|
511
387
|
};
|
|
512
388
|
arrayTraps.set = function(state, prop, value) {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
return objectTraps.set.call(this, state[0], prop, value, state[0]);
|
|
389
|
+
if (process.env.NODE_ENV !== "production" && prop !== "length" && isNaN(parseInt(prop))) die(14);
|
|
390
|
+
return objectTraps.set.call(this, state[0], prop, value, state[0]);
|
|
516
391
|
};
|
|
517
392
|
function peek(draft, prop) {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
return source[prop];
|
|
393
|
+
const state = draft[DRAFT_STATE];
|
|
394
|
+
return (state ? latest(state) : draft)[prop];
|
|
521
395
|
}
|
|
522
396
|
function readPropFromProto(state, source, prop) {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
// This is a very special case, if the prop is a getter defined by the
|
|
526
|
-
// prototype, we should invoke it with the draft as context!
|
|
527
|
-
desc.get?.call(state.draft_)
|
|
528
|
-
) : void 0;
|
|
397
|
+
const desc = getDescriptorFromProto(source, prop);
|
|
398
|
+
return desc ? `value` in desc ? desc.value : desc.get?.call(state.draft_) : void 0;
|
|
529
399
|
}
|
|
530
400
|
function getDescriptorFromProto(source, prop) {
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
proto = getPrototypeOf(proto);
|
|
539
|
-
}
|
|
540
|
-
return void 0;
|
|
401
|
+
if (!(prop in source)) return void 0;
|
|
402
|
+
let proto = getPrototypeOf(source);
|
|
403
|
+
while (proto) {
|
|
404
|
+
const desc = Object.getOwnPropertyDescriptor(proto, prop);
|
|
405
|
+
if (desc) return desc;
|
|
406
|
+
proto = getPrototypeOf(proto);
|
|
407
|
+
}
|
|
541
408
|
}
|
|
542
409
|
function markChanged(state) {
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
}
|
|
548
|
-
}
|
|
410
|
+
if (!state.modified_) {
|
|
411
|
+
state.modified_ = true;
|
|
412
|
+
if (state.parent_) markChanged(state.parent_);
|
|
413
|
+
}
|
|
549
414
|
}
|
|
550
415
|
function prepareCopy(state) {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
state.base_,
|
|
554
|
-
state.scope_.immer_.useStrictShallowCopy_
|
|
555
|
-
);
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
// src/core/immerClass.ts
|
|
416
|
+
if (!state.copy_) state.copy_ = shallowCopy(state.base_, state.scope_.immer_.useStrictShallowCopy_);
|
|
417
|
+
}
|
|
560
418
|
var Immer2 = class {
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
for (i = patches.length - 1; i >= 0; i--) {
|
|
684
|
-
const patch = patches[i];
|
|
685
|
-
if (patch.path.length === 0 && patch.op === "replace") {
|
|
686
|
-
base = patch.value;
|
|
687
|
-
break;
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
if (i > -1) {
|
|
691
|
-
patches = patches.slice(i + 1);
|
|
692
|
-
}
|
|
693
|
-
const applyPatchesImpl = getPlugin("Patches").applyPatches_;
|
|
694
|
-
if (isDraft(base)) {
|
|
695
|
-
return applyPatchesImpl(base, patches);
|
|
696
|
-
}
|
|
697
|
-
return this.produce(
|
|
698
|
-
base,
|
|
699
|
-
(draft) => applyPatchesImpl(draft, patches)
|
|
700
|
-
);
|
|
701
|
-
}
|
|
419
|
+
constructor(config) {
|
|
420
|
+
this.autoFreeze_ = true;
|
|
421
|
+
this.useStrictShallowCopy_ = false;
|
|
422
|
+
/**
|
|
423
|
+
* The `produce` function takes a value and a "recipe function" (whose
|
|
424
|
+
* return value often depends on the base state). The recipe function is
|
|
425
|
+
* free to mutate its first argument however it wants. All mutations are
|
|
426
|
+
* only ever applied to a __copy__ of the base state.
|
|
427
|
+
*
|
|
428
|
+
* Pass only a function to create a "curried producer" which relieves you
|
|
429
|
+
* from passing the recipe function every time.
|
|
430
|
+
*
|
|
431
|
+
* Only plain objects and arrays are made mutable. All other objects are
|
|
432
|
+
* considered uncopyable.
|
|
433
|
+
*
|
|
434
|
+
* Note: This function is __bound__ to its `Immer` instance.
|
|
435
|
+
*
|
|
436
|
+
* @param {any} base - the initial state
|
|
437
|
+
* @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modified
|
|
438
|
+
* @param {Function} patchListener - optional function that will be called with all the patches produced here
|
|
439
|
+
* @returns {any} a new state, or the initial state if nothing was modified
|
|
440
|
+
*/
|
|
441
|
+
this.produce = (base, recipe, patchListener) => {
|
|
442
|
+
if (typeof base === "function" && typeof recipe !== "function") {
|
|
443
|
+
const defaultBase = recipe;
|
|
444
|
+
recipe = base;
|
|
445
|
+
const self = this;
|
|
446
|
+
return function curriedProduce(base2 = defaultBase, ...args) {
|
|
447
|
+
return self.produce(base2, (draft) => recipe.call(this, draft, ...args));
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
if (typeof recipe !== "function") die(6);
|
|
451
|
+
if (patchListener !== void 0 && typeof patchListener !== "function") die(7);
|
|
452
|
+
let result;
|
|
453
|
+
if (isDraftable(base)) {
|
|
454
|
+
const scope = enterScope(this);
|
|
455
|
+
const proxy = createProxy(base, void 0);
|
|
456
|
+
let hasError = true;
|
|
457
|
+
try {
|
|
458
|
+
result = recipe(proxy);
|
|
459
|
+
hasError = false;
|
|
460
|
+
} finally {
|
|
461
|
+
if (hasError) revokeScope(scope);
|
|
462
|
+
else leaveScope(scope);
|
|
463
|
+
}
|
|
464
|
+
usePatchesInScope(scope, patchListener);
|
|
465
|
+
return processResult(result, scope);
|
|
466
|
+
} else if (!base || typeof base !== "object") {
|
|
467
|
+
result = recipe(base);
|
|
468
|
+
if (result === void 0) result = base;
|
|
469
|
+
if (result === NOTHING) result = void 0;
|
|
470
|
+
if (this.autoFreeze_) freeze(result, true);
|
|
471
|
+
if (patchListener) {
|
|
472
|
+
const p = [];
|
|
473
|
+
const ip = [];
|
|
474
|
+
getPlugin("Patches").generateReplacementPatches_(base, result, p, ip);
|
|
475
|
+
patchListener(p, ip);
|
|
476
|
+
}
|
|
477
|
+
return result;
|
|
478
|
+
} else die(1, base);
|
|
479
|
+
};
|
|
480
|
+
this.produceWithPatches = (base, recipe) => {
|
|
481
|
+
if (typeof base === "function") return (state, ...args) => this.produceWithPatches(state, (draft) => base(draft, ...args));
|
|
482
|
+
let patches, inversePatches;
|
|
483
|
+
return [
|
|
484
|
+
this.produce(base, recipe, (p, ip) => {
|
|
485
|
+
patches = p;
|
|
486
|
+
inversePatches = ip;
|
|
487
|
+
}),
|
|
488
|
+
patches,
|
|
489
|
+
inversePatches
|
|
490
|
+
];
|
|
491
|
+
};
|
|
492
|
+
if (typeof config?.autoFreeze === "boolean") this.setAutoFreeze(config.autoFreeze);
|
|
493
|
+
if (typeof config?.useStrictShallowCopy === "boolean") this.setUseStrictShallowCopy(config.useStrictShallowCopy);
|
|
494
|
+
}
|
|
495
|
+
createDraft(base) {
|
|
496
|
+
if (!isDraftable(base)) die(8);
|
|
497
|
+
if (isDraft(base)) base = current(base);
|
|
498
|
+
const scope = enterScope(this);
|
|
499
|
+
const proxy = createProxy(base, void 0);
|
|
500
|
+
proxy[DRAFT_STATE].isManual_ = true;
|
|
501
|
+
leaveScope(scope);
|
|
502
|
+
return proxy;
|
|
503
|
+
}
|
|
504
|
+
finishDraft(draft, patchListener) {
|
|
505
|
+
const state = draft && draft[DRAFT_STATE];
|
|
506
|
+
if (!state || !state.isManual_) die(9);
|
|
507
|
+
const { scope_: scope } = state;
|
|
508
|
+
usePatchesInScope(scope, patchListener);
|
|
509
|
+
return processResult(void 0, scope);
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Pass true to automatically freeze all copies created by Immer.
|
|
513
|
+
*
|
|
514
|
+
* By default, auto-freezing is enabled.
|
|
515
|
+
*/
|
|
516
|
+
setAutoFreeze(value) {
|
|
517
|
+
this.autoFreeze_ = value;
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Pass true to enable strict shallow copy.
|
|
521
|
+
*
|
|
522
|
+
* By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.
|
|
523
|
+
*/
|
|
524
|
+
setUseStrictShallowCopy(value) {
|
|
525
|
+
this.useStrictShallowCopy_ = value;
|
|
526
|
+
}
|
|
527
|
+
applyPatches(base, patches) {
|
|
528
|
+
let i;
|
|
529
|
+
for (i = patches.length - 1; i >= 0; i--) {
|
|
530
|
+
const patch = patches[i];
|
|
531
|
+
if (patch.path.length === 0 && patch.op === "replace") {
|
|
532
|
+
base = patch.value;
|
|
533
|
+
break;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
if (i > -1) patches = patches.slice(i + 1);
|
|
537
|
+
const applyPatchesImpl = getPlugin("Patches").applyPatches_;
|
|
538
|
+
if (isDraft(base)) return applyPatchesImpl(base, patches);
|
|
539
|
+
return this.produce(base, (draft) => applyPatchesImpl(draft, patches));
|
|
540
|
+
}
|
|
702
541
|
};
|
|
703
542
|
function createProxy(value, parent) {
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
return draft;
|
|
543
|
+
const draft = isMap(value) ? getPlugin("MapSet").proxyMap_(value, parent) : isSet(value) ? getPlugin("MapSet").proxySet_(value, parent) : createProxyProxy(value, parent);
|
|
544
|
+
(parent ? parent.scope_ : getCurrentScope()).drafts_.push(draft);
|
|
545
|
+
return draft;
|
|
708
546
|
}
|
|
709
|
-
|
|
710
|
-
// src/core/current.ts
|
|
711
547
|
function current(value) {
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
return currentImpl(value);
|
|
548
|
+
if (!isDraft(value)) die(10, value);
|
|
549
|
+
return currentImpl(value);
|
|
715
550
|
}
|
|
716
551
|
function currentImpl(value) {
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
});
|
|
732
|
-
if (state) {
|
|
733
|
-
state.finalized_ = false;
|
|
734
|
-
}
|
|
735
|
-
return copy;
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
// src/immer.ts
|
|
552
|
+
if (!isDraftable(value) || isFrozen(value)) return value;
|
|
553
|
+
const state = value[DRAFT_STATE];
|
|
554
|
+
let copy;
|
|
555
|
+
if (state) {
|
|
556
|
+
if (!state.modified_) return state.base_;
|
|
557
|
+
state.finalized_ = true;
|
|
558
|
+
copy = shallowCopy(value, state.scope_.immer_.useStrictShallowCopy_);
|
|
559
|
+
} else copy = shallowCopy(value, true);
|
|
560
|
+
each(copy, (key, childValue) => {
|
|
561
|
+
set(copy, key, currentImpl(childValue));
|
|
562
|
+
});
|
|
563
|
+
if (state) state.finalized_ = false;
|
|
564
|
+
return copy;
|
|
565
|
+
}
|
|
739
566
|
var immer = new Immer2();
|
|
740
567
|
var produce = immer.produce;
|
|
741
|
-
immer.produceWithPatches.bind(
|
|
742
|
-
immer
|
|
743
|
-
);
|
|
568
|
+
immer.produceWithPatches.bind(immer);
|
|
744
569
|
immer.setAutoFreeze.bind(immer);
|
|
745
570
|
immer.setUseStrictShallowCopy.bind(immer);
|
|
746
571
|
immer.applyPatches.bind(immer);
|
|
747
572
|
immer.createDraft.bind(immer);
|
|
748
573
|
immer.finishDraft.bind(immer);
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
574
|
+
//#endregion
|
|
575
|
+
//#region src/index.ts
|
|
576
|
+
/**
|
|
577
|
+
* 创建预览 iframe 控制器
|
|
578
|
+
* @param options - 创建控制器的选项
|
|
579
|
+
* @returns 预览 iframe 控制器实例
|
|
580
|
+
*/
|
|
756
581
|
function createController(options) {
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
this.bootstrapStatus = "failure";
|
|
945
|
-
}
|
|
946
|
-
break;
|
|
947
|
-
case "log-message":
|
|
948
|
-
this.updateState((state) => {
|
|
949
|
-
state.logs.push(event);
|
|
950
|
-
});
|
|
951
|
-
break;
|
|
952
|
-
case "action":
|
|
953
|
-
this.updateState((state) => {
|
|
954
|
-
state.actions.push(event);
|
|
955
|
-
});
|
|
956
|
-
break;
|
|
957
|
-
case "toggle-inspect": {
|
|
958
|
-
console.log("\u89E6\u53D1toggle-inspect", event.enabled);
|
|
959
|
-
const window2 = this.options.getIframe()?.contentWindow;
|
|
960
|
-
if (window2) {
|
|
961
|
-
window2.__PREVIEWJS_IFRAME__.toggleInspect = event.enabled;
|
|
962
|
-
window2.__PREVIEWJS_IFRAME__.refresh({
|
|
963
|
-
refetchWrapperModule: true
|
|
964
|
-
});
|
|
965
|
-
}
|
|
966
|
-
break;
|
|
967
|
-
}
|
|
968
|
-
default:
|
|
969
|
-
throw assertNever_2(event);
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
/**
|
|
973
|
-
* 更新状态
|
|
974
|
-
* @param stateModifier - 状态修改函数
|
|
975
|
-
*/
|
|
976
|
-
updateState(stateModifier) {
|
|
977
|
-
this.state = produce(this.state, stateModifier);
|
|
978
|
-
this.options.onStateUpdate?.(this.state);
|
|
979
|
-
}
|
|
980
|
-
/**
|
|
981
|
-
* 清除期望渲染的超时定时器
|
|
982
|
-
*/
|
|
983
|
-
clearExpectRenderTimeout() {
|
|
984
|
-
if (this.expectRenderTimeout) {
|
|
985
|
-
clearTimeout(this.expectRenderTimeout);
|
|
986
|
-
this.expectRenderTimeout = null;
|
|
987
|
-
}
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
|
|
582
|
+
const controller = new PreviewIframeControllerImpl(options);
|
|
583
|
+
window.__PREVIEWJS_CONTROLLER__ = controller;
|
|
584
|
+
return controller;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* 预览 iframe 控制器的实现类
|
|
588
|
+
*/
|
|
589
|
+
var PreviewIframeControllerImpl = class {
|
|
590
|
+
/**
|
|
591
|
+
* 构造函数
|
|
592
|
+
* @param options - 创建控制器的选项
|
|
593
|
+
*/
|
|
594
|
+
constructor(options) {
|
|
595
|
+
this.options = options;
|
|
596
|
+
this.lastRender = null;
|
|
597
|
+
this.bootstrapStatus = "pending";
|
|
598
|
+
this.state = {
|
|
599
|
+
loading: false,
|
|
600
|
+
rendered: false,
|
|
601
|
+
errors: [],
|
|
602
|
+
logs: [],
|
|
603
|
+
actions: []
|
|
604
|
+
};
|
|
605
|
+
this.onViteBeforeUpdateLogsLength = 0;
|
|
606
|
+
this.onViteBeforeUpdateErrorsLength = 0;
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* 渲染预览
|
|
610
|
+
* @param previewableId - 可预览项的 ID
|
|
611
|
+
* @param options - 渲染选项
|
|
612
|
+
*/
|
|
613
|
+
async render(previewableId, options) {
|
|
614
|
+
const previousRender = this.lastRender;
|
|
615
|
+
this.lastRender = {
|
|
616
|
+
previewableId,
|
|
617
|
+
options
|
|
618
|
+
};
|
|
619
|
+
if (previousRender?.previewableId !== previewableId || this.bootstrapStatus === "failure") {
|
|
620
|
+
this.resetIframe(previewableId);
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
const iframeWindow = this.options.getIframe()?.contentWindow;
|
|
624
|
+
if (!iframeWindow || this.bootstrapStatus !== "success") return;
|
|
625
|
+
if (!iframeWindow.__PREVIEWJS_IFRAME__.render) {
|
|
626
|
+
iframeWindow.__PREVIEWJS_IFRAME__.reportEvent({
|
|
627
|
+
kind: "error",
|
|
628
|
+
source: "renderer",
|
|
629
|
+
message: "Iframe was bootstrapped but not fully initialised."
|
|
630
|
+
});
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
const renderPromise = iframeWindow.__PREVIEWJS_IFRAME__.render(options);
|
|
634
|
+
this.clearExpectRenderTimeout();
|
|
635
|
+
this.expectRenderTimeout = setTimeout(() => {
|
|
636
|
+
console.warn("Expected render did not occur after 5 seconds. Reloading iframe...");
|
|
637
|
+
this.resetIframe(previewableId);
|
|
638
|
+
}, 5e3);
|
|
639
|
+
await renderPromise;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* 重置 iframe
|
|
643
|
+
* @param id - 可预览项的 ID
|
|
644
|
+
*/
|
|
645
|
+
resetIframe(id) {
|
|
646
|
+
const iframe = this.options.getIframe();
|
|
647
|
+
if (!iframe) return;
|
|
648
|
+
iframe.src = `/${id}/?t=${Date.now()}`;
|
|
649
|
+
this.bootstrapStatus = "not-started";
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* 处理预览事件
|
|
653
|
+
* @param event - 预览事件对象
|
|
654
|
+
*/
|
|
655
|
+
onPreviewEvent(event) {
|
|
656
|
+
if (this.bootstrapStatus === "not-started") {
|
|
657
|
+
if (event.kind !== "bootstrapping") return;
|
|
658
|
+
}
|
|
659
|
+
switch (event.kind) {
|
|
660
|
+
case "bootstrapping":
|
|
661
|
+
this.bootstrapStatus = "pending";
|
|
662
|
+
break;
|
|
663
|
+
case "bootstrapped":
|
|
664
|
+
this.bootstrapStatus = "success";
|
|
665
|
+
this.onViteBeforeUpdateLogsLength = 0;
|
|
666
|
+
this.onViteBeforeUpdateErrorsLength = 0;
|
|
667
|
+
this.updateState((state) => {
|
|
668
|
+
state.loading = true;
|
|
669
|
+
state.rendered = false;
|
|
670
|
+
state.errors = [];
|
|
671
|
+
state.logs = [];
|
|
672
|
+
});
|
|
673
|
+
if (this.lastRender) this.render(this.lastRender.previewableId, this.lastRender.options).catch(console.error);
|
|
674
|
+
break;
|
|
675
|
+
case "vite-before-reload":
|
|
676
|
+
this.updateState((state) => {
|
|
677
|
+
state.logs = [];
|
|
678
|
+
state.errors = [];
|
|
679
|
+
});
|
|
680
|
+
if (this.lastRender) this.resetIframe(this.lastRender.previewableId);
|
|
681
|
+
break;
|
|
682
|
+
case "vite-before-update":
|
|
683
|
+
this.onViteBeforeUpdateLogsLength = this.state.logs.length;
|
|
684
|
+
this.updateState((state) => {
|
|
685
|
+
for (const update of event.payload.updates) state.errors = state.errors.filter((e) => e.source !== "hmr" || e.modulePath !== update.acceptedPath);
|
|
686
|
+
});
|
|
687
|
+
this.onViteBeforeUpdateErrorsLength = this.state.errors.length;
|
|
688
|
+
break;
|
|
689
|
+
case "vite-invalidate":
|
|
690
|
+
this.updateState((state) => {
|
|
691
|
+
this.onViteBeforeUpdateLogsLength = 0;
|
|
692
|
+
this.onViteBeforeUpdateErrorsLength = 0;
|
|
693
|
+
state.logs = [];
|
|
694
|
+
});
|
|
695
|
+
break;
|
|
696
|
+
case "vite-after-update":
|
|
697
|
+
this.updateState((state) => {
|
|
698
|
+
const logsSliceStart = this.onViteBeforeUpdateLogsLength;
|
|
699
|
+
const errorsSliceStart = this.onViteBeforeUpdateErrorsLength;
|
|
700
|
+
this.onViteBeforeUpdateLogsLength = 0;
|
|
701
|
+
this.onViteBeforeUpdateErrorsLength = 0;
|
|
702
|
+
state.logs = state.logs.slice(logsSliceStart);
|
|
703
|
+
state.errors = state.errors.slice(errorsSliceStart);
|
|
704
|
+
});
|
|
705
|
+
break;
|
|
706
|
+
case "before-render":
|
|
707
|
+
this.updateState((state) => {
|
|
708
|
+
this.onViteBeforeUpdateLogsLength = 0;
|
|
709
|
+
state.logs = [];
|
|
710
|
+
});
|
|
711
|
+
break;
|
|
712
|
+
case "rendered":
|
|
713
|
+
this.updateState((state) => {
|
|
714
|
+
state.loading = false;
|
|
715
|
+
state.rendered = true;
|
|
716
|
+
});
|
|
717
|
+
this.clearExpectRenderTimeout();
|
|
718
|
+
break;
|
|
719
|
+
case "error":
|
|
720
|
+
this.updateState((state) => {
|
|
721
|
+
state.loading = false;
|
|
722
|
+
if (event.source === "vite" && event.message.includes("Failed to fetch dynamically imported module") && state.errors.find((e) => e.source === "vite")) return;
|
|
723
|
+
state.errors = state.errors.filter((e) => e.source !== event.source);
|
|
724
|
+
state.errors.push(event);
|
|
725
|
+
});
|
|
726
|
+
this.clearExpectRenderTimeout();
|
|
727
|
+
if (this.bootstrapStatus === "pending") this.bootstrapStatus = "failure";
|
|
728
|
+
break;
|
|
729
|
+
case "log-message":
|
|
730
|
+
this.updateState((state) => {
|
|
731
|
+
state.logs.push(event);
|
|
732
|
+
});
|
|
733
|
+
break;
|
|
734
|
+
case "action":
|
|
735
|
+
this.updateState((state) => {
|
|
736
|
+
state.actions.push(event);
|
|
737
|
+
});
|
|
738
|
+
break;
|
|
739
|
+
case "toggle-inspect": {
|
|
740
|
+
const window = this.options.getIframe()?.contentWindow;
|
|
741
|
+
if (window) {
|
|
742
|
+
window.__PREVIEWJS_IFRAME__.toggleInspect = event.enabled;
|
|
743
|
+
window.__PREVIEWJS_IFRAME__.refresh({ refetchWrapperModule: true });
|
|
744
|
+
}
|
|
745
|
+
break;
|
|
746
|
+
}
|
|
747
|
+
default: throw (0, import_assert_never.assertNever)(event);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* 更新状态
|
|
752
|
+
* @param stateModifier - 状态修改函数
|
|
753
|
+
*/
|
|
754
|
+
updateState(stateModifier) {
|
|
755
|
+
this.state = produce(this.state, stateModifier);
|
|
756
|
+
this.options.onStateUpdate?.(this.state);
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* 清除期望渲染的超时定时器
|
|
760
|
+
*/
|
|
761
|
+
clearExpectRenderTimeout() {
|
|
762
|
+
if (this.expectRenderTimeout) {
|
|
763
|
+
clearTimeout(this.expectRenderTimeout);
|
|
764
|
+
this.expectRenderTimeout = null;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
//#endregion
|
|
991
769
|
export { createController };
|