@univerjs/drawing 0.15.0 → 0.15.1

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/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var Gt=Object.defineProperty;var Lt=(a,e,n)=>e in a?Gt(a,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):a[e]=n;var $=(a,e,n)=>Lt(a,typeof e!="symbol"?e+"":e,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const ce=require("@univerjs/core"),Ie=require("rxjs"),qt=500,Kt=500,Ht=10,Tt=5*1024*1024,At=["image/png","image/jpeg","image/jpg","image/gif","image/bmp"],ht=ce.createIdentifier("univer.drawing-manager.service"),Mt={id:"drawing.operation.set-drawing-selected",type:ce.CommandType.OPERATION,handler:(a,e)=>{const n=a.get(ht);return e==null?!1:(n.focusDrawing(e),!0)}},$t="drawing.config",vt={};var Ve={},Qe={},tt={},mt;function Ft(){if(mt)return tt;mt=1,Object.defineProperty(tt,"__esModule",{value:!0});function a(t,r){if(Array.isArray(r))return!1;for(let u in t)if(!n(t[u],r[u]))return!1;for(let u in r)if(t[u]===void 0)return!1;return!0}function e(t,r){if(!Array.isArray(r)||t.length!==r.length)return!1;for(let u=0;u<t.length;u++)if(!n(t[u],r[u]))return!1;return!0}function n(t,r){return t===r?!0:t===null||r===null||typeof t!="object"||typeof r!="object"?!1:Array.isArray(t)?e(t,r):a(t,r)}return tt.default=n,tt}var nt={},Ot;function Vt(){if(Ot)return nt;Ot=1,Object.defineProperty(nt,"__esModule",{value:!0});function a(e){if(e===null)return null;if(Array.isArray(e))return e.map(a);if(typeof e=="object"){const n={};for(let t in e)n[t]=a(e[t]);return n}else return e}return nt.default=a,nt}var at={},bt;function jt(){return bt||(bt=1,(function(a){Object.defineProperty(a,"__esModule",{value:!0}),a.eachChildOf=a.advancer=a.readCursor=a.writeCursor=a.WriteCursor=a.ReadCursor=a.isValidPathItem=void 0;function e(_,s){if(!_)throw new Error(s)}const n=_=>_!=null&&typeof _=="object"&&!Array.isArray(_),t=(_,s)=>typeof _==typeof s?_>s:typeof _=="string"&&typeof s=="number";function r(_,s){for(let c in _){const h=c;s.write(h,_[h])}}a.isValidPathItem=_=>typeof _=="number"||typeof _=="string"&&_!=="__proto__";class u{constructor(s=null){this.parents=[],this.indexes=[],this.lcIdx=-1,this.idx=-1,this.container=s}ascend(){e(this.parents.length===this.indexes.length/2),this.idx===0?this.parents.length?(this.lcIdx=this.indexes.pop(),this.container=this.parents.pop(),this.idx=this.indexes.pop()):(this.lcIdx=0,this.idx=-1):(e(this.idx>0),this.idx--,n(this.container[this.idx])&&this.idx--)}getPath(){const s=[];let c=this.container,h=this.parents.length-1,U=this.idx;for(;U>=0;)s.unshift(c[U]),U===0?(U=this.indexes[h*2],c=this.parents[h--]):U-=n(c[U-1])?2:1;return s}}class o extends u{get(){return this.container?this.container.slice(this.idx+1):null}getKey(){return e(this.container!=null,"Invalid call to getKey before cursor descended"),this.container[this.idx]}getComponent(){let s;return this.container&&this.container.length>this.idx+1&&n(s=this.container[this.idx+1])?s:null}descendFirst(){let s=this.idx+1;if(!this.container||s>=this.container.length||n(this.container[s])&&s+1>=this.container.length)return!1;n(this.container[s])&&s++;const c=this.container[s];return Array.isArray(c)?(this.indexes.push(this.idx),this.parents.push(this.container),this.indexes.push(s),this.idx=0,this.container=c):this.idx=s,!0}nextSibling(){if(e(this.parents.length===this.indexes.length/2),this.idx>0||this.parents.length===0)return!1;const s=this.indexes[this.indexes.length-1]+1,c=this.parents[this.parents.length-1];return s>=c.length?!1:(e(!isNaN(s)),this.indexes[this.indexes.length-1]=s,this.container=c[s],!0)}_init(s,c,h,U){this.container=s,this.idx=c,this.parents=h.slice(),this.indexes=U.slice()}clone(){const s=new o;return s._init(this.container,this.idx,this.parents,this.indexes),s}*[Symbol.iterator](){if(this.descendFirst()){do yield this.getKey();while(this.nextSibling());this.ascend()}}traverse(s,c){const h=this.getComponent();h&&c(h,s);for(const U of this)s&&s.descend(U),this.traverse(s,c),s&&s.ascend()}eachPick(s,c){this.traverse(s,(h,U)=>{h.p!=null&&c(h.p,U)})}eachDrop(s,c){this.traverse(s,(h,U)=>{h.d!=null&&c(h.d,U)})}}a.ReadCursor=o;class l extends u{constructor(s=null){super(s),this.pendingDescent=[],this._op=s}flushDescent(){e(this.parents.length===this.indexes.length/2),this.container===null&&(this._op=this.container=[]);for(let s=0;s<this.pendingDescent.length;s++){const c=this.pendingDescent[s];let h=this.idx+1;if(h<this.container.length&&n(this.container[h])&&h++,e(h===this.container.length||!n(this.container[h])),h===this.container.length)this.container.push(c),this.idx=h;else if(this.container[h]===c)this.idx=h;else{if(!Array.isArray(this.container[h])){const U=this.container.splice(h,this.container.length-h);this.container.push(U),this.lcIdx>-1&&(this.lcIdx=h)}for(this.indexes.push(this.idx),this.parents.push(this.container),this.lcIdx!==-1&&(e(t(c,this.container[this.lcIdx][0])),h=this.lcIdx+1,this.lcIdx=-1);h<this.container.length&&t(c,this.container[h][0]);)h++;if(this.indexes.push(h),this.idx=0,h<this.container.length&&this.container[h][0]===c)this.container=this.container[h];else{const U=[c];this.container.splice(h,0,U),this.container=U}}}this.pendingDescent.length=0}reset(){this.lcIdx=-1}getComponent(){this.flushDescent();const s=this.idx+1;if(s<this.container.length&&n(this.container[s]))return this.container[s];{const c={};return this.container.splice(s,0,c),c}}write(s,c){const h=this.getComponent();e(h[s]==null||h[s]===c,"Internal consistency error: Overwritten component. File a bug"),h[s]=c}get(){return this._op}descend(s){if(!a.isValidPathItem(s))throw Error("Invalid JSON key");this.pendingDescent.push(s)}descendPath(s){return this.pendingDescent.push(...s),this}ascend(){this.pendingDescent.length?this.pendingDescent.pop():super.ascend()}mergeTree(s,c=r){if(s===null)return;if(e(Array.isArray(s)),s===this._op)throw Error("Cannot merge into my own tree");const h=this.lcIdx,U=this.parents.length;let Y=0;for(let pe=0;pe<s.length;pe++){const Z=s[pe];typeof Z=="string"||typeof Z=="number"?(Y++,this.descend(Z)):Array.isArray(Z)?this.mergeTree(Z,c):typeof Z=="object"&&c(Z,this)}for(;Y--;)this.ascend();this.lcIdx=this.parents.length===U?h:-1}at(s,c){this.descendPath(s),c(this);for(let h=0;h<s.length;h++)this.ascend();return this}writeAtPath(s,c,h){return this.at(s,()=>this.write(c,h)),this.reset(),this}writeMove(s,c,h=0){return this.writeAtPath(s,"p",h).writeAtPath(c,"d",h)}getPath(){const s=super.getPath();return s.push(...this.pendingDescent),s}}a.WriteCursor=l,a.writeCursor=()=>new l,a.readCursor=_=>new o(_);function E(_,s,c){let h,U;U=h=_?_.descendFirst():!1;function Y(pe){let Z;for(;U;){const Ce=Z=_.getKey();if(pe!=null){let Ke=!1;if(s&&typeof Ce=="number"&&(Z=s(Ce,_.getComponent()),Z<0&&(Z=~Z,Ke=!0)),t(Z,pe))return null;if(Z===pe&&!Ke)return _}c&&typeof Z=="number"&&c(Z,_.getComponent()),U=_.nextSibling()}return null}return Y.end=()=>{h&&_.ascend()},Y}a.advancer=E;function x(_,s,c){let h,U,Y,pe;for(h=U=_&&_.descendFirst(),Y=pe=s&&s.descendFirst();h||Y;){let Z=h?_.getKey():null,Ce=Y?s.getKey():null;Z!==null&&Ce!==null&&(t(Ce,Z)?Ce=null:Z!==Ce&&(Z=null)),c(Z==null?Ce:Z,Z!=null?_:null,Ce!=null?s:null),Z!=null&&h&&(h=_.nextSibling()),Ce!=null&&Y&&(Y=s.nextSibling())}U&&_.ascend(),pe&&s.ascend()}a.eachChildOf=x})(at)),at}var lt={},wt;function Ut(){return wt||(wt=1,(function(a){Object.defineProperty(a,"__esModule",{value:!0}),a.ConflictType=void 0,(function(e){e[e.RM_UNEXPECTED_CONTENT=1]="RM_UNEXPECTED_CONTENT",e[e.DROP_COLLISION=2]="DROP_COLLISION",e[e.BLACKHOLE=3]="BLACKHOLE"})(a.ConflictType||(a.ConflictType={}))})(lt)),lt}var ke={},ze={},It;function gt(){return It||(It=1,Object.defineProperty(ze,"__esModule",{value:!0}),ze.uniToStrPos=ze.strPosToUni=void 0,ze.strPosToUni=(a,e=a.length)=>{let n=0,t=0;for(;t<e;t++){const r=a.charCodeAt(t);r>=55296&&r<=57343&&(n++,t++)}if(t!==e)throw Error("Invalid offset - splits unicode bytes");return t-n},ze.uniToStrPos=(a,e)=>{let n=0;for(;e>0;e--){const t=a.charCodeAt(n);n+=t>=55296&&t<=57343?2:1}return n}),ze}var ut={},Ct;function ft(){return Ct||(Ct=1,(function(a){Object.defineProperty(a,"__esModule",{value:!0}),a.uniSlice=a.dlen=a.eachOp=void 0;const e=gt(),n=f=>{if(!Array.isArray(f))throw Error("Op must be an array of components");let y=null;for(let w=0;w<f.length;w++){const q=f[w];switch(typeof q){case"object":if(typeof q.d!="number"&&typeof q.d!="string")throw Error("Delete must be number or string");if(a.dlen(q.d)<=0)throw Error("Deletes must not be empty");break;case"string":if(!(q.length>0))throw Error("Inserts cannot be empty");break;case"number":if(!(q>0))throw Error("Skip components must be >0");if(typeof y=="number")throw Error("Adjacent skip components should be combined");break}y=q}if(typeof y=="number")throw Error("Op has a trailing skip")};function t(f,y){let w=0,q=0;for(let z=0;z<f.length;z++){const K=f[z];switch(y(K,w,q),typeof K){case"object":w+=a.dlen(K.d);break;case"string":q+=e.strPosToUni(K);break;case"number":w+=K,q+=K;break}}}a.eachOp=t;function r(f,y){const w=[],q=l(w);return t(f,(z,K,De)=>{q(y(z,K,De))}),s(w)}const u=f=>f,o=f=>r(f,u);a.dlen=f=>typeof f=="number"?f:e.strPosToUni(f);const l=f=>y=>{if(!(!y||y.d===0||y.d===""))if(f.length===0)f.push(y);else if(typeof y==typeof f[f.length-1])if(typeof y=="object"){const w=f[f.length-1];w.d=typeof w.d=="string"&&typeof y.d=="string"?w.d+y.d:a.dlen(w.d)+a.dlen(y.d)}else f[f.length-1]+=y;else f.push(y)},E=f=>typeof f=="number"?f:typeof f=="string"?e.strPosToUni(f):typeof f.d=="number"?f.d:e.strPosToUni(f.d);a.uniSlice=(f,y,w)=>{const q=e.uniToStrPos(f,y),z=w==null?1/0:e.uniToStrPos(f,w);return f.slice(q,z)};const x=(f,y,w)=>typeof f=="number"?w==null?f-y:Math.min(f,w)-y:a.uniSlice(f,y,w),_=f=>{let y=0,w=0;return{take:(K,De)=>{if(y===f.length)return K===-1?null:K;const de=f[y];let ne;if(typeof de=="number")return K===-1||de-w<=K?(ne=de-w,++y,w=0,ne):(w+=K,K);if(typeof de=="string"){if(K===-1||De==="i"||e.strPosToUni(de.slice(w))<=K)return ne=de.slice(w),++y,w=0,ne;{const le=w+e.uniToStrPos(de.slice(w),K);return ne=de.slice(w,le),w=le,ne}}else{if(K===-1||De==="d"||a.dlen(de.d)-w<=K)return ne={d:x(de.d,w)},++y,w=0,ne;{let le=x(de.d,w,w+K);return w+=K,{d:le}}}},peek:()=>f[y]}},s=f=>(f.length>0&&typeof f[f.length-1]=="number"&&f.pop(),f);function c(f,y,w){if(w!=="left"&&w!=="right")throw Error("side ("+w+") must be 'left' or 'right'");n(f),n(y);const q=[],z=l(q),{take:K,peek:De}=_(f);for(let ne=0;ne<y.length;ne++){const le=y[ne];let ye,Se;switch(typeof le){case"number":for(ye=le;ye>0;)Se=K(ye,"i"),z(Se),typeof Se!="string"&&(ye-=E(Se));break;case"string":w==="left"&&typeof De()=="string"&&z(K(-1)),z(e.strPosToUni(le));break;case"object":for(ye=a.dlen(le.d);ye>0;)switch(Se=K(ye,"i"),typeof Se){case"number":ye-=Se;break;case"string":z(Se);break;case"object":ye-=a.dlen(Se.d)}break}}let de;for(;de=K(-1);)z(de);return s(q)}function h(f,y){n(f),n(y);const w=[],q=l(w),{take:z}=_(f);for(let De=0;De<y.length;De++){const de=y[De];let ne,le;switch(typeof de){case"number":for(ne=de;ne>0;)le=z(ne,"d"),q(le),typeof le!="object"&&(ne-=E(le));break;case"string":q(de);break;case"object":ne=a.dlen(de.d);let ye=0;for(;ye<ne;)switch(le=z(ne-ye,"d"),typeof le){case"number":q({d:x(de.d,ye,ye+le)}),ye+=le;break;case"string":ye+=e.strPosToUni(le);break;case"object":q(le)}break}}let K;for(;K=z(-1);)q(K);return s(w)}const U=(f,y)=>{let w=0;for(let q=0;q<y.length&&f>w;q++){const z=y[q];switch(typeof z){case"number":{w+=z;break}case"string":const K=e.strPosToUni(z);w+=K,f+=K;break;case"object":f-=Math.min(a.dlen(z.d),f-w);break}}return f},Y=(f,y)=>typeof f=="number"?U(f,y):f.map(w=>U(w,y));function pe(f,y,w){return r(f,(q,z)=>typeof q=="object"&&typeof q.d=="number"?{d:w.slice(y,z,z+q.d)}:q)}function Z(f){return r(f,y=>{switch(typeof y){case"object":if(typeof y.d=="number")throw Error("Cannot invert text op: Deleted characters missing from operation. makeInvertible must be called first.");return y.d;case"string":return{d:y};case"number":return y}})}function Ce(f){return r(f,y=>typeof y=="object"&&typeof y.d=="string"?{d:e.strPosToUni(y.d)}:y)}function Ke(f){let y=!0;return t(f,w=>{typeof w=="object"&&typeof w.d=="number"&&(y=!1)}),y}function me(f){return{name:"text-unicode",uri:"http://sharejs.org/types/text-unicode",trim:s,normalize:o,checkOp:n,create(y=""){if(typeof y!="string")throw Error("Initial data must be a string");return f.create(y)},apply(y,w){n(w);const q=f.builder(y);for(let z=0;z<w.length;z++){const K=w[z];switch(typeof K){case"number":q.skip(K);break;case"string":q.append(K);break;case"object":q.del(a.dlen(K.d));break}}return q.build()},transform:c,compose:h,transformPosition:U,transformSelection:Y,isInvertible:Ke,makeInvertible(y,w){return pe(y,w,f)},stripInvertible:Ce,invert:Z,invertWithDoc(y,w){return Z(pe(y,w,f))},isNoop:y=>y.length===0}}a.default=me})(ut)),ut}var rt={},Et;function zt(){if(Et)return rt;Et=1,Object.defineProperty(rt,"__esModule",{value:!0});const a=ft(),e=gt();function n(t,r){return{get:t,getLength(){return t().length},insert(u,o,l){const E=e.strPosToUni(t(),u);return r([E,o],l)},remove(u,o,l){const E=e.strPosToUni(t(),u);return r([E,{d:o}],l)},_onOp(u){a.eachOp(u,(o,l,E)=>{switch(typeof o){case"string":this.onInsert&&this.onInsert(E,o);break;case"object":const x=a.dlen(o.d);this.onRemove&&this.onRemove(E,x)}})},onInsert:null,onRemove:null}}return rt.default=n,n.provides={text:!0},rt}var Dt;function Xt(){return Dt||(Dt=1,(function(a){var e=ke&&ke.__createBinding||(Object.create?(function(c,h,U,Y){Y===void 0&&(Y=U),Object.defineProperty(c,Y,{enumerable:!0,get:function(){return h[U]}})}):(function(c,h,U,Y){Y===void 0&&(Y=U),c[Y]=h[U]})),n=ke&&ke.__setModuleDefault||(Object.create?(function(c,h){Object.defineProperty(c,"default",{enumerable:!0,value:h})}):function(c,h){c.default=h}),t=ke&&ke.__importStar||function(c){if(c&&c.__esModule)return c;var h={};if(c!=null)for(var U in c)Object.hasOwnProperty.call(c,U)&&e(h,c,U);return n(h,c),h},r=ke&&ke.__importDefault||function(c){return c&&c.__esModule?c:{default:c}};Object.defineProperty(a,"__esModule",{value:!0}),a.type=a.remove=a.insert=void 0;const u=gt(),o=t(ft()),l=r(zt()),E={create(c){return c},toString(c){return c},builder(c){if(typeof c!="string")throw Error("Invalid document snapshot: "+c);const h=[];return{skip(U){let Y=u.uniToStrPos(c,U);if(Y>c.length)throw Error("The op is too long for this document");h.push(c.slice(0,Y)),c=c.slice(Y)},append(U){h.push(U)},del(U){c=c.slice(u.uniToStrPos(c,U))},build(){return h.join("")+c}}},slice:o.uniSlice},x=o.default(E),_=Object.assign(Object.assign({},x),{api:l.default});a.type=_,a.insert=(c,h)=>h.length===0?[]:c===0?[h]:[c,h],a.remove=(c,h)=>o.dlen(h)===0?[]:c===0?[{d:h}]:[c,{d:h}];var s=ft();Object.defineProperty(a,"makeType",{enumerable:!0,get:function(){return s.default}})})(ke)),ke}var St;function Jt(){return St||(St=1,(function(a){var e=Qe&&Qe.__importDefault||function(i){return i&&i.__esModule?i:{default:i}};Object.defineProperty(a,"__esModule",{value:!0}),a.editOp=a.replaceOp=a.insertOp=a.moveOp=a.removeOp=a.type=void 0;const n=e(Ft()),t=e(Vt()),r=jt(),u=Ut();function o(i,d){if(!i)throw new Error(d)}a.type={name:"json1",uri:"http://sharejs.org/types/JSONv1",readCursor:r.readCursor,writeCursor:r.writeCursor,create:i=>i,isNoop:i=>i==null,setDebug(i){},registerSubtype:Z,checkValidOp:z,normalize:K,apply:De,transformPosition:de,compose:ne,tryTransform:st,transform:xt,makeInvertible:Se,invert:le,invertWithDoc:Wt,RM_UNEXPECTED_CONTENT:u.ConflictType.RM_UNEXPECTED_CONTENT,DROP_COLLISION:u.ConflictType.DROP_COLLISION,BLACKHOLE:u.ConflictType.BLACKHOLE,transformNoConflict:(i,d,m)=>yt(()=>!0,i,d,m),typeAllowingConflictsPred:i=>Object.assign(Object.assign({},a.type),{transform:(d,m,D)=>yt(i,d,m,D)})};const l=i=>i?i.getComponent():null;function E(i){return i&&typeof i=="object"&&!Array.isArray(i)}const x=i=>Array.isArray(i)?i.slice():i!==null&&typeof i=="object"?Object.assign({},i):i,_=i=>i&&(i.p!=null||i.r!==void 0),s=i=>i&&(i.d!=null||i.i!==void 0);function c(i,d){return o(i!=null),typeof d=="number"?(o(Array.isArray(i),"Invalid key - child is not an array"),(i=i.slice()).splice(d,1)):(o(E(i),"Invalid key - child is not an object"),delete(i=Object.assign({},i))[d]),i}function h(i,d,m){return typeof d=="number"?(o(i!=null,"Container is missing for key"),o(Array.isArray(i),"Cannot use numerical key for object container"),o(i.length>=d,"Cannot insert into out of bounds index"),i.splice(d,0,m)):(o(E(i),"Cannot insert into missing item"),o(i[d]===void 0,"Trying to overwrite value at key. Your op needs to remove it first"),i[d]=m),m}a.removeOp=(i,d=!0)=>r.writeCursor().writeAtPath(i,"r",d).get(),a.moveOp=(i,d)=>r.writeCursor().writeMove(i,d).get(),a.insertOp=(i,d)=>r.writeCursor().writeAtPath(i,"i",d).get(),a.replaceOp=(i,d,m)=>r.writeCursor().at(i,D=>{D.write("r",d),D.write("i",m)}).get(),a.editOp=(i,d,m,D=!1)=>r.writeCursor().at(i,O=>y(O,d,m,D)).get();const U=(i,d)=>i!=null&&(typeof d=="number"?Array.isArray(i):typeof i=="object"),Y=(i,d)=>U(i,d)?i[d]:void 0,pe={};function Z(i){let d=i.type?i.type:i;d.name&&(pe[d.name]=d),d.uri&&(pe[d.uri]=d)}const Ce=i=>{const d=pe[i];if(d)return d;throw Error("Missing type: "+i)};Z(Xt());const Ke=(i,d)=>i+d;Z({name:"number",apply:Ke,compose:Ke,invert:i=>-i,transform:i=>i});const me=i=>i==null?null:i.et?Ce(i.et):i.es?pe["text-unicode"]:i.ena!=null?pe.number:null,f=i=>i.es?i.es:i.ena!=null?i.ena:i.e,y=(i,d,m,D=!1)=>{const[O,C]=typeof d=="string"?[Ce(d),d]:[d,d.name];!D&&O.isNoop&&O.isNoop(m)||(C==="number"?i.write("ena",m):C==="text-unicode"?i.write("es",m):(i.write("et",C),i.write("e",m)))};function w(i){o(typeof i=="number"),o(i>=0),o(i===(0|i))}function q(i){typeof i=="number"?w(i):o(typeof i=="string")}function z(i){if(i===null)return;const d=new Set,m=new Set,D=C=>{let W=!0,N=!1;for(let g in C){const v=C[g];if(W=!1,o(g==="p"||g==="r"||g==="d"||g==="i"||g==="e"||g==="es"||g==="ena"||g==="et","Invalid component item '"+g+"'"),g==="p")w(v),o(!d.has(v)),d.add(v),o(C.r===void 0);else if(g==="d")w(v),o(!m.has(v)),m.add(v),o(C.i===void 0);else if(g==="e"||g==="es"||g==="ena"){o(!N),N=!0;const b=me(C);o(b,"Missing type in edit"),b.checkValidOp&&b.checkValidOp(f(C))}}o(!W)},O=(C,W,N)=>{if(!Array.isArray(C))throw Error("Op must be null or a list");if(C.length===0)throw Error("Empty descent");W||q(C[0]);let g=1,v=0,b=0;for(let I=0;I<C.length;I++){const R=C[I];if(o(R!=null),Array.isArray(R)){const B=O(R,!1);if(v){const p=typeof b,M=typeof B;p===M?o(b<B,"descent keys are not in order"):o(p==="number"&&M==="string")}b=B,v++,g=3}else typeof R=="object"?(o(g===1,`Prev not scalar - instead ${g}`),D(R),g=2):(o(g!==3),q(R),o(r.isValidPathItem(R),"Invalid path key"),g=1)}return o(v!==1,"Operation makes multiple descents. Remove some []"),o(g===2||g===3),C[0]};O(i,!0),o(d.size===m.size,"Mismatched picks and drops in op");for(let C=0;C<d.size;C++)o(d.has(C)),o(m.has(C))}function K(i){let d=0,m=[];const D=r.writeCursor();return D.mergeTree(i,(O,C)=>{const W=me(O);if(W){const g=f(O);y(C,W,W.normalize?W.normalize(g):g)}for(const g of["r","p","i","d"])if(O[g]!==void 0){const v=g==="p"||g==="d"?(N=O[g],m[N]==null&&(m[N]=d++),m[N]):O[g];C.write(g,v)}var N}),D.get()}function De(i,d){if(z(d),d===null)return i;const m=[];return(function D(O,C){let W=O,N=0,g={root:O},v=0,b=g,I="root";function R(){for(;v<N;v++){let B=C[v];typeof B!="object"&&(o(U(b,I)),b=b[I]=x(b[I]),I=B)}}for(;N<C.length;N++){const B=C[N];if(Array.isArray(B)){const p=D(W,B);p!==W&&p!==void 0&&(R(),W=b[I]=p)}else if(typeof B=="object"){B.d!=null?(R(),W=h(b,I,m[B.d])):B.i!==void 0&&(R(),W=h(b,I,B.i));const p=me(B);if(p)R(),W=b[I]=p.apply(W,f(B));else if(B.e!==void 0)throw Error("Subtype "+B.et+" undefined")}else W=Y(W,B)}return g.root})(i=(function D(O,C){const W=[];let N=0;for(;N<C.length;N++){const I=C[N];if(Array.isArray(I))break;typeof I!="object"&&(W.push(O),O=Y(O,I))}for(let I=C.length-1;I>=N;I--)O=D(O,C[I]);for(--N;N>=0;N--){const I=C[N];if(typeof I!="object"){const R=W.pop();O=O===Y(R,I)?R:O===void 0?c(R,I):(v=I,b=O,(g=x(g=R))[v]=b,g)}else _(I)&&(o(O!==void 0,"Cannot pick up or remove undefined"),I.p!=null&&(m[I.p]=O),O=void 0)}var g,v,b;return O})(i,d),d)}function de(i,d){i=i.slice(),z(d);const m=r.readCursor(d);let D,O,C=!1;const W=[];for(let g=0;;g++){const v=i[g],b=m.getComponent();if(b&&(b.r!==void 0?C=!0:b.p!=null&&(C=!1,D=b.p,O=g)),g>=i.length)break;let I=0;const R=r.advancer(m,void 0,(p,M)=>{_(M)&&I++});W.unshift(R);const B=R(v);if(typeof v=="number"&&(i[g]-=I),!B)break}if(W.forEach(g=>g.end()),C)return null;const N=()=>{let g=0;if(D!=null){const v=m.getPath();g=v.length,i=v.concat(i.slice(O))}for(;g<i.length;g++){const v=i[g],b=l(m),I=me(b);if(I){const p=f(b);I.transformPosition&&(i[g]=I.transformPosition(i[g],p));break}let R=0;const B=r.advancer(m,(p,M)=>s(M)?~(p-R):p-R,(p,M)=>{s(M)&&R++})(v);if(typeof v=="number"&&(i[g]+=R),!B)break}};return D!=null?m.eachDrop(null,g=>{g===D&&N()}):N(),i}function ne(i,d){if(z(i),z(d),i==null)return d;if(d==null)return i;let m=0;const D=r.readCursor(i),O=r.readCursor(d),C=r.writeCursor(),W=[],N=[],g=[],v=[],b=[],I=[],R=new Set;D.traverse(null,p=>{p.p!=null&&(g[p.p]=D.clone())}),O.traverse(null,p=>{p.d!=null&&(v[p.d]=O.clone())});const B=r.writeCursor();return(function p(M,re,te,H,se,We,be,_e){o(re||te);const oe=l(re),Pe=l(te),Ae=!!Pe&&Pe.r!==void 0,He=!!oe&&oe.i!==void 0,Te=oe?oe.d:null,Ee=Pe?Pe.p:null,xe=(We||Ae)&&Ee==null;if(Ee!=null)H=v[Ee],be=N[Ee]=new r.WriteCursor;else if(Pe&&Pe.r!==void 0)H=null;else{const P=l(H);P&&P.d!=null&&(H=null)}const Q=l(H);if(Te!=null)if(M=g[Te],_e=W[Te]=new r.WriteCursor,xe)We&&!Ae&&_e.write("r",!0);else{const P=b[Te]=m++;be.write("d",P)}else if(oe&&oe.i!==void 0)M=null;else{const P=l(M);P&&P.p!=null&&(M=null)}let T;He?(o(se===void 0),T=oe.i):T=se;const G=(Ee==null?!He||We||Ae:T===void 0)?null:be.getComponent();if(Ee!=null){if(!(se!==void 0||He)){const P=Te!=null?b[Te]:m++;I[Ee]=P,_e.write("p",P)}}else Ae&&(He||se!==void 0||(Pe.r,_e.write("r",Pe.r)));const A=xe?null:me(oe),S=me(Q);if((A||S)&&(A&&A.name,S&&S.name),A&&S){o(A===S);const P=f(oe),L=f(Q),ge=A.compose(P,L);y(be,A,ge),R.add(Q)}else A?y(be,A,f(oe)):S&&(y(be,S,f(Q)),R.add(Q));const j=typeof T=="object"&&T!=null;let J=!1,X=0,ee=0,he=0,fe=0,ae=0;const Oe=r.advancer(H,(P,L)=>s(L)?fe-P-1:P-fe,(P,L)=>{s(L)&&fe++}),V=r.advancer(M,(P,L)=>_(L)?X-P-1:P-X,(P,L)=>{_(L)&&X++});if(r.eachChildOf(re,te,(P,L,ge)=>{let we,Be,$e=P,Me=P,Ze=P;if(typeof P=="number"){let ve=P+he;Be=Oe(ve),Me=ve+fe;let ue=P+ee;we=V(ue),s(l(Be))&&(we=null),$e=ue+X,Ze=P+ae,o($e>=0,"p1PickKey is negative"),o(Me>=0,"p2DropKey is negative");const je=s(l(L)),Ge=_(l(ge));(je||Ge&&!xe)&&ae--,je&&ee--,Ge&&he--}else we=V(P),Be=Oe(P);_e.descend($e),be.descend(Me);const Je=j&&!s(l(L))?T[Ze]:void 0,Ne=p(we,L,ge,Be,Je,xe,be,_e);var Re,k,ie;j&&!xe?Je!==Ne&&(J||(T=Array.isArray(T)?T.slice():Object.assign({},T),J=!0),Re=T,ie=Ne,typeof(k=Ze)=="number"?(o(Array.isArray(Re)),o(k<Re.length)):(o(!Array.isArray(Re)),o(Re[k]!==void 0)),ie===void 0?typeof k=="number"?Re.splice(k,1):delete Re[k]:Re[k]=ie):o(Ne===void 0),be.ascend(),_e.ascend()}),V.end(),Oe.end(),G!=null)G.i=T;else if(!We&&!Ae&&Ee==null)return T})(D,D.clone(),O,O.clone(),void 0,!1,C,B),C.reset(),C.mergeTree(B.get()),C.reset(),C.get(),W.map(p=>p.get()),N.map(p=>p.get()),D.traverse(C,(p,M)=>{const re=p.p;if(re!=null){const te=b[re];te!=null&&M.write("p",te);const H=W[re];H&&H.get(),H&&M.mergeTree(H.get())}else p.r!==void 0&&M.write("r",p.r)}),C.reset(),C.get(),O.traverse(C,(p,M)=>{const re=p.d;if(re!=null){const H=I[re];H!=null&&M.write("d",H);const se=N[re];se&&M.mergeTree(se.get())}else p.i!==void 0&&M.write("i",p.i);const te=me(p);te&&!R.has(p)&&y(M,te,f(p))}),C.get()}function le(i){if(i==null)return null;const d=new r.ReadCursor(i),m=new r.WriteCursor;let D;const O=[],C=[];return(function W(N,g,v){const b=N.getComponent();let I,R=!1;if(b){b.p!=null&&(g.write("d",b.p),O[b.p]=N.clone()),b.r!==void 0&&g.write("i",b.r),b.d!=null&&(g.write("p",b.d),v=void 0),b.i!==void 0&&(v=I=b.i);const p=me(b);p&&(v===void 0?(D||(D=new Set),D.add(b)):(f(b),v=p.apply(v,f(b)),R=!0))}let B=0;for(const p of N){g.descend(p);const M=typeof p=="number"?p-B:p,re=Y(v,M);s(N.getComponent())&&B++;const te=W(N,g,re);if(v!==void 0&&te!==void 0){if(R||(R=!0,v=x(v)),!U(v,M))throw Error("Cannot modify child - invalid operation");v[M]=te}g.ascend()}if(I===void 0)return R?v:void 0;g.write("r",v)})(d,m,void 0),D&&(m.reset(),(function W(N,g,v){const b=g.getComponent();if(b){const p=b.d;if(p!=null&&(N=O[p],v=C[p]=r.writeCursor()),D.has(b)){const M=me(b);if(!M.invert)throw Error(`Cannot invert subtype ${M.name}`);y(v,M,M.invert(f(b)))}}let I=0,R=0;const B=r.advancer(N,(p,M)=>_(M)?I-p-1:p-I,(p,M)=>{_(M)&&I++});for(const p of g)if(typeof p=="number"){const M=p-R,re=B(M),te=M+I;v.descend(te),W(re,g,v),s(g.getComponent())&&R++,v.ascend()}else v.descend(p),W(B(p),g,v),v.ascend();B.end()})(d.clone(),d,m),C.length&&(m.reset(),d.traverse(m,(W,N)=>{const g=W.p;if(g!=null){const v=C[g];v&&v.get(),v&&N.mergeTree(v.get())}}))),m.get()}const ye=(i,d)=>i.some(m=>typeof m=="object"&&(Array.isArray(m)?ye(m,d):d(m)));function Se(i,d){if(i==null||!ye(i,g=>{var v;return g.r!==void 0||((v=me(g))===null||v===void 0?void 0:v.makeInvertible)!=null}))return i;const m=new r.ReadCursor(i),D=new r.WriteCursor;let O=!1;const C=[],W=[],N=(g,v,b)=>{const I=g.getComponent();let R=!1;if(I){I.d!=null&&v.write("d",I.d),I.i!==void 0&&v.write("i",I.i);const p=I.p;if(p!=null&&(C[p]=g.clone(),o(b!==void 0,"Operation picks up at an invalid key"),W[p]=b,v.write("p",I.p)),I.r!==void 0&&b===void 0)throw Error("Invalid doc / op in makeInvertible: removed item missing from doc");const M=me(I);M&&(M.makeInvertible?O=!0:y(v,M,f(I),!0))}let B=0;for(const p of g){v.descend(p);const M=typeof p=="number"?p-B:p,re=Y(b,M),te=N(g,v,re);re!==te&&(R||(R=!0,b=x(b)),te===void 0?(b=c(b,M),typeof p=="number"&&B++):b[M]=te),v.ascend()}return I&&(I.r!==void 0?(v.write("r",t.default(b)),b=void 0):I.p!=null&&(b=void 0)),b};return N(m,D,d),D.get(),O&&(D.reset(),(function g(v,b,I,R,B){const p=b.getComponent();if(p){p.i!==void 0?(R=p.i,B=!0):p.d!=null&&(R=W[p.d],v=C[p.d],B=!1,p.d);let H=me(p);if(H&&H.makeInvertible){const se=f(p);y(I,H,H.makeInvertible(se,R),!0)}}let M=0,re=0;const te=r.advancer(v,(H,se)=>_(se)?M-H-1:H-M,(H,se)=>{_(se)&&M++});for(const H of b)if(typeof H=="number"){const se=H-re,We=te(se),be=se+M,_e=Y(R,B?se:be);I.descend(H),g(We,b,I,_e,B),s(b.getComponent())&&re++,I.ascend()}else{const se=Y(R,H);I.descend(H),g(te(H),b,I,se,B),I.ascend()}te.end()})(m.clone(),m,D,d,!1)),D.get()}function Wt(i,d){return le(Se(i,d))}const it=i=>{if(i==null)return null;const d=i.slice();for(let m=0;m<i.length;m++){const D=d[m];Array.isArray(D)&&(d[m]=it(D))}return d};function st(i,d,m){o(m==="left"||m==="right","Direction must be left or right");const D=m==="left"?0:1;if(d==null)return{ok:!0,result:i};z(i),z(d);let O=null;const C=[],W=[],N=[],g=[],v=[],b=[],I=[],R=[],B=[],p=[],M=[],re=[],te=[],H=[],se=[];let We=0;const be=r.readCursor(i),_e=r.readCursor(d),oe=r.writeCursor();if((function Q(T,G=null,A){const S=l(G);S&&(S.r!==void 0?A=G.clone():S.p!=null&&(A=null,b[S.p]=T.clone()));const j=T.getComponent();let J;j&&(J=j.p)!=null&&(v[J]=G?G.clone():null,N[J]=T.clone(),A&&(p[J]=!0,B[J]=A),S&&S.p!=null&&(H[J]=S.p));const X=r.advancer(G);for(const ee of T)Q(T,X(ee),A);X.end()})(_e,be,null),(function Q(T,G,A,S,j){const J=A.getComponent();let X,ee=!1;J&&((X=J.d)!=null?(g[X]=A.clone(),S!=null&&(se[S]==null&&(se[S]=[]),se[S].push(X)),p[X],T=v[X]||null,G=N[X]||null,p[X]?(j&&(M[X]=!0),j=B[X]||null):!j||D!==1&&H[X]!=null||O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(j.getPath()),op2:a.moveOp(G.getPath(),A.getPath())}),ee=!0):J.i!==void 0&&(T=G=null,ee=!0,j&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(j.getPath()),op2:a.insertOp(A.getPath(),J.i)})));const he=l(T);he&&(he.r!==void 0?j=T.clone():he.p!=null&&(he.p,S=he.p,j=null));const fe=me(J);fe&&j&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(j.getPath()),op2:a.editOp(A.getPath(),fe,f(J),!0)});let ae=0,Oe=0;const V=r.advancer(G,(L,ge)=>_(ge)?ae-L-1:L-ae,(L,ge)=>{_(ge)&&ae++}),P=r.advancer(T);for(const L of A)if(typeof L=="number"){const ge=L-Oe,we=V(ge);Oe+=+Q(P(ge+ae),we,A,S,j)}else{const ge=V(L);Q(P(L),ge,A,S,j)}return V.end(),P.end(),ee})(be,_e,_e.clone(),null,null),g.map(Q=>Q&&Q.get()),O)return{ok:!1,conflict:O};M.map(Q=>!!Q);const Pe=[];let Ae=null;(function Q(T,G,A,S,j){let J=!1;const X=l(G);if(_(X)){const V=X.p;V!=null?(A=g[V],S=re[V]=r.writeCursor(),J=!0,j=null):(A=null,j=G.clone())}else s(l(A))&&(A=null);const ee=T.getComponent();if(ee){const V=ee.p;V!=null?(j&&(R[V]=j),Pe[V]=j||D===1&&J?null:S.getComponent(),C[V]=T.clone(),A&&(I[V]=A.clone())):ee.r!==void 0&&(j||S.write("r",!0),(j||J)&&(Ae==null&&(Ae=new Set),Ae.add(ee)))}let he=0,fe=0;const ae=r.advancer(G,void 0,(V,P)=>{_(P)&&he++}),Oe=r.advancer(A,(V,P)=>s(P)?~(V-fe):V-fe,(V,P)=>{s(P)&&fe++});if(T)for(const V of T)if(typeof V=="string"){const P=ae(V),L=Oe(V);S.descend(V),Q(T,P,L,S,j),S.ascend()}else{const P=ae(V),L=V-he,ge=_(l(P))?null:Oe(L),we=L+fe;o(we>=0),S.descend(we),Q(T,P,ge,S,j),S.ascend()}ae.end(),Oe.end()})(be,_e,_e.clone(),oe,null),oe.reset();let He=[];if((function Q(T,G,A,S,j,J){o(G);const X=G.getComponent();let ee=l(S),he=!1;const fe=(k,ie,ve)=>k?a.moveOp(k.getPath(),ie.getPath()):a.insertOp(ie.getPath(),ve.i);if(s(X)){const k=X.d;k!=null&&(W[k]=G.clone());const ie=k!=null?Pe[k]:null;let ve=!1;if(X.i!==void 0||k!=null&&ie){let ue;ee&&(ee.i!==void 0||(ue=ee.d)!=null&&!p[ue])&&(ve=ue!=null?k!=null&&k===H[ue]:n.default(ee.i,X.i),ve||ue!=null&&D!==1&&H[ue]!=null||O==null&&(O={type:u.ConflictType.DROP_COLLISION,op1:fe(k!=null?C[k]:null,G,X),op2:fe(ue!=null?N[ue]:null,S,ee)})),ve||(J?O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:fe(k!=null?C[k]:null,G,X),op2:a.removeOp(J.getPath())}):(k!=null?(He[We]=k,j.write("d",ie.p=We++)):j.write("i",t.default(X.i)),he=!0))}else if(k!=null&&!ie){const ue=R[k];ue&&(J=ue.clone())}k!=null?(T=C[k],A=b[k],S=I[k]):X.i!==void 0&&(T=A=null,ve||(S=null))}else _(l(T))&&(T=A=S=null);const ae=l(T),Oe=l(A);if(_(Oe)){const k=Oe.p;Oe.r!==void 0&&(!ae||ae.r===void 0)||p[k]?(S=null,J=A.clone()):k!=null&&(S=g[k],D!==1&&H[k]!=null||((j=te[k])||(j=te[k]=r.writeCursor()),j.reset(),J=null))}else!s(X)&&s(ee)&&(S=null);ee=S!=null?S.getComponent():null;const V=me(X);if(V){const k=f(X);if(J)O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.editOp(G.getPath(),V,k,!0),op2:a.removeOp(J.getPath())});else{const ie=me(ee);let ve;if(ie){if(V!==ie)throw Error("Transforming incompatible types");const ue=f(ee);ve=V.transform(k,ue,m)}else ve=t.default(k);y(j,V,ve)}}let P=0,L=0,ge=0,we=0,Be=0,$e=0,Me=T!=null&&T.descendFirst(),Ze=Me;const Je=r.advancer(A,void 0,(k,ie)=>{_(ie)&&ge++});let Ne=S!=null&&S.descendFirst(),Re=Ne;for(const k of G)if(typeof k=="number"){let ie;const ve=s(G.getComponent()),ue=k-L;{let Le;for(;Me&&typeof(Le=T.getKey())=="number";){Le+=P;const Ue=T.getComponent(),Ye=_(Ue);if(Le>ue||Le===ue&&(!Ye||D===0&&ve))break;if(Ye){P--;const Fe=Ue.p;H.includes(Fe),Ue.d,l(te[Ue.d]),_(l(te[Ue.d])),(Ue.r===void 0||Ae&&Ae.has(Ue))&&(Fe==null||!Pe[Fe]||D!==1&&H.includes(Fe))||Be--}Me=T.nextSibling()}ie=Me&&Le===ue?T:null}const je=ue-P;let Ge=Je(je);const ot=je-ge;let et=null;{let Le,Ue;for(;Ne&&typeof(Le=S.getKey())=="number";){Ue=Le-we;const Ye=S.getComponent(),Fe=s(Ye);if(Ue>ot)break;if(Ue===ot){if(!Fe){et=S;break}{if(D===0&&ve){et=S;break}const qe=Ge&&_(Ge.getComponent());if(D===0&&qe)break}}if(Fe){const qe=Ye.d;p[qe],H[qe],Ye.i===void 0&&(p[qe]||H[qe]!=null&&D!==1)?(p[qe]||H[qe]!=null&&D===0)&&(we++,$e--):we++}Ne=S.nextSibling()}}const _t=ot+we+Be+$e;o(_t>=0,"trying to descend to a negative index"),j.descend(_t),ve&&(ie=Ge=et=null,L++),Q(ie,G,Ge,et,j,J)&&$e++,j.ascend()}else{let ie;for(;Me&&(ie=T.getKey(),typeof ie!="string"||!(ie>k||ie===k));)Me=T.nextSibling();const ve=Me&&ie===k?T:null,ue=Je(k);let je;for(;Ne&&(je=S.getKey(),typeof je!="string"||!(je>k||je===k));)Ne=S.nextSibling();const Ge=Ne&&je===k?S:null;j.descend(k),Q(ve,G,ue,Ge,j,J),j.ascend()}return Je.end(),Ze&&T.ascend(),Re&&S.ascend(),he})(be,be.clone(),_e,_e.clone(),oe,null),O)return{ok:!1,conflict:O};oe.reset();const Te=(Q,T,G)=>Q.traverse(T,(A,S)=>{A.d!=null&&G(A.d,Q,S)});(p.length||re.length)&&(Te(_e,oe,(Q,T,G)=>{p[Q]&&!M[Q]&&G.write("r",!0),re[Q]&&G.mergeTree(re[Q].get())}),oe.reset());const Ee=[],xe=[];if((te.length||p.length)&&!O){const Q=r.readCursor(it(oe.get()));if(Te(Q,null,(T,G)=>{Ee[T]=G.clone()}),te.forEach(T=>{T&&Te(r.readCursor(T.get()),null,(G,A)=>{Ee[G]=A.clone()})}),(function T(G,A,S,j,J,X){const ee=l(A);if(ee&&_(ee))if(ee.p!=null){const P=ee.p;Ee[P].getPath(),S=Ee[P],j=xe[P]=r.writeCursor()}else ee.r!==void 0&&(S=null);else s(l(S))&&(S=null);const he=G.getComponent();if(he){let P;if((P=he.d)!=null){const L=te[P];L&&(L.get(),j.mergeTree(L.get()),S=r.readCursor(L.get()))}}let fe=0,ae=0;const Oe=r.advancer(A,void 0,(P,L)=>{_(L)&&fe--}),V=r.advancer(S,(P,L)=>s(L)?-(P-ae)-1:P-ae,(P,L)=>{s(L)&&ae++});for(const P of G)if(typeof P=="number"){const L=Oe(P),ge=P+fe,we=V(ge),Be=ge+ae;j.descend(Be),T(G,L,we,j),j.ascend()}else j.descend(P),T(G,Oe(P),V(P),j),j.ascend();Oe.end(),V.end()})(_e,Q,Q.clone(),oe),oe.reset(),O)return{ok:!1,conflict:O};if(oe.get(),xe.length){const T=xe.map(A=>A?A.get():null),G=r.readCursor(it(oe.get()));if(Te(G,oe,(A,S,j)=>{const J=T[A];J&&(j.mergeTree(J),T[A]=null)}),T.find(A=>A)){const A=r.writeCursor(),S=r.writeCursor();let j=0,J=0;T.forEach(X=>{X!=null&&Te(r.readCursor(X),null,ee=>{const he=He[ee];A.writeMove(C[he].getPath(),W[he].getPath(),j++);const fe=se[he];fe&&fe.forEach(ae=>{p[ae]||D!==1&&H[ae]!=null||S.writeMove(N[ae].getPath(),g[ae].getPath(),J++)})})}),O={type:u.ConflictType.BLACKHOLE,op1:A.get(),op2:S.get()}}}}return O?{ok:!1,conflict:O}:{ok:!0,result:oe.get()}}const pt=i=>{const d=new Error("Transform detected write conflict");throw d.conflict=i,d.type=d.name="writeConflict",d};function xt(i,d,m){const D=st(i,d,m);if(D.ok)return D.result;pt(D.conflict)}const Xe=i=>{const d=r.writeCursor();return r.readCursor(i).traverse(d,(m,D)=>{(s(m)||me(m))&&D.write("r",!0)}),d.get()},Bt=(i,d)=>{const{type:m,op1:D,op2:O}=i;switch(m){case u.ConflictType.DROP_COLLISION:return d==="left"?[null,Xe(O)]:[Xe(D),null];case u.ConflictType.RM_UNEXPECTED_CONTENT:let C=!1;return r.readCursor(D).traverse(null,W=>{W.r!==void 0&&(C=!0)}),C?[null,Xe(O)]:[Xe(D),null];case u.ConflictType.BLACKHOLE:return[Xe(D),Xe(O)];default:throw Error("Unrecognised conflict: "+m)}};function yt(i,d,m,D){let O=null;for(;;){const C=st(d,m,D);if(C.ok)return ne(O,C.result);{const{conflict:W}=C;i(W)||pt(W);const[N,g]=Bt(W,D);d=ne(K(d),N),m=ne(K(m),g),O=ne(O,g)}}}})(Qe)),Qe}var Pt;function Yt(){return Pt||(Pt=1,(function(a){var e=Ve&&Ve.__createBinding||(Object.create?(function(u,o,l,E){E===void 0&&(E=l),Object.defineProperty(u,E,{enumerable:!0,get:function(){return o[l]}})}):(function(u,o,l,E){E===void 0&&(E=l),u[E]=o[l]})),n=Ve&&Ve.__exportStar||function(u,o){for(var l in u)l!=="default"&&!o.hasOwnProperty(l)&&e(o,u,l)};Object.defineProperty(a,"__esModule",{value:!0}),n(Jt(),a);var t=jt();Object.defineProperty(a,"ReadCursor",{enumerable:!0,get:function(){return t.ReadCursor}}),Object.defineProperty(a,"WriteCursor",{enumerable:!0,get:function(){return t.WriteCursor}});var r=Ut();Object.defineProperty(a,"ConflictType",{enumerable:!0,get:function(){return r.ConflictType}})})(Ve)),Ve}var F=Yt();class kt{constructor(){$(this,"drawingManagerData",{});$(this,"_oldDrawingManagerData",{});$(this,"_focusDrawings",[]);$(this,"_remove$",new Ie.Subject);$(this,"remove$",this._remove$.asObservable());$(this,"_add$",new Ie.Subject);$(this,"add$",this._add$.asObservable());$(this,"_update$",new Ie.Subject);$(this,"update$",this._update$.asObservable());$(this,"_order$",new Ie.Subject);$(this,"order$",this._order$.asObservable());$(this,"_group$",new Ie.Subject);$(this,"group$",this._group$.asObservable());$(this,"_ungroup$",new Ie.Subject);$(this,"ungroup$",this._ungroup$.asObservable());$(this,"_refreshTransform$",new Ie.Subject);$(this,"refreshTransform$",this._refreshTransform$.asObservable());$(this,"_visible$",new Ie.Subject);$(this,"visible$",this._visible$.asObservable());$(this,"_focus$",new Ie.Subject);$(this,"focus$",this._focus$.asObservable());$(this,"_featurePluginUpdate$",new Ie.Subject);$(this,"featurePluginUpdate$",this._featurePluginUpdate$.asObservable());$(this,"_featurePluginAdd$",new Ie.Subject);$(this,"featurePluginAdd$",this._featurePluginAdd$.asObservable());$(this,"_featurePluginRemove$",new Ie.Subject);$(this,"featurePluginRemove$",this._featurePluginRemove$.asObservable());$(this,"_featurePluginOrderUpdate$",new Ie.Subject);$(this,"featurePluginOrderUpdate$",this._featurePluginOrderUpdate$.asObservable());$(this,"_featurePluginGroupUpdate$",new Ie.Subject);$(this,"featurePluginGroupUpdate$",this._featurePluginGroupUpdate$.asObservable());$(this,"_featurePluginUngroupUpdate$",new Ie.Subject);$(this,"featurePluginUngroupUpdate$",this._featurePluginUngroupUpdate$.asObservable());$(this,"_visible",!0);$(this,"_editable",!0)}dispose(){this._remove$.complete(),this._add$.complete(),this._update$.complete(),this._order$.complete(),this._focus$.complete(),this._featurePluginUpdate$.complete(),this._featurePluginAdd$.complete(),this._featurePluginRemove$.complete(),this._featurePluginOrderUpdate$.complete(),this.drawingManagerData={},this._oldDrawingManagerData={}}visibleNotification(e){this._visible$.next(e)}refreshTransform(e){e.forEach(n=>{const t=this._getCurrentBySearch(n);t!=null&&(t.transform=n.transform,t.transforms=n.transforms,t.isMultiTransform=n.isMultiTransform)}),this.refreshTransformNotification(e)}getDrawingDataForUnit(e){return this.drawingManagerData[e]||{}}removeDrawingDataForUnit(e){const n=this.drawingManagerData[e];if(n==null)return;delete this.drawingManagerData[e];const t=[];Object.keys(n).forEach(r=>{const u=n[r];(u==null?void 0:u.data)!=null&&Object.keys(u.data).forEach(o=>{t.push({unitId:e,subUnitId:r,drawingId:o})})}),t.length>0&&this.removeNotification(t)}registerDrawingData(e,n){this.drawingManagerData[e]=n}initializeNotification(e){const n=[],t=this.drawingManagerData[e];t!=null&&(Object.keys(t).forEach(r=>{this._establishDrawingMap(e,r);const u=t[r];Object.keys(u.data).forEach(o=>{const l=u.data[o];l.unitId=e,l.subUnitId=r,n.push(l)})}),n.length>0&&this.addNotification(n))}getDrawingData(e,n){return this._getDrawingData(e,n)}setDrawingData(e,n,t){this.drawingManagerData[e][n].data=t}getBatchAddOp(e){const n=[],t=[],r=[];e.forEach(x=>{const{op:_,invertOp:s}=this._addByParam(x);n.push({unitId:x.unitId,subUnitId:x.subUnitId,drawingId:x.drawingId}),t.push(_),r.push(s)});const u=t.reduce(F.type.compose,null),o=r.reduce(F.type.compose,null),{unitId:l,subUnitId:E}=e[0];return{undo:o,redo:u,unitId:l,subUnitId:E,objects:n}}getBatchRemoveOp(e){const n=[],t=[];e.forEach(E=>{const{op:x,invertOp:_}=this._removeByParam(E);n.unshift(x),t.push(_)});const r=n.reduce(F.type.compose,null),u=t.reduce(F.type.compose,null),{unitId:o,subUnitId:l}=e[0];return{undo:u,redo:r,unitId:o,subUnitId:l,objects:e}}getBatchUpdateOp(e){const n=[],t=[],r=[];e.forEach(x=>{const{op:_,invertOp:s}=this._updateByParam(x);n.push({unitId:x.unitId,subUnitId:x.subUnitId,drawingId:x.drawingId}),t.push(_),r.push(s)});const u=t.reduce(F.type.compose,null),o=r.reduce(F.type.compose,null),{unitId:l,subUnitId:E}=e[0];return{undo:o,redo:u,unitId:l,subUnitId:E,objects:n}}removeNotification(e){this._remove$.next(e)}addNotification(e){this._add$.next(e)}updateNotification(e){this._update$.next(e)}orderNotification(e){this._order$.next(e)}groupUpdateNotification(e){this._group$.next(e)}ungroupUpdateNotification(e){this._ungroup$.next(e)}refreshTransformNotification(e){this._refreshTransform$.next(e)}getGroupDrawingOp(e){const n=[],{unitId:t,subUnitId:r}=e[0].parent;e.forEach(l=>{n.push(this._getGroupDrawingOp(l))});const u=n.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(u,this.drawingManagerData),redo:u,unitId:t,subUnitId:r,objects:e}}getUngroupDrawingOp(e){const n=[],{unitId:t,subUnitId:r}=e[0].parent;e.forEach(l=>{n.push(this._getUngroupDrawingOp(l))});const u=n.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(u,this.drawingManagerData),redo:u,unitId:t,subUnitId:r,objects:e}}getDrawingsByGroup(e){const{unitId:n,subUnitId:t,drawingId:r}=e;if(this.getDrawingByParam({unitId:n,subUnitId:t,drawingId:r})==null)return[];const o=this._getDrawingData(n,t),l=[];return Object.keys(o).forEach(E=>{const x=o[E];x.groupId===r&&l.push(x)}),l}_getGroupDrawingOp(e){const{parent:n,children:t}=e,{unitId:r,subUnitId:u,drawingId:o}=n,l=[];l.push(F.insertOp([r,u,"data",o],n));let E=Number.NEGATIVE_INFINITY;return t.forEach(x=>{const{unitId:_,subUnitId:s,drawingId:c}=x,h=this._hasDrawingOrder({unitId:_,subUnitId:s,drawingId:c});E=Math.max(E,h),l.push(...this._getUpdateParamCompareOp(x,this.getDrawingByParam({unitId:_,subUnitId:s,drawingId:c})))}),E===Number.NEGATIVE_INFINITY&&(E=this._getDrawingOrder(r,u).length),l.push(F.insertOp([r,u,"order",E],o)),l.reduce(F.type.compose,null)}_getUngroupDrawingOp(e){const{parent:n,children:t}=e,{unitId:r,subUnitId:u,drawingId:o}=n,l=[];return t.forEach(E=>{const{unitId:x,subUnitId:_,drawingId:s}=E;l.push(...this._getUpdateParamCompareOp(E,this.getDrawingByParam({unitId:x,subUnitId:_,drawingId:s})))}),l.push(F.removeOp([r,u,"data",o],!0)),l.push(F.removeOp([r,u,"order",this._getDrawingOrder(r,u).indexOf(o)],!0)),l.reduce(F.type.compose,null)}applyJson1(e,n,t){this._establishDrawingMap(e,n),this._oldDrawingManagerData={...this.drawingManagerData},this.drawingManagerData=F.type.apply(this.drawingManagerData,t)}featurePluginUpdateNotification(e){this._featurePluginUpdate$.next(e)}featurePluginOrderUpdateNotification(e){this._featurePluginOrderUpdate$.next(e)}featurePluginAddNotification(e){this._featurePluginAdd$.next(e)}featurePluginRemoveNotification(e){this._featurePluginRemove$.next(e)}featurePluginGroupUpdateNotification(e){this._featurePluginGroupUpdate$.next(e)}featurePluginUngroupUpdateNotification(e){this._featurePluginUngroupUpdate$.next(e)}getDrawingByParam(e){return this._getCurrentBySearch(e)}getOldDrawingByParam(e){return this._getOldBySearch(e)}getDrawingOKey(e){const[n,t,r]=e.split("#-#");return this._getCurrentBySearch({unitId:n,subUnitId:t,drawingId:r})}focusDrawing(e){if(e==null||e.length===0){this._focusDrawings=[],this._focus$.next([]);return}const n=[];e.forEach(t=>{var E;const{unitId:r,subUnitId:u,drawingId:o}=t,l=(E=this._getDrawingData(r,u))==null?void 0:E[o];l!=null&&n.push(l)}),n.length>0&&(this._focusDrawings=n,this._focus$.next(n))}getFocusDrawings(){const e=[];return this._focusDrawings.forEach(n=>{var l;const{unitId:t,subUnitId:r,drawingId:u}=n,o=(l=this._getDrawingData(t,r))==null?void 0:l[u];o!=null&&e.push(o)}),e}getDrawingOrder(e,n){return this._getDrawingOrder(e,n)}setDrawingOrder(e,n,t){this.drawingManagerData[e][n].order=t}orderUpdateNotification(e){this._order$.next(e)}getForwardDrawingsOp(e){const{unitId:n,subUnitId:t,drawingIds:r}=e,u=[],o=this.getDrawingOrder(n,t),l=[...r];r.forEach(_=>{const s=this._hasDrawingOrder({unitId:n,subUnitId:t,drawingId:_});if(s===-1||s===o.length-1)return;const c=F.moveOp([n,t,"order",s],[n,t,"order",s+1]);u.push(c),l.includes(o[s+1])||l.push(o[s+1])});const E=u.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(E,this.drawingManagerData),redo:E,unitId:n,subUnitId:t,objects:{...e,drawingIds:l}}}getBackwardDrawingOp(e){const{unitId:n,subUnitId:t,drawingIds:r}=e,u=[],o=this.getDrawingOrder(n,t),l=[...r];r.forEach(_=>{const s=this._hasDrawingOrder({unitId:n,subUnitId:t,drawingId:_});if(s===-1||s===0)return;const c=F.moveOp([n,t,"order",s],[n,t,"order",s-1]);u.push(c),l.includes(o[s-1])||l.push(o[s-1])});const E=u.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(E,this.drawingManagerData),redo:E,unitId:n,subUnitId:t,objects:{...e,drawingIds:l}}}getFrontDrawingsOp(e){const{unitId:n,subUnitId:t,drawingIds:r}=e,u=this._getOrderFromSearchParams(n,t,r),o=[...r],l=this.getDrawingOrder(n,t),E=[];u.forEach(s=>{const{drawingId:c}=s,h=this._getDrawingCount(n,t)-1,U=F.moveOp([n,t,"order",this._getDrawingOrder(n,t).indexOf(c)],[n,t,"order",h]);E.push(U),o.includes(l[h])||o.push(l[h])});const x=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(x,this.drawingManagerData),redo:x,unitId:n,subUnitId:t,objects:{...e,drawingIds:o}}}getBackDrawingsOp(e){const{unitId:n,subUnitId:t,drawingIds:r}=e,u=this._getOrderFromSearchParams(n,t,r,!0),o=[...r],l=this.getDrawingOrder(n,t),E=[];u.forEach(s=>{const{drawingId:c}=s,h=F.moveOp([n,t,"order",this._getDrawingOrder(n,t).indexOf(c)],[n,t,"order",0]);E.push(h),o.includes(l[0])||o.push(l[0])});const x=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(x,this.drawingManagerData),redo:x,unitId:n,subUnitId:t,objects:{...e,drawingIds:o}}}_getDrawingCount(e,n){return this.getDrawingOrder(e,n).length||0}_getOrderFromSearchParams(e,n,t,r=!1){return t.map(u=>{const o=this._hasDrawingOrder({unitId:e,subUnitId:n,drawingId:u});return{drawingId:u,zIndex:o}}).sort(r===!1?ce.sortRules:ce.sortRulesByDesc)}_hasDrawingOrder(e){if(e==null)return-1;const{unitId:n,subUnitId:t,drawingId:r}=e;return this._establishDrawingMap(n,t),this._getDrawingOrder(n,t).indexOf(r)}_getCurrentBySearch(e){var u,o,l;if(e==null)return;const{unitId:n,subUnitId:t,drawingId:r}=e;return(l=(o=(u=this.drawingManagerData[n])==null?void 0:u[t])==null?void 0:o.data)==null?void 0:l[r]}_getOldBySearch(e){var u,o,l;if(e==null)return;const{unitId:n,subUnitId:t,drawingId:r}=e;return(l=(o=(u=this._oldDrawingManagerData[n])==null?void 0:u[t])==null?void 0:o.data)==null?void 0:l[r]}_establishDrawingMap(e,n,t){var r;return this.drawingManagerData[e]||(this.drawingManagerData[e]={}),this.drawingManagerData[e][n]||(this.drawingManagerData[e][n]={data:{},order:[]}),t==null?null:(r=this.drawingManagerData[e][n].data)==null?void 0:r[t]}_addByParam(e){const{unitId:n,subUnitId:t,drawingId:r}=e;this._establishDrawingMap(n,t,r);const u=F.insertOp([n,t,"data",r],e),o=F.insertOp([n,t,"order",this._getDrawingOrder(n,t).length],r),l=[u,o].reduce(F.type.compose,null),E=F.type.invertWithDoc(l,this.drawingManagerData);return{op:l,invertOp:E}}_removeByParam(e){if(e==null)return{op:[],invertOp:[]};const{unitId:n,subUnitId:t,drawingId:r}=e;if(this._establishDrawingMap(n,t,r)==null)return{op:[],invertOp:[]};const o=F.removeOp([n,t,"data",r],!0),l=F.removeOp([n,t,"order",this._getDrawingOrder(n,t).indexOf(r)],!0),E=[o,l].reduce(F.type.compose,null),x=F.type.invertWithDoc(E,this.drawingManagerData);return{op:E,invertOp:x}}_updateByParam(e){const{unitId:n,subUnitId:t,drawingId:r}=e,u=this._establishDrawingMap(n,t,r);if(u==null)return{op:[],invertOp:[]};const l=this._getUpdateParamCompareOp(e,u).reduce(F.type.compose,null),E=F.type.invertWithDoc(l,this.drawingManagerData);return{op:l,invertOp:E}}_getUpdateParamCompareOp(e,n){const{unitId:t,subUnitId:r,drawingId:u}=e,o=[];return Object.keys(e).forEach(l=>{const E=e[l],x=n[l];x!==E&&o.push(F.replaceOp([t,r,"data",u,l],x,E))}),o}_getDrawingData(e,n){var t,r;return((r=(t=this.drawingManagerData[e])==null?void 0:t[n])==null?void 0:r.data)||{}}_getDrawingOrder(e,n){var t,r;return((r=(t=this.drawingManagerData[e])==null?void 0:t[n])==null?void 0:r.order)||[]}getDrawingVisible(){return this._visible}getDrawingEditable(){return this._editable}setDrawingVisible(e){this._visible=e}setDrawingEditable(e){this._editable=e}}class Nt extends kt{}class Rt{constructor(){$(this,"_waitCount",0);$(this,"_change$",new Ie.Subject);$(this,"change$",this._change$);$(this,"_imageSourceCache",new Map)}setWaitCount(e){this._waitCount=e,this._change$.next(e)}getImageSourceCache(e,n){if(n===ce.ImageSourceType.BASE64){const t=new Image;return t.src=e,t}return this._imageSourceCache.get(e)}addImageSourceCache(e,n,t){n===ce.ImageSourceType.BASE64||t==null||this._imageSourceCache.set(e,t)}async getImage(e){return Promise.resolve(e)}async saveImage(e){return new Promise((n,t)=>{if(!At.includes(e.type)){t(new Error(ce.ImageUploadStatusType.ERROR_IMAGE_TYPE)),this._decreaseWaiting();return}if(e.size>Tt){t(new Error(ce.ImageUploadStatusType.ERROR_EXCEED_SIZE)),this._decreaseWaiting();return}const r=new FileReader;r.readAsDataURL(e),r.onload=u=>{var E;const o=(E=u.target)==null?void 0:E.result;if(o==null){t(new Error(ce.ImageUploadStatusType.ERROR_IMAGE)),this._decreaseWaiting();return}const l=ce.generateRandomId(6);n({imageId:l,imageSourceType:ce.ImageSourceType.BASE64,source:o,base64Cache:o,status:ce.ImageUploadStatusType.SUCCUSS}),this._decreaseWaiting()}})}_decreaseWaiting(){this._waitCount-=1,this._change$.next(this._waitCount)}}var Zt=Object.getOwnPropertyDescriptor,Qt=(a,e,n,t)=>{for(var r=t>1?void 0:t?Zt(e,n):e,u=a.length-1,o;u>=0;u--)(o=a[u])&&(r=o(r)||r);return r},ct=(a,e)=>(n,t)=>e(n,t,a);const en="UNIVER_DRAWING_PLUGIN";var dt;exports.UniverDrawingPlugin=(dt=class extends ce.Plugin{constructor(e=vt,n,t,r){super(),this._config=e,this._injector=n,this._configService=t,this._commandService=r;const{...u}=ce.merge({},vt,this._config);this._configService.setConfig($t,u)}onStarting(){this._initCommands(),this._initDependencies()}_initDependencies(){var t;const e=[[ce.IImageIoService,{useClass:Rt}],[ht,{useClass:Nt}]];ce.mergeOverrideWithDependencies(e,(t=this._config)==null?void 0:t.override).forEach(r=>this._injector.add(r))}_initCommands(){[Mt].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}},$(dt,"pluginName",en),dt);exports.UniverDrawingPlugin=Qt([ct(1,ce.Inject(ce.Injector)),ct(2,ce.IConfigService),ct(3,ce.ICommandService)],exports.UniverDrawingPlugin);function tn({unitId:a,subUnitId:e,drawingId:n},t){return typeof t=="number"?`${a}#-#${e}#-#${n}#-#${t}`:`${a}#-#${e}#-#${n}`}const nn=async a=>new Promise((e,n)=>{const t=new Image;t.src=a,t.onload=()=>{e({width:t.width,height:t.height,image:t})},t.onerror=r=>{n(r)}});Object.defineProperty(exports,"IImageIoService",{enumerable:!0,get:()=>ce.IImageIoService});Object.defineProperty(exports,"ImageSourceType",{enumerable:!0,get:()=>ce.ImageSourceType});Object.defineProperty(exports,"ImageUploadStatusType",{enumerable:!0,get:()=>ce.ImageUploadStatusType});exports.DRAWING_IMAGE_ALLOW_IMAGE_LIST=At;exports.DRAWING_IMAGE_ALLOW_SIZE=Tt;exports.DRAWING_IMAGE_COUNT_LIMIT=Ht;exports.DRAWING_IMAGE_HEIGHT_LIMIT=Kt;exports.DRAWING_IMAGE_WIDTH_LIMIT=qt;exports.DrawingManagerService=Nt;exports.IDrawingManagerService=ht;exports.ImageIoService=Rt;exports.SetDrawingSelectedOperation=Mt;exports.UnitDrawingService=kt;exports.getDrawingShapeKeyByDrawingSearch=tn;exports.getImageSize=nn;
1
+ "use strict";var Lt=Object.defineProperty;var qt=(a,e,t)=>e in a?Lt(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var H=(a,e,t)=>qt(a,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const ie=require("@univerjs/core"),Ie=require("rxjs"),Kt=500,$t=500,Ht=10,Tt=5*1024*1024,At=["image/png","image/jpeg","image/jpg","image/gif","image/bmp"],ht=ie.createIdentifier("univer.drawing-manager.service"),Mt={id:"drawing.operation.set-drawing-selected",type:ie.CommandType.OPERATION,handler:(a,e)=>{const t=a.get(ht);return e==null?!1:(t.focusDrawing(e),!0)}},Ft="drawing.config",mt={};var Ve={},Qe={},tt={},vt;function Vt(){if(vt)return tt;vt=1,Object.defineProperty(tt,"__esModule",{value:!0});function a(n,r){if(Array.isArray(r))return!1;for(let u in n)if(!t(n[u],r[u]))return!1;for(let u in r)if(n[u]===void 0)return!1;return!0}function e(n,r){if(!Array.isArray(r)||n.length!==r.length)return!1;for(let u=0;u<n.length;u++)if(!t(n[u],r[u]))return!1;return!0}function t(n,r){return n===r?!0:n===null||r===null||typeof n!="object"||typeof r!="object"?!1:Array.isArray(n)?e(n,r):a(n,r)}return tt.default=t,tt}var nt={},Ot;function zt(){if(Ot)return nt;Ot=1,Object.defineProperty(nt,"__esModule",{value:!0});function a(e){if(e===null)return null;if(Array.isArray(e))return e.map(a);if(typeof e=="object"){const t={};for(let n in e)t[n]=a(e[n]);return t}else return e}return nt.default=a,nt}var at={},wt;function Ut(){return wt||(wt=1,(function(a){Object.defineProperty(a,"__esModule",{value:!0}),a.eachChildOf=a.advancer=a.readCursor=a.writeCursor=a.WriteCursor=a.ReadCursor=a.isValidPathItem=void 0;function e(_,s){if(!_)throw new Error(s)}const t=_=>_!=null&&typeof _=="object"&&!Array.isArray(_),n=(_,s)=>typeof _==typeof s?_>s:typeof _=="string"&&typeof s=="number";function r(_,s){for(let c in _){const h=c;s.write(h,_[h])}}a.isValidPathItem=_=>typeof _=="number"||typeof _=="string"&&_!=="__proto__";class u{constructor(s=null){this.parents=[],this.indexes=[],this.lcIdx=-1,this.idx=-1,this.container=s}ascend(){e(this.parents.length===this.indexes.length/2),this.idx===0?this.parents.length?(this.lcIdx=this.indexes.pop(),this.container=this.parents.pop(),this.idx=this.indexes.pop()):(this.lcIdx=0,this.idx=-1):(e(this.idx>0),this.idx--,t(this.container[this.idx])&&this.idx--)}getPath(){const s=[];let c=this.container,h=this.parents.length-1,j=this.idx;for(;j>=0;)s.unshift(c[j]),j===0?(j=this.indexes[h*2],c=this.parents[h--]):j-=t(c[j-1])?2:1;return s}}class o extends u{get(){return this.container?this.container.slice(this.idx+1):null}getKey(){return e(this.container!=null,"Invalid call to getKey before cursor descended"),this.container[this.idx]}getComponent(){let s;return this.container&&this.container.length>this.idx+1&&t(s=this.container[this.idx+1])?s:null}descendFirst(){let s=this.idx+1;if(!this.container||s>=this.container.length||t(this.container[s])&&s+1>=this.container.length)return!1;t(this.container[s])&&s++;const c=this.container[s];return Array.isArray(c)?(this.indexes.push(this.idx),this.parents.push(this.container),this.indexes.push(s),this.idx=0,this.container=c):this.idx=s,!0}nextSibling(){if(e(this.parents.length===this.indexes.length/2),this.idx>0||this.parents.length===0)return!1;const s=this.indexes[this.indexes.length-1]+1,c=this.parents[this.parents.length-1];return s>=c.length?!1:(e(!isNaN(s)),this.indexes[this.indexes.length-1]=s,this.container=c[s],!0)}_init(s,c,h,j){this.container=s,this.idx=c,this.parents=h.slice(),this.indexes=j.slice()}clone(){const s=new o;return s._init(this.container,this.idx,this.parents,this.indexes),s}*[Symbol.iterator](){if(this.descendFirst()){do yield this.getKey();while(this.nextSibling());this.ascend()}}traverse(s,c){const h=this.getComponent();h&&c(h,s);for(const j of this)s&&s.descend(j),this.traverse(s,c),s&&s.ascend()}eachPick(s,c){this.traverse(s,(h,j)=>{h.p!=null&&c(h.p,j)})}eachDrop(s,c){this.traverse(s,(h,j)=>{h.d!=null&&c(h.d,j)})}}a.ReadCursor=o;class l extends u{constructor(s=null){super(s),this.pendingDescent=[],this._op=s}flushDescent(){e(this.parents.length===this.indexes.length/2),this.container===null&&(this._op=this.container=[]);for(let s=0;s<this.pendingDescent.length;s++){const c=this.pendingDescent[s];let h=this.idx+1;if(h<this.container.length&&t(this.container[h])&&h++,e(h===this.container.length||!t(this.container[h])),h===this.container.length)this.container.push(c),this.idx=h;else if(this.container[h]===c)this.idx=h;else{if(!Array.isArray(this.container[h])){const j=this.container.splice(h,this.container.length-h);this.container.push(j),this.lcIdx>-1&&(this.lcIdx=h)}for(this.indexes.push(this.idx),this.parents.push(this.container),this.lcIdx!==-1&&(e(n(c,this.container[this.lcIdx][0])),h=this.lcIdx+1,this.lcIdx=-1);h<this.container.length&&n(c,this.container[h][0]);)h++;if(this.indexes.push(h),this.idx=0,h<this.container.length&&this.container[h][0]===c)this.container=this.container[h];else{const j=[c];this.container.splice(h,0,j),this.container=j}}}this.pendingDescent.length=0}reset(){this.lcIdx=-1}getComponent(){this.flushDescent();const s=this.idx+1;if(s<this.container.length&&t(this.container[s]))return this.container[s];{const c={};return this.container.splice(s,0,c),c}}write(s,c){const h=this.getComponent();e(h[s]==null||h[s]===c,"Internal consistency error: Overwritten component. File a bug"),h[s]=c}get(){return this._op}descend(s){if(!a.isValidPathItem(s))throw Error("Invalid JSON key");this.pendingDescent.push(s)}descendPath(s){return this.pendingDescent.push(...s),this}ascend(){this.pendingDescent.length?this.pendingDescent.pop():super.ascend()}mergeTree(s,c=r){if(s===null)return;if(e(Array.isArray(s)),s===this._op)throw Error("Cannot merge into my own tree");const h=this.lcIdx,j=this.parents.length;let Y=0;for(let pe=0;pe<s.length;pe++){const Z=s[pe];typeof Z=="string"||typeof Z=="number"?(Y++,this.descend(Z)):Array.isArray(Z)?this.mergeTree(Z,c):typeof Z=="object"&&c(Z,this)}for(;Y--;)this.ascend();this.lcIdx=this.parents.length===j?h:-1}at(s,c){this.descendPath(s),c(this);for(let h=0;h<s.length;h++)this.ascend();return this}writeAtPath(s,c,h){return this.at(s,()=>this.write(c,h)),this.reset(),this}writeMove(s,c,h=0){return this.writeAtPath(s,"p",h).writeAtPath(c,"d",h)}getPath(){const s=super.getPath();return s.push(...this.pendingDescent),s}}a.WriteCursor=l,a.writeCursor=()=>new l,a.readCursor=_=>new o(_);function D(_,s,c){let h,j;j=h=_?_.descendFirst():!1;function Y(pe){let Z;for(;j;){const Ce=Z=_.getKey();if(pe!=null){let Ke=!1;if(s&&typeof Ce=="number"&&(Z=s(Ce,_.getComponent()),Z<0&&(Z=~Z,Ke=!0)),n(Z,pe))return null;if(Z===pe&&!Ke)return _}c&&typeof Z=="number"&&c(Z,_.getComponent()),j=_.nextSibling()}return null}return Y.end=()=>{h&&_.ascend()},Y}a.advancer=D;function x(_,s,c){let h,j,Y,pe;for(h=j=_&&_.descendFirst(),Y=pe=s&&s.descendFirst();h||Y;){let Z=h?_.getKey():null,Ce=Y?s.getKey():null;Z!==null&&Ce!==null&&(n(Ce,Z)?Ce=null:Z!==Ce&&(Z=null)),c(Z==null?Ce:Z,Z!=null?_:null,Ce!=null?s:null),Z!=null&&h&&(h=_.nextSibling()),Ce!=null&&Y&&(Y=s.nextSibling())}j&&_.ascend(),pe&&s.ascend()}a.eachChildOf=x})(at)),at}var lt={},bt;function jt(){return bt||(bt=1,(function(a){Object.defineProperty(a,"__esModule",{value:!0}),a.ConflictType=void 0,(function(e){e[e.RM_UNEXPECTED_CONTENT=1]="RM_UNEXPECTED_CONTENT",e[e.DROP_COLLISION=2]="DROP_COLLISION",e[e.BLACKHOLE=3]="BLACKHOLE"})(a.ConflictType||(a.ConflictType={}))})(lt)),lt}var ke={},ze={},It;function gt(){return It||(It=1,Object.defineProperty(ze,"__esModule",{value:!0}),ze.uniToStrPos=ze.strPosToUni=void 0,ze.strPosToUni=(a,e=a.length)=>{let t=0,n=0;for(;n<e;n++){const r=a.charCodeAt(n);r>=55296&&r<=57343&&(t++,n++)}if(n!==e)throw Error("Invalid offset - splits unicode bytes");return n-t},ze.uniToStrPos=(a,e)=>{let t=0;for(;e>0;e--){const n=a.charCodeAt(t);t+=n>=55296&&n<=57343?2:1}return t}),ze}var ut={},Ct;function ft(){return Ct||(Ct=1,(function(a){Object.defineProperty(a,"__esModule",{value:!0}),a.uniSlice=a.dlen=a.eachOp=void 0;const e=gt(),t=f=>{if(!Array.isArray(f))throw Error("Op must be an array of components");let y=null;for(let b=0;b<f.length;b++){const q=f[b];switch(typeof q){case"object":if(typeof q.d!="number"&&typeof q.d!="string")throw Error("Delete must be number or string");if(a.dlen(q.d)<=0)throw Error("Deletes must not be empty");break;case"string":if(!(q.length>0))throw Error("Inserts cannot be empty");break;case"number":if(!(q>0))throw Error("Skip components must be >0");if(typeof y=="number")throw Error("Adjacent skip components should be combined");break}y=q}if(typeof y=="number")throw Error("Op has a trailing skip")};function n(f,y){let b=0,q=0;for(let z=0;z<f.length;z++){const K=f[z];switch(y(K,b,q),typeof K){case"object":b+=a.dlen(K.d);break;case"string":q+=e.strPosToUni(K);break;case"number":b+=K,q+=K;break}}}a.eachOp=n;function r(f,y){const b=[],q=l(b);return n(f,(z,K,Ee)=>{q(y(z,K,Ee))}),s(b)}const u=f=>f,o=f=>r(f,u);a.dlen=f=>typeof f=="number"?f:e.strPosToUni(f);const l=f=>y=>{if(!(!y||y.d===0||y.d===""))if(f.length===0)f.push(y);else if(typeof y==typeof f[f.length-1])if(typeof y=="object"){const b=f[f.length-1];b.d=typeof b.d=="string"&&typeof y.d=="string"?b.d+y.d:a.dlen(b.d)+a.dlen(y.d)}else f[f.length-1]+=y;else f.push(y)},D=f=>typeof f=="number"?f:typeof f=="string"?e.strPosToUni(f):typeof f.d=="number"?f.d:e.strPosToUni(f.d);a.uniSlice=(f,y,b)=>{const q=e.uniToStrPos(f,y),z=b==null?1/0:e.uniToStrPos(f,b);return f.slice(q,z)};const x=(f,y,b)=>typeof f=="number"?b==null?f-y:Math.min(f,b)-y:a.uniSlice(f,y,b),_=f=>{let y=0,b=0;return{take:(K,Ee)=>{if(y===f.length)return K===-1?null:K;const de=f[y];let ne;if(typeof de=="number")return K===-1||de-b<=K?(ne=de-b,++y,b=0,ne):(b+=K,K);if(typeof de=="string"){if(K===-1||Ee==="i"||e.strPosToUni(de.slice(b))<=K)return ne=de.slice(b),++y,b=0,ne;{const ue=b+e.uniToStrPos(de.slice(b),K);return ne=de.slice(b,ue),b=ue,ne}}else{if(K===-1||Ee==="d"||a.dlen(de.d)-b<=K)return ne={d:x(de.d,b)},++y,b=0,ne;{let ue=x(de.d,b,b+K);return b+=K,{d:ue}}}},peek:()=>f[y]}},s=f=>(f.length>0&&typeof f[f.length-1]=="number"&&f.pop(),f);function c(f,y,b){if(b!=="left"&&b!=="right")throw Error("side ("+b+") must be 'left' or 'right'");t(f),t(y);const q=[],z=l(q),{take:K,peek:Ee}=_(f);for(let ne=0;ne<y.length;ne++){const ue=y[ne];let ye,Se;switch(typeof ue){case"number":for(ye=ue;ye>0;)Se=K(ye,"i"),z(Se),typeof Se!="string"&&(ye-=D(Se));break;case"string":b==="left"&&typeof Ee()=="string"&&z(K(-1)),z(e.strPosToUni(ue));break;case"object":for(ye=a.dlen(ue.d);ye>0;)switch(Se=K(ye,"i"),typeof Se){case"number":ye-=Se;break;case"string":z(Se);break;case"object":ye-=a.dlen(Se.d)}break}}let de;for(;de=K(-1);)z(de);return s(q)}function h(f,y){t(f),t(y);const b=[],q=l(b),{take:z}=_(f);for(let Ee=0;Ee<y.length;Ee++){const de=y[Ee];let ne,ue;switch(typeof de){case"number":for(ne=de;ne>0;)ue=z(ne,"d"),q(ue),typeof ue!="object"&&(ne-=D(ue));break;case"string":q(de);break;case"object":ne=a.dlen(de.d);let ye=0;for(;ye<ne;)switch(ue=z(ne-ye,"d"),typeof ue){case"number":q({d:x(de.d,ye,ye+ue)}),ye+=ue;break;case"string":ye+=e.strPosToUni(ue);break;case"object":q(ue)}break}}let K;for(;K=z(-1);)q(K);return s(b)}const j=(f,y)=>{let b=0;for(let q=0;q<y.length&&f>b;q++){const z=y[q];switch(typeof z){case"number":{b+=z;break}case"string":const K=e.strPosToUni(z);b+=K,f+=K;break;case"object":f-=Math.min(a.dlen(z.d),f-b);break}}return f},Y=(f,y)=>typeof f=="number"?j(f,y):f.map(b=>j(b,y));function pe(f,y,b){return r(f,(q,z)=>typeof q=="object"&&typeof q.d=="number"?{d:b.slice(y,z,z+q.d)}:q)}function Z(f){return r(f,y=>{switch(typeof y){case"object":if(typeof y.d=="number")throw Error("Cannot invert text op: Deleted characters missing from operation. makeInvertible must be called first.");return y.d;case"string":return{d:y};case"number":return y}})}function Ce(f){return r(f,y=>typeof y=="object"&&typeof y.d=="string"?{d:e.strPosToUni(y.d)}:y)}function Ke(f){let y=!0;return n(f,b=>{typeof b=="object"&&typeof b.d=="number"&&(y=!1)}),y}function ve(f){return{name:"text-unicode",uri:"http://sharejs.org/types/text-unicode",trim:s,normalize:o,checkOp:t,create(y=""){if(typeof y!="string")throw Error("Initial data must be a string");return f.create(y)},apply(y,b){t(b);const q=f.builder(y);for(let z=0;z<b.length;z++){const K=b[z];switch(typeof K){case"number":q.skip(K);break;case"string":q.append(K);break;case"object":q.del(a.dlen(K.d));break}}return q.build()},transform:c,compose:h,transformPosition:j,transformSelection:Y,isInvertible:Ke,makeInvertible(y,b){return pe(y,b,f)},stripInvertible:Ce,invert:Z,invertWithDoc(y,b){return Z(pe(y,b,f))},isNoop:y=>y.length===0}}a.default=ve})(ut)),ut}var rt={},Dt;function Xt(){if(Dt)return rt;Dt=1,Object.defineProperty(rt,"__esModule",{value:!0});const a=ft(),e=gt();function t(n,r){return{get:n,getLength(){return n().length},insert(u,o,l){const D=e.strPosToUni(n(),u);return r([D,o],l)},remove(u,o,l){const D=e.strPosToUni(n(),u);return r([D,{d:o}],l)},_onOp(u){a.eachOp(u,(o,l,D)=>{switch(typeof o){case"string":this.onInsert&&this.onInsert(D,o);break;case"object":const x=a.dlen(o.d);this.onRemove&&this.onRemove(D,x)}})},onInsert:null,onRemove:null}}return rt.default=t,t.provides={text:!0},rt}var Et;function Jt(){return Et||(Et=1,(function(a){var e=ke&&ke.__createBinding||(Object.create?(function(c,h,j,Y){Y===void 0&&(Y=j),Object.defineProperty(c,Y,{enumerable:!0,get:function(){return h[j]}})}):(function(c,h,j,Y){Y===void 0&&(Y=j),c[Y]=h[j]})),t=ke&&ke.__setModuleDefault||(Object.create?(function(c,h){Object.defineProperty(c,"default",{enumerable:!0,value:h})}):function(c,h){c.default=h}),n=ke&&ke.__importStar||function(c){if(c&&c.__esModule)return c;var h={};if(c!=null)for(var j in c)Object.hasOwnProperty.call(c,j)&&e(h,c,j);return t(h,c),h},r=ke&&ke.__importDefault||function(c){return c&&c.__esModule?c:{default:c}};Object.defineProperty(a,"__esModule",{value:!0}),a.type=a.remove=a.insert=void 0;const u=gt(),o=n(ft()),l=r(Xt()),D={create(c){return c},toString(c){return c},builder(c){if(typeof c!="string")throw Error("Invalid document snapshot: "+c);const h=[];return{skip(j){let Y=u.uniToStrPos(c,j);if(Y>c.length)throw Error("The op is too long for this document");h.push(c.slice(0,Y)),c=c.slice(Y)},append(j){h.push(j)},del(j){c=c.slice(u.uniToStrPos(c,j))},build(){return h.join("")+c}}},slice:o.uniSlice},x=o.default(D),_=Object.assign(Object.assign({},x),{api:l.default});a.type=_,a.insert=(c,h)=>h.length===0?[]:c===0?[h]:[c,h],a.remove=(c,h)=>o.dlen(h)===0?[]:c===0?[{d:h}]:[c,{d:h}];var s=ft();Object.defineProperty(a,"makeType",{enumerable:!0,get:function(){return s.default}})})(ke)),ke}var St;function Yt(){return St||(St=1,(function(a){var e=Qe&&Qe.__importDefault||function(i){return i&&i.__esModule?i:{default:i}};Object.defineProperty(a,"__esModule",{value:!0}),a.editOp=a.replaceOp=a.insertOp=a.moveOp=a.removeOp=a.type=void 0;const t=e(Vt()),n=e(zt()),r=Ut(),u=jt();function o(i,d){if(!i)throw new Error(d)}a.type={name:"json1",uri:"http://sharejs.org/types/JSONv1",readCursor:r.readCursor,writeCursor:r.writeCursor,create:i=>i,isNoop:i=>i==null,setDebug(i){},registerSubtype:Z,checkValidOp:z,normalize:K,apply:Ee,transformPosition:de,compose:ne,tryTransform:st,transform:Bt,makeInvertible:Se,invert:ue,invertWithDoc:xt,RM_UNEXPECTED_CONTENT:u.ConflictType.RM_UNEXPECTED_CONTENT,DROP_COLLISION:u.ConflictType.DROP_COLLISION,BLACKHOLE:u.ConflictType.BLACKHOLE,transformNoConflict:(i,d,v)=>yt(()=>!0,i,d,v),typeAllowingConflictsPred:i=>Object.assign(Object.assign({},a.type),{transform:(d,v,E)=>yt(i,d,v,E)})};const l=i=>i?i.getComponent():null;function D(i){return i&&typeof i=="object"&&!Array.isArray(i)}const x=i=>Array.isArray(i)?i.slice():i!==null&&typeof i=="object"?Object.assign({},i):i,_=i=>i&&(i.p!=null||i.r!==void 0),s=i=>i&&(i.d!=null||i.i!==void 0);function c(i,d){return o(i!=null),typeof d=="number"?(o(Array.isArray(i),"Invalid key - child is not an array"),(i=i.slice()).splice(d,1)):(o(D(i),"Invalid key - child is not an object"),delete(i=Object.assign({},i))[d]),i}function h(i,d,v){return typeof d=="number"?(o(i!=null,"Container is missing for key"),o(Array.isArray(i),"Cannot use numerical key for object container"),o(i.length>=d,"Cannot insert into out of bounds index"),i.splice(d,0,v)):(o(D(i),"Cannot insert into missing item"),o(i[d]===void 0,"Trying to overwrite value at key. Your op needs to remove it first"),i[d]=v),v}a.removeOp=(i,d=!0)=>r.writeCursor().writeAtPath(i,"r",d).get(),a.moveOp=(i,d)=>r.writeCursor().writeMove(i,d).get(),a.insertOp=(i,d)=>r.writeCursor().writeAtPath(i,"i",d).get(),a.replaceOp=(i,d,v)=>r.writeCursor().at(i,E=>{E.write("r",d),E.write("i",v)}).get(),a.editOp=(i,d,v,E=!1)=>r.writeCursor().at(i,O=>y(O,d,v,E)).get();const j=(i,d)=>i!=null&&(typeof d=="number"?Array.isArray(i):typeof i=="object"),Y=(i,d)=>j(i,d)?i[d]:void 0,pe={};function Z(i){let d=i.type?i.type:i;d.name&&(pe[d.name]=d),d.uri&&(pe[d.uri]=d)}const Ce=i=>{const d=pe[i];if(d)return d;throw Error("Missing type: "+i)};Z(Jt());const Ke=(i,d)=>i+d;Z({name:"number",apply:Ke,compose:Ke,invert:i=>-i,transform:i=>i});const ve=i=>i==null?null:i.et?Ce(i.et):i.es?pe["text-unicode"]:i.ena!=null?pe.number:null,f=i=>i.es?i.es:i.ena!=null?i.ena:i.e,y=(i,d,v,E=!1)=>{const[O,C]=typeof d=="string"?[Ce(d),d]:[d,d.name];!E&&O.isNoop&&O.isNoop(v)||(C==="number"?i.write("ena",v):C==="text-unicode"?i.write("es",v):(i.write("et",C),i.write("e",v)))};function b(i){o(typeof i=="number"),o(i>=0),o(i===(0|i))}function q(i){typeof i=="number"?b(i):o(typeof i=="string")}function z(i){if(i===null)return;const d=new Set,v=new Set,E=C=>{let W=!0,N=!1;for(let g in C){const m=C[g];if(W=!1,o(g==="p"||g==="r"||g==="d"||g==="i"||g==="e"||g==="es"||g==="ena"||g==="et","Invalid component item '"+g+"'"),g==="p")b(m),o(!d.has(m)),d.add(m),o(C.r===void 0);else if(g==="d")b(m),o(!v.has(m)),v.add(m),o(C.i===void 0);else if(g==="e"||g==="es"||g==="ena"){o(!N),N=!0;const w=ve(C);o(w,"Missing type in edit"),w.checkValidOp&&w.checkValidOp(f(C))}}o(!W)},O=(C,W,N)=>{if(!Array.isArray(C))throw Error("Op must be null or a list");if(C.length===0)throw Error("Empty descent");W||q(C[0]);let g=1,m=0,w=0;for(let I=0;I<C.length;I++){const R=C[I];if(o(R!=null),Array.isArray(R)){const B=O(R,!1);if(m){const p=typeof w,M=typeof B;p===M?o(w<B,"descent keys are not in order"):o(p==="number"&&M==="string")}w=B,m++,g=3}else typeof R=="object"?(o(g===1,`Prev not scalar - instead ${g}`),E(R),g=2):(o(g!==3),q(R),o(r.isValidPathItem(R),"Invalid path key"),g=1)}return o(m!==1,"Operation makes multiple descents. Remove some []"),o(g===2||g===3),C[0]};O(i,!0),o(d.size===v.size,"Mismatched picks and drops in op");for(let C=0;C<d.size;C++)o(d.has(C)),o(v.has(C))}function K(i){let d=0,v=[];const E=r.writeCursor();return E.mergeTree(i,(O,C)=>{const W=ve(O);if(W){const g=f(O);y(C,W,W.normalize?W.normalize(g):g)}for(const g of["r","p","i","d"])if(O[g]!==void 0){const m=g==="p"||g==="d"?(N=O[g],v[N]==null&&(v[N]=d++),v[N]):O[g];C.write(g,m)}var N}),E.get()}function Ee(i,d){if(z(d),d===null)return i;const v=[];return(function E(O,C){let W=O,N=0,g={root:O},m=0,w=g,I="root";function R(){for(;m<N;m++){let B=C[m];typeof B!="object"&&(o(j(w,I)),w=w[I]=x(w[I]),I=B)}}for(;N<C.length;N++){const B=C[N];if(Array.isArray(B)){const p=E(W,B);p!==W&&p!==void 0&&(R(),W=w[I]=p)}else if(typeof B=="object"){B.d!=null?(R(),W=h(w,I,v[B.d])):B.i!==void 0&&(R(),W=h(w,I,B.i));const p=ve(B);if(p)R(),W=w[I]=p.apply(W,f(B));else if(B.e!==void 0)throw Error("Subtype "+B.et+" undefined")}else W=Y(W,B)}return g.root})(i=(function E(O,C){const W=[];let N=0;for(;N<C.length;N++){const I=C[N];if(Array.isArray(I))break;typeof I!="object"&&(W.push(O),O=Y(O,I))}for(let I=C.length-1;I>=N;I--)O=E(O,C[I]);for(--N;N>=0;N--){const I=C[N];if(typeof I!="object"){const R=W.pop();O=O===Y(R,I)?R:O===void 0?c(R,I):(m=I,w=O,(g=x(g=R))[m]=w,g)}else _(I)&&(o(O!==void 0,"Cannot pick up or remove undefined"),I.p!=null&&(v[I.p]=O),O=void 0)}var g,m,w;return O})(i,d),d)}function de(i,d){i=i.slice(),z(d);const v=r.readCursor(d);let E,O,C=!1;const W=[];for(let g=0;;g++){const m=i[g],w=v.getComponent();if(w&&(w.r!==void 0?C=!0:w.p!=null&&(C=!1,E=w.p,O=g)),g>=i.length)break;let I=0;const R=r.advancer(v,void 0,(p,M)=>{_(M)&&I++});W.unshift(R);const B=R(m);if(typeof m=="number"&&(i[g]-=I),!B)break}if(W.forEach(g=>g.end()),C)return null;const N=()=>{let g=0;if(E!=null){const m=v.getPath();g=m.length,i=m.concat(i.slice(O))}for(;g<i.length;g++){const m=i[g],w=l(v),I=ve(w);if(I){const p=f(w);I.transformPosition&&(i[g]=I.transformPosition(i[g],p));break}let R=0;const B=r.advancer(v,(p,M)=>s(M)?~(p-R):p-R,(p,M)=>{s(M)&&R++})(m);if(typeof m=="number"&&(i[g]+=R),!B)break}};return E!=null?v.eachDrop(null,g=>{g===E&&N()}):N(),i}function ne(i,d){if(z(i),z(d),i==null)return d;if(d==null)return i;let v=0;const E=r.readCursor(i),O=r.readCursor(d),C=r.writeCursor(),W=[],N=[],g=[],m=[],w=[],I=[],R=new Set;E.traverse(null,p=>{p.p!=null&&(g[p.p]=E.clone())}),O.traverse(null,p=>{p.d!=null&&(m[p.d]=O.clone())});const B=r.writeCursor();return(function p(M,re,te,$,oe,We,we,_e){o(re||te);const ae=l(re),Pe=l(te),Ae=!!Pe&&Pe.r!==void 0,$e=!!ae&&ae.i!==void 0,Te=ae?ae.d:null,De=Pe?Pe.p:null,xe=(We||Ae)&&De==null;if(De!=null)$=m[De],we=N[De]=new r.WriteCursor;else if(Pe&&Pe.r!==void 0)$=null;else{const P=l($);P&&P.d!=null&&($=null)}const Q=l($);if(Te!=null)if(M=g[Te],_e=W[Te]=new r.WriteCursor,xe)We&&!Ae&&_e.write("r",!0);else{const P=w[Te]=v++;we.write("d",P)}else if(ae&&ae.i!==void 0)M=null;else{const P=l(M);P&&P.p!=null&&(M=null)}let T;$e?(o(oe===void 0),T=ae.i):T=oe;const G=(De==null?!$e||We||Ae:T===void 0)?null:we.getComponent();if(De!=null){if(!(oe!==void 0||$e)){const P=Te!=null?w[Te]:v++;I[De]=P,_e.write("p",P)}}else Ae&&($e||oe!==void 0||(Pe.r,_e.write("r",Pe.r)));const A=xe?null:ve(ae),S=ve(Q);if((A||S)&&(A&&A.name,S&&S.name),A&&S){o(A===S);const P=f(ae),L=f(Q),ge=A.compose(P,L);y(we,A,ge),R.add(Q)}else A?y(we,A,f(ae)):S&&(y(we,S,f(Q)),R.add(Q));const U=typeof T=="object"&&T!=null;let J=!1,X=0,ee=0,he=0,fe=0,le=0;const Oe=r.advancer($,(P,L)=>s(L)?fe-P-1:P-fe,(P,L)=>{s(L)&&fe++}),V=r.advancer(M,(P,L)=>_(L)?X-P-1:P-X,(P,L)=>{_(L)&&X++});if(r.eachChildOf(re,te,(P,L,ge)=>{let be,Be,He=P,Me=P,Ze=P;if(typeof P=="number"){let me=P+he;Be=Oe(me),Me=me+fe;let ce=P+ee;be=V(ce),s(l(Be))&&(be=null),He=ce+X,Ze=P+le,o(He>=0,"p1PickKey is negative"),o(Me>=0,"p2DropKey is negative");const Ue=s(l(L)),Ge=_(l(ge));(Ue||Ge&&!xe)&&le--,Ue&&ee--,Ge&&he--}else be=V(P),Be=Oe(P);_e.descend(He),we.descend(Me);const Je=U&&!s(l(L))?T[Ze]:void 0,Ne=p(be,L,ge,Be,Je,xe,we,_e);var Re,k,se;U&&!xe?Je!==Ne&&(J||(T=Array.isArray(T)?T.slice():Object.assign({},T),J=!0),Re=T,se=Ne,typeof(k=Ze)=="number"?(o(Array.isArray(Re)),o(k<Re.length)):(o(!Array.isArray(Re)),o(Re[k]!==void 0)),se===void 0?typeof k=="number"?Re.splice(k,1):delete Re[k]:Re[k]=se):o(Ne===void 0),we.ascend(),_e.ascend()}),V.end(),Oe.end(),G!=null)G.i=T;else if(!We&&!Ae&&De==null)return T})(E,E.clone(),O,O.clone(),void 0,!1,C,B),C.reset(),C.mergeTree(B.get()),C.reset(),C.get(),W.map(p=>p.get()),N.map(p=>p.get()),E.traverse(C,(p,M)=>{const re=p.p;if(re!=null){const te=w[re];te!=null&&M.write("p",te);const $=W[re];$&&$.get(),$&&M.mergeTree($.get())}else p.r!==void 0&&M.write("r",p.r)}),C.reset(),C.get(),O.traverse(C,(p,M)=>{const re=p.d;if(re!=null){const $=I[re];$!=null&&M.write("d",$);const oe=N[re];oe&&M.mergeTree(oe.get())}else p.i!==void 0&&M.write("i",p.i);const te=ve(p);te&&!R.has(p)&&y(M,te,f(p))}),C.get()}function ue(i){if(i==null)return null;const d=new r.ReadCursor(i),v=new r.WriteCursor;let E;const O=[],C=[];return(function W(N,g,m){const w=N.getComponent();let I,R=!1;if(w){w.p!=null&&(g.write("d",w.p),O[w.p]=N.clone()),w.r!==void 0&&g.write("i",w.r),w.d!=null&&(g.write("p",w.d),m=void 0),w.i!==void 0&&(m=I=w.i);const p=ve(w);p&&(m===void 0?(E||(E=new Set),E.add(w)):(f(w),m=p.apply(m,f(w)),R=!0))}let B=0;for(const p of N){g.descend(p);const M=typeof p=="number"?p-B:p,re=Y(m,M);s(N.getComponent())&&B++;const te=W(N,g,re);if(m!==void 0&&te!==void 0){if(R||(R=!0,m=x(m)),!j(m,M))throw Error("Cannot modify child - invalid operation");m[M]=te}g.ascend()}if(I===void 0)return R?m:void 0;g.write("r",m)})(d,v,void 0),E&&(v.reset(),(function W(N,g,m){const w=g.getComponent();if(w){const p=w.d;if(p!=null&&(N=O[p],m=C[p]=r.writeCursor()),E.has(w)){const M=ve(w);if(!M.invert)throw Error(`Cannot invert subtype ${M.name}`);y(m,M,M.invert(f(w)))}}let I=0,R=0;const B=r.advancer(N,(p,M)=>_(M)?I-p-1:p-I,(p,M)=>{_(M)&&I++});for(const p of g)if(typeof p=="number"){const M=p-R,re=B(M),te=M+I;m.descend(te),W(re,g,m),s(g.getComponent())&&R++,m.ascend()}else m.descend(p),W(B(p),g,m),m.ascend();B.end()})(d.clone(),d,v),C.length&&(v.reset(),d.traverse(v,(W,N)=>{const g=W.p;if(g!=null){const m=C[g];m&&m.get(),m&&N.mergeTree(m.get())}}))),v.get()}const ye=(i,d)=>i.some(v=>typeof v=="object"&&(Array.isArray(v)?ye(v,d):d(v)));function Se(i,d){if(i==null||!ye(i,g=>{var m;return g.r!==void 0||((m=ve(g))===null||m===void 0?void 0:m.makeInvertible)!=null}))return i;const v=new r.ReadCursor(i),E=new r.WriteCursor;let O=!1;const C=[],W=[],N=(g,m,w)=>{const I=g.getComponent();let R=!1;if(I){I.d!=null&&m.write("d",I.d),I.i!==void 0&&m.write("i",I.i);const p=I.p;if(p!=null&&(C[p]=g.clone(),o(w!==void 0,"Operation picks up at an invalid key"),W[p]=w,m.write("p",I.p)),I.r!==void 0&&w===void 0)throw Error("Invalid doc / op in makeInvertible: removed item missing from doc");const M=ve(I);M&&(M.makeInvertible?O=!0:y(m,M,f(I),!0))}let B=0;for(const p of g){m.descend(p);const M=typeof p=="number"?p-B:p,re=Y(w,M),te=N(g,m,re);re!==te&&(R||(R=!0,w=x(w)),te===void 0?(w=c(w,M),typeof p=="number"&&B++):w[M]=te),m.ascend()}return I&&(I.r!==void 0?(m.write("r",n.default(w)),w=void 0):I.p!=null&&(w=void 0)),w};return N(v,E,d),E.get(),O&&(E.reset(),(function g(m,w,I,R,B){const p=w.getComponent();if(p){p.i!==void 0?(R=p.i,B=!0):p.d!=null&&(R=W[p.d],m=C[p.d],B=!1,p.d);let $=ve(p);if($&&$.makeInvertible){const oe=f(p);y(I,$,$.makeInvertible(oe,R),!0)}}let M=0,re=0;const te=r.advancer(m,($,oe)=>_(oe)?M-$-1:$-M,($,oe)=>{_(oe)&&M++});for(const $ of w)if(typeof $=="number"){const oe=$-re,We=te(oe),we=oe+M,_e=Y(R,B?oe:we);I.descend($),g(We,w,I,_e,B),s(w.getComponent())&&re++,I.ascend()}else{const oe=Y(R,$);I.descend($),g(te($),w,I,oe,B),I.ascend()}te.end()})(v.clone(),v,E,d,!1)),E.get()}function xt(i,d){return ue(Se(i,d))}const it=i=>{if(i==null)return null;const d=i.slice();for(let v=0;v<i.length;v++){const E=d[v];Array.isArray(E)&&(d[v]=it(E))}return d};function st(i,d,v){o(v==="left"||v==="right","Direction must be left or right");const E=v==="left"?0:1;if(d==null)return{ok:!0,result:i};z(i),z(d);let O=null;const C=[],W=[],N=[],g=[],m=[],w=[],I=[],R=[],B=[],p=[],M=[],re=[],te=[],$=[],oe=[];let We=0;const we=r.readCursor(i),_e=r.readCursor(d),ae=r.writeCursor();if((function Q(T,G=null,A){const S=l(G);S&&(S.r!==void 0?A=G.clone():S.p!=null&&(A=null,w[S.p]=T.clone()));const U=T.getComponent();let J;U&&(J=U.p)!=null&&(m[J]=G?G.clone():null,N[J]=T.clone(),A&&(p[J]=!0,B[J]=A),S&&S.p!=null&&($[J]=S.p));const X=r.advancer(G);for(const ee of T)Q(T,X(ee),A);X.end()})(_e,we,null),(function Q(T,G,A,S,U){const J=A.getComponent();let X,ee=!1;J&&((X=J.d)!=null?(g[X]=A.clone(),S!=null&&(oe[S]==null&&(oe[S]=[]),oe[S].push(X)),p[X],T=m[X]||null,G=N[X]||null,p[X]?(U&&(M[X]=!0),U=B[X]||null):!U||E!==1&&$[X]!=null||O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(U.getPath()),op2:a.moveOp(G.getPath(),A.getPath())}),ee=!0):J.i!==void 0&&(T=G=null,ee=!0,U&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(U.getPath()),op2:a.insertOp(A.getPath(),J.i)})));const he=l(T);he&&(he.r!==void 0?U=T.clone():he.p!=null&&(he.p,S=he.p,U=null));const fe=ve(J);fe&&U&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(U.getPath()),op2:a.editOp(A.getPath(),fe,f(J),!0)});let le=0,Oe=0;const V=r.advancer(G,(L,ge)=>_(ge)?le-L-1:L-le,(L,ge)=>{_(ge)&&le++}),P=r.advancer(T);for(const L of A)if(typeof L=="number"){const ge=L-Oe,be=V(ge);Oe+=+Q(P(ge+le),be,A,S,U)}else{const ge=V(L);Q(P(L),ge,A,S,U)}return V.end(),P.end(),ee})(we,_e,_e.clone(),null,null),g.map(Q=>Q&&Q.get()),O)return{ok:!1,conflict:O};M.map(Q=>!!Q);const Pe=[];let Ae=null;(function Q(T,G,A,S,U){let J=!1;const X=l(G);if(_(X)){const V=X.p;V!=null?(A=g[V],S=re[V]=r.writeCursor(),J=!0,U=null):(A=null,U=G.clone())}else s(l(A))&&(A=null);const ee=T.getComponent();if(ee){const V=ee.p;V!=null?(U&&(R[V]=U),Pe[V]=U||E===1&&J?null:S.getComponent(),C[V]=T.clone(),A&&(I[V]=A.clone())):ee.r!==void 0&&(U||S.write("r",!0),(U||J)&&(Ae==null&&(Ae=new Set),Ae.add(ee)))}let he=0,fe=0;const le=r.advancer(G,void 0,(V,P)=>{_(P)&&he++}),Oe=r.advancer(A,(V,P)=>s(P)?~(V-fe):V-fe,(V,P)=>{s(P)&&fe++});if(T)for(const V of T)if(typeof V=="string"){const P=le(V),L=Oe(V);S.descend(V),Q(T,P,L,S,U),S.ascend()}else{const P=le(V),L=V-he,ge=_(l(P))?null:Oe(L),be=L+fe;o(be>=0),S.descend(be),Q(T,P,ge,S,U),S.ascend()}le.end(),Oe.end()})(we,_e,_e.clone(),ae,null),ae.reset();let $e=[];if((function Q(T,G,A,S,U,J){o(G);const X=G.getComponent();let ee=l(S),he=!1;const fe=(k,se,me)=>k?a.moveOp(k.getPath(),se.getPath()):a.insertOp(se.getPath(),me.i);if(s(X)){const k=X.d;k!=null&&(W[k]=G.clone());const se=k!=null?Pe[k]:null;let me=!1;if(X.i!==void 0||k!=null&&se){let ce;ee&&(ee.i!==void 0||(ce=ee.d)!=null&&!p[ce])&&(me=ce!=null?k!=null&&k===$[ce]:t.default(ee.i,X.i),me||ce!=null&&E!==1&&$[ce]!=null||O==null&&(O={type:u.ConflictType.DROP_COLLISION,op1:fe(k!=null?C[k]:null,G,X),op2:fe(ce!=null?N[ce]:null,S,ee)})),me||(J?O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:fe(k!=null?C[k]:null,G,X),op2:a.removeOp(J.getPath())}):(k!=null?($e[We]=k,U.write("d",se.p=We++)):U.write("i",n.default(X.i)),he=!0))}else if(k!=null&&!se){const ce=R[k];ce&&(J=ce.clone())}k!=null?(T=C[k],A=w[k],S=I[k]):X.i!==void 0&&(T=A=null,me||(S=null))}else _(l(T))&&(T=A=S=null);const le=l(T),Oe=l(A);if(_(Oe)){const k=Oe.p;Oe.r!==void 0&&(!le||le.r===void 0)||p[k]?(S=null,J=A.clone()):k!=null&&(S=g[k],E!==1&&$[k]!=null||((U=te[k])||(U=te[k]=r.writeCursor()),U.reset(),J=null))}else!s(X)&&s(ee)&&(S=null);ee=S!=null?S.getComponent():null;const V=ve(X);if(V){const k=f(X);if(J)O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.editOp(G.getPath(),V,k,!0),op2:a.removeOp(J.getPath())});else{const se=ve(ee);let me;if(se){if(V!==se)throw Error("Transforming incompatible types");const ce=f(ee);me=V.transform(k,ce,v)}else me=n.default(k);y(U,V,me)}}let P=0,L=0,ge=0,be=0,Be=0,He=0,Me=T!=null&&T.descendFirst(),Ze=Me;const Je=r.advancer(A,void 0,(k,se)=>{_(se)&&ge++});let Ne=S!=null&&S.descendFirst(),Re=Ne;for(const k of G)if(typeof k=="number"){let se;const me=s(G.getComponent()),ce=k-L;{let Le;for(;Me&&typeof(Le=T.getKey())=="number";){Le+=P;const je=T.getComponent(),Ye=_(je);if(Le>ce||Le===ce&&(!Ye||E===0&&me))break;if(Ye){P--;const Fe=je.p;$.includes(Fe),je.d,l(te[je.d]),_(l(te[je.d])),(je.r===void 0||Ae&&Ae.has(je))&&(Fe==null||!Pe[Fe]||E!==1&&$.includes(Fe))||Be--}Me=T.nextSibling()}se=Me&&Le===ce?T:null}const Ue=ce-P;let Ge=Je(Ue);const ot=Ue-ge;let et=null;{let Le,je;for(;Ne&&typeof(Le=S.getKey())=="number";){je=Le-be;const Ye=S.getComponent(),Fe=s(Ye);if(je>ot)break;if(je===ot){if(!Fe){et=S;break}{if(E===0&&me){et=S;break}const qe=Ge&&_(Ge.getComponent());if(E===0&&qe)break}}if(Fe){const qe=Ye.d;p[qe],$[qe],Ye.i===void 0&&(p[qe]||$[qe]!=null&&E!==1)?(p[qe]||$[qe]!=null&&E===0)&&(be++,He--):be++}Ne=S.nextSibling()}}const _t=ot+be+Be+He;o(_t>=0,"trying to descend to a negative index"),U.descend(_t),me&&(se=Ge=et=null,L++),Q(se,G,Ge,et,U,J)&&He++,U.ascend()}else{let se;for(;Me&&(se=T.getKey(),typeof se!="string"||!(se>k||se===k));)Me=T.nextSibling();const me=Me&&se===k?T:null,ce=Je(k);let Ue;for(;Ne&&(Ue=S.getKey(),typeof Ue!="string"||!(Ue>k||Ue===k));)Ne=S.nextSibling();const Ge=Ne&&Ue===k?S:null;U.descend(k),Q(me,G,ce,Ge,U,J),U.ascend()}return Je.end(),Ze&&T.ascend(),Re&&S.ascend(),he})(we,we.clone(),_e,_e.clone(),ae,null),O)return{ok:!1,conflict:O};ae.reset();const Te=(Q,T,G)=>Q.traverse(T,(A,S)=>{A.d!=null&&G(A.d,Q,S)});(p.length||re.length)&&(Te(_e,ae,(Q,T,G)=>{p[Q]&&!M[Q]&&G.write("r",!0),re[Q]&&G.mergeTree(re[Q].get())}),ae.reset());const De=[],xe=[];if((te.length||p.length)&&!O){const Q=r.readCursor(it(ae.get()));if(Te(Q,null,(T,G)=>{De[T]=G.clone()}),te.forEach(T=>{T&&Te(r.readCursor(T.get()),null,(G,A)=>{De[G]=A.clone()})}),(function T(G,A,S,U,J,X){const ee=l(A);if(ee&&_(ee))if(ee.p!=null){const P=ee.p;De[P].getPath(),S=De[P],U=xe[P]=r.writeCursor()}else ee.r!==void 0&&(S=null);else s(l(S))&&(S=null);const he=G.getComponent();if(he){let P;if((P=he.d)!=null){const L=te[P];L&&(L.get(),U.mergeTree(L.get()),S=r.readCursor(L.get()))}}let fe=0,le=0;const Oe=r.advancer(A,void 0,(P,L)=>{_(L)&&fe--}),V=r.advancer(S,(P,L)=>s(L)?-(P-le)-1:P-le,(P,L)=>{s(L)&&le++});for(const P of G)if(typeof P=="number"){const L=Oe(P),ge=P+fe,be=V(ge),Be=ge+le;U.descend(Be),T(G,L,be,U),U.ascend()}else U.descend(P),T(G,Oe(P),V(P),U),U.ascend();Oe.end(),V.end()})(_e,Q,Q.clone(),ae),ae.reset(),O)return{ok:!1,conflict:O};if(ae.get(),xe.length){const T=xe.map(A=>A?A.get():null),G=r.readCursor(it(ae.get()));if(Te(G,ae,(A,S,U)=>{const J=T[A];J&&(U.mergeTree(J),T[A]=null)}),T.find(A=>A)){const A=r.writeCursor(),S=r.writeCursor();let U=0,J=0;T.forEach(X=>{X!=null&&Te(r.readCursor(X),null,ee=>{const he=$e[ee];A.writeMove(C[he].getPath(),W[he].getPath(),U++);const fe=oe[he];fe&&fe.forEach(le=>{p[le]||E!==1&&$[le]!=null||S.writeMove(N[le].getPath(),g[le].getPath(),J++)})})}),O={type:u.ConflictType.BLACKHOLE,op1:A.get(),op2:S.get()}}}}return O?{ok:!1,conflict:O}:{ok:!0,result:ae.get()}}const pt=i=>{const d=new Error("Transform detected write conflict");throw d.conflict=i,d.type=d.name="writeConflict",d};function Bt(i,d,v){const E=st(i,d,v);if(E.ok)return E.result;pt(E.conflict)}const Xe=i=>{const d=r.writeCursor();return r.readCursor(i).traverse(d,(v,E)=>{(s(v)||ve(v))&&E.write("r",!0)}),d.get()},Gt=(i,d)=>{const{type:v,op1:E,op2:O}=i;switch(v){case u.ConflictType.DROP_COLLISION:return d==="left"?[null,Xe(O)]:[Xe(E),null];case u.ConflictType.RM_UNEXPECTED_CONTENT:let C=!1;return r.readCursor(E).traverse(null,W=>{W.r!==void 0&&(C=!0)}),C?[null,Xe(O)]:[Xe(E),null];case u.ConflictType.BLACKHOLE:return[Xe(E),Xe(O)];default:throw Error("Unrecognised conflict: "+v)}};function yt(i,d,v,E){let O=null;for(;;){const C=st(d,v,E);if(C.ok)return ne(O,C.result);{const{conflict:W}=C;i(W)||pt(W);const[N,g]=Gt(W,E);d=ne(K(d),N),v=ne(K(v),g),O=ne(O,g)}}}})(Qe)),Qe}var Pt;function Zt(){return Pt||(Pt=1,(function(a){var e=Ve&&Ve.__createBinding||(Object.create?(function(u,o,l,D){D===void 0&&(D=l),Object.defineProperty(u,D,{enumerable:!0,get:function(){return o[l]}})}):(function(u,o,l,D){D===void 0&&(D=l),u[D]=o[l]})),t=Ve&&Ve.__exportStar||function(u,o){for(var l in u)l!=="default"&&!o.hasOwnProperty(l)&&e(o,u,l)};Object.defineProperty(a,"__esModule",{value:!0}),t(Yt(),a);var n=Ut();Object.defineProperty(a,"ReadCursor",{enumerable:!0,get:function(){return n.ReadCursor}}),Object.defineProperty(a,"WriteCursor",{enumerable:!0,get:function(){return n.WriteCursor}});var r=jt();Object.defineProperty(a,"ConflictType",{enumerable:!0,get:function(){return r.ConflictType}})})(Ve)),Ve}var F=Zt();class kt{constructor(){H(this,"drawingManagerData",{});H(this,"_oldDrawingManagerData",{});H(this,"_focusDrawings",[]);H(this,"_remove$",new Ie.Subject);H(this,"remove$",this._remove$.asObservable());H(this,"_add$",new Ie.Subject);H(this,"add$",this._add$.asObservable());H(this,"_update$",new Ie.Subject);H(this,"update$",this._update$.asObservable());H(this,"_order$",new Ie.Subject);H(this,"order$",this._order$.asObservable());H(this,"_group$",new Ie.Subject);H(this,"group$",this._group$.asObservable());H(this,"_ungroup$",new Ie.Subject);H(this,"ungroup$",this._ungroup$.asObservable());H(this,"_refreshTransform$",new Ie.Subject);H(this,"refreshTransform$",this._refreshTransform$.asObservable());H(this,"_visible$",new Ie.Subject);H(this,"visible$",this._visible$.asObservable());H(this,"_focus$",new Ie.Subject);H(this,"focus$",this._focus$.asObservable());H(this,"_featurePluginUpdate$",new Ie.Subject);H(this,"featurePluginUpdate$",this._featurePluginUpdate$.asObservable());H(this,"_featurePluginAdd$",new Ie.Subject);H(this,"featurePluginAdd$",this._featurePluginAdd$.asObservable());H(this,"_featurePluginRemove$",new Ie.Subject);H(this,"featurePluginRemove$",this._featurePluginRemove$.asObservable());H(this,"_featurePluginOrderUpdate$",new Ie.Subject);H(this,"featurePluginOrderUpdate$",this._featurePluginOrderUpdate$.asObservable());H(this,"_featurePluginGroupUpdate$",new Ie.Subject);H(this,"featurePluginGroupUpdate$",this._featurePluginGroupUpdate$.asObservable());H(this,"_featurePluginUngroupUpdate$",new Ie.Subject);H(this,"featurePluginUngroupUpdate$",this._featurePluginUngroupUpdate$.asObservable());H(this,"_visible",!0);H(this,"_editable",!0)}dispose(){this._remove$.complete(),this._add$.complete(),this._update$.complete(),this._order$.complete(),this._focus$.complete(),this._featurePluginUpdate$.complete(),this._featurePluginAdd$.complete(),this._featurePluginRemove$.complete(),this._featurePluginOrderUpdate$.complete(),this.drawingManagerData={},this._oldDrawingManagerData={}}visibleNotification(e){this._visible$.next(e)}refreshTransform(e){e.forEach(t=>{const n=this._getCurrentBySearch(t);n!=null&&(n.transform=t.transform,n.transforms=t.transforms,n.isMultiTransform=t.isMultiTransform)}),this.refreshTransformNotification(e)}getDrawingDataForUnit(e){return this.drawingManagerData[e]||{}}removeDrawingDataForUnit(e){const t=this.drawingManagerData[e];if(t==null)return;delete this.drawingManagerData[e];const n=[];Object.keys(t).forEach(r=>{const u=t[r];(u==null?void 0:u.data)!=null&&Object.keys(u.data).forEach(o=>{n.push({unitId:e,subUnitId:r,drawingId:o})})}),n.length>0&&this.removeNotification(n)}registerDrawingData(e,t){this.drawingManagerData[e]=t}initializeNotification(e){const t=[],n=this.drawingManagerData[e];n!=null&&(Object.keys(n).forEach(r=>{this._establishDrawingMap(e,r);const u=n[r];Object.keys(u.data).forEach(o=>{const l=u.data[o];l.unitId=e,l.subUnitId=r,t.push(l)})}),t.length>0&&this.addNotification(t))}getDrawingData(e,t){return this._getDrawingData(e,t)}setDrawingData(e,t,n){this.drawingManagerData[e][t].data=n}getBatchAddOp(e){const t=[],n=[],r=[];e.forEach(x=>{const{op:_,invertOp:s}=this._addByParam(x);t.push({unitId:x.unitId,subUnitId:x.subUnitId,drawingId:x.drawingId}),n.push(_),r.push(s)});const u=n.reduce(F.type.compose,null),o=r.reduce(F.type.compose,null),{unitId:l,subUnitId:D}=e[0];return{undo:o,redo:u,unitId:l,subUnitId:D,objects:t}}getBatchRemoveOp(e){const t=[],n=[];e.forEach(D=>{const{op:x,invertOp:_}=this._removeByParam(D);t.unshift(x),n.push(_)});const r=t.reduce(F.type.compose,null),u=n.reduce(F.type.compose,null),{unitId:o,subUnitId:l}=e[0];return{undo:u,redo:r,unitId:o,subUnitId:l,objects:e}}getBatchUpdateOp(e){const t=[],n=[],r=[];e.forEach(x=>{const{op:_,invertOp:s}=this._updateByParam(x);t.push({unitId:x.unitId,subUnitId:x.subUnitId,drawingId:x.drawingId}),n.push(_),r.push(s)});const u=n.reduce(F.type.compose,null),o=r.reduce(F.type.compose,null),{unitId:l,subUnitId:D}=e[0];return{undo:o,redo:u,unitId:l,subUnitId:D,objects:t}}removeNotification(e){this._remove$.next(e)}addNotification(e){this._add$.next(e)}updateNotification(e){this._update$.next(e)}orderNotification(e){this._order$.next(e)}groupUpdateNotification(e){this._group$.next(e)}ungroupUpdateNotification(e){this._ungroup$.next(e)}refreshTransformNotification(e){this._refreshTransform$.next(e)}getGroupDrawingOp(e){const t=[],{unitId:n,subUnitId:r}=e[0].parent;e.forEach(l=>{t.push(this._getGroupDrawingOp(l))});const u=t.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(u,this.drawingManagerData),redo:u,unitId:n,subUnitId:r,objects:e}}getUngroupDrawingOp(e){const t=[],{unitId:n,subUnitId:r}=e[0].parent;e.forEach(l=>{t.push(this._getUngroupDrawingOp(l))});const u=t.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(u,this.drawingManagerData),redo:u,unitId:n,subUnitId:r,objects:e}}getDrawingsByGroup(e){const{unitId:t,subUnitId:n,drawingId:r}=e;if(this.getDrawingByParam({unitId:t,subUnitId:n,drawingId:r})==null)return[];const o=this._getDrawingData(t,n),l=[];return Object.keys(o).forEach(D=>{const x=o[D];x.groupId===r&&l.push(x)}),l}_getGroupDrawingOp(e){const{parent:t,children:n}=e,{unitId:r,subUnitId:u,drawingId:o}=t,l=[];l.push(F.insertOp([r,u,"data",o],t));let D=Number.NEGATIVE_INFINITY;return n.forEach(x=>{const{unitId:_,subUnitId:s,drawingId:c}=x,h=this._hasDrawingOrder({unitId:_,subUnitId:s,drawingId:c});D=Math.max(D,h),l.push(...this._getUpdateParamCompareOp(x,this.getDrawingByParam({unitId:_,subUnitId:s,drawingId:c})))}),D===Number.NEGATIVE_INFINITY&&(D=this._getDrawingOrder(r,u).length),l.push(F.insertOp([r,u,"order",D],o)),l.reduce(F.type.compose,null)}_getUngroupDrawingOp(e){const{parent:t,children:n}=e,{unitId:r,subUnitId:u,drawingId:o}=t,l=[];return n.forEach(D=>{const{unitId:x,subUnitId:_,drawingId:s}=D;l.push(...this._getUpdateParamCompareOp(D,this.getDrawingByParam({unitId:x,subUnitId:_,drawingId:s})))}),l.push(F.removeOp([r,u,"data",o],!0)),l.push(F.removeOp([r,u,"order",this._getDrawingOrder(r,u).indexOf(o)],!0)),l.reduce(F.type.compose,null)}applyJson1(e,t,n){this._establishDrawingMap(e,t),this._oldDrawingManagerData={...this.drawingManagerData},this.drawingManagerData=F.type.apply(this.drawingManagerData,n)}featurePluginUpdateNotification(e){this._featurePluginUpdate$.next(e)}featurePluginOrderUpdateNotification(e){this._featurePluginOrderUpdate$.next(e)}featurePluginAddNotification(e){this._featurePluginAdd$.next(e)}featurePluginRemoveNotification(e){this._featurePluginRemove$.next(e)}featurePluginGroupUpdateNotification(e){this._featurePluginGroupUpdate$.next(e)}featurePluginUngroupUpdateNotification(e){this._featurePluginUngroupUpdate$.next(e)}getDrawingByParam(e){return this._getCurrentBySearch(e)}getOldDrawingByParam(e){return this._getOldBySearch(e)}getDrawingOKey(e){const[t,n,r]=e.split("#-#");return this._getCurrentBySearch({unitId:t,subUnitId:n,drawingId:r})}focusDrawing(e){if(e==null||e.length===0){this._focusDrawings=[],this._focus$.next([]);return}const t=[];e.forEach(n=>{var D;const{unitId:r,subUnitId:u,drawingId:o}=n,l=(D=this._getDrawingData(r,u))==null?void 0:D[o];l!=null&&t.push(l)}),t.length>0&&(this._focusDrawings=t,this._focus$.next(t))}getFocusDrawings(){const e=[];return this._focusDrawings.forEach(t=>{var l;const{unitId:n,subUnitId:r,drawingId:u}=t,o=(l=this._getDrawingData(n,r))==null?void 0:l[u];o!=null&&e.push(o)}),e}getDrawingOrder(e,t){return this._getDrawingOrder(e,t)}setDrawingOrder(e,t,n){this.drawingManagerData[e][t].order=n}orderUpdateNotification(e){this._order$.next(e)}getForwardDrawingsOp(e){const{unitId:t,subUnitId:n,drawingIds:r}=e,u=[],o=this.getDrawingOrder(t,n),l=[...r];r.forEach(_=>{const s=this._hasDrawingOrder({unitId:t,subUnitId:n,drawingId:_});if(s===-1||s===o.length-1)return;const c=F.moveOp([t,n,"order",s],[t,n,"order",s+1]);u.push(c),l.includes(o[s+1])||l.push(o[s+1])});const D=u.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(D,this.drawingManagerData),redo:D,unitId:t,subUnitId:n,objects:{...e,drawingIds:l}}}getBackwardDrawingOp(e){const{unitId:t,subUnitId:n,drawingIds:r}=e,u=[],o=this.getDrawingOrder(t,n),l=[...r];r.forEach(_=>{const s=this._hasDrawingOrder({unitId:t,subUnitId:n,drawingId:_});if(s===-1||s===0)return;const c=F.moveOp([t,n,"order",s],[t,n,"order",s-1]);u.push(c),l.includes(o[s-1])||l.push(o[s-1])});const D=u.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(D,this.drawingManagerData),redo:D,unitId:t,subUnitId:n,objects:{...e,drawingIds:l}}}getFrontDrawingsOp(e){const{unitId:t,subUnitId:n,drawingIds:r}=e,u=this._getOrderFromSearchParams(t,n,r),o=[...r],l=this.getDrawingOrder(t,n),D=[];u.forEach(s=>{const{drawingId:c}=s,h=this._getDrawingCount(t,n)-1,j=F.moveOp([t,n,"order",this._getDrawingOrder(t,n).indexOf(c)],[t,n,"order",h]);D.push(j),o.includes(l[h])||o.push(l[h])});const x=D.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(x,this.drawingManagerData),redo:x,unitId:t,subUnitId:n,objects:{...e,drawingIds:o}}}getBackDrawingsOp(e){const{unitId:t,subUnitId:n,drawingIds:r}=e,u=this._getOrderFromSearchParams(t,n,r,!0),o=[...r],l=this.getDrawingOrder(t,n),D=[];u.forEach(s=>{const{drawingId:c}=s,h=F.moveOp([t,n,"order",this._getDrawingOrder(t,n).indexOf(c)],[t,n,"order",0]);D.push(h),o.includes(l[0])||o.push(l[0])});const x=D.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(x,this.drawingManagerData),redo:x,unitId:t,subUnitId:n,objects:{...e,drawingIds:o}}}_getDrawingCount(e,t){return this.getDrawingOrder(e,t).length||0}_getOrderFromSearchParams(e,t,n,r=!1){return n.map(u=>{const o=this._hasDrawingOrder({unitId:e,subUnitId:t,drawingId:u});return{drawingId:u,zIndex:o}}).sort(r===!1?ie.sortRules:ie.sortRulesByDesc)}_hasDrawingOrder(e){if(e==null)return-1;const{unitId:t,subUnitId:n,drawingId:r}=e;return this._establishDrawingMap(t,n),this._getDrawingOrder(t,n).indexOf(r)}_getCurrentBySearch(e){var u,o,l;if(e==null)return;const{unitId:t,subUnitId:n,drawingId:r}=e;return(l=(o=(u=this.drawingManagerData[t])==null?void 0:u[n])==null?void 0:o.data)==null?void 0:l[r]}_getOldBySearch(e){var u,o,l;if(e==null)return;const{unitId:t,subUnitId:n,drawingId:r}=e;return(l=(o=(u=this._oldDrawingManagerData[t])==null?void 0:u[n])==null?void 0:o.data)==null?void 0:l[r]}_establishDrawingMap(e,t,n){var r;return this.drawingManagerData[e]||(this.drawingManagerData[e]={}),this.drawingManagerData[e][t]||(this.drawingManagerData[e][t]={data:{},order:[]}),n==null?null:(r=this.drawingManagerData[e][t].data)==null?void 0:r[n]}_addByParam(e){const{unitId:t,subUnitId:n,drawingId:r}=e;this._establishDrawingMap(t,n,r);const u=F.insertOp([t,n,"data",r],e),o=F.insertOp([t,n,"order",this._getDrawingOrder(t,n).length],r),l=[u,o].reduce(F.type.compose,null),D=F.type.invertWithDoc(l,this.drawingManagerData);return{op:l,invertOp:D}}_removeByParam(e){if(e==null)return{op:[],invertOp:[]};const{unitId:t,subUnitId:n,drawingId:r}=e;if(this._establishDrawingMap(t,n,r)==null)return{op:[],invertOp:[]};const o=F.removeOp([t,n,"data",r],!0),l=F.removeOp([t,n,"order",this._getDrawingOrder(t,n).indexOf(r)],!0),D=[o,l].reduce(F.type.compose,null),x=F.type.invertWithDoc(D,this.drawingManagerData);return{op:D,invertOp:x}}_updateByParam(e){const{unitId:t,subUnitId:n,drawingId:r}=e,u=this._establishDrawingMap(t,n,r);if(u==null)return{op:[],invertOp:[]};const l=this._getUpdateParamCompareOp(e,u).reduce(F.type.compose,null),D=F.type.invertWithDoc(l,this.drawingManagerData);return{op:l,invertOp:D}}_getUpdateParamCompareOp(e,t){const{unitId:n,subUnitId:r,drawingId:u}=e,o=[];return Object.keys(e).forEach(l=>{const D=e[l],x=t[l];x!==D&&o.push(F.replaceOp([n,r,"data",u,l],x,D))}),o}_getDrawingData(e,t){var n,r;return((r=(n=this.drawingManagerData[e])==null?void 0:n[t])==null?void 0:r.data)||{}}_getDrawingOrder(e,t){var n,r;return((r=(n=this.drawingManagerData[e])==null?void 0:n[t])==null?void 0:r.order)||[]}getDrawingVisible(){return this._visible}getDrawingEditable(){return this._editable}setDrawingVisible(e){this._visible=e}setDrawingEditable(e){this._editable=e}}class Nt extends kt{}class Rt{constructor(){H(this,"_waitCount",0);H(this,"_change$",new Ie.Subject);H(this,"change$",this._change$);H(this,"_imageSourceCache",new Map)}setWaitCount(e){this._waitCount=e,this._change$.next(e)}getImageSourceCache(e,t){if(t===ie.ImageSourceType.BASE64){const n=new Image;return n.src=e,n}return this._imageSourceCache.get(e)}addImageSourceCache(e,t,n){t===ie.ImageSourceType.BASE64||n==null||this._imageSourceCache.set(e,n)}async getImage(e){return Promise.resolve(e)}async saveImage(e){return new Promise((t,n)=>{if(!At.includes(e.type)){n(new Error(ie.ImageUploadStatusType.ERROR_IMAGE_TYPE)),this._decreaseWaiting();return}if(e.size>Tt){n(new Error(ie.ImageUploadStatusType.ERROR_EXCEED_SIZE)),this._decreaseWaiting();return}const r=new FileReader;r.readAsDataURL(e),r.onload=u=>{var D;const o=(D=u.target)==null?void 0:D.result;if(o==null){n(new Error(ie.ImageUploadStatusType.ERROR_IMAGE)),this._decreaseWaiting();return}const l=ie.generateRandomId(6);t({imageId:l,imageSourceType:ie.ImageSourceType.BASE64,source:o,base64Cache:o,status:ie.ImageUploadStatusType.SUCCUSS}),this._decreaseWaiting()}})}_decreaseWaiting(){this._waitCount-=1,this._change$.next(this._waitCount)}}class Wt extends ie.Disposable{constructor(){super(...arguments);H(this,"_urlImageDownloader",null)}registerURLImageDownloader(t){return this._urlImageDownloader=t,ie.toDisposable(()=>{this._urlImageDownloader=null})}async getImage(t){if(this._urlImageDownloader)try{return await this._urlImageDownloader(t)}catch(n){console.error(`Custom downloader failed for ${t}, falling back to default behavior:`,n)}return t}async downloadImage(t){if(this._urlImageDownloader)try{const r=await this._urlImageDownloader(t);return await(await fetch(r)).blob()}catch(r){console.error(`Custom downloader failed for ${t}, falling back to default fetch:`,r)}return await(await fetch(t)).blob()}}var Qt=Object.getOwnPropertyDescriptor,en=(a,e,t,n)=>{for(var r=n>1?void 0:n?Qt(e,t):e,u=a.length-1,o;u>=0;u--)(o=a[u])&&(r=o(r)||r);return r},ct=(a,e)=>(t,n)=>e(t,n,a);const tn="UNIVER_DRAWING_PLUGIN";var dt;exports.UniverDrawingPlugin=(dt=class extends ie.Plugin{constructor(e=mt,t,n,r){super(),this._config=e,this._injector=t,this._configService=n,this._commandService=r;const{...u}=ie.merge({},mt,this._config);this._configService.setConfig(Ft,u)}onStarting(){this._initCommands(),this._initDependencies()}_initDependencies(){var n;const e=[[ie.IImageIoService,{useClass:Rt}],[ie.IURLImageService,{useClass:Wt}],[ht,{useClass:Nt}]];ie.mergeOverrideWithDependencies(e,(n=this._config)==null?void 0:n.override).forEach(r=>this._injector.add(r))}_initCommands(){[Mt].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}},H(dt,"pluginName",tn),dt);exports.UniverDrawingPlugin=en([ct(1,ie.Inject(ie.Injector)),ct(2,ie.IConfigService),ct(3,ie.ICommandService)],exports.UniverDrawingPlugin);function nn({unitId:a,subUnitId:e,drawingId:t},n){return typeof n=="number"?`${a}#-#${e}#-#${t}#-#${n}`:`${a}#-#${e}#-#${t}`}const rn=async a=>new Promise((e,t)=>{const n=new Image;n.src=a,n.onload=()=>{e({width:n.width,height:n.height,image:n})},n.onerror=r=>{t(r)}});Object.defineProperty(exports,"IImageIoService",{enumerable:!0,get:()=>ie.IImageIoService});Object.defineProperty(exports,"ImageSourceType",{enumerable:!0,get:()=>ie.ImageSourceType});Object.defineProperty(exports,"ImageUploadStatusType",{enumerable:!0,get:()=>ie.ImageUploadStatusType});exports.DRAWING_IMAGE_ALLOW_IMAGE_LIST=At;exports.DRAWING_IMAGE_ALLOW_SIZE=Tt;exports.DRAWING_IMAGE_COUNT_LIMIT=Ht;exports.DRAWING_IMAGE_HEIGHT_LIMIT=$t;exports.DRAWING_IMAGE_WIDTH_LIMIT=Kt;exports.DrawingManagerService=Nt;exports.IDrawingManagerService=ht;exports.ImageIoService=Rt;exports.SetDrawingSelectedOperation=Mt;exports.URLImageService=Wt;exports.UnitDrawingService=kt;exports.getDrawingShapeKeyByDrawingSearch=nn;exports.getImageSize=rn;