@cfp-studio/iframe 18.0.1 → 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 +769 -1
- package/package.json +12 -7
- 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 +4 -3
- 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 +1,769 @@
|
|
|
1
|
-
var z={};Object.defineProperty(z,"__esModule",{value:!0});var Y=z.assertNever=U;function U(e,t){if(typeof t=="string")throw new Error(t);if(typeof t=="function")throw new Error(t(e));if(t)return e;throw new Error("Unhandled discriminated union member: ".concat(JSON.stringify(e)))}z.default=U;var V=Symbol.for("immer-nothing"),B=Symbol.for("immer-draftable"),f=Symbol.for("immer-state"),Z=process.env.NODE_ENV!=="production"?[function(e){return`The plugin for '${e}' has not been loaded into Immer. To enable the plugin, import and call \`enable${e}()\` when initializing your application.`},function(e){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 '${e}'`},"This object has been frozen and should not be mutated",function(e){return"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? "+e},"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.","Immer forbids circular references","The first or second argument to `produce` must be a function","The third argument to `produce` must be a function or undefined","First argument to `createDraft` must be a plain object, an array, or an immerable object","First argument to `finishDraft` must be a draft returned by `createDraft`",function(e){return`'current' expects a draft, got: ${e}`},"Object.defineProperty() cannot be used on an Immer draft","Object.setPrototypeOf() cannot be used on an Immer draft","Immer only supports deleting array indices","Immer only supports setting array indices and the 'length' property",function(e){return`'original' expects a draft, got: ${e}`}]:[];function c(e,...t){if(process.env.NODE_ENV!=="production"){const r=Z[e],n=typeof r=="function"?r.apply(null,t):r;throw new Error(`[Immer] ${n}`)}throw new Error(`[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`)}var _=Object.getPrototypeOf;function y(e){return!!e&&!!e[f]}function l(e){return e?M(e)||Array.isArray(e)||!!e[B]||!!e.constructor?.[B]||w(e)||S(e):!1}var ee=Object.prototype.constructor.toString();function M(e){if(!e||typeof e!="object")return!1;const t=_(e);if(t===null)return!0;const r=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return r===Object?!0:typeof r=="function"&&Function.toString.call(r)===ee}function E(e,t){P(e)===0?Reflect.ownKeys(e).forEach(r=>{t(r,e[r],e)}):e.forEach((r,n)=>t(n,r,e))}function P(e){const t=e[f];return t?t.type_:Array.isArray(e)?1:w(e)?2:S(e)?3:0}function F(e,t){return P(e)===2?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function W(e,t,r){const n=P(e);n===2?e.set(t,r):n===3?e.add(r):e[t]=r}function te(e,t){return e===t?e!==0||1/e===1/t:e!==e&&t!==t}function w(e){return e instanceof Map}function S(e){return e instanceof Set}function h(e){return e.copy_||e.base_}function D(e,t){if(w(e))return new Map(e);if(S(e))return new Set(e);if(Array.isArray(e))return Array.prototype.slice.call(e);const r=M(e);if(t===!0||t==="class_only"&&!r){const n=Object.getOwnPropertyDescriptors(e);delete n[f];let i=Reflect.ownKeys(n);for(let o=0;o<i.length;o++){const s=i[o],a=n[s];a.writable===!1&&(a.writable=!0,a.configurable=!0),(a.get||a.set)&&(n[s]={configurable:!0,writable:!0,enumerable:a.enumerable,value:e[s]})}return Object.create(_(e),n)}else{const n=_(e);if(n!==null&&r)return{...e};const i=Object.create(n);return Object.assign(i,e)}}function O(e,t=!1){return v(e)||y(e)||!l(e)||(P(e)>1&&(e.set=e.add=e.clear=e.delete=re),Object.freeze(e),t&&Object.entries(e).forEach(([r,n])=>O(n,!0))),e}function re(){c(2)}function v(e){return Object.isFrozen(e)}var ne={};function p(e){const t=ne[e];return t||c(0,e),t}var g;function j(){return g}function ie(e,t){return{drafts_:[],parent_:e,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}function $(e,t){t&&(p("Patches"),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function k(e){A(e),e.drafts_.forEach(oe),e.drafts_=null}function A(e){e===g&&(g=e.parent_)}function J(e){return g=ie(g,e)}function oe(e){const t=e[f];t.type_===0||t.type_===1?t.revoke_():t.revoked_=!0}function K(e,t){t.unfinalizedDrafts_=t.drafts_.length;const r=t.drafts_[0];return e!==void 0&&e!==r?(r[f].modified_&&(k(t),c(4)),l(e)&&(e=R(t,e),t.parent_||I(t,e)),t.patches_&&p("Patches").generateReplacementPatches_(r[f].base_,e,t.patches_,t.inversePatches_)):e=R(t,r,[]),k(t),t.patches_&&t.patchListener_(t.patches_,t.inversePatches_),e!==V?e:void 0}function R(e,t,r){if(v(t))return t;const n=t[f];if(!n)return E(t,(i,o)=>G(e,n,t,i,o,r)),t;if(n.scope_!==e)return t;if(!n.modified_)return I(e,n.base_,!0),n.base_;if(!n.finalized_){n.finalized_=!0,n.scope_.unfinalizedDrafts_--;const i=n.copy_;let o=i,s=!1;n.type_===3&&(o=new Set(i),i.clear(),s=!0),E(o,(a,d)=>G(e,n,i,a,d,r,s)),I(e,i,!1),r&&e.patches_&&p("Patches").generatePatches_(n,r,e.patches_,e.inversePatches_)}return n.copy_}function G(e,t,r,n,i,o,s){if(process.env.NODE_ENV!=="production"&&i===r&&c(5),y(i)){const a=o&&t&&t.type_!==3&&!F(t.assigned_,n)?o.concat(n):void 0,d=R(e,i,a);if(W(r,n,d),y(d))e.canAutoFreeze_=!1;else return}else s&&r.add(i);if(l(i)&&!v(i)){if(!e.immer_.autoFreeze_&&e.unfinalizedDrafts_<1)return;R(e,i),(!t||!t.scope_.parent_)&&typeof n!="symbol"&&Object.prototype.propertyIsEnumerable.call(r,n)&&I(e,i)}}function I(e,t,r=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&O(t,r)}function se(e,t){const r=Array.isArray(e),n={type_:r?1:0,scope_:t?t.scope_:j(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1};let i=n,o=N;r&&(i=[n],o=b);const{revoke:s,proxy:a}=Proxy.revocable(i,o);return n.draft_=a,n.revoke_=s,a}var N={get(e,t){if(t===f)return e;const r=h(e);if(!F(r,t))return ae(e,r,t);const n=r[t];return e.finalized_||!l(n)?n:n===L(e.base_,t)?(x(e),e.copy_[t]=C(n,e)):n},has(e,t){return t in h(e)},ownKeys(e){return Reflect.ownKeys(h(e))},set(e,t,r){const n=H(h(e),t);if(n?.set)return n.set.call(e.draft_,r),!0;if(!e.modified_){const i=L(h(e),t),o=i?.[f];if(o&&o.base_===r)return e.copy_[t]=r,e.assigned_[t]=!1,!0;if(te(r,i)&&(r!==void 0||F(e.base_,t)))return!0;x(e),T(e)}return e.copy_[t]===r&&(r!==void 0||t in e.copy_)||Number.isNaN(r)&&Number.isNaN(e.copy_[t])||(e.copy_[t]=r,e.assigned_[t]=!0),!0},deleteProperty(e,t){return L(e.base_,t)!==void 0||t in e.base_?(e.assigned_[t]=!1,x(e),T(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0},getOwnPropertyDescriptor(e,t){const r=h(e),n=Reflect.getOwnPropertyDescriptor(r,t);return n&&{writable:!0,configurable:e.type_!==1||t!=="length",enumerable:n.enumerable,value:r[t]}},defineProperty(){c(11)},getPrototypeOf(e){return _(e.base_)},setPrototypeOf(){c(12)}},b={};E(N,(e,t)=>{b[e]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}}),b.deleteProperty=function(e,t){return process.env.NODE_ENV!=="production"&&isNaN(parseInt(t))&&c(13),b.set.call(this,e,t,void 0)},b.set=function(e,t,r){return process.env.NODE_ENV!=="production"&&t!=="length"&&isNaN(parseInt(t))&&c(14),N.set.call(this,e[0],t,r,e[0])};function L(e,t){const r=e[f];return(r?h(r):e)[t]}function ae(e,t,r){const n=H(t,r);return n?"value"in n?n.value:n.get?.call(e.draft_):void 0}function H(e,t){if(!(t in e))return;let r=_(e);for(;r;){const n=Object.getOwnPropertyDescriptor(r,t);if(n)return n;r=_(r)}}function T(e){e.modified_||(e.modified_=!0,e.parent_&&T(e.parent_))}function x(e){e.copy_||(e.copy_=D(e.base_,e.scope_.immer_.useStrictShallowCopy_))}var ce=class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.produce=(t,r,n)=>{if(typeof t=="function"&&typeof r!="function"){const o=r;r=t;const s=this;return function(d=o,...q){return s.produce(d,Q=>r.call(this,Q,...q))}}typeof r!="function"&&c(6),n!==void 0&&typeof n!="function"&&c(7);let i;if(l(t)){const o=J(this),s=C(t,void 0);let a=!0;try{i=r(s),a=!1}finally{a?k(o):A(o)}return $(o,n),K(i,o)}else if(!t||typeof t!="object"){if(i=r(t),i===void 0&&(i=t),i===V&&(i=void 0),this.autoFreeze_&&O(i,!0),n){const o=[],s=[];p("Patches").generateReplacementPatches_(t,i,o,s),n(o,s)}return i}else c(1,t)},this.produceWithPatches=(t,r)=>{if(typeof t=="function")return(s,...a)=>this.produceWithPatches(s,d=>t(d,...a));let n,i;return[this.produce(t,r,(s,a)=>{n=s,i=a}),n,i]},typeof e?.autoFreeze=="boolean"&&this.setAutoFreeze(e.autoFreeze),typeof e?.useStrictShallowCopy=="boolean"&&this.setUseStrictShallowCopy(e.useStrictShallowCopy)}createDraft(e){l(e)||c(8),y(e)&&(e=fe(e));const t=J(this),r=C(e,void 0);return r[f].isManual_=!0,A(t),r}finishDraft(e,t){const r=e&&e[f];(!r||!r.isManual_)&&c(9);const{scope_:n}=r;return $(n,t),K(void 0,n)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}applyPatches(e,t){let r;for(r=t.length-1;r>=0;r--){const i=t[r];if(i.path.length===0&&i.op==="replace"){e=i.value;break}}r>-1&&(t=t.slice(r+1));const n=p("Patches").applyPatches_;return y(e)?n(e,t):this.produce(e,i=>n(i,t))}};function C(e,t){const r=w(e)?p("MapSet").proxyMap_(e,t):S(e)?p("MapSet").proxySet_(e,t):se(e,t);return(t?t.scope_:j()).drafts_.push(r),r}function fe(e){return y(e)||c(10,e),X(e)}function X(e){if(!l(e)||v(e))return e;const t=e[f];let r;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,r=D(e,t.scope_.immer_.useStrictShallowCopy_)}else r=D(e,!0);return E(r,(n,i)=>{W(r,n,X(i))}),t&&(t.finalized_=!1),r}var u=new ce,ue=u.produce;u.produceWithPatches.bind(u),u.setAutoFreeze.bind(u),u.setUseStrictShallowCopy.bind(u),u.applyPatches.bind(u),u.createDraft.bind(u),u.finishDraft.bind(u);var de=Object.defineProperty,le=(e,t,r)=>t in e?de(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,m=(e,t,r)=>(le(e,typeof t!="symbol"?t+"":t,r),r);function he(e){const t=new pe(e);return window.__PREVIEWJS_CONTROLLER__=t,t}class pe{constructor(t){this.options=t,m(this,"lastRender",null),m(this,"bootstrapStatus","pending"),m(this,"expectRenderTimeout"),m(this,"state",{loading:!1,rendered:!1,errors:[],logs:[],actions:[]}),m(this,"onViteBeforeUpdateLogsLength",0),m(this,"onViteBeforeUpdateErrorsLength",0)}async render(t,r){const n=this.lastRender;if(this.lastRender={previewableId:t,options:r},n?.previewableId!==t||this.bootstrapStatus==="failure"){this.resetIframe(t);return}const i=this.options.getIframe()?.contentWindow;if(!i||this.bootstrapStatus!=="success")return;if(!i.__PREVIEWJS_IFRAME__.render){i.__PREVIEWJS_IFRAME__.reportEvent({kind:"error",source:"renderer",message:"Iframe was bootstrapped but not fully initialised."});return}const o=i.__PREVIEWJS_IFRAME__.render(r);this.clearExpectRenderTimeout(),this.expectRenderTimeout=setTimeout(()=>{console.warn("Expected render did not occur after 5 seconds. Reloading iframe..."),this.resetIframe(t)},5e3),await o}resetIframe(t){const r=this.options.getIframe();r&&(r.src=`/${t}/?t=${Date.now()}`,this.bootstrapStatus="not-started")}onPreviewEvent(t){if(!(this.bootstrapStatus==="not-started"&&t.kind!=="bootstrapping"))switch(t.kind){case"bootstrapping":this.bootstrapStatus="pending";break;case"bootstrapped":this.bootstrapStatus="success",this.onViteBeforeUpdateLogsLength=0,this.onViteBeforeUpdateErrorsLength=0,this.updateState(r=>{r.loading=!0,r.rendered=!1,r.errors=[],r.logs=[]}),this.lastRender&&this.render(this.lastRender.previewableId,this.lastRender.options).catch(console.error);break;case"vite-before-reload":this.updateState(r=>{r.logs=[],r.errors=[]}),this.lastRender&&this.resetIframe(this.lastRender.previewableId);break;case"vite-before-update":this.onViteBeforeUpdateLogsLength=this.state.logs.length,this.updateState(r=>{for(const n of t.payload.updates)r.errors=r.errors.filter(i=>i.source!=="hmr"||i.modulePath!==n.acceptedPath)}),this.onViteBeforeUpdateErrorsLength=this.state.errors.length;break;case"vite-invalidate":this.updateState(r=>{this.onViteBeforeUpdateLogsLength=0,this.onViteBeforeUpdateErrorsLength=0,r.logs=[]});break;case"vite-after-update":this.updateState(r=>{const n=this.onViteBeforeUpdateLogsLength,i=this.onViteBeforeUpdateErrorsLength;this.onViteBeforeUpdateLogsLength=0,this.onViteBeforeUpdateErrorsLength=0,r.logs=r.logs.slice(n),r.errors=r.errors.slice(i)});break;case"before-render":this.updateState(r=>{this.onViteBeforeUpdateLogsLength=0,r.logs=[]});break;case"rendered":{this.updateState(r=>{r.loading=!1,r.rendered=!0}),this.clearExpectRenderTimeout();break}case"error":this.updateState(r=>{r.loading=!1,!(t.source==="vite"&&t.message.includes("Failed to fetch dynamically imported module")&&r.errors.find(n=>n.source==="vite"))&&(r.errors=r.errors.filter(n=>n.source!==t.source),r.errors.push(t))}),this.clearExpectRenderTimeout(),this.bootstrapStatus==="pending"&&(this.bootstrapStatus="failure");break;case"log-message":this.updateState(r=>{r.logs.push(t)});break;case"action":this.updateState(r=>{r.actions.push(t)});break;case"toggle-inspect":{console.log("\u89E6\u53D1toggle-inspect",t.enabled);const r=this.options.getIframe()?.contentWindow;r&&(r.__PREVIEWJS_IFRAME__.toggleInspect=t.enabled,r.__PREVIEWJS_IFRAME__.refresh({refetchWrapperModule:!0}));break}default:throw Y(t)}}updateState(t){this.state=ue(this.state,t),this.options.onStateUpdate?.(this.state)}clearExpectRenderTimeout(){this.expectRenderTimeout&&(clearTimeout(this.expectRenderTimeout),this.expectRenderTimeout=null)}}export{he as createController};
|
|
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
|
+
})))();
|
|
40
|
+
var NOTHING = Symbol.for("immer-nothing");
|
|
41
|
+
var DRAFTABLE = Symbol.for("immer-draftable");
|
|
42
|
+
var DRAFT_STATE = Symbol.for("immer-state");
|
|
43
|
+
var errors = process.env.NODE_ENV !== "production" ? [
|
|
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
|
+
}
|
|
70
|
+
] : [];
|
|
71
|
+
function die(error, ...args) {
|
|
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
|
+
}
|
|
79
|
+
var getPrototypeOf = Object.getPrototypeOf;
|
|
80
|
+
function isDraft(value) {
|
|
81
|
+
return !!value && !!value[DRAFT_STATE];
|
|
82
|
+
}
|
|
83
|
+
function isDraftable(value) {
|
|
84
|
+
if (!value) return false;
|
|
85
|
+
return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!value.constructor?.[DRAFTABLE] || isMap(value) || isSet(value);
|
|
86
|
+
}
|
|
87
|
+
var objectCtorString = Object.prototype.constructor.toString();
|
|
88
|
+
function isPlainObject(value) {
|
|
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;
|
|
95
|
+
}
|
|
96
|
+
function each(obj, iter) {
|
|
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));
|
|
101
|
+
}
|
|
102
|
+
function getArchtype(thing) {
|
|
103
|
+
const state = thing[DRAFT_STATE];
|
|
104
|
+
return state ? state.type_ : Array.isArray(thing) ? 1 : isMap(thing) ? 2 : isSet(thing) ? 3 : 0;
|
|
105
|
+
}
|
|
106
|
+
function has(thing, prop) {
|
|
107
|
+
return getArchtype(thing) === 2 ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
|
|
108
|
+
}
|
|
109
|
+
function set(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;
|
|
114
|
+
}
|
|
115
|
+
function is(x, y) {
|
|
116
|
+
if (x === y) return x !== 0 || 1 / x === 1 / y;
|
|
117
|
+
else return x !== x && y !== y;
|
|
118
|
+
}
|
|
119
|
+
function isMap(target) {
|
|
120
|
+
return target instanceof Map;
|
|
121
|
+
}
|
|
122
|
+
function isSet(target) {
|
|
123
|
+
return target instanceof Set;
|
|
124
|
+
}
|
|
125
|
+
function latest(state) {
|
|
126
|
+
return state.copy_ || state.base_;
|
|
127
|
+
}
|
|
128
|
+
function shallowCopy(base, strict) {
|
|
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
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function freeze(obj, deep = false) {
|
|
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;
|
|
165
|
+
}
|
|
166
|
+
function dontMutateFrozenCollections() {
|
|
167
|
+
die(2);
|
|
168
|
+
}
|
|
169
|
+
function isFrozen(obj) {
|
|
170
|
+
return Object.isFrozen(obj);
|
|
171
|
+
}
|
|
172
|
+
var plugins = {};
|
|
173
|
+
function getPlugin(pluginKey) {
|
|
174
|
+
const plugin = plugins[pluginKey];
|
|
175
|
+
if (!plugin) die(0, pluginKey);
|
|
176
|
+
return plugin;
|
|
177
|
+
}
|
|
178
|
+
var currentScope;
|
|
179
|
+
function getCurrentScope() {
|
|
180
|
+
return currentScope;
|
|
181
|
+
}
|
|
182
|
+
function createScope(parent_, immer_) {
|
|
183
|
+
return {
|
|
184
|
+
drafts_: [],
|
|
185
|
+
parent_,
|
|
186
|
+
immer_,
|
|
187
|
+
canAutoFreeze_: true,
|
|
188
|
+
unfinalizedDrafts_: 0
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function usePatchesInScope(scope, patchListener) {
|
|
192
|
+
if (patchListener) {
|
|
193
|
+
getPlugin("Patches");
|
|
194
|
+
scope.patches_ = [];
|
|
195
|
+
scope.inversePatches_ = [];
|
|
196
|
+
scope.patchListener_ = patchListener;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function revokeScope(scope) {
|
|
200
|
+
leaveScope(scope);
|
|
201
|
+
scope.drafts_.forEach(revokeDraft);
|
|
202
|
+
scope.drafts_ = null;
|
|
203
|
+
}
|
|
204
|
+
function leaveScope(scope) {
|
|
205
|
+
if (scope === currentScope) currentScope = scope.parent_;
|
|
206
|
+
}
|
|
207
|
+
function enterScope(immer2) {
|
|
208
|
+
return currentScope = createScope(currentScope, immer2);
|
|
209
|
+
}
|
|
210
|
+
function revokeDraft(draft) {
|
|
211
|
+
const state = draft[DRAFT_STATE];
|
|
212
|
+
if (state.type_ === 0 || state.type_ === 1) state.revoke_();
|
|
213
|
+
else state.revoked_ = true;
|
|
214
|
+
}
|
|
215
|
+
function processResult(result, scope) {
|
|
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;
|
|
232
|
+
}
|
|
233
|
+
function finalize(rootScope, value, path) {
|
|
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_;
|
|
261
|
+
}
|
|
262
|
+
function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath, targetIsSet) {
|
|
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
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function maybeFreeze(scope, value, deep = false) {
|
|
277
|
+
if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) freeze(value, deep);
|
|
278
|
+
}
|
|
279
|
+
function createProxyProxy(base, parent) {
|
|
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;
|
|
304
|
+
}
|
|
305
|
+
var objectTraps = {
|
|
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
|
+
}
|
|
376
|
+
};
|
|
377
|
+
var arrayTraps = {};
|
|
378
|
+
each(objectTraps, (key, fn) => {
|
|
379
|
+
arrayTraps[key] = function() {
|
|
380
|
+
arguments[0] = arguments[0][0];
|
|
381
|
+
return fn.apply(this, arguments);
|
|
382
|
+
};
|
|
383
|
+
});
|
|
384
|
+
arrayTraps.deleteProperty = function(state, prop) {
|
|
385
|
+
if (process.env.NODE_ENV !== "production" && isNaN(parseInt(prop))) die(13);
|
|
386
|
+
return arrayTraps.set.call(this, state, prop, void 0);
|
|
387
|
+
};
|
|
388
|
+
arrayTraps.set = function(state, prop, value) {
|
|
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]);
|
|
391
|
+
};
|
|
392
|
+
function peek(draft, prop) {
|
|
393
|
+
const state = draft[DRAFT_STATE];
|
|
394
|
+
return (state ? latest(state) : draft)[prop];
|
|
395
|
+
}
|
|
396
|
+
function readPropFromProto(state, source, prop) {
|
|
397
|
+
const desc = getDescriptorFromProto(source, prop);
|
|
398
|
+
return desc ? `value` in desc ? desc.value : desc.get?.call(state.draft_) : void 0;
|
|
399
|
+
}
|
|
400
|
+
function getDescriptorFromProto(source, prop) {
|
|
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
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function markChanged(state) {
|
|
410
|
+
if (!state.modified_) {
|
|
411
|
+
state.modified_ = true;
|
|
412
|
+
if (state.parent_) markChanged(state.parent_);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
function prepareCopy(state) {
|
|
416
|
+
if (!state.copy_) state.copy_ = shallowCopy(state.base_, state.scope_.immer_.useStrictShallowCopy_);
|
|
417
|
+
}
|
|
418
|
+
var Immer2 = class {
|
|
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
|
+
}
|
|
541
|
+
};
|
|
542
|
+
function createProxy(value, parent) {
|
|
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;
|
|
546
|
+
}
|
|
547
|
+
function current(value) {
|
|
548
|
+
if (!isDraft(value)) die(10, value);
|
|
549
|
+
return currentImpl(value);
|
|
550
|
+
}
|
|
551
|
+
function currentImpl(value) {
|
|
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
|
+
}
|
|
566
|
+
var immer = new Immer2();
|
|
567
|
+
var produce = immer.produce;
|
|
568
|
+
immer.produceWithPatches.bind(immer);
|
|
569
|
+
immer.setAutoFreeze.bind(immer);
|
|
570
|
+
immer.setUseStrictShallowCopy.bind(immer);
|
|
571
|
+
immer.applyPatches.bind(immer);
|
|
572
|
+
immer.createDraft.bind(immer);
|
|
573
|
+
immer.finishDraft.bind(immer);
|
|
574
|
+
//#endregion
|
|
575
|
+
//#region src/index.ts
|
|
576
|
+
/**
|
|
577
|
+
* 创建预览 iframe 控制器
|
|
578
|
+
* @param options - 创建控制器的选项
|
|
579
|
+
* @returns 预览 iframe 控制器实例
|
|
580
|
+
*/
|
|
581
|
+
function createController(options) {
|
|
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
|
|
769
|
+
export { createController };
|