UNPKG

react-box-tools

Version:

Box tools react components, utils and hooks

2 lines (1 loc) 17.4 kB
"use strict";var we=Object.defineProperty;var Ae=(e,t,s)=>t in e?we(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s;var C=(e,t,s)=>Ae(e,typeof t!="symbol"?t+"":t,s);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var O=(e=>(e[e.Object=0]="Object",e[e.Array=1]="Array",e[e.Map=2]="Map",e[e.Set=3]="Set",e))(O||{}),m=(e=>(e[e.ProxyObject=0]="ProxyObject",e[e.ProxyArray=1]="ProxyArray",e[e.Map=2]="Map",e[e.Set=3]="Set",e[e.ES5Object=4]="ES5Object",e[e.ES5Array=5]="ES5Array",e))(m||{});function _e(e){return pe(e)}function pe(e){if(!M(e))return e;const t=e[_];let s;const i=j(e);if(t){if(!t.modified_&&(t.type_<4||!E("ES5").hasChanges_(t)))return t.base_;t.finalized_=!0,s=ue(e,i),t.finalized_=!1}else s=ue(e,i);return z(s,(a,f)=>{t&&X(t.base_,a)===f||oe(s,a,pe(f))}),i===O.Set?new Set(s):s}function ue(e,t){switch(t){case O.Map:return new Map(e);case O.Set:return Array.from(e)}return Y(e)}function q(e,t){t.unfinalizedDrafts_=t.drafts_.length;const s=t.drafts_[0],i=e!==void 0&&e!==s;return t.immutable_.useProxies_||E("ES5").willFinalizeES5_(t,e,i),i?(s[_].modified_&&B(t),M(e)&&(e=L(t,e),t.parent_||J(t,e)),t.patches_&&E("Patches").generateReplacementPatches_(s[_].base_,e,t.patches_,t.inversePatches_)):e=L(t,s,[]),B(t),t.patches_&&t.patchListener_(t.patches_,t.inversePatches_),e!==ee?e:void 0}function L(e,t,s){if(V(t))return t;const i=t[_];if(!i)return z(t,(a,f)=>le(e,i,t,a,f,s),!0),t;if(i.scope_!==e)return t;if(!i.modified_)return J(e,i.base_,!0),i.base_;if(!i.finalized_){i.finalized_=!0,i.scope_.unfinalizedDrafts_--;const a=i.type_===m.ES5Object||i.type_===m.ES5Array?i.copy_=Y(i.draft_):i.copy_;z(i.type_===m.Set?new Set(a):a,(f,P)=>le(e,i,a,f,P,s)),J(e,a,!1),s&&e.patches_&&E("Patches").generatePatches_(i,s,e.patches_,e.inversePatches_)}return i.copy_}function le(e,t,s,i,a,f){if(x(a)){const P=f&&t&&t.type_!==m.Set&&!D(t.assigned_,i)?f.concat(i):void 0,S=L(e,a,P);if(oe(s,i,S),x(S))e.canAutoFreeze_=!1;else return}if(M(a)&&!V(a)){if(!e.immutable_.autoFreeze_&&e.unfinalizedDrafts_<1)return;L(e,a),(!t||!t.scope_.parent_)&&J(e,a)}}function J(e,t,s=!1){e.immutable_.autoFreeze_&&e.canAutoFreeze_&&Z(t,s)}class Ee{constructor(t){C(this,"useProxies_",be);C(this,"autoFreeze_",!0);C(this,"create",(t,s,i)=>{if(typeof t=="function"&&typeof s!="function"){const f=s;s=t;const P=this;return function(l=f,...y){return P.create(l,o=>s.call(this,o,...y))}}let a;if(M(t)){const f=ne(this),P=I(this,t,void 0);let S=!0;try{a=s(P),S=!1}finally{S?B(f):Q(f)}return typeof Promise<"u"&&a instanceof Promise?a.then(l=>(W(f,i),q(l,f)),l=>{throw B(f),l}):(W(f,i),q(a,f))}else if(!t||typeof t!="object"){if(a=s(t),a===void 0&&(a=t),a===ee&&(a=void 0),this.autoFreeze_&&Z(a,!0),i){const f=[],P=[];E("Patches").generateReplacementPatches_(t,a,f,P),i(f,P)}return a}});C(this,"produceWithPatches",(t,s)=>{if(typeof t=="function")return(P,...S)=>this.produceWithPatches(P,l=>t(l,...S));let i,a;const f=this.create(t,s,(P,S)=>{i=P,a=S});return typeof Promise<"u"&&f instanceof Promise?f.then(P=>[P,i,a]):[f,i,a]});typeof(t==null?void 0:t.useProxies)=="boolean"&&this.setUseProxies(t.useProxies),typeof(t==null?void 0:t.autoFreeze)=="boolean"&&this.setAutoFreeze(t.autoFreeze)}createDraft(t){x(t)&&(t=_e(t));const s=ne(this),i=I(this,t,void 0);return i[_].isManual_=!0,Q(s),i}finishDraft(t,s){const i=t&&t[_],{scope_:a}=i;return W(a,s),q(void 0,a)}setAutoFreeze(t){this.autoFreeze_=t}setUseProxies(t){this.useProxies_=t}applyPatches(t,s){let i;for(i=s.length-1;i>=0;i--){const f=s[i];if(f.path.length===0&&f.op==="replace"){t=f.value;break}}i>-1&&(s=s.slice(i+1));const a=E("Patches").applyPatches_;return x(t)?a(t,s):this.create(t,f=>a(f,s))}}function I(e,t,s){const i=H(t)?E("MapSet").proxyMap_(t,s):$(t)?E("MapSet").proxySet_(t,s):e.useProxies_?ye(t,s):E("ES5").createES5Proxy_(t,s);return(s?s.scope_:N()).drafts_.push(i),i}function ye(e,t){const s=Array.isArray(e),i={type_:s?m.ProxyArray:m.ProxyObject,scope_:t?t.scope_:N(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1},a={};z(G,(y,o)=>{a[y]=function(){return arguments[0]=arguments[0][0],o.apply(this,arguments)}}),a.deleteProperty=function(y,o){return a.set.call(this,y,o,void 0)},a.set=function(y,o,r){return G.set.call(this,y[0],o,r,y[0])};let f=i,P=G;s&&(f=[i],P=a);const{revoke:S,proxy:l}=Proxy.revocable(f,P);return i.draft_=l,i.revoke_=S,l}const G={get(e,t){if(t===_)return e;const s=w(e);if(!D(s,t))return Me(e,s,t);const i=s[t];return e.finalized_||!M(i)?i:i===re(e.base_,t)?(U(e),e.copy_[t]=I(e.scope_.immutable_,i,e)):i},has(e,t){return t in w(e)},ownKeys(e){return Reflect.ownKeys(w(e))},set(e,t,s){const i=de(w(e),t);if(i!=null&&i.set)return i.set.call(e.draft_,s),!0;if(!e.modified_){const a=re(w(e),t),f=a==null?void 0:a[_];if(f&&f.base_===s)return e.copy_[t]=s,e.assigned_[t]=!1,!0;if(ce(s,a)&&(s!==void 0||D(e.base_,t)))return!0;U(e),A(e)}return e.copy_[t]===s&&typeof s!="number"&&(s!==void 0||t in e.copy_)||(e.copy_[t]=s,e.assigned_[t]=!0),!0},deleteProperty(e,t){return re(e.base_,t)!==void 0||t in e.base_?(e.assigned_[t]=!1,U(e),A(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0},getOwnPropertyDescriptor(e,t){const s=w(e),i=Reflect.getOwnPropertyDescriptor(s,t);return i&&{writable:!0,configurable:e.type_!==m.ProxyArray||t!=="length",enumerable:i.enumerable,value:s[t]}},getPrototypeOf(e){return Object.getPrototypeOf(e.base_)}};function re(e,t){const s=e[_];return(s?w(s):e)[t]}function Me(e,t,s){var a;const i=de(t,s);return i?"value"in i?i.value:(a=i.get)==null?void 0:a.call(e.draft_):void 0}function de(e,t){if(!(t in e))return;let s=Object.getPrototypeOf(e);for(;s;){const i=Object.getOwnPropertyDescriptor(s,t);if(i)return i;s=Object.getPrototypeOf(s)}}function A(e){e.modified_||(e.modified_=!0,e.parent_&&A(e.parent_))}function U(e){e.copy_||(e.copy_=Y(e.base_))}let K;function N(){return K}function ze(e,t){return{drafts_:[],parent_:e,immutable_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}function W(e,t){t&&(E("Patches"),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function B(e){Q(e),e.drafts_.forEach(De),e.drafts_=null}function Q(e){e===K&&(K=e.parent_)}function ne(e){return K=ze(K,e)}function De(e){const t=e[_];t.type_===m.ProxyObject||t.type_===m.ProxyArray?t.revoke_():t.revoked_=!0}function x(e){return!!e&&!!e[_]}function M(e){return e?he(e)||Array.isArray(e)||!!e[R]||!!e.constructor[R]||H(e)||$(e):!1}const xe=Object.prototype.constructor.toString();function he(e){if(!e||typeof e!="object")return!1;const t=Object.getPrototypeOf(e);if(t===null)return!0;const s=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return s===Object?!0:typeof s=="function"&&Function.toString.call(s)===xe}function ve(e){return e[_].base_}const v=typeof Reflect<"u"&&Reflect.ownKeys?Reflect.ownKeys:typeof Object.getOwnPropertySymbols<"u"?e=>Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e)):Object.getOwnPropertyNames,ie=Object.getOwnPropertyDescriptors||function(t){const s={};return v(t).forEach(i=>{s[i]=Object.getOwnPropertyDescriptor(t,i)}),s};function z(e,t,s=!1){j(e)===O.Object?(s?Object.keys:v)(e).forEach(i=>{(!s||typeof i!="symbol")&&t(i,e[i],e)}):e&&e.forEach((i,a)=>t(a,i,e))}function j(e){const t=e[_];return t?t.type_>3?t.type_-4:t.type_:Array.isArray(e)?O.Array:H(e)?O.Map:$(e)?O.Set:O.Object}function D(e,t){return j(e)===O.Map?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function X(e,t){return j(e)===O.Map?e.get(t):e[t]}function oe(e,t,s){const i=j(e);i===O.Map?e.set(t,s):i===O.Set?(e.delete(t),e.add(s)):e[t]=s}function ce(e,t){return e===t?e!==0||1/e===1/t:e!==e&&t!==t}function H(e){return ge&&e instanceof Map}function $(e){return Pe&&e instanceof Set}function w(e){return e.copy_||e.base_}function Y(e){if(Array.isArray(e))return Array.prototype.slice.call(e);const t=ie(e);delete t[_];let s=v(t);for(let i=0;i<s.length;i++){const a=s[i],f=t[a];f.writable===!1&&(f.writable=!0,f.configurable=!0),(f.get||f.set)&&(t[a]={configurable:!0,writable:!0,enumerable:f.enumerable,value:e[a]})}return Object.create(Object.getPrototypeOf(e),t)}function Z(e,t=!1){return V(e)||x(e)||!M(e)||(Object.freeze(e),t&&z(e,(s,i)=>Z(i,!0),!0)),e}function V(e){return e==null||typeof e!="object"?!0:Object.isFrozen(e)}const ae=typeof Symbol<"u"&&typeof Symbol("x")=="symbol",ge=typeof Map<"u",Pe=typeof Set<"u",be=typeof Proxy<"u"&&typeof Proxy.revocable<"u"&&typeof Reflect<"u",ee=ae?Symbol.for("immutable-nothing"):{"immutable-nothing":!0},R=ae?Symbol.for("immutable-draftable"):"__$immutable_draftable",_=ae?Symbol.for("immutable-state"):"__$immutable_state",T=typeof Symbol<"u"&&Symbol.iterator||"@@iterator";class je{constructor(){C(this,"_")}}const se={};function E(e){return se[e]}function te(e,t){se[e]||(se[e]=t)}function Fe(){me(),Se(),Oe()}function me(){function e(o,r,n){n?x(r)&&r[_].scope_===o&&f(o.drafts_):(o.patches_&&P(o.drafts_[0]),f(o.drafts_))}function t(o,r){if(o){const n=new Array(r.length);for(let c=0;c<r.length;c++)Object.defineProperty(n,""+c,a(c,!0));return n}else{const n=ie(r);delete n[_];const c=v(n);for(let u=0;u<c.length;u++){const h=c[u];n[h]=a(h,o||!!n[h].enumerable)}return Object.create(Object.getPrototypeOf(r),n)}}function s(o,r){const n=Array.isArray(o),c=t(n,o),u={type_:n?m.ES5Array:m.ES5Object,scope_:r?r.scope_:N(),modified_:!1,finalized_:!1,assigned_:{},parent_:r,base_:o,draft_:c,copy_:null,revoked_:!1,isManual_:!1};return Object.defineProperty(c,_,{value:u,writable:!0}),c}const i={};function a(o,r){let n=i[o];return n?n.enumerable=r:i[o]=n={configurable:!0,enumerable:r,get(){const c=this[_];return objectTraps.get(c,o)},set(c){const u=this[_];objectTraps.set(u,o,c)}},n}function f(o){for(let r=o.length-1;r>=0;r--){const n=o[r][_];if(!n.modified_)switch(n.type_){case m.ES5Array:l(n)&&A(n);break;case m.ES5Object:S(n)&&A(n);break}}}function P(o){if(!o||typeof o!="object")return;const r=o[_];if(!r)return;const{base_:n,draft_:c,assigned_:u,type_:h}=r;if(h===m.ES5Object)z(c,d=>{d!==_&&(n[d]===void 0&&!D(n,d)?(u[d]=!0,A(r)):u[d]||P(c[d]))}),z(n,d=>{c[d]===void 0&&!D(c,d)&&(u[d]=!1,A(r))});else if(h===m.ES5Array){if(l(r)&&(A(r),u.length=!0),c.length<n.length)for(let p=c.length;p<n.length;p++)u[p]=!1;else for(let p=n.length;p<c.length;p++)u[p]=!0;const d=Math.min(c.length,n.length);for(let p=0;p<d;p++)c.hasOwnProperty(p)||(u[p]=!0),u[p]===void 0&&P(c[p])}}function S(o){const{base_:r,draft_:n}=o,c=v(n);for(let h=c.length-1;h>=0;h--){const d=c[h];if(d===_)continue;const p=r[d];if(p===void 0&&!D(r,d))return!0;{const g=n[d],b=g&&g[_];if(b?b.base_!==p:!ce(g,p))return!0}}const u=!!r[_];return c.length!==v(r).length+(u?0:1)}function l(o){const{draft_:r}=o;if(r.length!==o.base_.length)return!0;const n=Object.getOwnPropertyDescriptor(r,r.length-1);if(n&&!n.get)return!0;for(let c=0;c<r.length;c++)if(!r.hasOwnProperty(c))return!0;return!1}function y(o){return o.type_===m.ES5Object?S(o):l(o)}te("ES5",{createES5Proxy_:s,willFinalizeES5_:e,hasChanges_:y})}function Se(){var e=function(l,y){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(o,r){o.__proto__=r}||function(o,r){for(var n in r)r.hasOwnProperty(n)&&(o[n]=r[n])},e(l,y)};function t(l,y){e(l,y);function o(){this.constructor=l}l.prototype=(o.prototype=y.prototype,new o)}const s=function(l){t(y,l);function y(r,n){return this[_]={type_:m.Map,parent_:n,scope_:n?n.scope_:N(),modified_:!1,finalized_:!1,copy_:void 0,assigned_:void 0,base_:r,draft_:this,isManual_:!1,revoked_:!1},this}const o=y.prototype;return Object.defineProperty(o,"size",{get:function(){return w(this[_]).size}}),o.has=function(r){return w(this[_]).has(r)},o.set=function(r,n){const c=this[_];return(!w(c).has(r)||w(c).get(r)!==n)&&(a(c),A(c),c.assigned_.set(r,!0),c.copy_.set(r,n),c.assigned_.set(r,!0)),this},o.delete=function(r){if(!this.has(r))return!1;const n=this[_];return a(n),A(n),n.base_.has(r)?n.assigned_.set(r,!1):n.assigned_.delete(r),n.copy_.delete(r),!0},o.clear=function(){const r=this[_];w(r).size&&(a(r),A(r),r.assigned_=new Map,z(r.base_,n=>{r.assigned_.set(n,!1)}),r.copy_.clear())},o.forEach=function(r,n){const c=this[_];w(c).forEach((u,h,d)=>{r.call(n,this.get(h),h,this)})},o.get=function(r){const n=this[_],c=w(n).get(r);if(n.finalized_||!M(c)||c!==n.base_.get(r))return c;const u=I(n.scope_.immutable_,c,n);return a(n),n.copy_.set(r,u),u},o.keys=function(){return w(this[_]).keys()},o.values=function(){const r=this.keys();return{[T]:()=>this.values(),next:()=>{const n=r.next();return n.done?n:{done:!1,value:this.get(n.value)}}}},o.entries=function(){const r=this.keys();return{[T]:()=>this.entries(),next:()=>{const n=r.next();if(n.done)return n;const c=this.get(n.value);return{done:!1,value:[n.value,c]}}}},o[T]=function(){return this.entries()},y}(Map);function i(l,y){return new s(l,y)}function a(l){l.copy_||(l.assigned_=new Map,l.copy_=new Map(l.base_))}const f=function(l){t(y,l);function y(r,n){return this[_]={type_:m.Set,parent_:n,scope_:n?n.scope_:N(),modified_:!1,finalized_:!1,copy_:void 0,base_:r,draft_:this,drafts_:new Map,revoked_:!1,isManual_:!1},this}const o=y.prototype;return Object.defineProperty(o,"size",{get:function(){return w(this[_]).size}}),o.has=function(r){const n=this[_];return n.copy_?!!(n.copy_.has(r)||n.drafts_.has(r)&&n.copy_.has(n.drafts_.get(r))):n.base_.has(r)},o.add=function(r){const n=this[_];return this.has(r)||(S(n),A(n),n.copy_.add(r)),this},o.delete=function(r){if(!this.has(r))return!1;const n=this[_];return S(n),A(n),n.copy_.delete(r)||(n.drafts_.has(r)?n.copy_.delete(n.drafts_.get(r)):!1)},o.clear=function(){const r=this[_];w(r).size&&(S(r),A(r),r.copy_.clear())},o.values=function(){const r=this[_];return S(r),r.copy_.values()},o.entries=function(){const n=this[_];return S(n),n.copy_.entries()},o.keys=function(){return this.values()},o[T]=function(){return this.values()},o.forEach=function(n,c){const u=this.values();let h=u.next();for(;!h.done;)n.call(c,h.value,h.value,this),h=u.next()},y}(Set);function P(l,y){return new f(l,y)}function S(l){l.copy_||(l.copy_=new Set,l.base_.forEach(y=>{if(M(y)){const o=I(l.scope_.immutable_,y,l);l.drafts_.set(y,o),l.copy_.add(o)}else l.copy_.add(y)}))}te("MapSet",{proxyMap_:i,proxySet_:P})}function Oe(){const e="replace",t="add",s="remove";function i(r,n,c,u){switch(r.type_){case m.ProxyObject:case m.ES5Object:case m.Map:return f(r,n,c,u);case m.ES5Array:case m.ProxyArray:return a(r,n,c,u);case m.Set:return P(r,n,c,u)}}function a(r,n,c,u){let{base_:h,assigned_:d}=r,p=r.copy_;p.length<h.length&&([h,p]=[p,h],[c,u]=[u,c]);for(let g=0;g<h.length;g++)if(d[g]&&p[g]!==h[g]){const b=n.concat([g]);c.push({op:e,path:b,value:o(p[g])}),u.push({op:e,path:b,value:o(h[g])})}for(let g=h.length;g<p.length;g++){const b=n.concat([g]);c.push({op:t,path:b,value:o(p[g])})}h.length<p.length&&u.push({op:e,path:n.concat(["length"]),value:h.length})}function f(r,n,c,u){const{base_:h,copy_:d}=r;z(r.assigned_,(p,g)=>{const b=X(h,p),fe=X(d,p),F=g?D(h,p)?e:t:s;if(b===fe&&F===e)return;const k=n.concat(p);c.push(F===s?{op:F,path:k}:{op:F,path:k,value:fe}),u.push(F===t?{op:s,path:k}:F===s?{op:t,path:k,value:o(b)}:{op:e,path:k,value:o(b)})})}function P(r,n,c,u){let{base_:h,copy_:d}=r,p=0;h.forEach(g=>{if(!d.has(g)){const b=n.concat([p]);c.push({op:s,path:b,value:g}),u.unshift({op:t,path:b,value:g})}p++}),p=0,d.forEach(g=>{if(!h.has(g)){const b=n.concat([p]);c.push({op:t,path:b,value:g}),u.unshift({op:s,path:b,value:g})}p++})}function S(r,n,c,u){c.push({op:e,path:[],value:n===ee?void 0:n}),u.push({op:e,path:[],value:r})}function l(r,n){return n.forEach(c=>{const{path:u,op:h}=c;let d=r;const p=j(d),g=y(c.value),b=u[u.length-1];switch(h){case e:switch(p){case O.Map:return d.set(b,g);case O.Set:default:return d[b]=g}case t:switch(p){case O.Array:return b==="-"?d.push(g):d.splice(b,0,g);case O.Map:return d.set(b,g);case O.Set:return d.add(g);default:return d[b]=g}case s:switch(p){case O.Array:return d.splice(b,1);case O.Map:return d.delete(b);case O.Set:return d.delete(c.value);default:return delete d[b]}}}),r}function y(r){if(!M(r))return r;if(Array.isArray(r))return r.map(y);if(H(r))return new Map(Array.from(r.entries()).map(([c,u])=>[c,y(u)]));if($(r))return new Set(Array.from(r).map(y));const n=Object.create(Object.getPrototypeOf(r));for(const c in r)n[c]=y(r[c]);return D(r,R)&&(n[R]=r[R]),n}function o(r){return x(r)?y(r):r}te("Patches",{applyPatches_:l,generatePatches_:i,generateReplacementPatches_:S})}exports.Archtype=O;exports.DRAFTABLE=R;exports.DRAFT_STATE=_;exports.ImmutableManage=Ee;exports.NOTHING=ee;exports.Nothing=je;exports.ProxyType=m;exports.createProxy=I;exports.createProxyDraft=ye;exports.current=_e;exports.eachProperty=z;exports.enableAllPlugins=Fe;exports.enableES5=me;exports.enableMapSet=Se;exports.enablePatches=Oe;exports.enterScope=ne;exports.freeze=Z;exports.getArchtype=j;exports.getCurrentScope=N;exports.getOwnPropertyDescriptors=ie;exports.getPlugin=E;exports.getProperty=X;exports.hasMap=ge;exports.hasProperty=D;exports.hasProxies=be;exports.hasSet=Pe;exports.isDraft=x;exports.isDraftable=M;exports.isEqual=ce;exports.isFrozen=V;exports.isMap=H;exports.isPlainObject=he;exports.isSet=$;exports.iteratorSymbol=T;exports.latest=w;exports.leaveScope=Q;exports.loadPlugin=te;exports.markChanged=A;exports.objectTraps=G;exports.originalState=ve;exports.ownKeys=v;exports.prepareCopy=U;exports.processResult=q;exports.revokeScope=B;exports.setProperty=oe;exports.shallowCopy=Y;exports.usePatchesInScope=W;