@univerjs/drawing 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var xt=Object.defineProperty;var Bt=(a,e,n)=>e in a?xt(a,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):a[e]=n;var H=(a,e,n)=>Bt(a,typeof e!="symbol"?e+"":e,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const pe=require("@univerjs/core"),Ie=require("rxjs"),Gt=500,Lt=500,qt=10,Pt=5*1024*1024,Tt=["image/png","image/jpeg","image/jpg","image/gif","image/bmp"];var Ve={},Qe={},tt={},yt;function Kt(){if(yt)return tt;yt=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={},_t;function Ht(){if(_t)return nt;_t=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={},vt;function At(){return vt||(vt=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 ge=0;ge<s.length;ge++){const Z=s[ge];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(ge){let Z;for(;U;){const Ce=Z=_.getKey();if(ge!=null){let Le=!1;if(s&&typeof Ce=="number"&&(Z=s(Ce,_.getComponent()),Z<0&&(Z=~Z,Le=!0)),t(Z,ge))return null;if(Z===ge&&!Le)return _}c&&typeof Z=="number"&&c(Z,_.getComponent()),U=_.nextSibling()}return null}return Y.end=()=>{h&&_.ascend()},Y}a.advancer=E;function W(_,s,c){let h,U,Y,ge;for(h=U=_&&_.descendFirst(),Y=ge=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(),ge&&s.ascend()}a.eachChildOf=W}(at)),at}var lt={},mt;function Mt(){return mt||(mt=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={},Ot;function ft(){return Ot||(Ot=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={},bt;function dt(){return bt||(bt=1,function(a){Object.defineProperty(a,"__esModule",{value:!0}),a.uniSlice=a.dlen=a.eachOp=void 0;const e=ft(),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 L=f[w];switch(typeof L){case"object":if(typeof L.d!="number"&&typeof L.d!="string")throw Error("Delete must be number or string");if(a.dlen(L.d)<=0)throw Error("Deletes must not be empty");break;case"string":if(!(L.length>0))throw Error("Inserts cannot be empty");break;case"number":if(!(L>0))throw Error("Skip components must be >0");if(typeof y=="number")throw Error("Adjacent skip components should be combined");break}y=L}if(typeof y=="number")throw Error("Op has a trailing skip")};function t(f,y){let w=0,L=0;for(let z=0;z<f.length;z++){const q=f[z];switch(y(q,w,L),typeof q){case"object":w+=a.dlen(q.d);break;case"string":L+=e.strPosToUni(q);break;case"number":w+=q,L+=q;break}}}a.eachOp=t;function r(f,y){const w=[],L=l(w);return t(f,(z,q,De)=>{L(y(z,q,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 L=e.uniToStrPos(f,y),z=w==null?1/0:e.uniToStrPos(f,w);return f.slice(L,z)};const W=(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:(q,De)=>{if(y===f.length)return q===-1?null:q;const ce=f[y];let ne;if(typeof ce=="number")return q===-1||ce-w<=q?(ne=ce-w,++y,w=0,ne):(w+=q,q);if(typeof ce=="string"){if(q===-1||De==="i"||e.strPosToUni(ce.slice(w))<=q)return ne=ce.slice(w),++y,w=0,ne;{const le=w+e.uniToStrPos(ce.slice(w),q);return ne=ce.slice(w,le),w=le,ne}}else{if(q===-1||De==="d"||a.dlen(ce.d)-w<=q)return ne={d:W(ce.d,w)},++y,w=0,ne;{let le=W(ce.d,w,w+q);return w+=q,{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 L=[],z=l(L),{take:q,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=q(ye,"i"),z(Se),typeof Se!="string"&&(ye-=E(Se));break;case"string":w==="left"&&typeof De()=="string"&&z(q(-1)),z(e.strPosToUni(le));break;case"object":for(ye=a.dlen(le.d);ye>0;)switch(Se=q(ye,"i"),typeof Se){case"number":ye-=Se;break;case"string":z(Se);break;case"object":ye-=a.dlen(Se.d)}break}}let ce;for(;ce=q(-1);)z(ce);return s(L)}function h(f,y){n(f),n(y);const w=[],L=l(w),{take:z}=_(f);for(let De=0;De<y.length;De++){const ce=y[De];let ne,le;switch(typeof ce){case"number":for(ne=ce;ne>0;)le=z(ne,"d"),L(le),typeof le!="object"&&(ne-=E(le));break;case"string":L(ce);break;case"object":ne=a.dlen(ce.d);let ye=0;for(;ye<ne;)switch(le=z(ne-ye,"d"),typeof le){case"number":L({d:W(ce.d,ye,ye+le)}),ye+=le;break;case"string":ye+=e.strPosToUni(le);break;case"object":L(le)}break}}let q;for(;q=z(-1);)L(q);return s(w)}const U=(f,y)=>{let w=0;for(let L=0;L<y.length&&f>w;L++){const z=y[L];switch(typeof z){case"number":{w+=z;break}case"string":const q=e.strPosToUni(z);w+=q,f+=q;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 ge(f,y,w){return r(f,(L,z)=>typeof L=="object"&&typeof L.d=="number"?{d:w.slice(y,z,z+L.d)}:L)}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 Le(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 L=f.builder(y);for(let z=0;z<w.length;z++){const q=w[z];switch(typeof q){case"number":L.skip(q);break;case"string":L.append(q);break;case"object":L.del(a.dlen(q.d));break}}return L.build()},transform:c,compose:h,transformPosition:U,transformSelection:Y,isInvertible:Le,makeInvertible(y,w){return ge(y,w,f)},stripInvertible:Ce,invert:Z,invertWithDoc(y,w){return Z(ge(y,w,f))},isNoop:y=>y.length===0}}a.default=me}(ut)),ut}var rt={},wt;function Ft(){if(wt)return rt;wt=1,Object.defineProperty(rt,"__esModule",{value:!0});const a=dt(),e=ft();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 W=a.dlen(o.d);this.onRemove&&this.onRemove(E,W)}})},onInsert:null,onRemove:null}}return rt.default=n,n.provides={text:!0},rt}var It;function Vt(){return It||(It=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=ft(),o=t(dt()),l=r(Ft()),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},W=o.default(E),_=Object.assign(Object.assign({},W),{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=dt();Object.defineProperty(a,"makeType",{enumerable:!0,get:function(){return s.default}})}(ke)),ke}var Ct;function zt(){return Ct||(Ct=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(Kt()),t=e(Ht()),r=At(),u=Mt();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:q,apply:De,transformPosition:ce,compose:ne,tryTransform:st,transform:Rt,makeInvertible:Se,invert:le,invertWithDoc:$t,RM_UNEXPECTED_CONTENT:u.ConflictType.RM_UNEXPECTED_CONTENT,DROP_COLLISION:u.ConflictType.DROP_COLLISION,BLACKHOLE:u.ConflictType.BLACKHOLE,transformNoConflict:(i,d,m)=>pt(()=>!0,i,d,m),typeAllowingConflictsPred:i=>Object.assign(Object.assign({},a.type),{transform:(d,m,D)=>pt(i,d,m,D)})};const l=i=>i?i.getComponent():null;function E(i){return i&&typeof i=="object"&&!Array.isArray(i)}const W=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,ge={};function Z(i){let d=i.type?i.type:i;d.name&&(ge[d.name]=d),d.uri&&(ge[d.uri]=d)}const Ce=i=>{const d=ge[i];if(d)return d;throw Error("Missing type: "+i)};Z(Vt());const Le=(i,d)=>i+d;Z({name:"number",apply:Le,compose:Le,invert:i=>-i,transform:i=>i});const me=i=>i==null?null:i.et?Ce(i.et):i.es?ge["text-unicode"]:i.ena!=null?ge.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 L(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 R=!0,N=!1;for(let p in C){const v=C[p];if(R=!1,o(p==="p"||p==="r"||p==="d"||p==="i"||p==="e"||p==="es"||p==="ena"||p==="et","Invalid component item '"+p+"'"),p==="p")w(v),o(!d.has(v)),d.add(v),o(C.r===void 0);else if(p==="d")w(v),o(!m.has(v)),m.add(v),o(C.i===void 0);else if(p==="e"||p==="es"||p==="ena"){o(!N),N=!0;const b=me(C);o(b,"Missing type in edit"),b.checkValidOp&&b.checkValidOp(f(C))}}o(!R)},O=(C,R,N)=>{if(!Array.isArray(C))throw Error("Op must be null or a list");if(C.length===0)throw Error("Empty descent");R||L(C[0]);let p=1,v=0,b=0;for(let I=0;I<C.length;I++){const $=C[I];if(o($!=null),Array.isArray($)){const x=O($,!1);if(v){const g=typeof b,M=typeof x;g===M?o(b<x,"descent keys are not in order"):o(g==="number"&&M==="string")}b=x,v++,p=3}else typeof $=="object"?(o(p===1,`Prev not scalar - instead ${p}`),D($),p=2):(o(p!==3),L($),o(r.isValidPathItem($),"Invalid path key"),p=1)}return o(v!==1,"Operation makes multiple descents. Remove some []"),o(p===2||p===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 q(i){let d=0,m=[];const D=r.writeCursor();return D.mergeTree(i,(O,C)=>{const R=me(O);if(R){const p=f(O);y(C,R,R.normalize?R.normalize(p):p)}for(const p of["r","p","i","d"])if(O[p]!==void 0){const v=p==="p"||p==="d"?(N=O[p],m[N]==null&&(m[N]=d++),m[N]):O[p];C.write(p,v)}var N}),D.get()}function De(i,d){if(z(d),d===null)return i;const m=[];return function D(O,C){let R=O,N=0,p={root:O},v=0,b=p,I="root";function $(){for(;v<N;v++){let x=C[v];typeof x!="object"&&(o(U(b,I)),b=b[I]=W(b[I]),I=x)}}for(;N<C.length;N++){const x=C[N];if(Array.isArray(x)){const g=D(R,x);g!==R&&g!==void 0&&($(),R=b[I]=g)}else if(typeof x=="object"){x.d!=null?($(),R=h(b,I,m[x.d])):x.i!==void 0&&($(),R=h(b,I,x.i));const g=me(x);if(g)$(),R=b[I]=g.apply(R,f(x));else if(x.e!==void 0)throw Error("Subtype "+x.et+" undefined")}else R=Y(R,x)}return p.root}(i=function D(O,C){const R=[];let N=0;for(;N<C.length;N++){const I=C[N];if(Array.isArray(I))break;typeof I!="object"&&(R.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.pop();O=O===Y($,I)?$:O===void 0?c($,I):(v=I,b=O,(p=W(p=$))[v]=b,p)}else _(I)&&(o(O!==void 0,"Cannot pick up or remove undefined"),I.p!=null&&(m[I.p]=O),O=void 0)}var p,v,b;return O}(i,d),d)}function ce(i,d){i=i.slice(),z(d);const m=r.readCursor(d);let D,O,C=!1;const R=[];for(let p=0;;p++){const v=i[p],b=m.getComponent();if(b&&(b.r!==void 0?C=!0:b.p!=null&&(C=!1,D=b.p,O=p)),p>=i.length)break;let I=0;const $=r.advancer(m,void 0,(g,M)=>{_(M)&&I++});R.unshift($);const x=$(v);if(typeof v=="number"&&(i[p]-=I),!x)break}if(R.forEach(p=>p.end()),C)return null;const N=()=>{let p=0;if(D!=null){const v=m.getPath();p=v.length,i=v.concat(i.slice(O))}for(;p<i.length;p++){const v=i[p],b=l(m),I=me(b);if(I){const g=f(b);I.transformPosition&&(i[p]=I.transformPosition(i[p],g));break}let $=0;const x=r.advancer(m,(g,M)=>s(M)?~(g-$):g-$,(g,M)=>{s(M)&&$++})(v);if(typeof v=="number"&&(i[p]+=$),!x)break}};return D!=null?m.eachDrop(null,p=>{p===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(),R=[],N=[],p=[],v=[],b=[],I=[],$=new Set;D.traverse(null,g=>{g.p!=null&&(p[g.p]=D.clone())}),O.traverse(null,g=>{g.d!=null&&(v[g.d]=O.clone())});const x=r.writeCursor();return function g(M,re,te,K,se,Re,be,_e){o(re||te);const oe=l(re),Pe=l(te),Ae=!!Pe&&Pe.r!==void 0,qe=!!oe&&oe.i!==void 0,Te=oe?oe.d:null,Ee=Pe?Pe.p:null,We=(Re||Ae)&&Ee==null;if(Ee!=null)K=v[Ee],be=N[Ee]=new r.WriteCursor;else if(Pe&&Pe.r!==void 0)K=null;else{const P=l(K);P&&P.d!=null&&(K=null)}const Q=l(K);if(Te!=null)if(M=p[Te],_e=R[Te]=new r.WriteCursor,We)Re&&!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;qe?(o(se===void 0),T=oe.i):T=se;const B=(Ee==null?!qe||Re||Ae:T===void 0)?null:be.getComponent();if(Ee!=null){if(!(se!==void 0||qe)){const P=Te!=null?b[Te]:m++;I[Ee]=P,_e.write("p",P)}}else Ae&&(qe||se!==void 0||(Pe.r,_e.write("r",Pe.r)));const A=We?null:me(oe),S=me(Q);if((A||S)&&(A&&A.name,S&&S.name),A&&S){o(A===S);const P=f(oe),G=f(Q),he=A.compose(P,G);y(be,A,he),$.add(Q)}else A?y(be,A,f(oe)):S&&(y(be,S,f(Q)),$.add(Q));const j=typeof T=="object"&&T!=null;let X=!1,J=0,ee=0,fe=0,de=0,ae=0;const Oe=r.advancer(K,(P,G)=>s(G)?de-P-1:P-de,(P,G)=>{s(G)&&de++}),V=r.advancer(M,(P,G)=>_(G)?J-P-1:P-J,(P,G)=>{_(G)&&J++});if(r.eachChildOf(re,te,(P,G,he)=>{let we,xe,Ke=P,Me=P,Ze=P;if(typeof P=="number"){let ve=P+fe;xe=Oe(ve),Me=ve+de;let ue=P+ee;we=V(ue),s(l(xe))&&(we=null),Ke=ue+J,Ze=P+ae,o(Ke>=0,"p1PickKey is negative"),o(Me>=0,"p2DropKey is negative");const je=s(l(G)),Be=_(l(he));(je||Be&&!We)&&ae--,je&&ee--,Be&&fe--}else we=V(P),xe=Oe(P);_e.descend(Ke),be.descend(Me);const Je=j&&!s(l(G))?T[Ze]:void 0,Ne=g(we,G,he,xe,Je,We,be,_e);var $e,k,ie;j&&!We?Je!==Ne&&(X||(T=Array.isArray(T)?T.slice():Object.assign({},T),X=!0),$e=T,ie=Ne,typeof(k=Ze)=="number"?(o(Array.isArray($e)),o(k<$e.length)):(o(!Array.isArray($e)),o($e[k]!==void 0)),ie===void 0?typeof k=="number"?$e.splice(k,1):delete $e[k]:$e[k]=ie):o(Ne===void 0),be.ascend(),_e.ascend()}),V.end(),Oe.end(),B!=null)B.i=T;else if(!Re&&!Ae&&Ee==null)return T}(D,D.clone(),O,O.clone(),void 0,!1,C,x),C.reset(),C.mergeTree(x.get()),C.reset(),C.get(),R.map(g=>g.get()),N.map(g=>g.get()),D.traverse(C,(g,M)=>{const re=g.p;if(re!=null){const te=b[re];te!=null&&M.write("p",te);const K=R[re];K&&K.get(),K&&M.mergeTree(K.get())}else g.r!==void 0&&M.write("r",g.r)}),C.reset(),C.get(),O.traverse(C,(g,M)=>{const re=g.d;if(re!=null){const K=I[re];K!=null&&M.write("d",K);const se=N[re];se&&M.mergeTree(se.get())}else g.i!==void 0&&M.write("i",g.i);const te=me(g);te&&!$.has(g)&&y(M,te,f(g))}),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 R(N,p,v){const b=N.getComponent();let I,$=!1;if(b){b.p!=null&&(p.write("d",b.p),O[b.p]=N.clone()),b.r!==void 0&&p.write("i",b.r),b.d!=null&&(p.write("p",b.d),v=void 0),b.i!==void 0&&(v=I=b.i);const g=me(b);g&&(v===void 0?(D||(D=new Set),D.add(b)):(f(b),v=g.apply(v,f(b)),$=!0))}let x=0;for(const g of N){p.descend(g);const M=typeof g=="number"?g-x:g,re=Y(v,M);s(N.getComponent())&&x++;const te=R(N,p,re);if(v!==void 0&&te!==void 0){if($||($=!0,v=W(v)),!U(v,M))throw Error("Cannot modify child - invalid operation");v[M]=te}p.ascend()}if(I===void 0)return $?v:void 0;p.write("r",v)}(d,m,void 0),D&&(m.reset(),function R(N,p,v){const b=p.getComponent();if(b){const g=b.d;if(g!=null&&(N=O[g],v=C[g]=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,$=0;const x=r.advancer(N,(g,M)=>_(M)?I-g-1:g-I,(g,M)=>{_(M)&&I++});for(const g of p)if(typeof g=="number"){const M=g-$,re=x(M),te=M+I;v.descend(te),R(re,p,v),s(p.getComponent())&&$++,v.ascend()}else v.descend(g),R(x(g),p,v),v.ascend();x.end()}(d.clone(),d,m),C.length&&(m.reset(),d.traverse(m,(R,N)=>{const p=R.p;if(p!=null){const v=C[p];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,p=>{var v;return p.r!==void 0||((v=me(p))===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=[],R=[],N=(p,v,b)=>{const I=p.getComponent();let $=!1;if(I){I.d!=null&&v.write("d",I.d),I.i!==void 0&&v.write("i",I.i);const g=I.p;if(g!=null&&(C[g]=p.clone(),o(b!==void 0,"Operation picks up at an invalid key"),R[g]=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 x=0;for(const g of p){v.descend(g);const M=typeof g=="number"?g-x:g,re=Y(b,M),te=N(p,v,re);re!==te&&($||($=!0,b=W(b)),te===void 0?(b=c(b,M),typeof g=="number"&&x++):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 p(v,b,I,$,x){const g=b.getComponent();if(g){g.i!==void 0?($=g.i,x=!0):g.d!=null&&($=R[g.d],v=C[g.d],x=!1,g.d);let K=me(g);if(K&&K.makeInvertible){const se=f(g);y(I,K,K.makeInvertible(se,$),!0)}}let M=0,re=0;const te=r.advancer(v,(K,se)=>_(se)?M-K-1:K-M,(K,se)=>{_(se)&&M++});for(const K of b)if(typeof K=="number"){const se=K-re,Re=te(se),be=se+M,_e=Y($,x?se:be);I.descend(K),p(Re,b,I,_e,x),s(b.getComponent())&&re++,I.ascend()}else{const se=Y($,K);I.descend(K),p(te(K),b,I,se,x),I.ascend()}te.end()}(m.clone(),m,D,d,!1)),D.get()}function $t(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=[],R=[],N=[],p=[],v=[],b=[],I=[],$=[],x=[],g=[],M=[],re=[],te=[],K=[],se=[];let Re=0;const be=r.readCursor(i),_e=r.readCursor(d),oe=r.writeCursor();if(function Q(T,B=null,A){const S=l(B);S&&(S.r!==void 0?A=B.clone():S.p!=null&&(A=null,b[S.p]=T.clone()));const j=T.getComponent();let X;j&&(X=j.p)!=null&&(v[X]=B?B.clone():null,N[X]=T.clone(),A&&(g[X]=!0,x[X]=A),S&&S.p!=null&&(K[X]=S.p));const J=r.advancer(B);for(const ee of T)Q(T,J(ee),A);J.end()}(_e,be,null),function Q(T,B,A,S,j){const X=A.getComponent();let J,ee=!1;X&&((J=X.d)!=null?(p[J]=A.clone(),S!=null&&(se[S]==null&&(se[S]=[]),se[S].push(J)),T=v[J]||null,B=N[J]||null,g[J]?(j&&(M[J]=!0),j=x[J]||null):!j||D!==1&&K[J]!=null||O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(j.getPath()),op2:a.moveOp(B.getPath(),A.getPath())}),ee=!0):X.i!==void 0&&(T=B=null,ee=!0,j&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(j.getPath()),op2:a.insertOp(A.getPath(),X.i)})));const fe=l(T);fe&&(fe.r!==void 0?j=T.clone():fe.p!=null&&(fe.p,S=fe.p,j=null));const de=me(X);de&&j&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(j.getPath()),op2:a.editOp(A.getPath(),de,f(X),!0)});let ae=0,Oe=0;const V=r.advancer(B,(G,he)=>_(he)?ae-G-1:G-ae,(G,he)=>{_(he)&&ae++}),P=r.advancer(T);for(const G of A)if(typeof G=="number"){const he=G-Oe,we=V(he);Oe+=+Q(P(he+ae),we,A,S,j)}else{const he=V(G);Q(P(G),he,A,S,j)}return V.end(),P.end(),ee}(be,_e,_e.clone(),null,null),p.map(Q=>Q&&Q.get()),O)return{ok:!1,conflict:O};M.map(Q=>!!Q);const Pe=[];let Ae=null;(function Q(T,B,A,S,j){let X=!1;const J=l(B);if(_(J)){const V=J.p;V!=null?(A=p[V],S=re[V]=r.writeCursor(),X=!0,j=null):(A=null,j=B.clone())}else s(l(A))&&(A=null);const ee=T.getComponent();if(ee){const V=ee.p;V!=null?(j&&($[V]=j),Pe[V]=j||D===1&&X?null:S.getComponent(),C[V]=T.clone(),A&&(I[V]=A.clone())):ee.r!==void 0&&(j||S.write("r",!0),(j||X)&&(Ae==null&&(Ae=new Set),Ae.add(ee)))}let fe=0,de=0;const ae=r.advancer(B,void 0,(V,P)=>{_(P)&&fe++}),Oe=r.advancer(A,(V,P)=>s(P)?~(V-de):V-de,(V,P)=>{s(P)&&de++});if(T)for(const V of T)if(typeof V=="string"){const P=ae(V),G=Oe(V);S.descend(V),Q(T,P,G,S,j),S.ascend()}else{const P=ae(V),G=V-fe,he=_(l(P))?null:Oe(G),we=G+de;o(we>=0),S.descend(we),Q(T,P,he,S,j),S.ascend()}ae.end(),Oe.end()})(be,_e,_e.clone(),oe,null),oe.reset();let qe=[];if(function Q(T,B,A,S,j,X){o(B);const J=B.getComponent();let ee=l(S),fe=!1;const de=(k,ie,ve)=>k?a.moveOp(k.getPath(),ie.getPath()):a.insertOp(ie.getPath(),ve.i);if(s(J)){const k=J.d;k!=null&&(R[k]=B.clone());const ie=k!=null?Pe[k]:null;let ve=!1;if(J.i!==void 0||k!=null&&ie){let ue;ee&&(ee.i!==void 0||(ue=ee.d)!=null&&!g[ue])&&(ve=ue!=null?k!=null&&k===K[ue]:n.default(ee.i,J.i),ve||ue!=null&&D!==1&&K[ue]!=null||O==null&&(O={type:u.ConflictType.DROP_COLLISION,op1:de(k!=null?C[k]:null,B,J),op2:de(ue!=null?N[ue]:null,S,ee)})),ve||(X?O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:de(k!=null?C[k]:null,B,J),op2:a.removeOp(X.getPath())}):(k!=null?(qe[Re]=k,j.write("d",ie.p=Re++)):j.write("i",t.default(J.i)),fe=!0))}else if(k!=null&&!ie){const ue=$[k];ue&&(X=ue.clone())}k!=null?(T=C[k],A=b[k],S=I[k]):J.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)||g[k]?(S=null,X=A.clone()):k!=null&&(S=p[k],D!==1&&K[k]!=null||((j=te[k])||(j=te[k]=r.writeCursor()),j.reset(),X=null))}else!s(J)&&s(ee)&&(S=null);ee=S!=null?S.getComponent():null;const V=me(J);if(V){const k=f(J);if(X)O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.editOp(B.getPath(),V,k,!0),op2:a.removeOp(X.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,G=0,he=0,we=0,xe=0,Ke=0,Me=T!=null&&T.descendFirst(),Ze=Me;const Je=r.advancer(A,void 0,(k,ie)=>{_(ie)&&he++});let Ne=S!=null&&S.descendFirst(),$e=Ne;for(const k of B)if(typeof k=="number"){let ie;const ve=s(B.getComponent()),ue=k-G;{let Ge;for(;Me&&typeof(Ge=T.getKey())=="number";){Ge+=P;const Ue=T.getComponent(),Ye=_(Ue);if(Ge>ue||Ge===ue&&(!Ye||D===0&&ve))break;if(Ye){P--;const He=Ue.p;K.includes(He),Ue.d,l(te[Ue.d]),_(l(te[Ue.d])),(Ue.r===void 0||Ae&&Ae.has(Ue))&&(He==null||!Pe[He]||D!==1&&K.includes(He))||xe--}Me=T.nextSibling()}ie=Me&&Ge===ue?T:null}const je=ue-P;let Be=Je(je);const ot=je-he;let et=null;{let Ge,Ue;for(;Ne&&typeof(Ge=S.getKey())=="number";){Ue=Ge-we;const Ye=S.getComponent(),He=s(Ye);if(Ue>ot)break;if(Ue===ot){if(!He){et=S;break}{if(D===0&&ve){et=S;break}const Fe=Be&&_(Be.getComponent());if(D===0&&Fe)break}}if(He){const Fe=Ye.d;K[Fe],Ye.i===void 0&&(g[Fe]||K[Fe]!=null&&D!==1)?(g[Fe]||K[Fe]!=null&&D===0)&&(we++,Ke--):we++}Ne=S.nextSibling()}}const gt=ot+we+xe+Ke;o(gt>=0,"trying to descend to a negative index"),j.descend(gt),ve&&(ie=Be=et=null,G++),Q(ie,B,Be,et,j,X)&&Ke++,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 Be=Ne&&je===k?S:null;j.descend(k),Q(ve,B,ue,Be,j,X),j.ascend()}return Je.end(),Ze&&T.ascend(),$e&&S.ascend(),fe}(be,be.clone(),_e,_e.clone(),oe,null),O)return{ok:!1,conflict:O};oe.reset();const Te=(Q,T,B)=>Q.traverse(T,(A,S)=>{A.d!=null&&B(A.d,Q,S)});(g.length||re.length)&&(Te(_e,oe,(Q,T,B)=>{g[Q]&&!M[Q]&&B.write("r",!0),re[Q]&&B.mergeTree(re[Q].get())}),oe.reset());const Ee=[],We=[];if((te.length||g.length)&&!O){const Q=r.readCursor(it(oe.get()));if(Te(Q,null,(T,B)=>{Ee[T]=B.clone()}),te.forEach(T=>{T&&Te(r.readCursor(T.get()),null,(B,A)=>{Ee[B]=A.clone()})}),function T(B,A,S,j,X,J){const ee=l(A);if(ee&&_(ee))if(ee.p!=null){const P=ee.p;Ee[P].getPath(),S=Ee[P],j=We[P]=r.writeCursor()}else ee.r!==void 0&&(S=null);else s(l(S))&&(S=null);const fe=B.getComponent();if(fe){let P;if((P=fe.d)!=null){const G=te[P];G&&(G.get(),j.mergeTree(G.get()),S=r.readCursor(G.get()))}}let de=0,ae=0;const Oe=r.advancer(A,void 0,(P,G)=>{_(G)&&de--}),V=r.advancer(S,(P,G)=>s(G)?-(P-ae)-1:P-ae,(P,G)=>{s(G)&&ae++});for(const P of B)if(typeof P=="number"){const G=Oe(P),he=P+de,we=V(he),xe=he+ae;j.descend(xe),T(B,G,we,j),j.ascend()}else j.descend(P),T(B,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(),We.length){const T=We.map(A=>A?A.get():null),B=r.readCursor(it(oe.get()));if(Te(B,oe,(A,S,j)=>{const X=T[A];X&&(j.mergeTree(X),T[A]=null)}),T.find(A=>A)){const A=r.writeCursor(),S=r.writeCursor();let j=0,X=0;T.forEach(J=>{J!=null&&Te(r.readCursor(J),null,ee=>{const fe=qe[ee];A.writeMove(C[fe].getPath(),R[fe].getPath(),j++);const de=se[fe];de&&de.forEach(ae=>{g[ae]||D!==1&&K[ae]!=null||S.writeMove(N[ae].getPath(),p[ae].getPath(),X++)})})}),O={type:u.ConflictType.BLACKHOLE,op1:A.get(),op2:S.get()}}}}return O?{ok:!1,conflict:O}:{ok:!0,result:oe.get()}}const ht=i=>{const d=new Error("Transform detected write conflict");throw d.conflict=i,d.type=d.name="writeConflict",d};function Rt(i,d,m){const D=st(i,d,m);if(D.ok)return D.result;ht(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()},Wt=(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,R=>{R.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 pt(i,d,m,D){let O=null;for(;;){const C=st(d,m,D);if(C.ok)return ne(O,C.result);{const{conflict:R}=C;i(R)||ht(R);const[N,p]=Wt(R,D);d=ne(q(d),N),m=ne(q(m),p),O=ne(O,p)}}}}(Qe)),Qe}var Et;function Xt(){return Et||(Et=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(zt(),a);var t=At();Object.defineProperty(a,"ReadCursor",{enumerable:!0,get:function(){return t.ReadCursor}}),Object.defineProperty(a,"WriteCursor",{enumerable:!0,get:function(){return t.WriteCursor}});var r=Mt();Object.defineProperty(a,"ConflictType",{enumerable:!0,get:function(){return r.ConflictType}})}(Ve)),Ve}var F=Xt();class jt{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(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(W=>{const{op:_,invertOp:s}=this._addByParam(W);n.push({unitId:W.unitId,subUnitId:W.subUnitId,drawingId:W.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:W,invertOp:_}=this._removeByParam(E);n.unshift(W),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(W=>{const{op:_,invertOp:s}=this._updateByParam(W);n.push({unitId:W.unitId,subUnitId:W.subUnitId,drawingId:W.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 W=o[E];W.groupId===r&&l.push(W)}),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(W=>{const{unitId:_,subUnitId:s,drawingId:c}=W,h=this._hasDrawingOrder({unitId:_,subUnitId:s,drawingId:c});E=Math.max(E,h),l.push(...this._getUpdateParamCompareOp(W,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:W,subUnitId:_,drawingId:s}=E;l.push(...this._getUpdateParamCompareOp(E,this.getDrawingByParam({unitId:W,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){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 W=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(W,this.drawingManagerData),redo:W,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 W=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(W,this.drawingManagerData),redo:W,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?pe.sortRules:pe.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),W=F.type.invertWithDoc(E,this.drawingManagerData);return{op:E,invertOp:W}}_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],W=n[l];W!==E&&o.push(F.replaceOp([t,r,"data",u,l],W,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 Ut extends jt{}function Jt({unitId:a,subUnitId:e,drawingId:n},t){return typeof t=="number"?`${a}#-#${e}#-#${n}#-#${t}`:`${a}#-#${e}#-#${n}`}const Yt=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)}}),Zt="drawing.config",Dt={},kt=pe.createIdentifier("univer.drawing-manager.service");class Nt{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,n){if(n===pe.ImageSourceType.BASE64){const t=new Image;return t.src=e,t}return this._imageSourceCache.get(e)}addImageSourceCache(e,n,t){n===pe.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(!Tt.includes(e.type)){t(new Error(pe.ImageUploadStatusType.ERROR_IMAGE_TYPE)),this._decreaseWaiting();return}if(e.size>Pt){t(new Error(pe.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(pe.ImageUploadStatusType.ERROR_IMAGE)),this._decreaseWaiting();return}const l=pe.Tools.generateRandomId(6);n({imageId:l,imageSourceType:pe.ImageSourceType.BASE64,source:o,base64Cache:o,status:pe.ImageUploadStatusType.SUCCUSS}),this._decreaseWaiting()}})}_decreaseWaiting(){this._waitCount-=1,this._change$.next(this._waitCount)}}var Qt=Object.defineProperty,en=Object.getOwnPropertyDescriptor,tn=(a,e,n,t)=>{for(var r=t>1?void 0:t?en(e,n):e,u=a.length-1,o;u>=0;u--)(o=a[u])&&(r=(t?o(e,n,r):o(r))||r);return t&&r&&Qt(e,n,r),r},St=(a,e)=>(n,t)=>e(n,t,a);const nn="UNIVER_DRAWING_PLUGIN";var ct;exports.UniverDrawingPlugin=(ct=class extends pe.Plugin{constructor(e=Dt,n,t){super(),this._config=e,this._injector=n,this._configService=t;const{...r}=pe.merge({},Dt,this._config);this._configService.setConfig(Zt,r)}onStarting(){this._initDependencies()}_initDependencies(){var t;const e=[[pe.IImageIoService,{useClass:Nt}],[kt,{useClass:Ut}]];pe.mergeOverrideWithDependencies(e,(t=this._config)==null?void 0:t.override).forEach(r=>this._injector.add(r))}},H(ct,"pluginName",nn),ct);exports.UniverDrawingPlugin=tn([St(1,pe.Inject(pe.Injector)),St(2,pe.IConfigService)],exports.UniverDrawingPlugin);Object.defineProperty(exports,"IImageIoService",{enumerable:!0,get:()=>pe.IImageIoService});Object.defineProperty(exports,"ImageSourceType",{enumerable:!0,get:()=>pe.ImageSourceType});Object.defineProperty(exports,"ImageUploadStatusType",{enumerable:!0,get:()=>pe.ImageUploadStatusType});exports.DRAWING_IMAGE_ALLOW_IMAGE_LIST=Tt;exports.DRAWING_IMAGE_ALLOW_SIZE=Pt;exports.DRAWING_IMAGE_COUNT_LIMIT=qt;exports.DRAWING_IMAGE_HEIGHT_LIMIT=Lt;exports.DRAWING_IMAGE_WIDTH_LIMIT=Gt;exports.DrawingManagerService=Ut;exports.IDrawingManagerService=kt;exports.ImageIoService=Nt;exports.UnitDrawingService=jt;exports.getDrawingShapeKeyByDrawingSearch=Jt;exports.getImageSize=Yt;
1
+ "use strict";var xt=Object.defineProperty;var Bt=(a,e,n)=>e in a?xt(a,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):a[e]=n;var H=(a,e,n)=>Bt(a,typeof e!="symbol"?e+"":e,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const pe=require("@univerjs/core"),Ie=require("rxjs"),Gt=500,Lt=500,qt=10,Pt=5*1024*1024,Tt=["image/png","image/jpeg","image/jpg","image/gif","image/bmp"];var Ze={},et={},tt={},yt;function Kt(){if(yt)return tt;yt=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={},_t;function Ht(){if(_t)return nt;_t=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={},vt;function At(){return vt||(vt=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 ge=0;ge<s.length;ge++){const Z=s[ge];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(ge){let Z;for(;U;){const Ce=Z=_.getKey();if(ge!=null){let Le=!1;if(s&&typeof Ce=="number"&&(Z=s(Ce,_.getComponent()),Z<0&&(Z=~Z,Le=!0)),t(Z,ge))return null;if(Z===ge&&!Le)return _}c&&typeof Z=="number"&&c(Z,_.getComponent()),U=_.nextSibling()}return null}return Y.end=()=>{h&&_.ascend()},Y}a.advancer=E;function W(_,s,c){let h,U,Y,ge;for(h=U=_&&_.descendFirst(),Y=ge=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(),ge&&s.ascend()}a.eachChildOf=W}(at)),at}var lt={},mt;function Mt(){return mt||(mt=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 Fe={},Ve={},Ot;function ft(){return Ot||(Ot=1,Object.defineProperty(Ve,"__esModule",{value:!0}),Ve.uniToStrPos=Ve.strPosToUni=void 0,Ve.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},Ve.uniToStrPos=(a,e)=>{let n=0;for(;e>0;e--){const t=a.charCodeAt(n);n+=t>=55296&&t<=57343?2:1}return n}),Ve}var ut={},bt;function dt(){return bt||(bt=1,function(a){Object.defineProperty(a,"__esModule",{value:!0}),a.uniSlice=a.dlen=a.eachOp=void 0;const e=ft(),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 L=f[w];switch(typeof L){case"object":if(typeof L.d!="number"&&typeof L.d!="string")throw Error("Delete must be number or string");if(a.dlen(L.d)<=0)throw Error("Deletes must not be empty");break;case"string":if(!(L.length>0))throw Error("Inserts cannot be empty");break;case"number":if(!(L>0))throw Error("Skip components must be >0");if(typeof y=="number")throw Error("Adjacent skip components should be combined");break}y=L}if(typeof y=="number")throw Error("Op has a trailing skip")};function t(f,y){let w=0,L=0;for(let z=0;z<f.length;z++){const q=f[z];switch(y(q,w,L),typeof q){case"object":w+=a.dlen(q.d);break;case"string":L+=e.strPosToUni(q);break;case"number":w+=q,L+=q;break}}}a.eachOp=t;function r(f,y){const w=[],L=l(w);return t(f,(z,q,De)=>{L(y(z,q,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 L=e.uniToStrPos(f,y),z=w==null?1/0:e.uniToStrPos(f,w);return f.slice(L,z)};const W=(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:(q,De)=>{if(y===f.length)return q===-1?null:q;const ce=f[y];let ne;if(typeof ce=="number")return q===-1||ce-w<=q?(ne=ce-w,++y,w=0,ne):(w+=q,q);if(typeof ce=="string"){if(q===-1||De==="i"||e.strPosToUni(ce.slice(w))<=q)return ne=ce.slice(w),++y,w=0,ne;{const le=w+e.uniToStrPos(ce.slice(w),q);return ne=ce.slice(w,le),w=le,ne}}else{if(q===-1||De==="d"||a.dlen(ce.d)-w<=q)return ne={d:W(ce.d,w)},++y,w=0,ne;{let le=W(ce.d,w,w+q);return w+=q,{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 L=[],z=l(L),{take:q,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=q(ye,"i"),z(Se),typeof Se!="string"&&(ye-=E(Se));break;case"string":w==="left"&&typeof De()=="string"&&z(q(-1)),z(e.strPosToUni(le));break;case"object":for(ye=a.dlen(le.d);ye>0;)switch(Se=q(ye,"i"),typeof Se){case"number":ye-=Se;break;case"string":z(Se);break;case"object":ye-=a.dlen(Se.d)}break}}let ce;for(;ce=q(-1);)z(ce);return s(L)}function h(f,y){n(f),n(y);const w=[],L=l(w),{take:z}=_(f);for(let De=0;De<y.length;De++){const ce=y[De];let ne,le;switch(typeof ce){case"number":for(ne=ce;ne>0;)le=z(ne,"d"),L(le),typeof le!="object"&&(ne-=E(le));break;case"string":L(ce);break;case"object":ne=a.dlen(ce.d);let ye=0;for(;ye<ne;)switch(le=z(ne-ye,"d"),typeof le){case"number":L({d:W(ce.d,ye,ye+le)}),ye+=le;break;case"string":ye+=e.strPosToUni(le);break;case"object":L(le)}break}}let q;for(;q=z(-1);)L(q);return s(w)}const U=(f,y)=>{let w=0;for(let L=0;L<y.length&&f>w;L++){const z=y[L];switch(typeof z){case"number":{w+=z;break}case"string":const q=e.strPosToUni(z);w+=q,f+=q;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 ge(f,y,w){return r(f,(L,z)=>typeof L=="object"&&typeof L.d=="number"?{d:w.slice(y,z,z+L.d)}:L)}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 Le(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 L=f.builder(y);for(let z=0;z<w.length;z++){const q=w[z];switch(typeof q){case"number":L.skip(q);break;case"string":L.append(q);break;case"object":L.del(a.dlen(q.d));break}}return L.build()},transform:c,compose:h,transformPosition:U,transformSelection:Y,isInvertible:Le,makeInvertible(y,w){return ge(y,w,f)},stripInvertible:Ce,invert:Z,invertWithDoc(y,w){return Z(ge(y,w,f))},isNoop:y=>y.length===0}}a.default=me}(ut)),ut}var rt={},wt;function Ft(){if(wt)return rt;wt=1,Object.defineProperty(rt,"__esModule",{value:!0});const a=dt(),e=ft();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 W=a.dlen(o.d);this.onRemove&&this.onRemove(E,W)}})},onInsert:null,onRemove:null}}return rt.default=n,n.provides={text:!0},rt}var It;function Vt(){return It||(It=1,function(a){var e=Fe.__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=Fe.__setModuleDefault||(Object.create?function(c,h){Object.defineProperty(c,"default",{enumerable:!0,value:h})}:function(c,h){c.default=h}),t=Fe.__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=Fe.__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=ft(),o=t(dt()),l=r(Ft()),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},W=o.default(E),_=Object.assign(Object.assign({},W),{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=dt();Object.defineProperty(a,"makeType",{enumerable:!0,get:function(){return s.default}})}(Fe)),Fe}var Ct;function zt(){return Ct||(Ct=1,function(a){var e=et.__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(Kt()),t=e(Ht()),r=At(),u=Mt();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:q,apply:De,transformPosition:ce,compose:ne,tryTransform:st,transform:Rt,makeInvertible:Se,invert:le,invertWithDoc:$t,RM_UNEXPECTED_CONTENT:u.ConflictType.RM_UNEXPECTED_CONTENT,DROP_COLLISION:u.ConflictType.DROP_COLLISION,BLACKHOLE:u.ConflictType.BLACKHOLE,transformNoConflict:(i,d,m)=>pt(()=>!0,i,d,m),typeAllowingConflictsPred:i=>Object.assign(Object.assign({},a.type),{transform:(d,m,D)=>pt(i,d,m,D)})};const l=i=>i?i.getComponent():null;function E(i){return i&&typeof i=="object"&&!Array.isArray(i)}const W=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,ge={};function Z(i){let d=i.type?i.type:i;d.name&&(ge[d.name]=d),d.uri&&(ge[d.uri]=d)}const Ce=i=>{const d=ge[i];if(d)return d;throw Error("Missing type: "+i)};Z(Vt());const Le=(i,d)=>i+d;Z({name:"number",apply:Le,compose:Le,invert:i=>-i,transform:i=>i});const me=i=>i==null?null:i.et?Ce(i.et):i.es?ge["text-unicode"]:i.ena!=null?ge.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 L(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 R=!0,N=!1;for(let p in C){const v=C[p];if(R=!1,o(p==="p"||p==="r"||p==="d"||p==="i"||p==="e"||p==="es"||p==="ena"||p==="et","Invalid component item '"+p+"'"),p==="p")w(v),o(!d.has(v)),d.add(v),o(C.r===void 0);else if(p==="d")w(v),o(!m.has(v)),m.add(v),o(C.i===void 0);else if(p==="e"||p==="es"||p==="ena"){o(!N),N=!0;const b=me(C);o(b,"Missing type in edit"),b.checkValidOp&&b.checkValidOp(f(C))}}o(!R)},O=(C,R,N)=>{if(!Array.isArray(C))throw Error("Op must be null or a list");if(C.length===0)throw Error("Empty descent");R||L(C[0]);let p=1,v=0,b=0;for(let I=0;I<C.length;I++){const $=C[I];if(o($!=null),Array.isArray($)){const x=O($,!1);if(v){const g=typeof b,M=typeof x;g===M?o(b<x,"descent keys are not in order"):o(g==="number"&&M==="string")}b=x,v++,p=3}else typeof $=="object"?(o(p===1,`Prev not scalar - instead ${p}`),D($),p=2):(o(p!==3),L($),o(r.isValidPathItem($),"Invalid path key"),p=1)}return o(v!==1,"Operation makes multiple descents. Remove some []"),o(p===2||p===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 q(i){let d=0,m=[];const D=r.writeCursor();return D.mergeTree(i,(O,C)=>{const R=me(O);if(R){const p=f(O);y(C,R,R.normalize?R.normalize(p):p)}for(const p of["r","p","i","d"])if(O[p]!==void 0){const v=p==="p"||p==="d"?(N=O[p],m[N]==null&&(m[N]=d++),m[N]):O[p];C.write(p,v)}var N}),D.get()}function De(i,d){if(z(d),d===null)return i;const m=[];return function D(O,C){let R=O,N=0,p={root:O},v=0,b=p,I="root";function $(){for(;v<N;v++){let x=C[v];typeof x!="object"&&(o(U(b,I)),b=b[I]=W(b[I]),I=x)}}for(;N<C.length;N++){const x=C[N];if(Array.isArray(x)){const g=D(R,x);g!==R&&g!==void 0&&($(),R=b[I]=g)}else if(typeof x=="object"){x.d!=null?($(),R=h(b,I,m[x.d])):x.i!==void 0&&($(),R=h(b,I,x.i));const g=me(x);if(g)$(),R=b[I]=g.apply(R,f(x));else if(x.e!==void 0)throw Error("Subtype "+x.et+" undefined")}else R=Y(R,x)}return p.root}(i=function D(O,C){const R=[];let N=0;for(;N<C.length;N++){const I=C[N];if(Array.isArray(I))break;typeof I!="object"&&(R.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.pop();O=O===Y($,I)?$:O===void 0?c($,I):(v=I,b=O,(p=W(p=$))[v]=b,p)}else _(I)&&(o(O!==void 0,"Cannot pick up or remove undefined"),I.p!=null&&(m[I.p]=O),O=void 0)}var p,v,b;return O}(i,d),d)}function ce(i,d){i=i.slice(),z(d);const m=r.readCursor(d);let D,O,C=!1;const R=[];for(let p=0;;p++){const v=i[p],b=m.getComponent();if(b&&(b.r!==void 0?C=!0:b.p!=null&&(C=!1,D=b.p,O=p)),p>=i.length)break;let I=0;const $=r.advancer(m,void 0,(g,M)=>{_(M)&&I++});R.unshift($);const x=$(v);if(typeof v=="number"&&(i[p]-=I),!x)break}if(R.forEach(p=>p.end()),C)return null;const N=()=>{let p=0;if(D!=null){const v=m.getPath();p=v.length,i=v.concat(i.slice(O))}for(;p<i.length;p++){const v=i[p],b=l(m),I=me(b);if(I){const g=f(b);I.transformPosition&&(i[p]=I.transformPosition(i[p],g));break}let $=0;const x=r.advancer(m,(g,M)=>s(M)?~(g-$):g-$,(g,M)=>{s(M)&&$++})(v);if(typeof v=="number"&&(i[p]+=$),!x)break}};return D!=null?m.eachDrop(null,p=>{p===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(),R=[],N=[],p=[],v=[],b=[],I=[],$=new Set;D.traverse(null,g=>{g.p!=null&&(p[g.p]=D.clone())}),O.traverse(null,g=>{g.d!=null&&(v[g.d]=O.clone())});const x=r.writeCursor();return function g(M,re,te,K,se,$e,be,_e){o(re||te);const oe=l(re),Pe=l(te),Ae=!!Pe&&Pe.r!==void 0,qe=!!oe&&oe.i!==void 0,Te=oe?oe.d:null,Ee=Pe?Pe.p:null,Re=($e||Ae)&&Ee==null;if(Ee!=null)K=v[Ee],be=N[Ee]=new r.WriteCursor;else if(Pe&&Pe.r!==void 0)K=null;else{const P=l(K);P&&P.d!=null&&(K=null)}const Q=l(K);if(Te!=null)if(M=p[Te],_e=R[Te]=new r.WriteCursor,Re)$e&&!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;qe?(o(se===void 0),T=oe.i):T=se;const B=(Ee==null?!qe||$e||Ae:T===void 0)?null:be.getComponent();if(Ee!=null){if(!(se!==void 0||qe)){const P=Te!=null?b[Te]:m++;I[Ee]=P,_e.write("p",P)}}else Ae&&(qe||se!==void 0||(Pe.r,_e.write("r",Pe.r)));const A=Re?null:me(oe),S=me(Q);if((A||S)&&(A&&A.name,S&&S.name),A&&S){o(A===S);const P=f(oe),G=f(Q),he=A.compose(P,G);y(be,A,he),$.add(Q)}else A?y(be,A,f(oe)):S&&(y(be,S,f(Q)),$.add(Q));const j=typeof T=="object"&&T!=null;let J=!1,X=0,ee=0,fe=0,de=0,ae=0;const Oe=r.advancer(K,(P,G)=>s(G)?de-P-1:P-de,(P,G)=>{s(G)&&de++}),V=r.advancer(M,(P,G)=>_(G)?X-P-1:P-X,(P,G)=>{_(G)&&X++});if(r.eachChildOf(re,te,(P,G,he)=>{let we,We,Ke=P,Me=P,Ye=P;if(typeof P=="number"){let ve=P+fe;We=Oe(ve),Me=ve+de;let ue=P+ee;we=V(ue),s(l(We))&&(we=null),Ke=ue+X,Ye=P+ae,o(Ke>=0,"p1PickKey is negative"),o(Me>=0,"p2DropKey is negative");const je=s(l(G)),xe=_(l(he));(je||xe&&!Re)&&ae--,je&&ee--,xe&&fe--}else we=V(P),We=Oe(P);_e.descend(Ke),be.descend(Me);const Xe=j&&!s(l(G))?T[Ye]:void 0,ke=g(we,G,he,We,Xe,Re,be,_e);var Ne,k,ie;j&&!Re?Xe!==ke&&(J||(T=Array.isArray(T)?T.slice():Object.assign({},T),J=!0),Ne=T,ie=ke,typeof(k=Ye)=="number"?(o(Array.isArray(Ne)),o(k<Ne.length)):(o(!Array.isArray(Ne)),o(Ne[k]!==void 0)),ie===void 0?typeof k=="number"?Ne.splice(k,1):delete Ne[k]:Ne[k]=ie):o(ke===void 0),be.ascend(),_e.ascend()}),V.end(),Oe.end(),B!=null)B.i=T;else if(!$e&&!Ae&&Ee==null)return T}(D,D.clone(),O,O.clone(),void 0,!1,C,x),C.reset(),C.mergeTree(x.get()),C.reset(),C.get(),R.map(g=>g.get()),N.map(g=>g.get()),D.traverse(C,(g,M)=>{const re=g.p;if(re!=null){const te=b[re];te!=null&&M.write("p",te);const K=R[re];K&&K.get(),K&&M.mergeTree(K.get())}else g.r!==void 0&&M.write("r",g.r)}),C.reset(),C.get(),O.traverse(C,(g,M)=>{const re=g.d;if(re!=null){const K=I[re];K!=null&&M.write("d",K);const se=N[re];se&&M.mergeTree(se.get())}else g.i!==void 0&&M.write("i",g.i);const te=me(g);te&&!$.has(g)&&y(M,te,f(g))}),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 R(N,p,v){const b=N.getComponent();let I,$=!1;if(b){b.p!=null&&(p.write("d",b.p),O[b.p]=N.clone()),b.r!==void 0&&p.write("i",b.r),b.d!=null&&(p.write("p",b.d),v=void 0),b.i!==void 0&&(v=I=b.i);const g=me(b);g&&(v===void 0?(D||(D=new Set),D.add(b)):(f(b),v=g.apply(v,f(b)),$=!0))}let x=0;for(const g of N){p.descend(g);const M=typeof g=="number"?g-x:g,re=Y(v,M);s(N.getComponent())&&x++;const te=R(N,p,re);if(v!==void 0&&te!==void 0){if($||($=!0,v=W(v)),!U(v,M))throw Error("Cannot modify child - invalid operation");v[M]=te}p.ascend()}if(I===void 0)return $?v:void 0;p.write("r",v)}(d,m,void 0),D&&(m.reset(),function R(N,p,v){const b=p.getComponent();if(b){const g=b.d;if(g!=null&&(N=O[g],v=C[g]=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,$=0;const x=r.advancer(N,(g,M)=>_(M)?I-g-1:g-I,(g,M)=>{_(M)&&I++});for(const g of p)if(typeof g=="number"){const M=g-$,re=x(M),te=M+I;v.descend(te),R(re,p,v),s(p.getComponent())&&$++,v.ascend()}else v.descend(g),R(x(g),p,v),v.ascend();x.end()}(d.clone(),d,m),C.length&&(m.reset(),d.traverse(m,(R,N)=>{const p=R.p;if(p!=null){const v=C[p];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,p=>{var v;return p.r!==void 0||((v=me(p))===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=[],R=[],N=(p,v,b)=>{const I=p.getComponent();let $=!1;if(I){I.d!=null&&v.write("d",I.d),I.i!==void 0&&v.write("i",I.i);const g=I.p;if(g!=null&&(C[g]=p.clone(),o(b!==void 0,"Operation picks up at an invalid key"),R[g]=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 x=0;for(const g of p){v.descend(g);const M=typeof g=="number"?g-x:g,re=Y(b,M),te=N(p,v,re);re!==te&&($||($=!0,b=W(b)),te===void 0?(b=c(b,M),typeof g=="number"&&x++):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 p(v,b,I,$,x){const g=b.getComponent();if(g){g.i!==void 0?($=g.i,x=!0):g.d!=null&&($=R[g.d],v=C[g.d],x=!1,g.d);let K=me(g);if(K&&K.makeInvertible){const se=f(g);y(I,K,K.makeInvertible(se,$),!0)}}let M=0,re=0;const te=r.advancer(v,(K,se)=>_(se)?M-K-1:K-M,(K,se)=>{_(se)&&M++});for(const K of b)if(typeof K=="number"){const se=K-re,$e=te(se),be=se+M,_e=Y($,x?se:be);I.descend(K),p($e,b,I,_e,x),s(b.getComponent())&&re++,I.ascend()}else{const se=Y($,K);I.descend(K),p(te(K),b,I,se,x),I.ascend()}te.end()}(m.clone(),m,D,d,!1)),D.get()}function $t(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=[],R=[],N=[],p=[],v=[],b=[],I=[],$=[],x=[],g=[],M=[],re=[],te=[],K=[],se=[];let $e=0;const be=r.readCursor(i),_e=r.readCursor(d),oe=r.writeCursor();if(function Q(T,B=null,A){const S=l(B);S&&(S.r!==void 0?A=B.clone():S.p!=null&&(A=null,b[S.p]=T.clone()));const j=T.getComponent();let J;j&&(J=j.p)!=null&&(v[J]=B?B.clone():null,N[J]=T.clone(),A&&(g[J]=!0,x[J]=A),S&&S.p!=null&&(K[J]=S.p));const X=r.advancer(B);for(const ee of T)Q(T,X(ee),A);X.end()}(_e,be,null),function Q(T,B,A,S,j){const J=A.getComponent();let X,ee=!1;J&&((X=J.d)!=null?(p[X]=A.clone(),S!=null&&(se[S]==null&&(se[S]=[]),se[S].push(X)),g[X],T=v[X]||null,B=N[X]||null,g[X]?(j&&(M[X]=!0),j=x[X]||null):!j||D!==1&&K[X]!=null||O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(j.getPath()),op2:a.moveOp(B.getPath(),A.getPath())}),ee=!0):J.i!==void 0&&(T=B=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 fe=l(T);fe&&(fe.r!==void 0?j=T.clone():fe.p!=null&&(fe.p,S=fe.p,j=null));const de=me(J);de&&j&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:a.removeOp(j.getPath()),op2:a.editOp(A.getPath(),de,f(J),!0)});let ae=0,Oe=0;const V=r.advancer(B,(G,he)=>_(he)?ae-G-1:G-ae,(G,he)=>{_(he)&&ae++}),P=r.advancer(T);for(const G of A)if(typeof G=="number"){const he=G-Oe,we=V(he);Oe+=+Q(P(he+ae),we,A,S,j)}else{const he=V(G);Q(P(G),he,A,S,j)}return V.end(),P.end(),ee}(be,_e,_e.clone(),null,null),p.map(Q=>Q&&Q.get()),O)return{ok:!1,conflict:O};M.map(Q=>!!Q);const Pe=[];let Ae=null;(function Q(T,B,A,S,j){let J=!1;const X=l(B);if(_(X)){const V=X.p;V!=null?(A=p[V],S=re[V]=r.writeCursor(),J=!0,j=null):(A=null,j=B.clone())}else s(l(A))&&(A=null);const ee=T.getComponent();if(ee){const V=ee.p;V!=null?(j&&($[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 fe=0,de=0;const ae=r.advancer(B,void 0,(V,P)=>{_(P)&&fe++}),Oe=r.advancer(A,(V,P)=>s(P)?~(V-de):V-de,(V,P)=>{s(P)&&de++});if(T)for(const V of T)if(typeof V=="string"){const P=ae(V),G=Oe(V);S.descend(V),Q(T,P,G,S,j),S.ascend()}else{const P=ae(V),G=V-fe,he=_(l(P))?null:Oe(G),we=G+de;o(we>=0),S.descend(we),Q(T,P,he,S,j),S.ascend()}ae.end(),Oe.end()})(be,_e,_e.clone(),oe,null),oe.reset();let qe=[];if(function Q(T,B,A,S,j,J){o(B);const X=B.getComponent();let ee=l(S),fe=!1;const de=(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&&(R[k]=B.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&&!g[ue])&&(ve=ue!=null?k!=null&&k===K[ue]:n.default(ee.i,X.i),ve||ue!=null&&D!==1&&K[ue]!=null||O==null&&(O={type:u.ConflictType.DROP_COLLISION,op1:de(k!=null?C[k]:null,B,X),op2:de(ue!=null?N[ue]:null,S,ee)})),ve||(J?O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:de(k!=null?C[k]:null,B,X),op2:a.removeOp(J.getPath())}):(k!=null?(qe[$e]=k,j.write("d",ie.p=$e++)):j.write("i",t.default(X.i)),fe=!0))}else if(k!=null&&!ie){const ue=$[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)||g[k]?(S=null,J=A.clone()):k!=null&&(S=p[k],D!==1&&K[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(B.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,G=0,he=0,we=0,We=0,Ke=0,Me=T!=null&&T.descendFirst(),Ye=Me;const Xe=r.advancer(A,void 0,(k,ie)=>{_(ie)&&he++});let ke=S!=null&&S.descendFirst(),Ne=ke;for(const k of B)if(typeof k=="number"){let ie;const ve=s(B.getComponent()),ue=k-G;{let Be;for(;Me&&typeof(Be=T.getKey())=="number";){Be+=P;const Ue=T.getComponent(),Je=_(Ue);if(Be>ue||Be===ue&&(!Je||D===0&&ve))break;if(Je){P--;const He=Ue.p;K.includes(He),Ue.d,l(te[Ue.d]),_(l(te[Ue.d])),(Ue.r===void 0||Ae&&Ae.has(Ue))&&(He==null||!Pe[He]||D!==1&&K.includes(He))||We--}Me=T.nextSibling()}ie=Me&&Be===ue?T:null}const je=ue-P;let xe=Xe(je);const ot=je-he;let Qe=null;{let Be,Ue;for(;ke&&typeof(Be=S.getKey())=="number";){Ue=Be-we;const Je=S.getComponent(),He=s(Je);if(Ue>ot)break;if(Ue===ot){if(!He){Qe=S;break}{if(D===0&&ve){Qe=S;break}const Ge=xe&&_(xe.getComponent());if(D===0&&Ge)break}}if(He){const Ge=Je.d;g[Ge],K[Ge],Je.i===void 0&&(g[Ge]||K[Ge]!=null&&D!==1)?(g[Ge]||K[Ge]!=null&&D===0)&&(we++,Ke--):we++}ke=S.nextSibling()}}const gt=ot+we+We+Ke;o(gt>=0,"trying to descend to a negative index"),j.descend(gt),ve&&(ie=xe=Qe=null,G++),Q(ie,B,xe,Qe,j,J)&&Ke++,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=Xe(k);let je;for(;ke&&(je=S.getKey(),typeof je!="string"||!(je>k||je===k));)ke=S.nextSibling();const xe=ke&&je===k?S:null;j.descend(k),Q(ve,B,ue,xe,j,J),j.ascend()}return Xe.end(),Ye&&T.ascend(),Ne&&S.ascend(),fe}(be,be.clone(),_e,_e.clone(),oe,null),O)return{ok:!1,conflict:O};oe.reset();const Te=(Q,T,B)=>Q.traverse(T,(A,S)=>{A.d!=null&&B(A.d,Q,S)});(g.length||re.length)&&(Te(_e,oe,(Q,T,B)=>{g[Q]&&!M[Q]&&B.write("r",!0),re[Q]&&B.mergeTree(re[Q].get())}),oe.reset());const Ee=[],Re=[];if((te.length||g.length)&&!O){const Q=r.readCursor(it(oe.get()));if(Te(Q,null,(T,B)=>{Ee[T]=B.clone()}),te.forEach(T=>{T&&Te(r.readCursor(T.get()),null,(B,A)=>{Ee[B]=A.clone()})}),function T(B,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=Re[P]=r.writeCursor()}else ee.r!==void 0&&(S=null);else s(l(S))&&(S=null);const fe=B.getComponent();if(fe){let P;if((P=fe.d)!=null){const G=te[P];G&&(G.get(),j.mergeTree(G.get()),S=r.readCursor(G.get()))}}let de=0,ae=0;const Oe=r.advancer(A,void 0,(P,G)=>{_(G)&&de--}),V=r.advancer(S,(P,G)=>s(G)?-(P-ae)-1:P-ae,(P,G)=>{s(G)&&ae++});for(const P of B)if(typeof P=="number"){const G=Oe(P),he=P+de,we=V(he),We=he+ae;j.descend(We),T(B,G,we,j),j.ascend()}else j.descend(P),T(B,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(),Re.length){const T=Re.map(A=>A?A.get():null),B=r.readCursor(it(oe.get()));if(Te(B,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 fe=qe[ee];A.writeMove(C[fe].getPath(),R[fe].getPath(),j++);const de=se[fe];de&&de.forEach(ae=>{g[ae]||D!==1&&K[ae]!=null||S.writeMove(N[ae].getPath(),p[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 ht=i=>{const d=new Error("Transform detected write conflict");throw d.conflict=i,d.type=d.name="writeConflict",d};function Rt(i,d,m){const D=st(i,d,m);if(D.ok)return D.result;ht(D.conflict)}const ze=i=>{const d=r.writeCursor();return r.readCursor(i).traverse(d,(m,D)=>{(s(m)||me(m))&&D.write("r",!0)}),d.get()},Wt=(i,d)=>{const{type:m,op1:D,op2:O}=i;switch(m){case u.ConflictType.DROP_COLLISION:return d==="left"?[null,ze(O)]:[ze(D),null];case u.ConflictType.RM_UNEXPECTED_CONTENT:let C=!1;return r.readCursor(D).traverse(null,R=>{R.r!==void 0&&(C=!0)}),C?[null,ze(O)]:[ze(D),null];case u.ConflictType.BLACKHOLE:return[ze(D),ze(O)];default:throw Error("Unrecognised conflict: "+m)}};function pt(i,d,m,D){let O=null;for(;;){const C=st(d,m,D);if(C.ok)return ne(O,C.result);{const{conflict:R}=C;i(R)||ht(R);const[N,p]=Wt(R,D);d=ne(q(d),N),m=ne(q(m),p),O=ne(O,p)}}}}(et)),et}var Et;function Xt(){return Et||(Et=1,function(a){var e=Ze.__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=Ze.__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(zt(),a);var t=At();Object.defineProperty(a,"ReadCursor",{enumerable:!0,get:function(){return t.ReadCursor}}),Object.defineProperty(a,"WriteCursor",{enumerable:!0,get:function(){return t.WriteCursor}});var r=Mt();Object.defineProperty(a,"ConflictType",{enumerable:!0,get:function(){return r.ConflictType}})}(Ze)),Ze}var F=Xt();class jt{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(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(W=>{const{op:_,invertOp:s}=this._addByParam(W);n.push({unitId:W.unitId,subUnitId:W.subUnitId,drawingId:W.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:W,invertOp:_}=this._removeByParam(E);n.unshift(W),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(W=>{const{op:_,invertOp:s}=this._updateByParam(W);n.push({unitId:W.unitId,subUnitId:W.subUnitId,drawingId:W.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 W=o[E];W.groupId===r&&l.push(W)}),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(W=>{const{unitId:_,subUnitId:s,drawingId:c}=W,h=this._hasDrawingOrder({unitId:_,subUnitId:s,drawingId:c});E=Math.max(E,h),l.push(...this._getUpdateParamCompareOp(W,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:W,subUnitId:_,drawingId:s}=E;l.push(...this._getUpdateParamCompareOp(E,this.getDrawingByParam({unitId:W,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){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 W=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(W,this.drawingManagerData),redo:W,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 W=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(W,this.drawingManagerData),redo:W,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?pe.sortRules:pe.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),W=F.type.invertWithDoc(E,this.drawingManagerData);return{op:E,invertOp:W}}_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],W=n[l];W!==E&&o.push(F.replaceOp([t,r,"data",u,l],W,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 Ut extends jt{}function Jt({unitId:a,subUnitId:e,drawingId:n},t){return typeof t=="number"?`${a}#-#${e}#-#${n}#-#${t}`:`${a}#-#${e}#-#${n}`}const Yt=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)}}),Zt="drawing.config",Dt={},kt=pe.createIdentifier("univer.drawing-manager.service");class Nt{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,n){if(n===pe.ImageSourceType.BASE64){const t=new Image;return t.src=e,t}return this._imageSourceCache.get(e)}addImageSourceCache(e,n,t){n===pe.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(!Tt.includes(e.type)){t(new Error(pe.ImageUploadStatusType.ERROR_IMAGE_TYPE)),this._decreaseWaiting();return}if(e.size>Pt){t(new Error(pe.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(pe.ImageUploadStatusType.ERROR_IMAGE)),this._decreaseWaiting();return}const l=pe.Tools.generateRandomId(6);n({imageId:l,imageSourceType:pe.ImageSourceType.BASE64,source:o,base64Cache:o,status:pe.ImageUploadStatusType.SUCCUSS}),this._decreaseWaiting()}})}_decreaseWaiting(){this._waitCount-=1,this._change$.next(this._waitCount)}}var Qt=Object.defineProperty,en=Object.getOwnPropertyDescriptor,tn=(a,e,n,t)=>{for(var r=t>1?void 0:t?en(e,n):e,u=a.length-1,o;u>=0;u--)(o=a[u])&&(r=(t?o(e,n,r):o(r))||r);return t&&r&&Qt(e,n,r),r},St=(a,e)=>(n,t)=>e(n,t,a);const nn="UNIVER_DRAWING_PLUGIN";var ct;exports.UniverDrawingPlugin=(ct=class extends pe.Plugin{constructor(e=Dt,n,t){super(),this._config=e,this._injector=n,this._configService=t;const{...r}=pe.merge({},Dt,this._config);this._configService.setConfig(Zt,r)}onStarting(){this._initDependencies()}_initDependencies(){var t;const e=[[pe.IImageIoService,{useClass:Nt}],[kt,{useClass:Ut}]];pe.mergeOverrideWithDependencies(e,(t=this._config)==null?void 0:t.override).forEach(r=>this._injector.add(r))}},H(ct,"pluginName",nn),ct);exports.UniverDrawingPlugin=tn([St(1,pe.Inject(pe.Injector)),St(2,pe.IConfigService)],exports.UniverDrawingPlugin);Object.defineProperty(exports,"IImageIoService",{enumerable:!0,get:()=>pe.IImageIoService});Object.defineProperty(exports,"ImageSourceType",{enumerable:!0,get:()=>pe.ImageSourceType});Object.defineProperty(exports,"ImageUploadStatusType",{enumerable:!0,get:()=>pe.ImageUploadStatusType});exports.DRAWING_IMAGE_ALLOW_IMAGE_LIST=Tt;exports.DRAWING_IMAGE_ALLOW_SIZE=Pt;exports.DRAWING_IMAGE_COUNT_LIMIT=qt;exports.DRAWING_IMAGE_HEIGHT_LIMIT=Lt;exports.DRAWING_IMAGE_WIDTH_LIMIT=Gt;exports.DrawingManagerService=Ut;exports.IDrawingManagerService=kt;exports.ImageIoService=Nt;exports.UnitDrawingService=jt;exports.getDrawingShapeKeyByDrawingSearch=Jt;exports.getImageSize=Yt;
package/lib/es/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  var $t = Object.defineProperty;
2
2
  var Nt = (a, e, n) => e in a ? $t(a, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : a[e] = n;
3
3
  var F = (a, e, n) => Nt(a, typeof e != "symbol" ? e + "" : e, n);
4
- import { sortRules as Rt, sortRulesByDesc as xt, createIdentifier as Bt, ImageSourceType as at, ImageUploadStatusType as et, Tools as Wt, Inject as Gt, Injector as Lt, Plugin as qt, merge as Kt, mergeOverrideWithDependencies as Ft, IConfigService as Vt, IImageIoService as Ht } from "@univerjs/core";
4
+ import { sortRules as Rt, sortRulesByDesc as xt, createIdentifier as Bt, ImageSourceType as at, ImageUploadStatusType as Qe, Tools as Wt, Inject as Gt, Injector as Lt, Plugin as qt, merge as Kt, mergeOverrideWithDependencies as Ft, IConfigService as Vt, IImageIoService as Ht } from "@univerjs/core";
5
5
  import { IImageIoService as bn, ImageSourceType as Cn, ImageUploadStatusType as In } from "@univerjs/core";
6
6
  import { Subject as be } from "rxjs";
7
7
  const gn = 500, yn = 500, _n = 10, zt = 5 * 1024 * 1024, Xt = ["image/png", "image/jpeg", "image/jpg", "image/gif", "image/bmp"];
8
- var Ve = {}, Ze = {}, tt = {}, _t;
8
+ var Ye = {}, et = {}, tt = {}, _t;
9
9
  function Jt() {
10
10
  if (_t) return tt;
11
11
  _t = 1, Object.defineProperty(tt, "__esModule", { value: !0 });
@@ -289,9 +289,9 @@ function Ut() {
289
289
  }(a.ConflictType || (a.ConflictType = {}));
290
290
  }(ut)), ut;
291
291
  }
292
- var Se = {}, He = {}, wt;
292
+ var Fe = {}, Ve = {}, wt;
293
293
  function ht() {
294
- return wt || (wt = 1, Object.defineProperty(He, "__esModule", { value: !0 }), He.uniToStrPos = He.strPosToUni = void 0, He.strPosToUni = (a, e = a.length) => {
294
+ return wt || (wt = 1, Object.defineProperty(Ve, "__esModule", { value: !0 }), Ve.uniToStrPos = Ve.strPosToUni = void 0, Ve.strPosToUni = (a, e = a.length) => {
295
295
  let n = 0, t = 0;
296
296
  for (; t < e; t++) {
297
297
  const r = a.charCodeAt(t);
@@ -300,14 +300,14 @@ function ht() {
300
300
  if (t !== e)
301
301
  throw Error("Invalid offset - splits unicode bytes");
302
302
  return t - n;
303
- }, He.uniToStrPos = (a, e) => {
303
+ }, Ve.uniToStrPos = (a, e) => {
304
304
  let n = 0;
305
305
  for (; e > 0; e--) {
306
306
  const t = a.charCodeAt(n);
307
307
  n += t >= 55296 && t <= 57343 ? 2 : 1;
308
308
  }
309
309
  return n;
310
- }), He;
310
+ }), Ve;
311
311
  }
312
312
  var ct = {}, bt;
313
313
  function ft() {
@@ -634,22 +634,22 @@ function Zt() {
634
634
  var It;
635
635
  function Qt() {
636
636
  return It || (It = 1, function(a) {
637
- var e = Se && Se.__createBinding || (Object.create ? function(c, h, S, Y) {
637
+ var e = Fe.__createBinding || (Object.create ? function(c, h, S, Y) {
638
638
  Y === void 0 && (Y = S), Object.defineProperty(c, Y, { enumerable: !0, get: function() {
639
639
  return h[S];
640
640
  } });
641
641
  } : function(c, h, S, Y) {
642
642
  Y === void 0 && (Y = S), c[Y] = h[S];
643
- }), n = Se && Se.__setModuleDefault || (Object.create ? function(c, h) {
643
+ }), n = Fe.__setModuleDefault || (Object.create ? function(c, h) {
644
644
  Object.defineProperty(c, "default", { enumerable: !0, value: h });
645
645
  } : function(c, h) {
646
646
  c.default = h;
647
- }), t = Se && Se.__importStar || function(c) {
647
+ }), t = Fe.__importStar || function(c) {
648
648
  if (c && c.__esModule) return c;
649
649
  var h = {};
650
650
  if (c != null) for (var S in c) Object.hasOwnProperty.call(c, S) && e(h, c, S);
651
651
  return n(h, c), h;
652
- }, r = Se && Se.__importDefault || function(c) {
652
+ }, r = Fe.__importDefault || function(c) {
653
653
  return c && c.__esModule ? c : { default: c };
654
654
  };
655
655
  Object.defineProperty(a, "__esModule", { value: !0 }), a.type = a.remove = a.insert = void 0;
@@ -689,12 +689,12 @@ function Qt() {
689
689
  Object.defineProperty(a, "makeType", { enumerable: !0, get: function() {
690
690
  return s.default;
691
691
  } });
692
- }(Se)), Se;
692
+ }(Fe)), Fe;
693
693
  }
694
694
  var Et;
695
695
  function en() {
696
696
  return Et || (Et = 1, function(a) {
697
- var e = Ze && Ze.__importDefault || function(i) {
697
+ var e = et.__importDefault || function(i) {
698
698
  return i && i.__esModule ? i : {
699
699
  default: i
700
700
  };
@@ -927,9 +927,9 @@ function en() {
927
927
  g.d != null && (v[g.d] = O.clone());
928
928
  });
929
929
  const B = r.writeCursor();
930
- return function g(U, re, te, K, se, Ne, Oe, ye) {
930
+ return function g(U, re, te, K, se, $e, Oe, ye) {
931
931
  o(re || te);
932
- const oe = l(re), Pe = l(te), Ae = !!Pe && Pe.r !== void 0, Le = !!oe && oe.i !== void 0, Te = oe ? oe.d : null, Ie = Pe ? Pe.p : null, Re = (Ne || Ae) && Ie == null;
932
+ const oe = l(re), Pe = l(te), Ae = !!Pe && Pe.r !== void 0, Le = !!oe && oe.i !== void 0, Te = oe ? oe.d : null, Ie = Pe ? Pe.p : null, Ne = ($e || Ae) && Ie == null;
933
933
  if (Ie != null) K = v[Ie], Oe = $[Ie] = new r.WriteCursor();
934
934
  else if (Pe && Pe.r !== void 0) K = null;
935
935
  else {
@@ -937,7 +937,7 @@ function en() {
937
937
  T && T.d != null && (K = null);
938
938
  }
939
939
  const Q = l(K);
940
- if (Te != null) if (U = p[Te], ye = R[Te] = new r.WriteCursor(), Re) Ne && !Ae && ye.write("r", !0);
940
+ if (Te != null) if (U = p[Te], ye = R[Te] = new r.WriteCursor(), Ne) $e && !Ae && ye.write("r", !0);
941
941
  else {
942
942
  const T = w[Te] = m++;
943
943
  Oe.write("d", T);
@@ -949,42 +949,42 @@ function en() {
949
949
  }
950
950
  let A;
951
951
  Le ? (o(se === void 0), A = oe.i) : A = se;
952
- const W = (Ie == null ? !Le || Ne || Ae : A === void 0) ? null : Oe.getComponent();
952
+ const W = (Ie == null ? !Le || $e || Ae : A === void 0) ? null : Oe.getComponent();
953
953
  if (Ie != null) {
954
954
  if (!(se !== void 0 || Le)) {
955
955
  const T = Te != null ? w[Te] : m++;
956
956
  C[Ie] = T, ye.write("p", T);
957
957
  }
958
958
  } else Ae && (Le || se !== void 0 || (Pe.r, ye.write("r", Pe.r)));
959
- const M = Re ? null : ve(oe), P = ve(Q);
959
+ const M = Ne ? null : ve(oe), P = ve(Q);
960
960
  if ((M || P) && (M && M.name, P && P.name), M && P) {
961
961
  o(M === P);
962
962
  const T = f(oe), G = f(Q), he = M.compose(T, G);
963
963
  y(Oe, M, he), N.add(Q);
964
964
  } else M ? y(Oe, M, f(oe)) : P && (y(Oe, P, f(Q)), N.add(Q));
965
965
  const k = typeof A == "object" && A != null;
966
- let X = !1, J = 0, ee = 0, fe = 0, de = 0, ae = 0;
966
+ let J = !1, X = 0, ee = 0, fe = 0, de = 0, ae = 0;
967
967
  const me = r.advancer(K, (T, G) => s(G) ? de - T - 1 : T - de, (T, G) => {
968
968
  s(G) && de++;
969
- }), H = r.advancer(U, (T, G) => _(G) ? J - T - 1 : T - J, (T, G) => {
970
- _(G) && J++;
969
+ }), H = r.advancer(U, (T, G) => _(G) ? X - T - 1 : T - X, (T, G) => {
970
+ _(G) && X++;
971
971
  });
972
972
  if (r.eachChildOf(re, te, (T, G, he) => {
973
- let we, xe, qe = T, Me = T, Ye = T;
973
+ let we, Re, qe = T, Me = T, Je = T;
974
974
  if (typeof T == "number") {
975
975
  let _e = T + fe;
976
- xe = me(_e), Me = _e + de;
976
+ Re = me(_e), Me = _e + de;
977
977
  let ue = T + ee;
978
- we = H(ue), s(l(xe)) && (we = null), qe = ue + J, Ye = T + ae, o(qe >= 0, "p1PickKey is negative"), o(Me >= 0, "p2DropKey is negative");
979
- const Ue = s(l(G)), Be = _(l(he));
980
- (Ue || Be && !Re) && ae--, Ue && ee--, Be && fe--;
981
- } else we = H(T), xe = me(T);
978
+ we = H(ue), s(l(Re)) && (we = null), qe = ue + X, Je = T + ae, o(qe >= 0, "p1PickKey is negative"), o(Me >= 0, "p2DropKey is negative");
979
+ const Ue = s(l(G)), xe = _(l(he));
980
+ (Ue || xe && !Ne) && ae--, Ue && ee--, xe && fe--;
981
+ } else we = H(T), Re = me(T);
982
982
  ye.descend(qe), Oe.descend(Me);
983
- const Xe = k && !s(l(G)) ? A[Ye] : void 0, je = g(we, G, he, xe, Xe, Re, Oe, ye);
984
- var $e, j, ie;
985
- k && !Re ? Xe !== je && (X || (A = Array.isArray(A) ? A.slice() : Object.assign({}, A), X = !0), $e = A, ie = je, typeof (j = Ye) == "number" ? (o(Array.isArray($e)), o(j < $e.length)) : (o(!Array.isArray($e)), o($e[j] !== void 0)), ie === void 0 ? typeof j == "number" ? $e.splice(j, 1) : delete $e[j] : $e[j] = ie) : o(je === void 0), Oe.ascend(), ye.ascend();
983
+ const ze = k && !s(l(G)) ? A[Je] : void 0, Se = g(we, G, he, Re, ze, Ne, Oe, ye);
984
+ var je, j, ie;
985
+ k && !Ne ? ze !== Se && (J || (A = Array.isArray(A) ? A.slice() : Object.assign({}, A), J = !0), je = A, ie = Se, typeof (j = Je) == "number" ? (o(Array.isArray(je)), o(j < je.length)) : (o(!Array.isArray(je)), o(je[j] !== void 0)), ie === void 0 ? typeof j == "number" ? je.splice(j, 1) : delete je[j] : je[j] = ie) : o(Se === void 0), Oe.ascend(), ye.ascend();
986
986
  }), H.end(), me.end(), W != null) W.i = A;
987
- else if (!Ne && !Ae && Ie == null) return A;
987
+ else if (!$e && !Ae && Ie == null) return A;
988
988
  }(D, D.clone(), O, O.clone(), void 0, !1, I, B), I.reset(), I.mergeTree(B.get()), I.reset(), I.get(), R.map((g) => g.get()), $.map((g) => g.get()), D.traverse(I, (g, U) => {
989
989
  const re = g.p;
990
990
  if (re != null) {
@@ -1100,8 +1100,8 @@ function en() {
1100
1100
  _(se) && U++;
1101
1101
  });
1102
1102
  for (const K of w) if (typeof K == "number") {
1103
- const se = K - re, Ne = te(se), Oe = se + U, ye = Y(N, B ? se : Oe);
1104
- C.descend(K), p(Ne, w, C, ye, B), s(w.getComponent()) && re++, C.ascend();
1103
+ const se = K - re, $e = te(se), Oe = se + U, ye = Y(N, B ? se : Oe);
1104
+ C.descend(K), p($e, w, C, ye, B), s(w.getComponent()) && re++, C.ascend();
1105
1105
  } else {
1106
1106
  const se = Y(N, K);
1107
1107
  C.descend(K), p(te(K), w, C, se, B), C.ascend();
@@ -1131,36 +1131,36 @@ function en() {
1131
1131
  z(i), z(d);
1132
1132
  let O = null;
1133
1133
  const I = [], R = [], $ = [], p = [], v = [], w = [], C = [], N = [], B = [], g = [], U = [], re = [], te = [], K = [], se = [];
1134
- let Ne = 0;
1134
+ let $e = 0;
1135
1135
  const Oe = r.readCursor(i), ye = r.readCursor(d), oe = r.writeCursor();
1136
1136
  if (function Q(A, W = null, M) {
1137
1137
  const P = l(W);
1138
1138
  P && (P.r !== void 0 ? M = W.clone() : P.p != null && (M = null, w[P.p] = A.clone()));
1139
1139
  const k = A.getComponent();
1140
- let X;
1141
- k && (X = k.p) != null && (v[X] = W ? W.clone() : null, $[X] = A.clone(), M && (g[X] = !0, B[X] = M), P && P.p != null && (K[X] = P.p));
1142
- const J = r.advancer(W);
1143
- for (const ee of A) Q(A, J(ee), M);
1144
- J.end();
1140
+ let J;
1141
+ k && (J = k.p) != null && (v[J] = W ? W.clone() : null, $[J] = A.clone(), M && (g[J] = !0, B[J] = M), P && P.p != null && (K[J] = P.p));
1142
+ const X = r.advancer(W);
1143
+ for (const ee of A) Q(A, X(ee), M);
1144
+ X.end();
1145
1145
  }(ye, Oe, null), function Q(A, W, M, P, k) {
1146
- const X = M.getComponent();
1147
- let J, ee = !1;
1148
- X && ((J = X.d) != null ? (p[J] = M.clone(), P != null && (se[P] == null && (se[P] = []), se[P].push(J)), A = v[J] || null, W = $[J] || null, g[J] ? (k && (U[J] = !0), k = B[J] || null) : !k || D !== 1 && K[J] != null || O == null && (O = {
1146
+ const J = M.getComponent();
1147
+ let X, ee = !1;
1148
+ J && ((X = J.d) != null ? (p[X] = M.clone(), P != null && (se[P] == null && (se[P] = []), se[P].push(X)), g[X], A = v[X] || null, W = $[X] || null, g[X] ? (k && (U[X] = !0), k = B[X] || null) : !k || D !== 1 && K[X] != null || O == null && (O = {
1149
1149
  type: u.ConflictType.RM_UNEXPECTED_CONTENT,
1150
1150
  op1: a.removeOp(k.getPath()),
1151
1151
  op2: a.moveOp(W.getPath(), M.getPath())
1152
- }), ee = !0) : X.i !== void 0 && (A = W = null, ee = !0, k && O == null && (O = {
1152
+ }), ee = !0) : J.i !== void 0 && (A = W = null, ee = !0, k && O == null && (O = {
1153
1153
  type: u.ConflictType.RM_UNEXPECTED_CONTENT,
1154
1154
  op1: a.removeOp(k.getPath()),
1155
- op2: a.insertOp(M.getPath(), X.i)
1155
+ op2: a.insertOp(M.getPath(), J.i)
1156
1156
  })));
1157
1157
  const fe = l(A);
1158
1158
  fe && (fe.r !== void 0 ? k = A.clone() : fe.p != null && (fe.p, P = fe.p, k = null));
1159
- const de = ve(X);
1159
+ const de = ve(J);
1160
1160
  de && k && O == null && (O = {
1161
1161
  type: u.ConflictType.RM_UNEXPECTED_CONTENT,
1162
1162
  op1: a.removeOp(k.getPath()),
1163
- op2: a.editOp(M.getPath(), de, f(X), !0)
1163
+ op2: a.editOp(M.getPath(), de, f(J), !0)
1164
1164
  });
1165
1165
  let ae = 0, me = 0;
1166
1166
  const H = r.advancer(W, (G, he) => _(he) ? ae - G - 1 : G - ae, (G, he) => {
@@ -1182,16 +1182,16 @@ function en() {
1182
1182
  const Pe = [];
1183
1183
  let Ae = null;
1184
1184
  (function Q(A, W, M, P, k) {
1185
- let X = !1;
1186
- const J = l(W);
1187
- if (_(J)) {
1188
- const H = J.p;
1189
- H != null ? (M = p[H], P = re[H] = r.writeCursor(), X = !0, k = null) : (M = null, k = W.clone());
1185
+ let J = !1;
1186
+ const X = l(W);
1187
+ if (_(X)) {
1188
+ const H = X.p;
1189
+ H != null ? (M = p[H], P = re[H] = r.writeCursor(), J = !0, k = null) : (M = null, k = W.clone());
1190
1190
  } else s(l(M)) && (M = null);
1191
1191
  const ee = A.getComponent();
1192
1192
  if (ee) {
1193
1193
  const H = ee.p;
1194
- H != null ? (k && (N[H] = k), Pe[H] = k || D === 1 && X ? null : P.getComponent(), I[H] = A.clone(), M && (C[H] = M.clone())) : ee.r !== void 0 && (k || P.write("r", !0), (k || X) && (Ae == null && (Ae = /* @__PURE__ */ new Set()), Ae.add(ee)));
1194
+ H != null ? (k && (N[H] = k), Pe[H] = k || D === 1 && J ? null : P.getComponent(), I[H] = A.clone(), M && (C[H] = M.clone())) : ee.r !== void 0 && (k || P.write("r", !0), (k || J) && (Ae == null && (Ae = /* @__PURE__ */ new Set()), Ae.add(ee)));
1195
1195
  }
1196
1196
  let fe = 0, de = 0;
1197
1197
  const ae = r.advancer(W, void 0, (H, T) => {
@@ -1209,46 +1209,46 @@ function en() {
1209
1209
  ae.end(), me.end();
1210
1210
  })(Oe, ye, ye.clone(), oe, null), oe.reset();
1211
1211
  let Le = [];
1212
- if (function Q(A, W, M, P, k, X) {
1212
+ if (function Q(A, W, M, P, k, J) {
1213
1213
  o(W);
1214
- const J = W.getComponent();
1214
+ const X = W.getComponent();
1215
1215
  let ee = l(P), fe = !1;
1216
1216
  const de = (j, ie, _e) => j ? a.moveOp(j.getPath(), ie.getPath()) : a.insertOp(ie.getPath(), _e.i);
1217
- if (s(J)) {
1218
- const j = J.d;
1217
+ if (s(X)) {
1218
+ const j = X.d;
1219
1219
  j != null && (R[j] = W.clone());
1220
1220
  const ie = j != null ? Pe[j] : null;
1221
1221
  let _e = !1;
1222
- if (J.i !== void 0 || j != null && ie) {
1222
+ if (X.i !== void 0 || j != null && ie) {
1223
1223
  let ue;
1224
- ee && (ee.i !== void 0 || (ue = ee.d) != null && !g[ue]) && (_e = ue != null ? j != null && j === K[ue] : n.default(ee.i, J.i), _e || ue != null && D !== 1 && K[ue] != null || O == null && (O = {
1224
+ ee && (ee.i !== void 0 || (ue = ee.d) != null && !g[ue]) && (_e = ue != null ? j != null && j === K[ue] : n.default(ee.i, X.i), _e || ue != null && D !== 1 && K[ue] != null || O == null && (O = {
1225
1225
  type: u.ConflictType.DROP_COLLISION,
1226
- op1: de(j != null ? I[j] : null, W, J),
1226
+ op1: de(j != null ? I[j] : null, W, X),
1227
1227
  op2: de(ue != null ? $[ue] : null, P, ee)
1228
- })), _e || (X ? O == null && (O = {
1228
+ })), _e || (J ? O == null && (O = {
1229
1229
  type: u.ConflictType.RM_UNEXPECTED_CONTENT,
1230
- op1: de(j != null ? I[j] : null, W, J),
1231
- op2: a.removeOp(X.getPath())
1232
- }) : (j != null ? (Le[Ne] = j, k.write("d", ie.p = Ne++)) : k.write("i", t.default(J.i)), fe = !0));
1230
+ op1: de(j != null ? I[j] : null, W, X),
1231
+ op2: a.removeOp(J.getPath())
1232
+ }) : (j != null ? (Le[$e] = j, k.write("d", ie.p = $e++)) : k.write("i", t.default(X.i)), fe = !0));
1233
1233
  } else if (j != null && !ie) {
1234
1234
  const ue = N[j];
1235
- ue && (X = ue.clone());
1235
+ ue && (J = ue.clone());
1236
1236
  }
1237
- j != null ? (A = I[j], M = w[j], P = C[j]) : J.i !== void 0 && (A = M = null, _e || (P = null));
1237
+ j != null ? (A = I[j], M = w[j], P = C[j]) : X.i !== void 0 && (A = M = null, _e || (P = null));
1238
1238
  } else _(l(A)) && (A = M = P = null);
1239
1239
  const ae = l(A), me = l(M);
1240
1240
  if (_(me)) {
1241
1241
  const j = me.p;
1242
- me.r !== void 0 && (!ae || ae.r === void 0) || g[j] ? (P = null, X = M.clone()) : j != null && (P = p[j], D !== 1 && K[j] != null || ((k = te[j]) || (k = te[j] = r.writeCursor()), k.reset(), X = null));
1243
- } else !s(J) && s(ee) && (P = null);
1242
+ me.r !== void 0 && (!ae || ae.r === void 0) || g[j] ? (P = null, J = M.clone()) : j != null && (P = p[j], D !== 1 && K[j] != null || ((k = te[j]) || (k = te[j] = r.writeCursor()), k.reset(), J = null));
1243
+ } else !s(X) && s(ee) && (P = null);
1244
1244
  ee = P != null ? P.getComponent() : null;
1245
- const H = ve(J);
1245
+ const H = ve(X);
1246
1246
  if (H) {
1247
- const j = f(J);
1248
- if (X) O == null && (O = {
1247
+ const j = f(X);
1248
+ if (J) O == null && (O = {
1249
1249
  type: u.ConflictType.RM_UNEXPECTED_CONTENT,
1250
1250
  op1: a.editOp(W.getPath(), H, j, !0),
1251
- op2: a.removeOp(X.getPath())
1251
+ op2: a.removeOp(J.getPath())
1252
1252
  });
1253
1253
  else {
1254
1254
  const ie = ve(ee);
@@ -1261,72 +1261,72 @@ function en() {
1261
1261
  y(k, H, _e);
1262
1262
  }
1263
1263
  }
1264
- let T = 0, G = 0, he = 0, we = 0, xe = 0, qe = 0, Me = A != null && A.descendFirst(), Ye = Me;
1265
- const Xe = r.advancer(M, void 0, (j, ie) => {
1264
+ let T = 0, G = 0, he = 0, we = 0, Re = 0, qe = 0, Me = A != null && A.descendFirst(), Je = Me;
1265
+ const ze = r.advancer(M, void 0, (j, ie) => {
1266
1266
  _(ie) && he++;
1267
1267
  });
1268
- let je = P != null && P.descendFirst(), $e = je;
1268
+ let Se = P != null && P.descendFirst(), je = Se;
1269
1269
  for (const j of W) if (typeof j == "number") {
1270
1270
  let ie;
1271
1271
  const _e = s(W.getComponent()), ue = j - G;
1272
1272
  {
1273
- let We;
1274
- for (; Me && typeof (We = A.getKey()) == "number"; ) {
1275
- We += T;
1276
- const ke = A.getComponent(), Je = _(ke);
1277
- if (We > ue || We === ue && (!Je || D === 0 && _e)) break;
1278
- if (Je) {
1273
+ let Be;
1274
+ for (; Me && typeof (Be = A.getKey()) == "number"; ) {
1275
+ Be += T;
1276
+ const ke = A.getComponent(), Xe = _(ke);
1277
+ if (Be > ue || Be === ue && (!Xe || D === 0 && _e)) break;
1278
+ if (Xe) {
1279
1279
  T--;
1280
1280
  const Ke = ke.p;
1281
- K.includes(Ke), ke.d, l(te[ke.d]), _(l(te[ke.d])), (ke.r === void 0 || Ae && Ae.has(ke)) && (Ke == null || !Pe[Ke] || D !== 1 && K.includes(Ke)) || xe--;
1281
+ K.includes(Ke), ke.d, l(te[ke.d]), _(l(te[ke.d])), (ke.r === void 0 || Ae && Ae.has(ke)) && (Ke == null || !Pe[Ke] || D !== 1 && K.includes(Ke)) || Re--;
1282
1282
  }
1283
1283
  Me = A.nextSibling();
1284
1284
  }
1285
- ie = Me && We === ue ? A : null;
1285
+ ie = Me && Be === ue ? A : null;
1286
1286
  }
1287
1287
  const Ue = ue - T;
1288
- let Be = Xe(Ue);
1288
+ let xe = ze(Ue);
1289
1289
  const ot = Ue - he;
1290
- let Qe = null;
1290
+ let Ze = null;
1291
1291
  {
1292
- let We, ke;
1293
- for (; je && typeof (We = P.getKey()) == "number"; ) {
1294
- ke = We - we;
1295
- const Je = P.getComponent(), Ke = s(Je);
1292
+ let Be, ke;
1293
+ for (; Se && typeof (Be = P.getKey()) == "number"; ) {
1294
+ ke = Be - we;
1295
+ const Xe = P.getComponent(), Ke = s(Xe);
1296
1296
  if (ke > ot) break;
1297
1297
  if (ke === ot) {
1298
1298
  if (!Ke) {
1299
- Qe = P;
1299
+ Ze = P;
1300
1300
  break;
1301
1301
  }
1302
1302
  {
1303
1303
  if (D === 0 && _e) {
1304
- Qe = P;
1304
+ Ze = P;
1305
1305
  break;
1306
1306
  }
1307
- const Fe = Be && _(Be.getComponent());
1308
- if (D === 0 && Fe) break;
1307
+ const We = xe && _(xe.getComponent());
1308
+ if (D === 0 && We) break;
1309
1309
  }
1310
1310
  }
1311
1311
  if (Ke) {
1312
- const Fe = Je.d;
1313
- K[Fe], Je.i === void 0 && (g[Fe] || K[Fe] != null && D !== 1) ? (g[Fe] || K[Fe] != null && D === 0) && (we++, qe--) : we++;
1312
+ const We = Xe.d;
1313
+ g[We], K[We], Xe.i === void 0 && (g[We] || K[We] != null && D !== 1) ? (g[We] || K[We] != null && D === 0) && (we++, qe--) : we++;
1314
1314
  }
1315
- je = P.nextSibling();
1315
+ Se = P.nextSibling();
1316
1316
  }
1317
1317
  }
1318
- const yt = ot + we + xe + qe;
1319
- o(yt >= 0, "trying to descend to a negative index"), k.descend(yt), _e && (ie = Be = Qe = null, G++), Q(ie, W, Be, Qe, k, X) && qe++, k.ascend();
1318
+ const yt = ot + we + Re + qe;
1319
+ o(yt >= 0, "trying to descend to a negative index"), k.descend(yt), _e && (ie = xe = Ze = null, G++), Q(ie, W, xe, Ze, k, J) && qe++, k.ascend();
1320
1320
  } else {
1321
1321
  let ie;
1322
1322
  for (; Me && (ie = A.getKey(), typeof ie != "string" || !(ie > j || ie === j)); ) Me = A.nextSibling();
1323
- const _e = Me && ie === j ? A : null, ue = Xe(j);
1323
+ const _e = Me && ie === j ? A : null, ue = ze(j);
1324
1324
  let Ue;
1325
- for (; je && (Ue = P.getKey(), typeof Ue != "string" || !(Ue > j || Ue === j)); ) je = P.nextSibling();
1326
- const Be = je && Ue === j ? P : null;
1327
- k.descend(j), Q(_e, W, ue, Be, k, X), k.ascend();
1325
+ for (; Se && (Ue = P.getKey(), typeof Ue != "string" || !(Ue > j || Ue === j)); ) Se = P.nextSibling();
1326
+ const xe = Se && Ue === j ? P : null;
1327
+ k.descend(j), Q(_e, W, ue, xe, k, J), k.ascend();
1328
1328
  }
1329
- return Xe.end(), Ye && A.ascend(), $e && P.ascend(), fe;
1329
+ return ze.end(), Je && A.ascend(), je && P.ascend(), fe;
1330
1330
  }(Oe, Oe.clone(), ye, ye.clone(), oe, null), O) return {
1331
1331
  ok: !1,
1332
1332
  conflict: O
@@ -1338,7 +1338,7 @@ function en() {
1338
1338
  (g.length || re.length) && (Te(ye, oe, (Q, A, W) => {
1339
1339
  g[Q] && !U[Q] && W.write("r", !0), re[Q] && W.mergeTree(re[Q].get());
1340
1340
  }), oe.reset());
1341
- const Ie = [], Re = [];
1341
+ const Ie = [], Ne = [];
1342
1342
  if ((te.length || g.length) && !O) {
1343
1343
  const Q = r.readCursor(it(oe.get()));
1344
1344
  if (Te(Q, null, (A, W) => {
@@ -1347,11 +1347,11 @@ function en() {
1347
1347
  A && Te(r.readCursor(A.get()), null, (W, M) => {
1348
1348
  Ie[W] = M.clone();
1349
1349
  });
1350
- }), function A(W, M, P, k, X, J) {
1350
+ }), function A(W, M, P, k, J, X) {
1351
1351
  const ee = l(M);
1352
1352
  if (ee && _(ee)) if (ee.p != null) {
1353
1353
  const T = ee.p;
1354
- Ie[T].getPath(), P = Ie[T], k = Re[T] = r.writeCursor();
1354
+ Ie[T].getPath(), P = Ie[T], k = Ne[T] = r.writeCursor();
1355
1355
  } else ee.r !== void 0 && (P = null);
1356
1356
  else s(l(P)) && (P = null);
1357
1357
  const fe = W.getComponent();
@@ -1369,29 +1369,29 @@ function en() {
1369
1369
  s(G) && ae++;
1370
1370
  });
1371
1371
  for (const T of W) if (typeof T == "number") {
1372
- const G = me(T), he = T + de, we = H(he), xe = he + ae;
1373
- k.descend(xe), A(W, G, we, k), k.ascend();
1372
+ const G = me(T), he = T + de, we = H(he), Re = he + ae;
1373
+ k.descend(Re), A(W, G, we, k), k.ascend();
1374
1374
  } else k.descend(T), A(W, me(T), H(T), k), k.ascend();
1375
1375
  me.end(), H.end();
1376
1376
  }(ye, Q, Q.clone(), oe), oe.reset(), O) return {
1377
1377
  ok: !1,
1378
1378
  conflict: O
1379
1379
  };
1380
- if (oe.get(), Re.length) {
1381
- const A = Re.map((M) => M ? M.get() : null), W = r.readCursor(it(oe.get()));
1380
+ if (oe.get(), Ne.length) {
1381
+ const A = Ne.map((M) => M ? M.get() : null), W = r.readCursor(it(oe.get()));
1382
1382
  if (Te(W, oe, (M, P, k) => {
1383
- const X = A[M];
1384
- X && (k.mergeTree(X), A[M] = null);
1383
+ const J = A[M];
1384
+ J && (k.mergeTree(J), A[M] = null);
1385
1385
  }), A.find((M) => M)) {
1386
1386
  const M = r.writeCursor(), P = r.writeCursor();
1387
- let k = 0, X = 0;
1388
- A.forEach((J) => {
1389
- J != null && Te(r.readCursor(J), null, (ee) => {
1387
+ let k = 0, J = 0;
1388
+ A.forEach((X) => {
1389
+ X != null && Te(r.readCursor(X), null, (ee) => {
1390
1390
  const fe = Le[ee];
1391
1391
  M.writeMove(I[fe].getPath(), R[fe].getPath(), k++);
1392
1392
  const de = se[fe];
1393
1393
  de && de.forEach((ae) => {
1394
- g[ae] || D !== 1 && K[ae] != null || P.writeMove($[ae].getPath(), p[ae].getPath(), X++);
1394
+ g[ae] || D !== 1 && K[ae] != null || P.writeMove($[ae].getPath(), p[ae].getPath(), J++);
1395
1395
  });
1396
1396
  });
1397
1397
  }), O = {
@@ -1419,7 +1419,7 @@ function en() {
1419
1419
  if (D.ok) return D.result;
1420
1420
  pt(D.conflict);
1421
1421
  }
1422
- const ze = (i) => {
1422
+ const He = (i) => {
1423
1423
  const d = r.writeCursor();
1424
1424
  return r.readCursor(i).traverse(d, (m, D) => {
1425
1425
  (s(m) || ve(m)) && D.write("r", !0);
@@ -1428,14 +1428,14 @@ function en() {
1428
1428
  const { type: m, op1: D, op2: O } = i;
1429
1429
  switch (m) {
1430
1430
  case u.ConflictType.DROP_COLLISION:
1431
- return d === "left" ? [null, ze(O)] : [ze(D), null];
1431
+ return d === "left" ? [null, He(O)] : [He(D), null];
1432
1432
  case u.ConflictType.RM_UNEXPECTED_CONTENT:
1433
1433
  let I = !1;
1434
1434
  return r.readCursor(D).traverse(null, (R) => {
1435
1435
  R.r !== void 0 && (I = !0);
1436
- }), I ? [null, ze(O)] : [ze(D), null];
1436
+ }), I ? [null, He(O)] : [He(D), null];
1437
1437
  case u.ConflictType.BLACKHOLE:
1438
- return [ze(D), ze(O)];
1438
+ return [He(D), He(O)];
1439
1439
  default:
1440
1440
  throw Error("Unrecognised conflict: " + m);
1441
1441
  }
@@ -1453,18 +1453,18 @@ function en() {
1453
1453
  }
1454
1454
  }
1455
1455
  }
1456
- }(Ze)), Ze;
1456
+ }(et)), et;
1457
1457
  }
1458
1458
  var Dt;
1459
1459
  function tn() {
1460
1460
  return Dt || (Dt = 1, function(a) {
1461
- var e = Ve && Ve.__createBinding || (Object.create ? function(u, o, l, E) {
1461
+ var e = Ye.__createBinding || (Object.create ? function(u, o, l, E) {
1462
1462
  E === void 0 && (E = l), Object.defineProperty(u, E, { enumerable: !0, get: function() {
1463
1463
  return o[l];
1464
1464
  } });
1465
1465
  } : function(u, o, l, E) {
1466
1466
  E === void 0 && (E = l), u[E] = o[l];
1467
- }), n = Ve && Ve.__exportStar || function(u, o) {
1467
+ }), n = Ye.__exportStar || function(u, o) {
1468
1468
  for (var l in u) l !== "default" && !o.hasOwnProperty(l) && e(o, u, l);
1469
1469
  };
1470
1470
  Object.defineProperty(a, "__esModule", { value: !0 }), n(en(), a);
@@ -1478,7 +1478,7 @@ function tn() {
1478
1478
  Object.defineProperty(a, "ConflictType", { enumerable: !0, get: function() {
1479
1479
  return r.ConflictType;
1480
1480
  } });
1481
- }(Ve)), Ve;
1481
+ }(Ye)), Ye;
1482
1482
  }
1483
1483
  var V = tn();
1484
1484
  class nn {
@@ -1933,11 +1933,11 @@ class an {
1933
1933
  async saveImage(e) {
1934
1934
  return new Promise((n, t) => {
1935
1935
  if (!Xt.includes(e.type)) {
1936
- t(new Error(et.ERROR_IMAGE_TYPE)), this._decreaseWaiting();
1936
+ t(new Error(Qe.ERROR_IMAGE_TYPE)), this._decreaseWaiting();
1937
1937
  return;
1938
1938
  }
1939
1939
  if (e.size > zt) {
1940
- t(new Error(et.ERROR_EXCEED_SIZE)), this._decreaseWaiting();
1940
+ t(new Error(Qe.ERROR_EXCEED_SIZE)), this._decreaseWaiting();
1941
1941
  return;
1942
1942
  }
1943
1943
  const r = new FileReader();
@@ -1945,7 +1945,7 @@ class an {
1945
1945
  var E;
1946
1946
  const o = (E = u.target) == null ? void 0 : E.result;
1947
1947
  if (o == null) {
1948
- t(new Error(et.ERROR_IMAGE)), this._decreaseWaiting();
1948
+ t(new Error(Qe.ERROR_IMAGE)), this._decreaseWaiting();
1949
1949
  return;
1950
1950
  }
1951
1951
  const l = Wt.generateRandomId(6);
@@ -1954,7 +1954,7 @@ class an {
1954
1954
  imageSourceType: at.BASE64,
1955
1955
  source: o,
1956
1956
  base64Cache: o,
1957
- status: et.SUCCUSS
1957
+ status: Qe.SUCCUSS
1958
1958
  }), this._decreaseWaiting();
1959
1959
  };
1960
1960
  });
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(ne,Y){typeof exports=="object"&&typeof module<"u"?Y(exports,require("@univerjs/core"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs"],Y):(ne=typeof globalThis<"u"?globalThis:ne||self,Y(ne.UniverDrawing={},ne.UniverCore,ne.rxjs))})(this,function(ne,Y,ge){"use strict";var Zt=Object.defineProperty;var Qt=(ne,Y,ge)=>Y in ne?Zt(ne,Y,{enumerable:!0,configurable:!0,writable:!0,value:ge}):ne[Y]=ge;var K=(ne,Y,ge)=>Qt(ne,typeof Y!="symbol"?Y+"":Y,ge);var ct;const gt=["image/png","image/jpeg","image/jpg","image/gif","image/bmp"];var xe={},Qe={},tt={},pt;function $t(){if(pt)return tt;pt=1,Object.defineProperty(tt,"__esModule",{value:!0});function l(t,n){if(Array.isArray(n))return!1;for(let u in t)if(!r(t[u],n[u]))return!1;for(let u in n)if(t[u]===void 0)return!1;return!0}function e(t,n){if(!Array.isArray(n)||t.length!==n.length)return!1;for(let u=0;u<t.length;u++)if(!r(t[u],n[u]))return!1;return!0}function r(t,n){return t===n?!0:t===null||n===null||typeof t!="object"||typeof n!="object"?!1:Array.isArray(t)?e(t,n):l(t,n)}return tt.default=r,tt}var nt={},yt;function Rt(){if(yt)return nt;yt=1,Object.defineProperty(nt,"__esModule",{value:!0});function l(e){if(e===null)return null;if(Array.isArray(e))return e.map(l);if(typeof e=="object"){const r={};for(let t in e)r[t]=l(e[t]);return r}else return e}return nt.default=l,nt}var st={},_t;function vt(){return _t||(_t=1,function(l){Object.defineProperty(l,"__esModule",{value:!0}),l.eachChildOf=l.advancer=l.readCursor=l.writeCursor=l.WriteCursor=l.ReadCursor=l.isValidPathItem=void 0;function e(_,s){if(!_)throw new Error(s)}const r=_=>_!=null&&typeof _=="object"&&!Array.isArray(_),t=(_,s)=>typeof _==typeof s?_>s:typeof _=="string"&&typeof s=="number";function n(_,s){for(let c in _){const h=c;s.write(h,_[h])}}l.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--,r(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-=r(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&&r(s=this.container[this.idx+1])?s:null}descendFirst(){let s=this.idx+1;if(!this.container||s>=this.container.length||r(this.container[s])&&s+1>=this.container.length)return!1;r(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)})}}l.ReadCursor=o;class a 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&&r(this.container[h])&&h++,e(h===this.container.length||!r(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(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 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&&r(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(!l.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=n){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 Z=0;for(let _e=0;_e<s.length;_e++){const Q=s[_e];typeof Q=="string"||typeof Q=="number"?(Z++,this.descend(Q)):Array.isArray(Q)?this.mergeTree(Q,c):typeof Q=="object"&&c(Q,this)}for(;Z--;)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}}l.WriteCursor=a,l.writeCursor=()=>new a,l.readCursor=_=>new o(_);function E(_,s,c){let h,j;j=h=_?_.descendFirst():!1;function Z(_e){let Q;for(;j;){const Ee=Q=_.getKey();if(_e!=null){let Ke=!1;if(s&&typeof Ee=="number"&&(Q=s(Ee,_.getComponent()),Q<0&&(Q=~Q,Ke=!0)),t(Q,_e))return null;if(Q===_e&&!Ke)return _}c&&typeof Q=="number"&&c(Q,_.getComponent()),j=_.nextSibling()}return null}return Z.end=()=>{h&&_.ascend()},Z}l.advancer=E;function W(_,s,c){let h,j,Z,_e;for(h=j=_&&_.descendFirst(),Z=_e=s&&s.descendFirst();h||Z;){let Q=h?_.getKey():null,Ee=Z?s.getKey():null;Q!==null&&Ee!==null&&(t(Ee,Q)?Ee=null:Q!==Ee&&(Q=null)),c(Q==null?Ee:Q,Q!=null?_:null,Ee!=null?s:null),Q!=null&&h&&(h=_.nextSibling()),Ee!=null&&Z&&(Z=s.nextSibling())}j&&_.ascend(),_e&&s.ascend()}l.eachChildOf=W}(st)),st}var ot={},mt;function Ot(){return mt||(mt=1,function(l){Object.defineProperty(l,"__esModule",{value:!0}),l.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"}(l.ConflictType||(l.ConflictType={}))}(ot)),ot}var Me={},He={},bt;function at(){return bt||(bt=1,Object.defineProperty(He,"__esModule",{value:!0}),He.uniToStrPos=He.strPosToUni=void 0,He.strPosToUni=(l,e=l.length)=>{let r=0,t=0;for(;t<e;t++){const n=l.charCodeAt(t);n>=55296&&n<=57343&&(r++,t++)}if(t!==e)throw Error("Invalid offset - splits unicode bytes");return t-r},He.uniToStrPos=(l,e)=>{let r=0;for(;e>0;e--){const t=l.charCodeAt(r);r+=t>=55296&&t<=57343?2:1}return r}),He}var lt={},wt;function ut(){return wt||(wt=1,function(l){Object.defineProperty(l,"__esModule",{value:!0}),l.uniSlice=l.dlen=l.eachOp=void 0;const e=at(),r=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(l.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 x=f[z];switch(y(x,w,q),typeof x){case"object":w+=l.dlen(x.d);break;case"string":q+=e.strPosToUni(x);break;case"number":w+=x,q+=x;break}}}l.eachOp=t;function n(f,y){const w=[],q=a(w);return t(f,(z,x,Te)=>{q(y(z,x,Te))}),s(w)}const u=f=>f,o=f=>n(f,u);l.dlen=f=>typeof f=="number"?f:e.strPosToUni(f);const a=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:l.dlen(w.d)+l.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);l.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 W=(f,y,w)=>typeof f=="number"?w==null?f-y:Math.min(f,w)-y:l.uniSlice(f,y,w),_=f=>{let y=0,w=0;return{take:(x,Te)=>{if(y===f.length)return x===-1?null:x;const fe=f[y];let ie;if(typeof fe=="number")return x===-1||fe-w<=x?(ie=fe-w,++y,w=0,ie):(w+=x,x);if(typeof fe=="string"){if(x===-1||Te==="i"||e.strPosToUni(fe.slice(w))<=x)return ie=fe.slice(w),++y,w=0,ie;{const ce=w+e.uniToStrPos(fe.slice(w),x);return ie=fe.slice(w,ce),w=ce,ie}}else{if(x===-1||Te==="d"||l.dlen(fe.d)-w<=x)return ie={d:W(fe.d,w)},++y,w=0,ie;{let ce=W(fe.d,w,w+x);return w+=x,{d:ce}}}},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'");r(f),r(y);const q=[],z=a(q),{take:x,peek:Te}=_(f);for(let ie=0;ie<y.length;ie++){const ce=y[ie];let ve,Se;switch(typeof ce){case"number":for(ve=ce;ve>0;)Se=x(ve,"i"),z(Se),typeof Se!="string"&&(ve-=E(Se));break;case"string":w==="left"&&typeof Te()=="string"&&z(x(-1)),z(e.strPosToUni(ce));break;case"object":for(ve=l.dlen(ce.d);ve>0;)switch(Se=x(ve,"i"),typeof Se){case"number":ve-=Se;break;case"string":z(Se);break;case"object":ve-=l.dlen(Se.d)}break}}let fe;for(;fe=x(-1);)z(fe);return s(q)}function h(f,y){r(f),r(y);const w=[],q=a(w),{take:z}=_(f);for(let Te=0;Te<y.length;Te++){const fe=y[Te];let ie,ce;switch(typeof fe){case"number":for(ie=fe;ie>0;)ce=z(ie,"d"),q(ce),typeof ce!="object"&&(ie-=E(ce));break;case"string":q(fe);break;case"object":ie=l.dlen(fe.d);let ve=0;for(;ve<ie;)switch(ce=z(ie-ve,"d"),typeof ce){case"number":q({d:W(fe.d,ve,ve+ce)}),ve+=ce;break;case"string":ve+=e.strPosToUni(ce);break;case"object":q(ce)}break}}let x;for(;x=z(-1);)q(x);return s(w)}const j=(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 x=e.strPosToUni(z);w+=x,f+=x;break;case"object":f-=Math.min(l.dlen(z.d),f-w);break}}return f},Z=(f,y)=>typeof f=="number"?j(f,y):f.map(w=>j(w,y));function _e(f,y,w){return n(f,(q,z)=>typeof q=="object"&&typeof q.d=="number"?{d:w.slice(y,z,z+q.d)}:q)}function Q(f){return n(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 Ee(f){return n(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 be(f){return{name:"text-unicode",uri:"http://sharejs.org/types/text-unicode",trim:s,normalize:o,checkOp:r,create(y=""){if(typeof y!="string")throw Error("Initial data must be a string");return f.create(y)},apply(y,w){r(w);const q=f.builder(y);for(let z=0;z<w.length;z++){const x=w[z];switch(typeof x){case"number":q.skip(x);break;case"string":q.append(x);break;case"object":q.del(l.dlen(x.d));break}}return q.build()},transform:c,compose:h,transformPosition:j,transformSelection:Z,isInvertible:Ke,makeInvertible(y,w){return _e(y,w,f)},stripInvertible:Ee,invert:Q,invertWithDoc(y,w){return Q(_e(y,w,f))},isNoop:y=>y.length===0}}l.default=be}(lt)),lt}var rt={},It;function Wt(){if(It)return rt;It=1,Object.defineProperty(rt,"__esModule",{value:!0});const l=ut(),e=at();function r(t,n){return{get:t,getLength(){return t().length},insert(u,o,a){const E=e.strPosToUni(t(),u);return n([E,o],a)},remove(u,o,a){const E=e.strPosToUni(t(),u);return n([E,{d:o}],a)},_onOp(u){l.eachOp(u,(o,a,E)=>{switch(typeof o){case"string":this.onInsert&&this.onInsert(E,o);break;case"object":const W=l.dlen(o.d);this.onRemove&&this.onRemove(E,W)}})},onInsert:null,onRemove:null}}return rt.default=r,r.provides={text:!0},rt}var Ct;function Gt(){return Ct||(Ct=1,function(l){var e=Me&&Me.__createBinding||(Object.create?function(c,h,j,Z){Z===void 0&&(Z=j),Object.defineProperty(c,Z,{enumerable:!0,get:function(){return h[j]}})}:function(c,h,j,Z){Z===void 0&&(Z=j),c[Z]=h[j]}),r=Me&&Me.__setModuleDefault||(Object.create?function(c,h){Object.defineProperty(c,"default",{enumerable:!0,value:h})}:function(c,h){c.default=h}),t=Me&&Me.__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 r(h,c),h},n=Me&&Me.__importDefault||function(c){return c&&c.__esModule?c:{default:c}};Object.defineProperty(l,"__esModule",{value:!0}),l.type=l.remove=l.insert=void 0;const u=at(),o=t(ut()),a=n(Wt()),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(j){let Z=u.uniToStrPos(c,j);if(Z>c.length)throw Error("The op is too long for this document");h.push(c.slice(0,Z)),c=c.slice(Z)},append(j){h.push(j)},del(j){c=c.slice(u.uniToStrPos(c,j))},build(){return h.join("")+c}}},slice:o.uniSlice},W=o.default(E),_=Object.assign(Object.assign({},W),{api:a.default});l.type=_,l.insert=(c,h)=>h.length===0?[]:c===0?[h]:[c,h],l.remove=(c,h)=>o.dlen(h)===0?[]:c===0?[{d:h}]:[c,{d:h}];var s=ut();Object.defineProperty(l,"makeType",{enumerable:!0,get:function(){return s.default}})}(Me)),Me}var Et;function Lt(){return Et||(Et=1,function(l){var e=Qe&&Qe.__importDefault||function(i){return i&&i.__esModule?i:{default:i}};Object.defineProperty(l,"__esModule",{value:!0}),l.editOp=l.replaceOp=l.insertOp=l.moveOp=l.removeOp=l.type=void 0;const r=e($t()),t=e(Rt()),n=vt(),u=Ot();function o(i,d){if(!i)throw new Error(d)}l.type={name:"json1",uri:"http://sharejs.org/types/JSONv1",readCursor:n.readCursor,writeCursor:n.writeCursor,create:i=>i,isNoop:i=>i==null,setDebug(i){},registerSubtype:Q,checkValidOp:z,normalize:x,apply:Te,transformPosition:fe,compose:ie,tryTransform:ft,transform:Jt,makeInvertible:Se,invert:ce,invertWithDoc:Xt,RM_UNEXPECTED_CONTENT:u.ConflictType.RM_UNEXPECTED_CONTENT,DROP_COLLISION:u.ConflictType.DROP_COLLISION,BLACKHOLE:u.ConflictType.BLACKHOLE,transformNoConflict:(i,d,m)=>kt(()=>!0,i,d,m),typeAllowingConflictsPred:i=>Object.assign(Object.assign({},l.type),{transform:(d,m,D)=>kt(i,d,m,D)})};const a=i=>i?i.getComponent():null;function E(i){return i&&typeof i=="object"&&!Array.isArray(i)}const W=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}l.removeOp=(i,d=!0)=>n.writeCursor().writeAtPath(i,"r",d).get(),l.moveOp=(i,d)=>n.writeCursor().writeMove(i,d).get(),l.insertOp=(i,d)=>n.writeCursor().writeAtPath(i,"i",d).get(),l.replaceOp=(i,d,m)=>n.writeCursor().at(i,D=>{D.write("r",d),D.write("i",m)}).get(),l.editOp=(i,d,m,D=!1)=>n.writeCursor().at(i,O=>y(O,d,m,D)).get();const j=(i,d)=>i!=null&&(typeof d=="number"?Array.isArray(i):typeof i=="object"),Z=(i,d)=>j(i,d)?i[d]:void 0,_e={};function Q(i){let d=i.type?i.type:i;d.name&&(_e[d.name]=d),d.uri&&(_e[d.uri]=d)}const Ee=i=>{const d=_e[i];if(d)return d;throw Error("Missing type: "+i)};Q(Gt());const Ke=(i,d)=>i+d;Q({name:"number",apply:Ke,compose:Ke,invert:i=>-i,transform:i=>i});const be=i=>i==null?null:i.et?Ee(i.et):i.es?_e["text-unicode"]:i.ena!=null?_e.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"?[Ee(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 R=!0,N=!1;for(let g in C){const v=C[g];if(R=!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=be(C);o(b,"Missing type in edit"),b.checkValidOp&&b.checkValidOp(f(C))}}o(!R)},O=(C,R,N)=>{if(!Array.isArray(C))throw Error("Op must be null or a list");if(C.length===0)throw Error("Empty descent");R||q(C[0]);let g=1,v=0,b=0;for(let I=0;I<C.length;I++){const $=C[I];if(o($!=null),Array.isArray($)){const G=O($,!1);if(v){const p=typeof b,M=typeof G;p===M?o(b<G,"descent keys are not in order"):o(p==="number"&&M==="string")}b=G,v++,g=3}else typeof $=="object"?(o(g===1,`Prev not scalar - instead ${g}`),D($),g=2):(o(g!==3),q($),o(n.isValidPathItem($),"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 x(i){let d=0,m=[];const D=n.writeCursor();return D.mergeTree(i,(O,C)=>{const R=be(O);if(R){const g=f(O);y(C,R,R.normalize?R.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 Te(i,d){if(z(d),d===null)return i;const m=[];return function D(O,C){let R=O,N=0,g={root:O},v=0,b=g,I="root";function $(){for(;v<N;v++){let G=C[v];typeof G!="object"&&(o(j(b,I)),b=b[I]=W(b[I]),I=G)}}for(;N<C.length;N++){const G=C[N];if(Array.isArray(G)){const p=D(R,G);p!==R&&p!==void 0&&($(),R=b[I]=p)}else if(typeof G=="object"){G.d!=null?($(),R=h(b,I,m[G.d])):G.i!==void 0&&($(),R=h(b,I,G.i));const p=be(G);if(p)$(),R=b[I]=p.apply(R,f(G));else if(G.e!==void 0)throw Error("Subtype "+G.et+" undefined")}else R=Z(R,G)}return g.root}(i=function D(O,C){const R=[];let N=0;for(;N<C.length;N++){const I=C[N];if(Array.isArray(I))break;typeof I!="object"&&(R.push(O),O=Z(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.pop();O=O===Z($,I)?$:O===void 0?c($,I):(v=I,b=O,(g=W(g=$))[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 fe(i,d){i=i.slice(),z(d);const m=n.readCursor(d);let D,O,C=!1;const R=[];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 $=n.advancer(m,void 0,(p,M)=>{_(M)&&I++});R.unshift($);const G=$(v);if(typeof v=="number"&&(i[g]-=I),!G)break}if(R.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=a(m),I=be(b);if(I){const p=f(b);I.transformPosition&&(i[g]=I.transformPosition(i[g],p));break}let $=0;const G=n.advancer(m,(p,M)=>s(M)?~(p-$):p-$,(p,M)=>{s(M)&&$++})(v);if(typeof v=="number"&&(i[g]+=$),!G)break}};return D!=null?m.eachDrop(null,g=>{g===D&&N()}):N(),i}function ie(i,d){if(z(i),z(d),i==null)return d;if(d==null)return i;let m=0;const D=n.readCursor(i),O=n.readCursor(d),C=n.writeCursor(),R=[],N=[],g=[],v=[],b=[],I=[],$=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 G=n.writeCursor();return function p(M,se,re,H,ae,We,Ie,me){o(se||re);const le=a(se),Pe=a(re),Ue=!!Pe&&Pe.r!==void 0,Fe=!!le&&le.i!==void 0,Ae=le?le.d:null,De=Pe?Pe.p:null,Ge=(We||Ue)&&De==null;if(De!=null)H=v[De],Ie=N[De]=new n.WriteCursor;else if(Pe&&Pe.r!==void 0)H=null;else{const S=a(H);S&&S.d!=null&&(H=null)}const ee=a(H);if(Ae!=null)if(M=g[Ae],me=R[Ae]=new n.WriteCursor,Ge)We&&!Ue&&me.write("r",!0);else{const S=b[Ae]=m++;Ie.write("d",S)}else if(le&&le.i!==void 0)M=null;else{const S=a(M);S&&S.p!=null&&(M=null)}let P;Fe?(o(ae===void 0),P=le.i):P=ae;const L=(De==null?!Fe||We||Ue:P===void 0)?null:Ie.getComponent();if(De!=null){if(!(ae!==void 0||Fe)){const S=Ae!=null?b[Ae]:m++;I[De]=S,me.write("p",S)}}else Ue&&(Fe||ae!==void 0||(Pe.r,me.write("r",Pe.r)));const A=Ge?null:be(le),T=be(ee);if((A||T)&&(A&&A.name,T&&T.name),A&&T){o(A===T);const S=f(le),B=f(ee),ye=A.compose(S,B);y(Ie,A,ye),$.add(ee)}else A?y(Ie,A,f(le)):T&&(y(Ie,T,f(ee)),$.add(ee));const U=typeof P=="object"&&P!=null;let X=!1,J=0,te=0,pe=0,he=0,ue=0;const we=n.advancer(H,(S,B)=>s(B)?he-S-1:S-he,(S,B)=>{s(B)&&he++}),V=n.advancer(M,(S,B)=>_(B)?J-S-1:S-J,(S,B)=>{_(B)&&J++});if(n.eachChildOf(se,re,(S,B,ye)=>{let Ce,Le,Ve=S,je=S,et=S;if(typeof S=="number"){let Oe=S+pe;Le=we(Oe),je=Oe+he;let de=S+te;Ce=V(de),s(a(Le))&&(Ce=null),Ve=de+J,et=S+ue,o(Ve>=0,"p1PickKey is negative"),o(je>=0,"p2DropKey is negative");const ke=s(a(B)),Be=_(a(ye));(ke||Be&&!Ge)&&ue--,ke&&te--,Be&&pe--}else Ce=V(S),Le=we(S);me.descend(Ve),Ie.descend(je);const Ye=U&&!s(a(B))?P[et]:void 0,$e=p(Ce,B,ye,Le,Ye,Ge,Ie,me);var Re,k,oe;U&&!Ge?Ye!==$e&&(X||(P=Array.isArray(P)?P.slice():Object.assign({},P),X=!0),Re=P,oe=$e,typeof(k=et)=="number"?(o(Array.isArray(Re)),o(k<Re.length)):(o(!Array.isArray(Re)),o(Re[k]!==void 0)),oe===void 0?typeof k=="number"?Re.splice(k,1):delete Re[k]:Re[k]=oe):o($e===void 0),Ie.ascend(),me.ascend()}),V.end(),we.end(),L!=null)L.i=P;else if(!We&&!Ue&&De==null)return P}(D,D.clone(),O,O.clone(),void 0,!1,C,G),C.reset(),C.mergeTree(G.get()),C.reset(),C.get(),R.map(p=>p.get()),N.map(p=>p.get()),D.traverse(C,(p,M)=>{const se=p.p;if(se!=null){const re=b[se];re!=null&&M.write("p",re);const H=R[se];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 se=p.d;if(se!=null){const H=I[se];H!=null&&M.write("d",H);const ae=N[se];ae&&M.mergeTree(ae.get())}else p.i!==void 0&&M.write("i",p.i);const re=be(p);re&&!$.has(p)&&y(M,re,f(p))}),C.get()}function ce(i){if(i==null)return null;const d=new n.ReadCursor(i),m=new n.WriteCursor;let D;const O=[],C=[];return function R(N,g,v){const b=N.getComponent();let I,$=!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=be(b);p&&(v===void 0?(D||(D=new Set),D.add(b)):(f(b),v=p.apply(v,f(b)),$=!0))}let G=0;for(const p of N){g.descend(p);const M=typeof p=="number"?p-G:p,se=Z(v,M);s(N.getComponent())&&G++;const re=R(N,g,se);if(v!==void 0&&re!==void 0){if($||($=!0,v=W(v)),!j(v,M))throw Error("Cannot modify child - invalid operation");v[M]=re}g.ascend()}if(I===void 0)return $?v:void 0;g.write("r",v)}(d,m,void 0),D&&(m.reset(),function R(N,g,v){const b=g.getComponent();if(b){const p=b.d;if(p!=null&&(N=O[p],v=C[p]=n.writeCursor()),D.has(b)){const M=be(b);if(!M.invert)throw Error(`Cannot invert subtype ${M.name}`);y(v,M,M.invert(f(b)))}}let I=0,$=0;const G=n.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-$,se=G(M),re=M+I;v.descend(re),R(se,g,v),s(g.getComponent())&&$++,v.ascend()}else v.descend(p),R(G(p),g,v),v.ascend();G.end()}(d.clone(),d,m),C.length&&(m.reset(),d.traverse(m,(R,N)=>{const g=R.p;if(g!=null){const v=C[g];v&&v.get(),v&&N.mergeTree(v.get())}}))),m.get()}const ve=(i,d)=>i.some(m=>typeof m=="object"&&(Array.isArray(m)?ve(m,d):d(m)));function Se(i,d){if(i==null||!ve(i,g=>{var v;return g.r!==void 0||((v=be(g))===null||v===void 0?void 0:v.makeInvertible)!=null}))return i;const m=new n.ReadCursor(i),D=new n.WriteCursor;let O=!1;const C=[],R=[],N=(g,v,b)=>{const I=g.getComponent();let $=!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"),R[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=be(I);M&&(M.makeInvertible?O=!0:y(v,M,f(I),!0))}let G=0;for(const p of g){v.descend(p);const M=typeof p=="number"?p-G:p,se=Z(b,M),re=N(g,v,se);se!==re&&($||($=!0,b=W(b)),re===void 0?(b=c(b,M),typeof p=="number"&&G++):b[M]=re),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,$,G){const p=b.getComponent();if(p){p.i!==void 0?($=p.i,G=!0):p.d!=null&&($=R[p.d],v=C[p.d],G=!1,p.d);let H=be(p);if(H&&H.makeInvertible){const ae=f(p);y(I,H,H.makeInvertible(ae,$),!0)}}let M=0,se=0;const re=n.advancer(v,(H,ae)=>_(ae)?M-H-1:H-M,(H,ae)=>{_(ae)&&M++});for(const H of b)if(typeof H=="number"){const ae=H-se,We=re(ae),Ie=ae+M,me=Z($,G?ae:Ie);I.descend(H),g(We,b,I,me,G),s(b.getComponent())&&se++,I.ascend()}else{const ae=Z($,H);I.descend(H),g(re(H),b,I,ae,G),I.ascend()}re.end()}(m.clone(),m,D,d,!1)),D.get()}function Xt(i,d){return ce(Se(i,d))}const dt=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]=dt(D))}return d};function ft(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=[],R=[],N=[],g=[],v=[],b=[],I=[],$=[],G=[],p=[],M=[],se=[],re=[],H=[],ae=[];let We=0;const Ie=n.readCursor(i),me=n.readCursor(d),le=n.writeCursor();if(function ee(P,L=null,A){const T=a(L);T&&(T.r!==void 0?A=L.clone():T.p!=null&&(A=null,b[T.p]=P.clone()));const U=P.getComponent();let X;U&&(X=U.p)!=null&&(v[X]=L?L.clone():null,N[X]=P.clone(),A&&(p[X]=!0,G[X]=A),T&&T.p!=null&&(H[X]=T.p));const J=n.advancer(L);for(const te of P)ee(P,J(te),A);J.end()}(me,Ie,null),function ee(P,L,A,T,U){const X=A.getComponent();let J,te=!1;X&&((J=X.d)!=null?(g[J]=A.clone(),T!=null&&(ae[T]==null&&(ae[T]=[]),ae[T].push(J)),P=v[J]||null,L=N[J]||null,p[J]?(U&&(M[J]=!0),U=G[J]||null):!U||D!==1&&H[J]!=null||O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:l.removeOp(U.getPath()),op2:l.moveOp(L.getPath(),A.getPath())}),te=!0):X.i!==void 0&&(P=L=null,te=!0,U&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:l.removeOp(U.getPath()),op2:l.insertOp(A.getPath(),X.i)})));const pe=a(P);pe&&(pe.r!==void 0?U=P.clone():pe.p!=null&&(pe.p,T=pe.p,U=null));const he=be(X);he&&U&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:l.removeOp(U.getPath()),op2:l.editOp(A.getPath(),he,f(X),!0)});let ue=0,we=0;const V=n.advancer(L,(B,ye)=>_(ye)?ue-B-1:B-ue,(B,ye)=>{_(ye)&&ue++}),S=n.advancer(P);for(const B of A)if(typeof B=="number"){const ye=B-we,Ce=V(ye);we+=+ee(S(ye+ue),Ce,A,T,U)}else{const ye=V(B);ee(S(B),ye,A,T,U)}return V.end(),S.end(),te}(Ie,me,me.clone(),null,null),g.map(ee=>ee&&ee.get()),O)return{ok:!1,conflict:O};M.map(ee=>!!ee);const Pe=[];let Ue=null;(function ee(P,L,A,T,U){let X=!1;const J=a(L);if(_(J)){const V=J.p;V!=null?(A=g[V],T=se[V]=n.writeCursor(),X=!0,U=null):(A=null,U=L.clone())}else s(a(A))&&(A=null);const te=P.getComponent();if(te){const V=te.p;V!=null?(U&&($[V]=U),Pe[V]=U||D===1&&X?null:T.getComponent(),C[V]=P.clone(),A&&(I[V]=A.clone())):te.r!==void 0&&(U||T.write("r",!0),(U||X)&&(Ue==null&&(Ue=new Set),Ue.add(te)))}let pe=0,he=0;const ue=n.advancer(L,void 0,(V,S)=>{_(S)&&pe++}),we=n.advancer(A,(V,S)=>s(S)?~(V-he):V-he,(V,S)=>{s(S)&&he++});if(P)for(const V of P)if(typeof V=="string"){const S=ue(V),B=we(V);T.descend(V),ee(P,S,B,T,U),T.ascend()}else{const S=ue(V),B=V-pe,ye=_(a(S))?null:we(B),Ce=B+he;o(Ce>=0),T.descend(Ce),ee(P,S,ye,T,U),T.ascend()}ue.end(),we.end()})(Ie,me,me.clone(),le,null),le.reset();let Fe=[];if(function ee(P,L,A,T,U,X){o(L);const J=L.getComponent();let te=a(T),pe=!1;const he=(k,oe,Oe)=>k?l.moveOp(k.getPath(),oe.getPath()):l.insertOp(oe.getPath(),Oe.i);if(s(J)){const k=J.d;k!=null&&(R[k]=L.clone());const oe=k!=null?Pe[k]:null;let Oe=!1;if(J.i!==void 0||k!=null&&oe){let de;te&&(te.i!==void 0||(de=te.d)!=null&&!p[de])&&(Oe=de!=null?k!=null&&k===H[de]:r.default(te.i,J.i),Oe||de!=null&&D!==1&&H[de]!=null||O==null&&(O={type:u.ConflictType.DROP_COLLISION,op1:he(k!=null?C[k]:null,L,J),op2:he(de!=null?N[de]:null,T,te)})),Oe||(X?O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:he(k!=null?C[k]:null,L,J),op2:l.removeOp(X.getPath())}):(k!=null?(Fe[We]=k,U.write("d",oe.p=We++)):U.write("i",t.default(J.i)),pe=!0))}else if(k!=null&&!oe){const de=$[k];de&&(X=de.clone())}k!=null?(P=C[k],A=b[k],T=I[k]):J.i!==void 0&&(P=A=null,Oe||(T=null))}else _(a(P))&&(P=A=T=null);const ue=a(P),we=a(A);if(_(we)){const k=we.p;we.r!==void 0&&(!ue||ue.r===void 0)||p[k]?(T=null,X=A.clone()):k!=null&&(T=g[k],D!==1&&H[k]!=null||((U=re[k])||(U=re[k]=n.writeCursor()),U.reset(),X=null))}else!s(J)&&s(te)&&(T=null);te=T!=null?T.getComponent():null;const V=be(J);if(V){const k=f(J);if(X)O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:l.editOp(L.getPath(),V,k,!0),op2:l.removeOp(X.getPath())});else{const oe=be(te);let Oe;if(oe){if(V!==oe)throw Error("Transforming incompatible types");const de=f(te);Oe=V.transform(k,de,m)}else Oe=t.default(k);y(U,V,Oe)}}let S=0,B=0,ye=0,Ce=0,Le=0,Ve=0,je=P!=null&&P.descendFirst(),et=je;const Ye=n.advancer(A,void 0,(k,oe)=>{_(oe)&&ye++});let $e=T!=null&&T.descendFirst(),Re=$e;for(const k of L)if(typeof k=="number"){let oe;const Oe=s(L.getComponent()),de=k-B;{let qe;for(;je&&typeof(qe=P.getKey())=="number";){qe+=S;const Ne=P.getComponent(),Ze=_(Ne);if(qe>de||qe===de&&(!Ze||D===0&&Oe))break;if(Ze){S--;const ze=Ne.p;H.includes(ze),Ne.d,a(re[Ne.d]),_(a(re[Ne.d])),(Ne.r===void 0||Ue&&Ue.has(Ne))&&(ze==null||!Pe[ze]||D!==1&&H.includes(ze))||Le--}je=P.nextSibling()}oe=je&&qe===de?P:null}const ke=de-S;let Be=Ye(ke);const ht=ke-ye;let it=null;{let qe,Ne;for(;$e&&typeof(qe=T.getKey())=="number";){Ne=qe-Ce;const Ze=T.getComponent(),ze=s(Ze);if(Ne>ht)break;if(Ne===ht){if(!ze){it=T;break}{if(D===0&&Oe){it=T;break}const Xe=Be&&_(Be.getComponent());if(D===0&&Xe)break}}if(ze){const Xe=Ze.d;H[Xe],Ze.i===void 0&&(p[Xe]||H[Xe]!=null&&D!==1)?(p[Xe]||H[Xe]!=null&&D===0)&&(Ce++,Ve--):Ce++}$e=T.nextSibling()}}const Nt=ht+Ce+Le+Ve;o(Nt>=0,"trying to descend to a negative index"),U.descend(Nt),Oe&&(oe=Be=it=null,B++),ee(oe,L,Be,it,U,X)&&Ve++,U.ascend()}else{let oe;for(;je&&(oe=P.getKey(),typeof oe!="string"||!(oe>k||oe===k));)je=P.nextSibling();const Oe=je&&oe===k?P:null,de=Ye(k);let ke;for(;$e&&(ke=T.getKey(),typeof ke!="string"||!(ke>k||ke===k));)$e=T.nextSibling();const Be=$e&&ke===k?T:null;U.descend(k),ee(Oe,L,de,Be,U,X),U.ascend()}return Ye.end(),et&&P.ascend(),Re&&T.ascend(),pe}(Ie,Ie.clone(),me,me.clone(),le,null),O)return{ok:!1,conflict:O};le.reset();const Ae=(ee,P,L)=>ee.traverse(P,(A,T)=>{A.d!=null&&L(A.d,ee,T)});(p.length||se.length)&&(Ae(me,le,(ee,P,L)=>{p[ee]&&!M[ee]&&L.write("r",!0),se[ee]&&L.mergeTree(se[ee].get())}),le.reset());const De=[],Ge=[];if((re.length||p.length)&&!O){const ee=n.readCursor(dt(le.get()));if(Ae(ee,null,(P,L)=>{De[P]=L.clone()}),re.forEach(P=>{P&&Ae(n.readCursor(P.get()),null,(L,A)=>{De[L]=A.clone()})}),function P(L,A,T,U,X,J){const te=a(A);if(te&&_(te))if(te.p!=null){const S=te.p;De[S].getPath(),T=De[S],U=Ge[S]=n.writeCursor()}else te.r!==void 0&&(T=null);else s(a(T))&&(T=null);const pe=L.getComponent();if(pe){let S;if((S=pe.d)!=null){const B=re[S];B&&(B.get(),U.mergeTree(B.get()),T=n.readCursor(B.get()))}}let he=0,ue=0;const we=n.advancer(A,void 0,(S,B)=>{_(B)&&he--}),V=n.advancer(T,(S,B)=>s(B)?-(S-ue)-1:S-ue,(S,B)=>{s(B)&&ue++});for(const S of L)if(typeof S=="number"){const B=we(S),ye=S+he,Ce=V(ye),Le=ye+ue;U.descend(Le),P(L,B,Ce,U),U.ascend()}else U.descend(S),P(L,we(S),V(S),U),U.ascend();we.end(),V.end()}(me,ee,ee.clone(),le),le.reset(),O)return{ok:!1,conflict:O};if(le.get(),Ge.length){const P=Ge.map(A=>A?A.get():null),L=n.readCursor(dt(le.get()));if(Ae(L,le,(A,T,U)=>{const X=P[A];X&&(U.mergeTree(X),P[A]=null)}),P.find(A=>A)){const A=n.writeCursor(),T=n.writeCursor();let U=0,X=0;P.forEach(J=>{J!=null&&Ae(n.readCursor(J),null,te=>{const pe=Fe[te];A.writeMove(C[pe].getPath(),R[pe].getPath(),U++);const he=ae[pe];he&&he.forEach(ue=>{p[ue]||D!==1&&H[ue]!=null||T.writeMove(N[ue].getPath(),g[ue].getPath(),X++)})})}),O={type:u.ConflictType.BLACKHOLE,op1:A.get(),op2:T.get()}}}}return O?{ok:!1,conflict:O}:{ok:!0,result:le.get()}}const jt=i=>{const d=new Error("Transform detected write conflict");throw d.conflict=i,d.type=d.name="writeConflict",d};function Jt(i,d,m){const D=ft(i,d,m);if(D.ok)return D.result;jt(D.conflict)}const Je=i=>{const d=n.writeCursor();return n.readCursor(i).traverse(d,(m,D)=>{(s(m)||be(m))&&D.write("r",!0)}),d.get()},Yt=(i,d)=>{const{type:m,op1:D,op2:O}=i;switch(m){case u.ConflictType.DROP_COLLISION:return d==="left"?[null,Je(O)]:[Je(D),null];case u.ConflictType.RM_UNEXPECTED_CONTENT:let C=!1;return n.readCursor(D).traverse(null,R=>{R.r!==void 0&&(C=!0)}),C?[null,Je(O)]:[Je(D),null];case u.ConflictType.BLACKHOLE:return[Je(D),Je(O)];default:throw Error("Unrecognised conflict: "+m)}};function kt(i,d,m,D){let O=null;for(;;){const C=ft(d,m,D);if(C.ok)return ie(O,C.result);{const{conflict:R}=C;i(R)||jt(R);const[N,g]=Yt(R,D);d=ie(x(d),N),m=ie(x(m),g),O=ie(O,g)}}}}(Qe)),Qe}var Dt;function Bt(){return Dt||(Dt=1,function(l){var e=xe&&xe.__createBinding||(Object.create?function(u,o,a,E){E===void 0&&(E=a),Object.defineProperty(u,E,{enumerable:!0,get:function(){return o[a]}})}:function(u,o,a,E){E===void 0&&(E=a),u[E]=o[a]}),r=xe&&xe.__exportStar||function(u,o){for(var a in u)a!=="default"&&!o.hasOwnProperty(a)&&e(o,u,a)};Object.defineProperty(l,"__esModule",{value:!0}),r(Lt(),l);var t=vt();Object.defineProperty(l,"ReadCursor",{enumerable:!0,get:function(){return t.ReadCursor}}),Object.defineProperty(l,"WriteCursor",{enumerable:!0,get:function(){return t.WriteCursor}});var n=Ot();Object.defineProperty(l,"ConflictType",{enumerable:!0,get:function(){return n.ConflictType}})}(xe)),xe}var F=Bt();class Tt{constructor(){K(this,"drawingManagerData",{});K(this,"_oldDrawingManagerData",{});K(this,"_focusDrawings",[]);K(this,"_remove$",new ge.Subject);K(this,"remove$",this._remove$.asObservable());K(this,"_add$",new ge.Subject);K(this,"add$",this._add$.asObservable());K(this,"_update$",new ge.Subject);K(this,"update$",this._update$.asObservable());K(this,"_order$",new ge.Subject);K(this,"order$",this._order$.asObservable());K(this,"_group$",new ge.Subject);K(this,"group$",this._group$.asObservable());K(this,"_ungroup$",new ge.Subject);K(this,"ungroup$",this._ungroup$.asObservable());K(this,"_refreshTransform$",new ge.Subject);K(this,"refreshTransform$",this._refreshTransform$.asObservable());K(this,"_visible$",new ge.Subject);K(this,"visible$",this._visible$.asObservable());K(this,"_focus$",new ge.Subject);K(this,"focus$",this._focus$.asObservable());K(this,"_featurePluginUpdate$",new ge.Subject);K(this,"featurePluginUpdate$",this._featurePluginUpdate$.asObservable());K(this,"_featurePluginAdd$",new ge.Subject);K(this,"featurePluginAdd$",this._featurePluginAdd$.asObservable());K(this,"_featurePluginRemove$",new ge.Subject);K(this,"featurePluginRemove$",this._featurePluginRemove$.asObservable());K(this,"_featurePluginOrderUpdate$",new ge.Subject);K(this,"featurePluginOrderUpdate$",this._featurePluginOrderUpdate$.asObservable());K(this,"_featurePluginGroupUpdate$",new ge.Subject);K(this,"featurePluginGroupUpdate$",this._featurePluginGroupUpdate$.asObservable());K(this,"_featurePluginUngroupUpdate$",new ge.Subject);K(this,"featurePluginUngroupUpdate$",this._featurePluginUngroupUpdate$.asObservable());K(this,"_visible",!0);K(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(r=>{const t=this._getCurrentBySearch(r);t!=null&&(t.transform=r.transform,t.transforms=r.transforms,t.isMultiTransform=r.isMultiTransform)}),this.refreshTransformNotification(e)}getDrawingDataForUnit(e){return this.drawingManagerData[e]||{}}removeDrawingDataForUnit(e){const r=this.drawingManagerData[e];if(r==null)return;delete this.drawingManagerData[e];const t=[];Object.keys(r).forEach(n=>{const u=r[n];(u==null?void 0:u.data)!=null&&Object.keys(u.data).forEach(o=>{t.push({unitId:e,subUnitId:n,drawingId:o})})}),t.length>0&&this.removeNotification(t)}registerDrawingData(e,r){this.drawingManagerData[e]=r}initializeNotification(e){const r=[],t=this.drawingManagerData[e];t!=null&&(Object.keys(t).forEach(n=>{this._establishDrawingMap(e,n);const u=t[n];Object.keys(u.data).forEach(o=>{const a=u.data[o];a.unitId=e,a.subUnitId=n,r.push(a)})}),r.length>0&&this.addNotification(r))}getDrawingData(e,r){return this._getDrawingData(e,r)}setDrawingData(e,r,t){this.drawingManagerData[e][r].data=t}getBatchAddOp(e){const r=[],t=[],n=[];e.forEach(W=>{const{op:_,invertOp:s}=this._addByParam(W);r.push({unitId:W.unitId,subUnitId:W.subUnitId,drawingId:W.drawingId}),t.push(_),n.push(s)});const u=t.reduce(F.type.compose,null),o=n.reduce(F.type.compose,null),{unitId:a,subUnitId:E}=e[0];return{undo:o,redo:u,unitId:a,subUnitId:E,objects:r}}getBatchRemoveOp(e){const r=[],t=[];e.forEach(E=>{const{op:W,invertOp:_}=this._removeByParam(E);r.unshift(W),t.push(_)});const n=r.reduce(F.type.compose,null),u=t.reduce(F.type.compose,null),{unitId:o,subUnitId:a}=e[0];return{undo:u,redo:n,unitId:o,subUnitId:a,objects:e}}getBatchUpdateOp(e){const r=[],t=[],n=[];e.forEach(W=>{const{op:_,invertOp:s}=this._updateByParam(W);r.push({unitId:W.unitId,subUnitId:W.subUnitId,drawingId:W.drawingId}),t.push(_),n.push(s)});const u=t.reduce(F.type.compose,null),o=n.reduce(F.type.compose,null),{unitId:a,subUnitId:E}=e[0];return{undo:o,redo:u,unitId:a,subUnitId:E,objects:r}}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 r=[],{unitId:t,subUnitId:n}=e[0].parent;e.forEach(a=>{r.push(this._getGroupDrawingOp(a))});const u=r.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(u,this.drawingManagerData),redo:u,unitId:t,subUnitId:n,objects:e}}getUngroupDrawingOp(e){const r=[],{unitId:t,subUnitId:n}=e[0].parent;e.forEach(a=>{r.push(this._getUngroupDrawingOp(a))});const u=r.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(u,this.drawingManagerData),redo:u,unitId:t,subUnitId:n,objects:e}}getDrawingsByGroup(e){const{unitId:r,subUnitId:t,drawingId:n}=e;if(this.getDrawingByParam({unitId:r,subUnitId:t,drawingId:n})==null)return[];const o=this._getDrawingData(r,t),a=[];return Object.keys(o).forEach(E=>{const W=o[E];W.groupId===n&&a.push(W)}),a}_getGroupDrawingOp(e){const{parent:r,children:t}=e,{unitId:n,subUnitId:u,drawingId:o}=r,a=[];a.push(F.insertOp([n,u,"data",o],r));let E=Number.NEGATIVE_INFINITY;return t.forEach(W=>{const{unitId:_,subUnitId:s,drawingId:c}=W,h=this._hasDrawingOrder({unitId:_,subUnitId:s,drawingId:c});E=Math.max(E,h),a.push(...this._getUpdateParamCompareOp(W,this.getDrawingByParam({unitId:_,subUnitId:s,drawingId:c})))}),E===Number.NEGATIVE_INFINITY&&(E=this._getDrawingOrder(n,u).length),a.push(F.insertOp([n,u,"order",E],o)),a.reduce(F.type.compose,null)}_getUngroupDrawingOp(e){const{parent:r,children:t}=e,{unitId:n,subUnitId:u,drawingId:o}=r,a=[];return t.forEach(E=>{const{unitId:W,subUnitId:_,drawingId:s}=E;a.push(...this._getUpdateParamCompareOp(E,this.getDrawingByParam({unitId:W,subUnitId:_,drawingId:s})))}),a.push(F.removeOp([n,u,"data",o],!0)),a.push(F.removeOp([n,u,"order",this._getDrawingOrder(n,u).indexOf(o)],!0)),a.reduce(F.type.compose,null)}applyJson1(e,r,t){this._establishDrawingMap(e,r),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[r,t,n]=e.split("#-#");return this._getCurrentBySearch({unitId:r,subUnitId:t,drawingId:n})}focusDrawing(e){if(e==null){this._focusDrawings=[],this._focus$.next([]);return}const r=[];e.forEach(t=>{var E;const{unitId:n,subUnitId:u,drawingId:o}=t,a=(E=this._getDrawingData(n,u))==null?void 0:E[o];a!=null&&r.push(a)}),r.length>0&&(this._focusDrawings=r,this._focus$.next(r))}getFocusDrawings(){const e=[];return this._focusDrawings.forEach(r=>{var a;const{unitId:t,subUnitId:n,drawingId:u}=r,o=(a=this._getDrawingData(t,n))==null?void 0:a[u];o!=null&&e.push(o)}),e}getDrawingOrder(e,r){return this._getDrawingOrder(e,r)}setDrawingOrder(e,r,t){this.drawingManagerData[e][r].order=t}orderUpdateNotification(e){this._order$.next(e)}getForwardDrawingsOp(e){const{unitId:r,subUnitId:t,drawingIds:n}=e,u=[],o=this.getDrawingOrder(r,t),a=[...n];n.forEach(_=>{const s=this._hasDrawingOrder({unitId:r,subUnitId:t,drawingId:_});if(s===-1||s===o.length-1)return;const c=F.moveOp([r,t,"order",s],[r,t,"order",s+1]);u.push(c),a.includes(o[s+1])||a.push(o[s+1])});const E=u.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(E,this.drawingManagerData),redo:E,unitId:r,subUnitId:t,objects:{...e,drawingIds:a}}}getBackwardDrawingOp(e){const{unitId:r,subUnitId:t,drawingIds:n}=e,u=[],o=this.getDrawingOrder(r,t),a=[...n];n.forEach(_=>{const s=this._hasDrawingOrder({unitId:r,subUnitId:t,drawingId:_});if(s===-1||s===0)return;const c=F.moveOp([r,t,"order",s],[r,t,"order",s-1]);u.push(c),a.includes(o[s-1])||a.push(o[s-1])});const E=u.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(E,this.drawingManagerData),redo:E,unitId:r,subUnitId:t,objects:{...e,drawingIds:a}}}getFrontDrawingsOp(e){const{unitId:r,subUnitId:t,drawingIds:n}=e,u=this._getOrderFromSearchParams(r,t,n),o=[...n],a=this.getDrawingOrder(r,t),E=[];u.forEach(s=>{const{drawingId:c}=s,h=this._getDrawingCount(r,t)-1,j=F.moveOp([r,t,"order",this._getDrawingOrder(r,t).indexOf(c)],[r,t,"order",h]);E.push(j),o.includes(a[h])||o.push(a[h])});const W=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(W,this.drawingManagerData),redo:W,unitId:r,subUnitId:t,objects:{...e,drawingIds:o}}}getBackDrawingsOp(e){const{unitId:r,subUnitId:t,drawingIds:n}=e,u=this._getOrderFromSearchParams(r,t,n,!0),o=[...n],a=this.getDrawingOrder(r,t),E=[];u.forEach(s=>{const{drawingId:c}=s,h=F.moveOp([r,t,"order",this._getDrawingOrder(r,t).indexOf(c)],[r,t,"order",0]);E.push(h),o.includes(a[0])||o.push(a[0])});const W=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(W,this.drawingManagerData),redo:W,unitId:r,subUnitId:t,objects:{...e,drawingIds:o}}}_getDrawingCount(e,r){return this.getDrawingOrder(e,r).length||0}_getOrderFromSearchParams(e,r,t,n=!1){return t.map(u=>{const o=this._hasDrawingOrder({unitId:e,subUnitId:r,drawingId:u});return{drawingId:u,zIndex:o}}).sort(n===!1?Y.sortRules:Y.sortRulesByDesc)}_hasDrawingOrder(e){if(e==null)return-1;const{unitId:r,subUnitId:t,drawingId:n}=e;return this._establishDrawingMap(r,t),this._getDrawingOrder(r,t).indexOf(n)}_getCurrentBySearch(e){var u,o,a;if(e==null)return;const{unitId:r,subUnitId:t,drawingId:n}=e;return(a=(o=(u=this.drawingManagerData[r])==null?void 0:u[t])==null?void 0:o.data)==null?void 0:a[n]}_getOldBySearch(e){var u,o,a;if(e==null)return;const{unitId:r,subUnitId:t,drawingId:n}=e;return(a=(o=(u=this._oldDrawingManagerData[r])==null?void 0:u[t])==null?void 0:o.data)==null?void 0:a[n]}_establishDrawingMap(e,r,t){var n;return this.drawingManagerData[e]||(this.drawingManagerData[e]={}),this.drawingManagerData[e][r]||(this.drawingManagerData[e][r]={data:{},order:[]}),t==null?null:(n=this.drawingManagerData[e][r].data)==null?void 0:n[t]}_addByParam(e){const{unitId:r,subUnitId:t,drawingId:n}=e;this._establishDrawingMap(r,t,n);const u=F.insertOp([r,t,"data",n],e),o=F.insertOp([r,t,"order",this._getDrawingOrder(r,t).length],n),a=[u,o].reduce(F.type.compose,null),E=F.type.invertWithDoc(a,this.drawingManagerData);return{op:a,invertOp:E}}_removeByParam(e){if(e==null)return{op:[],invertOp:[]};const{unitId:r,subUnitId:t,drawingId:n}=e;if(this._establishDrawingMap(r,t,n)==null)return{op:[],invertOp:[]};const o=F.removeOp([r,t,"data",n],!0),a=F.removeOp([r,t,"order",this._getDrawingOrder(r,t).indexOf(n)],!0),E=[o,a].reduce(F.type.compose,null),W=F.type.invertWithDoc(E,this.drawingManagerData);return{op:E,invertOp:W}}_updateByParam(e){const{unitId:r,subUnitId:t,drawingId:n}=e,u=this._establishDrawingMap(r,t,n);if(u==null)return{op:[],invertOp:[]};const a=this._getUpdateParamCompareOp(e,u).reduce(F.type.compose,null),E=F.type.invertWithDoc(a,this.drawingManagerData);return{op:a,invertOp:E}}_getUpdateParamCompareOp(e,r){const{unitId:t,subUnitId:n,drawingId:u}=e,o=[];return Object.keys(e).forEach(a=>{const E=e[a],W=r[a];W!==E&&o.push(F.replaceOp([t,n,"data",u,a],W,E))}),o}_getDrawingData(e,r){var t,n;return((n=(t=this.drawingManagerData[e])==null?void 0:t[r])==null?void 0:n.data)||{}}_getDrawingOrder(e,r){var t,n;return((n=(t=this.drawingManagerData[e])==null?void 0:t[r])==null?void 0:n.order)||[]}getDrawingVisible(){return this._visible}getDrawingEditable(){return this._editable}setDrawingVisible(e){this._visible=e}setDrawingEditable(e){this._editable=e}}class St extends Tt{}function qt({unitId:l,subUnitId:e,drawingId:r},t){return typeof t=="number"?`${l}#-#${e}#-#${r}#-#${t}`:`${l}#-#${e}#-#${r}`}const xt=async l=>new Promise((e,r)=>{const t=new Image;t.src=l,t.onload=()=>{e({width:t.width,height:t.height,image:t})},t.onerror=n=>{r(n)}}),Ht="drawing.config",Pt={},At=Y.createIdentifier("univer.drawing-manager.service");class Mt{constructor(){K(this,"_waitCount",0);K(this,"_change$",new ge.Subject);K(this,"change$",this._change$);K(this,"_imageSourceCache",new Map)}setWaitCount(e){this._waitCount=e,this._change$.next(e)}getImageSourceCache(e,r){if(r===Y.ImageSourceType.BASE64){const t=new Image;return t.src=e,t}return this._imageSourceCache.get(e)}addImageSourceCache(e,r,t){r===Y.ImageSourceType.BASE64||t==null||this._imageSourceCache.set(e,t)}async getImage(e){return Promise.resolve(e)}async saveImage(e){return new Promise((r,t)=>{if(!gt.includes(e.type)){t(new Error(Y.ImageUploadStatusType.ERROR_IMAGE_TYPE)),this._decreaseWaiting();return}if(e.size>5242880){t(new Error(Y.ImageUploadStatusType.ERROR_EXCEED_SIZE)),this._decreaseWaiting();return}const n=new FileReader;n.readAsDataURL(e),n.onload=u=>{var E;const o=(E=u.target)==null?void 0:E.result;if(o==null){t(new Error(Y.ImageUploadStatusType.ERROR_IMAGE)),this._decreaseWaiting();return}const a=Y.Tools.generateRandomId(6);r({imageId:a,imageSourceType:Y.ImageSourceType.BASE64,source:o,base64Cache:o,status:Y.ImageUploadStatusType.SUCCUSS}),this._decreaseWaiting()}})}_decreaseWaiting(){this._waitCount-=1,this._change$.next(this._waitCount)}}var Kt=Object.defineProperty,Ft=Object.getOwnPropertyDescriptor,Vt=(l,e,r,t)=>{for(var n=t>1?void 0:t?Ft(e,r):e,u=l.length-1,o;u>=0;u--)(o=l[u])&&(n=(t?o(e,r,n):o(n))||n);return t&&n&&Kt(e,r,n),n},Ut=(l,e)=>(r,t)=>e(r,t,l);const zt="UNIVER_DRAWING_PLUGIN";ne.UniverDrawingPlugin=(ct=class extends Y.Plugin{constructor(e=Pt,r,t){super(),this._config=e,this._injector=r,this._configService=t;const{...n}=Y.merge({},Pt,this._config);this._configService.setConfig(Ht,n)}onStarting(){this._initDependencies()}_initDependencies(){var t;const e=[[Y.IImageIoService,{useClass:Mt}],[At,{useClass:St}]];Y.mergeOverrideWithDependencies(e,(t=this._config)==null?void 0:t.override).forEach(n=>this._injector.add(n))}},K(ct,"pluginName",zt),ct),ne.UniverDrawingPlugin=Vt([Ut(1,Y.Inject(Y.Injector)),Ut(2,Y.IConfigService)],ne.UniverDrawingPlugin),Object.defineProperty(ne,"IImageIoService",{enumerable:!0,get:()=>Y.IImageIoService}),Object.defineProperty(ne,"ImageSourceType",{enumerable:!0,get:()=>Y.ImageSourceType}),Object.defineProperty(ne,"ImageUploadStatusType",{enumerable:!0,get:()=>Y.ImageUploadStatusType}),ne.DRAWING_IMAGE_ALLOW_IMAGE_LIST=gt,ne.DRAWING_IMAGE_ALLOW_SIZE=5242880,ne.DRAWING_IMAGE_COUNT_LIMIT=10,ne.DRAWING_IMAGE_HEIGHT_LIMIT=500,ne.DRAWING_IMAGE_WIDTH_LIMIT=500,ne.DrawingManagerService=St,ne.IDrawingManagerService=At,ne.ImageIoService=Mt,ne.UnitDrawingService=Tt,ne.getDrawingShapeKeyByDrawingSearch=qt,ne.getImageSize=xt,Object.defineProperty(ne,Symbol.toStringTag,{value:"Module"})});
1
+ (function(ne,Y){typeof exports=="object"&&typeof module<"u"?Y(exports,require("@univerjs/core"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","rxjs"],Y):(ne=typeof globalThis<"u"?globalThis:ne||self,Y(ne.UniverDrawing={},ne.UniverCore,ne.rxjs))})(this,function(ne,Y,ge){"use strict";var Zt=Object.defineProperty;var Qt=(ne,Y,ge)=>Y in ne?Zt(ne,Y,{enumerable:!0,configurable:!0,writable:!0,value:ge}):ne[Y]=ge;var K=(ne,Y,ge)=>Qt(ne,typeof Y!="symbol"?Y+"":Y,ge);var ct;const gt=["image/png","image/jpeg","image/jpg","image/gif","image/bmp"];var Ze={},et={},tt={},pt;function $t(){if(pt)return tt;pt=1,Object.defineProperty(tt,"__esModule",{value:!0});function l(t,n){if(Array.isArray(n))return!1;for(let u in t)if(!r(t[u],n[u]))return!1;for(let u in n)if(t[u]===void 0)return!1;return!0}function e(t,n){if(!Array.isArray(n)||t.length!==n.length)return!1;for(let u=0;u<t.length;u++)if(!r(t[u],n[u]))return!1;return!0}function r(t,n){return t===n?!0:t===null||n===null||typeof t!="object"||typeof n!="object"?!1:Array.isArray(t)?e(t,n):l(t,n)}return tt.default=r,tt}var nt={},yt;function Rt(){if(yt)return nt;yt=1,Object.defineProperty(nt,"__esModule",{value:!0});function l(e){if(e===null)return null;if(Array.isArray(e))return e.map(l);if(typeof e=="object"){const r={};for(let t in e)r[t]=l(e[t]);return r}else return e}return nt.default=l,nt}var st={},_t;function vt(){return _t||(_t=1,function(l){Object.defineProperty(l,"__esModule",{value:!0}),l.eachChildOf=l.advancer=l.readCursor=l.writeCursor=l.WriteCursor=l.ReadCursor=l.isValidPathItem=void 0;function e(_,s){if(!_)throw new Error(s)}const r=_=>_!=null&&typeof _=="object"&&!Array.isArray(_),t=(_,s)=>typeof _==typeof s?_>s:typeof _=="string"&&typeof s=="number";function n(_,s){for(let c in _){const h=c;s.write(h,_[h])}}l.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--,r(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-=r(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&&r(s=this.container[this.idx+1])?s:null}descendFirst(){let s=this.idx+1;if(!this.container||s>=this.container.length||r(this.container[s])&&s+1>=this.container.length)return!1;r(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)})}}l.ReadCursor=o;class a 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&&r(this.container[h])&&h++,e(h===this.container.length||!r(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(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 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&&r(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(!l.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=n){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 Z=0;for(let _e=0;_e<s.length;_e++){const Q=s[_e];typeof Q=="string"||typeof Q=="number"?(Z++,this.descend(Q)):Array.isArray(Q)?this.mergeTree(Q,c):typeof Q=="object"&&c(Q,this)}for(;Z--;)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}}l.WriteCursor=a,l.writeCursor=()=>new a,l.readCursor=_=>new o(_);function E(_,s,c){let h,j;j=h=_?_.descendFirst():!1;function Z(_e){let Q;for(;j;){const Ee=Q=_.getKey();if(_e!=null){let Ke=!1;if(s&&typeof Ee=="number"&&(Q=s(Ee,_.getComponent()),Q<0&&(Q=~Q,Ke=!0)),t(Q,_e))return null;if(Q===_e&&!Ke)return _}c&&typeof Q=="number"&&c(Q,_.getComponent()),j=_.nextSibling()}return null}return Z.end=()=>{h&&_.ascend()},Z}l.advancer=E;function W(_,s,c){let h,j,Z,_e;for(h=j=_&&_.descendFirst(),Z=_e=s&&s.descendFirst();h||Z;){let Q=h?_.getKey():null,Ee=Z?s.getKey():null;Q!==null&&Ee!==null&&(t(Ee,Q)?Ee=null:Q!==Ee&&(Q=null)),c(Q==null?Ee:Q,Q!=null?_:null,Ee!=null?s:null),Q!=null&&h&&(h=_.nextSibling()),Ee!=null&&Z&&(Z=s.nextSibling())}j&&_.ascend(),_e&&s.ascend()}l.eachChildOf=W}(st)),st}var ot={},mt;function Ot(){return mt||(mt=1,function(l){Object.defineProperty(l,"__esModule",{value:!0}),l.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"}(l.ConflictType||(l.ConflictType={}))}(ot)),ot}var xe={},He={},bt;function at(){return bt||(bt=1,Object.defineProperty(He,"__esModule",{value:!0}),He.uniToStrPos=He.strPosToUni=void 0,He.strPosToUni=(l,e=l.length)=>{let r=0,t=0;for(;t<e;t++){const n=l.charCodeAt(t);n>=55296&&n<=57343&&(r++,t++)}if(t!==e)throw Error("Invalid offset - splits unicode bytes");return t-r},He.uniToStrPos=(l,e)=>{let r=0;for(;e>0;e--){const t=l.charCodeAt(r);r+=t>=55296&&t<=57343?2:1}return r}),He}var lt={},wt;function ut(){return wt||(wt=1,function(l){Object.defineProperty(l,"__esModule",{value:!0}),l.uniSlice=l.dlen=l.eachOp=void 0;const e=at(),r=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(l.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 x=f[z];switch(y(x,w,q),typeof x){case"object":w+=l.dlen(x.d);break;case"string":q+=e.strPosToUni(x);break;case"number":w+=x,q+=x;break}}}l.eachOp=t;function n(f,y){const w=[],q=a(w);return t(f,(z,x,Te)=>{q(y(z,x,Te))}),s(w)}const u=f=>f,o=f=>n(f,u);l.dlen=f=>typeof f=="number"?f:e.strPosToUni(f);const a=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:l.dlen(w.d)+l.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);l.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 W=(f,y,w)=>typeof f=="number"?w==null?f-y:Math.min(f,w)-y:l.uniSlice(f,y,w),_=f=>{let y=0,w=0;return{take:(x,Te)=>{if(y===f.length)return x===-1?null:x;const fe=f[y];let ie;if(typeof fe=="number")return x===-1||fe-w<=x?(ie=fe-w,++y,w=0,ie):(w+=x,x);if(typeof fe=="string"){if(x===-1||Te==="i"||e.strPosToUni(fe.slice(w))<=x)return ie=fe.slice(w),++y,w=0,ie;{const ce=w+e.uniToStrPos(fe.slice(w),x);return ie=fe.slice(w,ce),w=ce,ie}}else{if(x===-1||Te==="d"||l.dlen(fe.d)-w<=x)return ie={d:W(fe.d,w)},++y,w=0,ie;{let ce=W(fe.d,w,w+x);return w+=x,{d:ce}}}},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'");r(f),r(y);const q=[],z=a(q),{take:x,peek:Te}=_(f);for(let ie=0;ie<y.length;ie++){const ce=y[ie];let ve,Se;switch(typeof ce){case"number":for(ve=ce;ve>0;)Se=x(ve,"i"),z(Se),typeof Se!="string"&&(ve-=E(Se));break;case"string":w==="left"&&typeof Te()=="string"&&z(x(-1)),z(e.strPosToUni(ce));break;case"object":for(ve=l.dlen(ce.d);ve>0;)switch(Se=x(ve,"i"),typeof Se){case"number":ve-=Se;break;case"string":z(Se);break;case"object":ve-=l.dlen(Se.d)}break}}let fe;for(;fe=x(-1);)z(fe);return s(q)}function h(f,y){r(f),r(y);const w=[],q=a(w),{take:z}=_(f);for(let Te=0;Te<y.length;Te++){const fe=y[Te];let ie,ce;switch(typeof fe){case"number":for(ie=fe;ie>0;)ce=z(ie,"d"),q(ce),typeof ce!="object"&&(ie-=E(ce));break;case"string":q(fe);break;case"object":ie=l.dlen(fe.d);let ve=0;for(;ve<ie;)switch(ce=z(ie-ve,"d"),typeof ce){case"number":q({d:W(fe.d,ve,ve+ce)}),ve+=ce;break;case"string":ve+=e.strPosToUni(ce);break;case"object":q(ce)}break}}let x;for(;x=z(-1);)q(x);return s(w)}const j=(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 x=e.strPosToUni(z);w+=x,f+=x;break;case"object":f-=Math.min(l.dlen(z.d),f-w);break}}return f},Z=(f,y)=>typeof f=="number"?j(f,y):f.map(w=>j(w,y));function _e(f,y,w){return n(f,(q,z)=>typeof q=="object"&&typeof q.d=="number"?{d:w.slice(y,z,z+q.d)}:q)}function Q(f){return n(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 Ee(f){return n(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 be(f){return{name:"text-unicode",uri:"http://sharejs.org/types/text-unicode",trim:s,normalize:o,checkOp:r,create(y=""){if(typeof y!="string")throw Error("Initial data must be a string");return f.create(y)},apply(y,w){r(w);const q=f.builder(y);for(let z=0;z<w.length;z++){const x=w[z];switch(typeof x){case"number":q.skip(x);break;case"string":q.append(x);break;case"object":q.del(l.dlen(x.d));break}}return q.build()},transform:c,compose:h,transformPosition:j,transformSelection:Z,isInvertible:Ke,makeInvertible(y,w){return _e(y,w,f)},stripInvertible:Ee,invert:Q,invertWithDoc(y,w){return Q(_e(y,w,f))},isNoop:y=>y.length===0}}l.default=be}(lt)),lt}var rt={},It;function Wt(){if(It)return rt;It=1,Object.defineProperty(rt,"__esModule",{value:!0});const l=ut(),e=at();function r(t,n){return{get:t,getLength(){return t().length},insert(u,o,a){const E=e.strPosToUni(t(),u);return n([E,o],a)},remove(u,o,a){const E=e.strPosToUni(t(),u);return n([E,{d:o}],a)},_onOp(u){l.eachOp(u,(o,a,E)=>{switch(typeof o){case"string":this.onInsert&&this.onInsert(E,o);break;case"object":const W=l.dlen(o.d);this.onRemove&&this.onRemove(E,W)}})},onInsert:null,onRemove:null}}return rt.default=r,r.provides={text:!0},rt}var Ct;function Gt(){return Ct||(Ct=1,function(l){var e=xe.__createBinding||(Object.create?function(c,h,j,Z){Z===void 0&&(Z=j),Object.defineProperty(c,Z,{enumerable:!0,get:function(){return h[j]}})}:function(c,h,j,Z){Z===void 0&&(Z=j),c[Z]=h[j]}),r=xe.__setModuleDefault||(Object.create?function(c,h){Object.defineProperty(c,"default",{enumerable:!0,value:h})}:function(c,h){c.default=h}),t=xe.__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 r(h,c),h},n=xe.__importDefault||function(c){return c&&c.__esModule?c:{default:c}};Object.defineProperty(l,"__esModule",{value:!0}),l.type=l.remove=l.insert=void 0;const u=at(),o=t(ut()),a=n(Wt()),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(j){let Z=u.uniToStrPos(c,j);if(Z>c.length)throw Error("The op is too long for this document");h.push(c.slice(0,Z)),c=c.slice(Z)},append(j){h.push(j)},del(j){c=c.slice(u.uniToStrPos(c,j))},build(){return h.join("")+c}}},slice:o.uniSlice},W=o.default(E),_=Object.assign(Object.assign({},W),{api:a.default});l.type=_,l.insert=(c,h)=>h.length===0?[]:c===0?[h]:[c,h],l.remove=(c,h)=>o.dlen(h)===0?[]:c===0?[{d:h}]:[c,{d:h}];var s=ut();Object.defineProperty(l,"makeType",{enumerable:!0,get:function(){return s.default}})}(xe)),xe}var Et;function Lt(){return Et||(Et=1,function(l){var e=et.__importDefault||function(i){return i&&i.__esModule?i:{default:i}};Object.defineProperty(l,"__esModule",{value:!0}),l.editOp=l.replaceOp=l.insertOp=l.moveOp=l.removeOp=l.type=void 0;const r=e($t()),t=e(Rt()),n=vt(),u=Ot();function o(i,d){if(!i)throw new Error(d)}l.type={name:"json1",uri:"http://sharejs.org/types/JSONv1",readCursor:n.readCursor,writeCursor:n.writeCursor,create:i=>i,isNoop:i=>i==null,setDebug(i){},registerSubtype:Q,checkValidOp:z,normalize:x,apply:Te,transformPosition:fe,compose:ie,tryTransform:ft,transform:Jt,makeInvertible:Se,invert:ce,invertWithDoc:Xt,RM_UNEXPECTED_CONTENT:u.ConflictType.RM_UNEXPECTED_CONTENT,DROP_COLLISION:u.ConflictType.DROP_COLLISION,BLACKHOLE:u.ConflictType.BLACKHOLE,transformNoConflict:(i,d,m)=>kt(()=>!0,i,d,m),typeAllowingConflictsPred:i=>Object.assign(Object.assign({},l.type),{transform:(d,m,D)=>kt(i,d,m,D)})};const a=i=>i?i.getComponent():null;function E(i){return i&&typeof i=="object"&&!Array.isArray(i)}const W=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}l.removeOp=(i,d=!0)=>n.writeCursor().writeAtPath(i,"r",d).get(),l.moveOp=(i,d)=>n.writeCursor().writeMove(i,d).get(),l.insertOp=(i,d)=>n.writeCursor().writeAtPath(i,"i",d).get(),l.replaceOp=(i,d,m)=>n.writeCursor().at(i,D=>{D.write("r",d),D.write("i",m)}).get(),l.editOp=(i,d,m,D=!1)=>n.writeCursor().at(i,O=>y(O,d,m,D)).get();const j=(i,d)=>i!=null&&(typeof d=="number"?Array.isArray(i):typeof i=="object"),Z=(i,d)=>j(i,d)?i[d]:void 0,_e={};function Q(i){let d=i.type?i.type:i;d.name&&(_e[d.name]=d),d.uri&&(_e[d.uri]=d)}const Ee=i=>{const d=_e[i];if(d)return d;throw Error("Missing type: "+i)};Q(Gt());const Ke=(i,d)=>i+d;Q({name:"number",apply:Ke,compose:Ke,invert:i=>-i,transform:i=>i});const be=i=>i==null?null:i.et?Ee(i.et):i.es?_e["text-unicode"]:i.ena!=null?_e.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"?[Ee(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 R=!0,N=!1;for(let g in C){const v=C[g];if(R=!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=be(C);o(b,"Missing type in edit"),b.checkValidOp&&b.checkValidOp(f(C))}}o(!R)},O=(C,R,N)=>{if(!Array.isArray(C))throw Error("Op must be null or a list");if(C.length===0)throw Error("Empty descent");R||q(C[0]);let g=1,v=0,b=0;for(let I=0;I<C.length;I++){const $=C[I];if(o($!=null),Array.isArray($)){const G=O($,!1);if(v){const p=typeof b,M=typeof G;p===M?o(b<G,"descent keys are not in order"):o(p==="number"&&M==="string")}b=G,v++,g=3}else typeof $=="object"?(o(g===1,`Prev not scalar - instead ${g}`),D($),g=2):(o(g!==3),q($),o(n.isValidPathItem($),"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 x(i){let d=0,m=[];const D=n.writeCursor();return D.mergeTree(i,(O,C)=>{const R=be(O);if(R){const g=f(O);y(C,R,R.normalize?R.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 Te(i,d){if(z(d),d===null)return i;const m=[];return function D(O,C){let R=O,N=0,g={root:O},v=0,b=g,I="root";function $(){for(;v<N;v++){let G=C[v];typeof G!="object"&&(o(j(b,I)),b=b[I]=W(b[I]),I=G)}}for(;N<C.length;N++){const G=C[N];if(Array.isArray(G)){const p=D(R,G);p!==R&&p!==void 0&&($(),R=b[I]=p)}else if(typeof G=="object"){G.d!=null?($(),R=h(b,I,m[G.d])):G.i!==void 0&&($(),R=h(b,I,G.i));const p=be(G);if(p)$(),R=b[I]=p.apply(R,f(G));else if(G.e!==void 0)throw Error("Subtype "+G.et+" undefined")}else R=Z(R,G)}return g.root}(i=function D(O,C){const R=[];let N=0;for(;N<C.length;N++){const I=C[N];if(Array.isArray(I))break;typeof I!="object"&&(R.push(O),O=Z(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.pop();O=O===Z($,I)?$:O===void 0?c($,I):(v=I,b=O,(g=W(g=$))[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 fe(i,d){i=i.slice(),z(d);const m=n.readCursor(d);let D,O,C=!1;const R=[];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 $=n.advancer(m,void 0,(p,M)=>{_(M)&&I++});R.unshift($);const G=$(v);if(typeof v=="number"&&(i[g]-=I),!G)break}if(R.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=a(m),I=be(b);if(I){const p=f(b);I.transformPosition&&(i[g]=I.transformPosition(i[g],p));break}let $=0;const G=n.advancer(m,(p,M)=>s(M)?~(p-$):p-$,(p,M)=>{s(M)&&$++})(v);if(typeof v=="number"&&(i[g]+=$),!G)break}};return D!=null?m.eachDrop(null,g=>{g===D&&N()}):N(),i}function ie(i,d){if(z(i),z(d),i==null)return d;if(d==null)return i;let m=0;const D=n.readCursor(i),O=n.readCursor(d),C=n.writeCursor(),R=[],N=[],g=[],v=[],b=[],I=[],$=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 G=n.writeCursor();return function p(M,se,re,H,ae,Re,Ie,me){o(se||re);const le=a(se),Pe=a(re),Me=!!Pe&&Pe.r!==void 0,Fe=!!le&&le.i!==void 0,Ae=le?le.d:null,De=Pe?Pe.p:null,We=(Re||Me)&&De==null;if(De!=null)H=v[De],Ie=N[De]=new n.WriteCursor;else if(Pe&&Pe.r!==void 0)H=null;else{const S=a(H);S&&S.d!=null&&(H=null)}const ee=a(H);if(Ae!=null)if(M=g[Ae],me=R[Ae]=new n.WriteCursor,We)Re&&!Me&&me.write("r",!0);else{const S=b[Ae]=m++;Ie.write("d",S)}else if(le&&le.i!==void 0)M=null;else{const S=a(M);S&&S.p!=null&&(M=null)}let P;Fe?(o(ae===void 0),P=le.i):P=ae;const L=(De==null?!Fe||Re||Me:P===void 0)?null:Ie.getComponent();if(De!=null){if(!(ae!==void 0||Fe)){const S=Ae!=null?b[Ae]:m++;I[De]=S,me.write("p",S)}}else Me&&(Fe||ae!==void 0||(Pe.r,me.write("r",Pe.r)));const A=We?null:be(le),T=be(ee);if((A||T)&&(A&&A.name,T&&T.name),A&&T){o(A===T);const S=f(le),B=f(ee),ye=A.compose(S,B);y(Ie,A,ye),$.add(ee)}else A?y(Ie,A,f(le)):T&&(y(Ie,T,f(ee)),$.add(ee));const U=typeof P=="object"&&P!=null;let J=!1,X=0,te=0,pe=0,he=0,ue=0;const we=n.advancer(H,(S,B)=>s(B)?he-S-1:S-he,(S,B)=>{s(B)&&he++}),V=n.advancer(M,(S,B)=>_(B)?X-S-1:S-X,(S,B)=>{_(B)&&X++});if(n.eachChildOf(se,re,(S,B,ye)=>{let Ce,Ge,Ve=S,Ue=S,Qe=S;if(typeof S=="number"){let Oe=S+pe;Ge=we(Oe),Ue=Oe+he;let de=S+te;Ce=V(de),s(a(Ge))&&(Ce=null),Ve=de+X,Qe=S+ue,o(Ve>=0,"p1PickKey is negative"),o(Ue>=0,"p2DropKey is negative");const je=s(a(B)),Le=_(a(ye));(je||Le&&!We)&&ue--,je&&te--,Le&&pe--}else Ce=V(S),Ge=we(S);me.descend(Ve),Ie.descend(Ue);const Je=U&&!s(a(B))?P[Qe]:void 0,Ne=p(Ce,B,ye,Ge,Je,We,Ie,me);var $e,k,oe;U&&!We?Je!==Ne&&(J||(P=Array.isArray(P)?P.slice():Object.assign({},P),J=!0),$e=P,oe=Ne,typeof(k=Qe)=="number"?(o(Array.isArray($e)),o(k<$e.length)):(o(!Array.isArray($e)),o($e[k]!==void 0)),oe===void 0?typeof k=="number"?$e.splice(k,1):delete $e[k]:$e[k]=oe):o(Ne===void 0),Ie.ascend(),me.ascend()}),V.end(),we.end(),L!=null)L.i=P;else if(!Re&&!Me&&De==null)return P}(D,D.clone(),O,O.clone(),void 0,!1,C,G),C.reset(),C.mergeTree(G.get()),C.reset(),C.get(),R.map(p=>p.get()),N.map(p=>p.get()),D.traverse(C,(p,M)=>{const se=p.p;if(se!=null){const re=b[se];re!=null&&M.write("p",re);const H=R[se];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 se=p.d;if(se!=null){const H=I[se];H!=null&&M.write("d",H);const ae=N[se];ae&&M.mergeTree(ae.get())}else p.i!==void 0&&M.write("i",p.i);const re=be(p);re&&!$.has(p)&&y(M,re,f(p))}),C.get()}function ce(i){if(i==null)return null;const d=new n.ReadCursor(i),m=new n.WriteCursor;let D;const O=[],C=[];return function R(N,g,v){const b=N.getComponent();let I,$=!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=be(b);p&&(v===void 0?(D||(D=new Set),D.add(b)):(f(b),v=p.apply(v,f(b)),$=!0))}let G=0;for(const p of N){g.descend(p);const M=typeof p=="number"?p-G:p,se=Z(v,M);s(N.getComponent())&&G++;const re=R(N,g,se);if(v!==void 0&&re!==void 0){if($||($=!0,v=W(v)),!j(v,M))throw Error("Cannot modify child - invalid operation");v[M]=re}g.ascend()}if(I===void 0)return $?v:void 0;g.write("r",v)}(d,m,void 0),D&&(m.reset(),function R(N,g,v){const b=g.getComponent();if(b){const p=b.d;if(p!=null&&(N=O[p],v=C[p]=n.writeCursor()),D.has(b)){const M=be(b);if(!M.invert)throw Error(`Cannot invert subtype ${M.name}`);y(v,M,M.invert(f(b)))}}let I=0,$=0;const G=n.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-$,se=G(M),re=M+I;v.descend(re),R(se,g,v),s(g.getComponent())&&$++,v.ascend()}else v.descend(p),R(G(p),g,v),v.ascend();G.end()}(d.clone(),d,m),C.length&&(m.reset(),d.traverse(m,(R,N)=>{const g=R.p;if(g!=null){const v=C[g];v&&v.get(),v&&N.mergeTree(v.get())}}))),m.get()}const ve=(i,d)=>i.some(m=>typeof m=="object"&&(Array.isArray(m)?ve(m,d):d(m)));function Se(i,d){if(i==null||!ve(i,g=>{var v;return g.r!==void 0||((v=be(g))===null||v===void 0?void 0:v.makeInvertible)!=null}))return i;const m=new n.ReadCursor(i),D=new n.WriteCursor;let O=!1;const C=[],R=[],N=(g,v,b)=>{const I=g.getComponent();let $=!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"),R[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=be(I);M&&(M.makeInvertible?O=!0:y(v,M,f(I),!0))}let G=0;for(const p of g){v.descend(p);const M=typeof p=="number"?p-G:p,se=Z(b,M),re=N(g,v,se);se!==re&&($||($=!0,b=W(b)),re===void 0?(b=c(b,M),typeof p=="number"&&G++):b[M]=re),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,$,G){const p=b.getComponent();if(p){p.i!==void 0?($=p.i,G=!0):p.d!=null&&($=R[p.d],v=C[p.d],G=!1,p.d);let H=be(p);if(H&&H.makeInvertible){const ae=f(p);y(I,H,H.makeInvertible(ae,$),!0)}}let M=0,se=0;const re=n.advancer(v,(H,ae)=>_(ae)?M-H-1:H-M,(H,ae)=>{_(ae)&&M++});for(const H of b)if(typeof H=="number"){const ae=H-se,Re=re(ae),Ie=ae+M,me=Z($,G?ae:Ie);I.descend(H),g(Re,b,I,me,G),s(b.getComponent())&&se++,I.ascend()}else{const ae=Z($,H);I.descend(H),g(re(H),b,I,ae,G),I.ascend()}re.end()}(m.clone(),m,D,d,!1)),D.get()}function Xt(i,d){return ce(Se(i,d))}const dt=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]=dt(D))}return d};function ft(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=[],R=[],N=[],g=[],v=[],b=[],I=[],$=[],G=[],p=[],M=[],se=[],re=[],H=[],ae=[];let Re=0;const Ie=n.readCursor(i),me=n.readCursor(d),le=n.writeCursor();if(function ee(P,L=null,A){const T=a(L);T&&(T.r!==void 0?A=L.clone():T.p!=null&&(A=null,b[T.p]=P.clone()));const U=P.getComponent();let J;U&&(J=U.p)!=null&&(v[J]=L?L.clone():null,N[J]=P.clone(),A&&(p[J]=!0,G[J]=A),T&&T.p!=null&&(H[J]=T.p));const X=n.advancer(L);for(const te of P)ee(P,X(te),A);X.end()}(me,Ie,null),function ee(P,L,A,T,U){const J=A.getComponent();let X,te=!1;J&&((X=J.d)!=null?(g[X]=A.clone(),T!=null&&(ae[T]==null&&(ae[T]=[]),ae[T].push(X)),p[X],P=v[X]||null,L=N[X]||null,p[X]?(U&&(M[X]=!0),U=G[X]||null):!U||D!==1&&H[X]!=null||O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:l.removeOp(U.getPath()),op2:l.moveOp(L.getPath(),A.getPath())}),te=!0):J.i!==void 0&&(P=L=null,te=!0,U&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:l.removeOp(U.getPath()),op2:l.insertOp(A.getPath(),J.i)})));const pe=a(P);pe&&(pe.r!==void 0?U=P.clone():pe.p!=null&&(pe.p,T=pe.p,U=null));const he=be(J);he&&U&&O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:l.removeOp(U.getPath()),op2:l.editOp(A.getPath(),he,f(J),!0)});let ue=0,we=0;const V=n.advancer(L,(B,ye)=>_(ye)?ue-B-1:B-ue,(B,ye)=>{_(ye)&&ue++}),S=n.advancer(P);for(const B of A)if(typeof B=="number"){const ye=B-we,Ce=V(ye);we+=+ee(S(ye+ue),Ce,A,T,U)}else{const ye=V(B);ee(S(B),ye,A,T,U)}return V.end(),S.end(),te}(Ie,me,me.clone(),null,null),g.map(ee=>ee&&ee.get()),O)return{ok:!1,conflict:O};M.map(ee=>!!ee);const Pe=[];let Me=null;(function ee(P,L,A,T,U){let J=!1;const X=a(L);if(_(X)){const V=X.p;V!=null?(A=g[V],T=se[V]=n.writeCursor(),J=!0,U=null):(A=null,U=L.clone())}else s(a(A))&&(A=null);const te=P.getComponent();if(te){const V=te.p;V!=null?(U&&($[V]=U),Pe[V]=U||D===1&&J?null:T.getComponent(),C[V]=P.clone(),A&&(I[V]=A.clone())):te.r!==void 0&&(U||T.write("r",!0),(U||J)&&(Me==null&&(Me=new Set),Me.add(te)))}let pe=0,he=0;const ue=n.advancer(L,void 0,(V,S)=>{_(S)&&pe++}),we=n.advancer(A,(V,S)=>s(S)?~(V-he):V-he,(V,S)=>{s(S)&&he++});if(P)for(const V of P)if(typeof V=="string"){const S=ue(V),B=we(V);T.descend(V),ee(P,S,B,T,U),T.ascend()}else{const S=ue(V),B=V-pe,ye=_(a(S))?null:we(B),Ce=B+he;o(Ce>=0),T.descend(Ce),ee(P,S,ye,T,U),T.ascend()}ue.end(),we.end()})(Ie,me,me.clone(),le,null),le.reset();let Fe=[];if(function ee(P,L,A,T,U,J){o(L);const X=L.getComponent();let te=a(T),pe=!1;const he=(k,oe,Oe)=>k?l.moveOp(k.getPath(),oe.getPath()):l.insertOp(oe.getPath(),Oe.i);if(s(X)){const k=X.d;k!=null&&(R[k]=L.clone());const oe=k!=null?Pe[k]:null;let Oe=!1;if(X.i!==void 0||k!=null&&oe){let de;te&&(te.i!==void 0||(de=te.d)!=null&&!p[de])&&(Oe=de!=null?k!=null&&k===H[de]:r.default(te.i,X.i),Oe||de!=null&&D!==1&&H[de]!=null||O==null&&(O={type:u.ConflictType.DROP_COLLISION,op1:he(k!=null?C[k]:null,L,X),op2:he(de!=null?N[de]:null,T,te)})),Oe||(J?O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:he(k!=null?C[k]:null,L,X),op2:l.removeOp(J.getPath())}):(k!=null?(Fe[Re]=k,U.write("d",oe.p=Re++)):U.write("i",t.default(X.i)),pe=!0))}else if(k!=null&&!oe){const de=$[k];de&&(J=de.clone())}k!=null?(P=C[k],A=b[k],T=I[k]):X.i!==void 0&&(P=A=null,Oe||(T=null))}else _(a(P))&&(P=A=T=null);const ue=a(P),we=a(A);if(_(we)){const k=we.p;we.r!==void 0&&(!ue||ue.r===void 0)||p[k]?(T=null,J=A.clone()):k!=null&&(T=g[k],D!==1&&H[k]!=null||((U=re[k])||(U=re[k]=n.writeCursor()),U.reset(),J=null))}else!s(X)&&s(te)&&(T=null);te=T!=null?T.getComponent():null;const V=be(X);if(V){const k=f(X);if(J)O==null&&(O={type:u.ConflictType.RM_UNEXPECTED_CONTENT,op1:l.editOp(L.getPath(),V,k,!0),op2:l.removeOp(J.getPath())});else{const oe=be(te);let Oe;if(oe){if(V!==oe)throw Error("Transforming incompatible types");const de=f(te);Oe=V.transform(k,de,m)}else Oe=t.default(k);y(U,V,Oe)}}let S=0,B=0,ye=0,Ce=0,Ge=0,Ve=0,Ue=P!=null&&P.descendFirst(),Qe=Ue;const Je=n.advancer(A,void 0,(k,oe)=>{_(oe)&&ye++});let Ne=T!=null&&T.descendFirst(),$e=Ne;for(const k of L)if(typeof k=="number"){let oe;const Oe=s(L.getComponent()),de=k-B;{let Be;for(;Ue&&typeof(Be=P.getKey())=="number";){Be+=S;const ke=P.getComponent(),Ye=_(ke);if(Be>de||Be===de&&(!Ye||D===0&&Oe))break;if(Ye){S--;const ze=ke.p;H.includes(ze),ke.d,a(re[ke.d]),_(a(re[ke.d])),(ke.r===void 0||Me&&Me.has(ke))&&(ze==null||!Pe[ze]||D!==1&&H.includes(ze))||Ge--}Ue=P.nextSibling()}oe=Ue&&Be===de?P:null}const je=de-S;let Le=Je(je);const ht=je-ye;let it=null;{let Be,ke;for(;Ne&&typeof(Be=T.getKey())=="number";){ke=Be-Ce;const Ye=T.getComponent(),ze=s(Ye);if(ke>ht)break;if(ke===ht){if(!ze){it=T;break}{if(D===0&&Oe){it=T;break}const qe=Le&&_(Le.getComponent());if(D===0&&qe)break}}if(ze){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)&&(Ce++,Ve--):Ce++}Ne=T.nextSibling()}}const Nt=ht+Ce+Ge+Ve;o(Nt>=0,"trying to descend to a negative index"),U.descend(Nt),Oe&&(oe=Le=it=null,B++),ee(oe,L,Le,it,U,J)&&Ve++,U.ascend()}else{let oe;for(;Ue&&(oe=P.getKey(),typeof oe!="string"||!(oe>k||oe===k));)Ue=P.nextSibling();const Oe=Ue&&oe===k?P:null,de=Je(k);let je;for(;Ne&&(je=T.getKey(),typeof je!="string"||!(je>k||je===k));)Ne=T.nextSibling();const Le=Ne&&je===k?T:null;U.descend(k),ee(Oe,L,de,Le,U,J),U.ascend()}return Je.end(),Qe&&P.ascend(),$e&&T.ascend(),pe}(Ie,Ie.clone(),me,me.clone(),le,null),O)return{ok:!1,conflict:O};le.reset();const Ae=(ee,P,L)=>ee.traverse(P,(A,T)=>{A.d!=null&&L(A.d,ee,T)});(p.length||se.length)&&(Ae(me,le,(ee,P,L)=>{p[ee]&&!M[ee]&&L.write("r",!0),se[ee]&&L.mergeTree(se[ee].get())}),le.reset());const De=[],We=[];if((re.length||p.length)&&!O){const ee=n.readCursor(dt(le.get()));if(Ae(ee,null,(P,L)=>{De[P]=L.clone()}),re.forEach(P=>{P&&Ae(n.readCursor(P.get()),null,(L,A)=>{De[L]=A.clone()})}),function P(L,A,T,U,J,X){const te=a(A);if(te&&_(te))if(te.p!=null){const S=te.p;De[S].getPath(),T=De[S],U=We[S]=n.writeCursor()}else te.r!==void 0&&(T=null);else s(a(T))&&(T=null);const pe=L.getComponent();if(pe){let S;if((S=pe.d)!=null){const B=re[S];B&&(B.get(),U.mergeTree(B.get()),T=n.readCursor(B.get()))}}let he=0,ue=0;const we=n.advancer(A,void 0,(S,B)=>{_(B)&&he--}),V=n.advancer(T,(S,B)=>s(B)?-(S-ue)-1:S-ue,(S,B)=>{s(B)&&ue++});for(const S of L)if(typeof S=="number"){const B=we(S),ye=S+he,Ce=V(ye),Ge=ye+ue;U.descend(Ge),P(L,B,Ce,U),U.ascend()}else U.descend(S),P(L,we(S),V(S),U),U.ascend();we.end(),V.end()}(me,ee,ee.clone(),le),le.reset(),O)return{ok:!1,conflict:O};if(le.get(),We.length){const P=We.map(A=>A?A.get():null),L=n.readCursor(dt(le.get()));if(Ae(L,le,(A,T,U)=>{const J=P[A];J&&(U.mergeTree(J),P[A]=null)}),P.find(A=>A)){const A=n.writeCursor(),T=n.writeCursor();let U=0,J=0;P.forEach(X=>{X!=null&&Ae(n.readCursor(X),null,te=>{const pe=Fe[te];A.writeMove(C[pe].getPath(),R[pe].getPath(),U++);const he=ae[pe];he&&he.forEach(ue=>{p[ue]||D!==1&&H[ue]!=null||T.writeMove(N[ue].getPath(),g[ue].getPath(),J++)})})}),O={type:u.ConflictType.BLACKHOLE,op1:A.get(),op2:T.get()}}}}return O?{ok:!1,conflict:O}:{ok:!0,result:le.get()}}const jt=i=>{const d=new Error("Transform detected write conflict");throw d.conflict=i,d.type=d.name="writeConflict",d};function Jt(i,d,m){const D=ft(i,d,m);if(D.ok)return D.result;jt(D.conflict)}const Xe=i=>{const d=n.writeCursor();return n.readCursor(i).traverse(d,(m,D)=>{(s(m)||be(m))&&D.write("r",!0)}),d.get()},Yt=(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 n.readCursor(D).traverse(null,R=>{R.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 kt(i,d,m,D){let O=null;for(;;){const C=ft(d,m,D);if(C.ok)return ie(O,C.result);{const{conflict:R}=C;i(R)||jt(R);const[N,g]=Yt(R,D);d=ie(x(d),N),m=ie(x(m),g),O=ie(O,g)}}}}(et)),et}var Dt;function Bt(){return Dt||(Dt=1,function(l){var e=Ze.__createBinding||(Object.create?function(u,o,a,E){E===void 0&&(E=a),Object.defineProperty(u,E,{enumerable:!0,get:function(){return o[a]}})}:function(u,o,a,E){E===void 0&&(E=a),u[E]=o[a]}),r=Ze.__exportStar||function(u,o){for(var a in u)a!=="default"&&!o.hasOwnProperty(a)&&e(o,u,a)};Object.defineProperty(l,"__esModule",{value:!0}),r(Lt(),l);var t=vt();Object.defineProperty(l,"ReadCursor",{enumerable:!0,get:function(){return t.ReadCursor}}),Object.defineProperty(l,"WriteCursor",{enumerable:!0,get:function(){return t.WriteCursor}});var n=Ot();Object.defineProperty(l,"ConflictType",{enumerable:!0,get:function(){return n.ConflictType}})}(Ze)),Ze}var F=Bt();class Tt{constructor(){K(this,"drawingManagerData",{});K(this,"_oldDrawingManagerData",{});K(this,"_focusDrawings",[]);K(this,"_remove$",new ge.Subject);K(this,"remove$",this._remove$.asObservable());K(this,"_add$",new ge.Subject);K(this,"add$",this._add$.asObservable());K(this,"_update$",new ge.Subject);K(this,"update$",this._update$.asObservable());K(this,"_order$",new ge.Subject);K(this,"order$",this._order$.asObservable());K(this,"_group$",new ge.Subject);K(this,"group$",this._group$.asObservable());K(this,"_ungroup$",new ge.Subject);K(this,"ungroup$",this._ungroup$.asObservable());K(this,"_refreshTransform$",new ge.Subject);K(this,"refreshTransform$",this._refreshTransform$.asObservable());K(this,"_visible$",new ge.Subject);K(this,"visible$",this._visible$.asObservable());K(this,"_focus$",new ge.Subject);K(this,"focus$",this._focus$.asObservable());K(this,"_featurePluginUpdate$",new ge.Subject);K(this,"featurePluginUpdate$",this._featurePluginUpdate$.asObservable());K(this,"_featurePluginAdd$",new ge.Subject);K(this,"featurePluginAdd$",this._featurePluginAdd$.asObservable());K(this,"_featurePluginRemove$",new ge.Subject);K(this,"featurePluginRemove$",this._featurePluginRemove$.asObservable());K(this,"_featurePluginOrderUpdate$",new ge.Subject);K(this,"featurePluginOrderUpdate$",this._featurePluginOrderUpdate$.asObservable());K(this,"_featurePluginGroupUpdate$",new ge.Subject);K(this,"featurePluginGroupUpdate$",this._featurePluginGroupUpdate$.asObservable());K(this,"_featurePluginUngroupUpdate$",new ge.Subject);K(this,"featurePluginUngroupUpdate$",this._featurePluginUngroupUpdate$.asObservable());K(this,"_visible",!0);K(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(r=>{const t=this._getCurrentBySearch(r);t!=null&&(t.transform=r.transform,t.transforms=r.transforms,t.isMultiTransform=r.isMultiTransform)}),this.refreshTransformNotification(e)}getDrawingDataForUnit(e){return this.drawingManagerData[e]||{}}removeDrawingDataForUnit(e){const r=this.drawingManagerData[e];if(r==null)return;delete this.drawingManagerData[e];const t=[];Object.keys(r).forEach(n=>{const u=r[n];(u==null?void 0:u.data)!=null&&Object.keys(u.data).forEach(o=>{t.push({unitId:e,subUnitId:n,drawingId:o})})}),t.length>0&&this.removeNotification(t)}registerDrawingData(e,r){this.drawingManagerData[e]=r}initializeNotification(e){const r=[],t=this.drawingManagerData[e];t!=null&&(Object.keys(t).forEach(n=>{this._establishDrawingMap(e,n);const u=t[n];Object.keys(u.data).forEach(o=>{const a=u.data[o];a.unitId=e,a.subUnitId=n,r.push(a)})}),r.length>0&&this.addNotification(r))}getDrawingData(e,r){return this._getDrawingData(e,r)}setDrawingData(e,r,t){this.drawingManagerData[e][r].data=t}getBatchAddOp(e){const r=[],t=[],n=[];e.forEach(W=>{const{op:_,invertOp:s}=this._addByParam(W);r.push({unitId:W.unitId,subUnitId:W.subUnitId,drawingId:W.drawingId}),t.push(_),n.push(s)});const u=t.reduce(F.type.compose,null),o=n.reduce(F.type.compose,null),{unitId:a,subUnitId:E}=e[0];return{undo:o,redo:u,unitId:a,subUnitId:E,objects:r}}getBatchRemoveOp(e){const r=[],t=[];e.forEach(E=>{const{op:W,invertOp:_}=this._removeByParam(E);r.unshift(W),t.push(_)});const n=r.reduce(F.type.compose,null),u=t.reduce(F.type.compose,null),{unitId:o,subUnitId:a}=e[0];return{undo:u,redo:n,unitId:o,subUnitId:a,objects:e}}getBatchUpdateOp(e){const r=[],t=[],n=[];e.forEach(W=>{const{op:_,invertOp:s}=this._updateByParam(W);r.push({unitId:W.unitId,subUnitId:W.subUnitId,drawingId:W.drawingId}),t.push(_),n.push(s)});const u=t.reduce(F.type.compose,null),o=n.reduce(F.type.compose,null),{unitId:a,subUnitId:E}=e[0];return{undo:o,redo:u,unitId:a,subUnitId:E,objects:r}}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 r=[],{unitId:t,subUnitId:n}=e[0].parent;e.forEach(a=>{r.push(this._getGroupDrawingOp(a))});const u=r.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(u,this.drawingManagerData),redo:u,unitId:t,subUnitId:n,objects:e}}getUngroupDrawingOp(e){const r=[],{unitId:t,subUnitId:n}=e[0].parent;e.forEach(a=>{r.push(this._getUngroupDrawingOp(a))});const u=r.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(u,this.drawingManagerData),redo:u,unitId:t,subUnitId:n,objects:e}}getDrawingsByGroup(e){const{unitId:r,subUnitId:t,drawingId:n}=e;if(this.getDrawingByParam({unitId:r,subUnitId:t,drawingId:n})==null)return[];const o=this._getDrawingData(r,t),a=[];return Object.keys(o).forEach(E=>{const W=o[E];W.groupId===n&&a.push(W)}),a}_getGroupDrawingOp(e){const{parent:r,children:t}=e,{unitId:n,subUnitId:u,drawingId:o}=r,a=[];a.push(F.insertOp([n,u,"data",o],r));let E=Number.NEGATIVE_INFINITY;return t.forEach(W=>{const{unitId:_,subUnitId:s,drawingId:c}=W,h=this._hasDrawingOrder({unitId:_,subUnitId:s,drawingId:c});E=Math.max(E,h),a.push(...this._getUpdateParamCompareOp(W,this.getDrawingByParam({unitId:_,subUnitId:s,drawingId:c})))}),E===Number.NEGATIVE_INFINITY&&(E=this._getDrawingOrder(n,u).length),a.push(F.insertOp([n,u,"order",E],o)),a.reduce(F.type.compose,null)}_getUngroupDrawingOp(e){const{parent:r,children:t}=e,{unitId:n,subUnitId:u,drawingId:o}=r,a=[];return t.forEach(E=>{const{unitId:W,subUnitId:_,drawingId:s}=E;a.push(...this._getUpdateParamCompareOp(E,this.getDrawingByParam({unitId:W,subUnitId:_,drawingId:s})))}),a.push(F.removeOp([n,u,"data",o],!0)),a.push(F.removeOp([n,u,"order",this._getDrawingOrder(n,u).indexOf(o)],!0)),a.reduce(F.type.compose,null)}applyJson1(e,r,t){this._establishDrawingMap(e,r),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[r,t,n]=e.split("#-#");return this._getCurrentBySearch({unitId:r,subUnitId:t,drawingId:n})}focusDrawing(e){if(e==null){this._focusDrawings=[],this._focus$.next([]);return}const r=[];e.forEach(t=>{var E;const{unitId:n,subUnitId:u,drawingId:o}=t,a=(E=this._getDrawingData(n,u))==null?void 0:E[o];a!=null&&r.push(a)}),r.length>0&&(this._focusDrawings=r,this._focus$.next(r))}getFocusDrawings(){const e=[];return this._focusDrawings.forEach(r=>{var a;const{unitId:t,subUnitId:n,drawingId:u}=r,o=(a=this._getDrawingData(t,n))==null?void 0:a[u];o!=null&&e.push(o)}),e}getDrawingOrder(e,r){return this._getDrawingOrder(e,r)}setDrawingOrder(e,r,t){this.drawingManagerData[e][r].order=t}orderUpdateNotification(e){this._order$.next(e)}getForwardDrawingsOp(e){const{unitId:r,subUnitId:t,drawingIds:n}=e,u=[],o=this.getDrawingOrder(r,t),a=[...n];n.forEach(_=>{const s=this._hasDrawingOrder({unitId:r,subUnitId:t,drawingId:_});if(s===-1||s===o.length-1)return;const c=F.moveOp([r,t,"order",s],[r,t,"order",s+1]);u.push(c),a.includes(o[s+1])||a.push(o[s+1])});const E=u.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(E,this.drawingManagerData),redo:E,unitId:r,subUnitId:t,objects:{...e,drawingIds:a}}}getBackwardDrawingOp(e){const{unitId:r,subUnitId:t,drawingIds:n}=e,u=[],o=this.getDrawingOrder(r,t),a=[...n];n.forEach(_=>{const s=this._hasDrawingOrder({unitId:r,subUnitId:t,drawingId:_});if(s===-1||s===0)return;const c=F.moveOp([r,t,"order",s],[r,t,"order",s-1]);u.push(c),a.includes(o[s-1])||a.push(o[s-1])});const E=u.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(E,this.drawingManagerData),redo:E,unitId:r,subUnitId:t,objects:{...e,drawingIds:a}}}getFrontDrawingsOp(e){const{unitId:r,subUnitId:t,drawingIds:n}=e,u=this._getOrderFromSearchParams(r,t,n),o=[...n],a=this.getDrawingOrder(r,t),E=[];u.forEach(s=>{const{drawingId:c}=s,h=this._getDrawingCount(r,t)-1,j=F.moveOp([r,t,"order",this._getDrawingOrder(r,t).indexOf(c)],[r,t,"order",h]);E.push(j),o.includes(a[h])||o.push(a[h])});const W=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(W,this.drawingManagerData),redo:W,unitId:r,subUnitId:t,objects:{...e,drawingIds:o}}}getBackDrawingsOp(e){const{unitId:r,subUnitId:t,drawingIds:n}=e,u=this._getOrderFromSearchParams(r,t,n,!0),o=[...n],a=this.getDrawingOrder(r,t),E=[];u.forEach(s=>{const{drawingId:c}=s,h=F.moveOp([r,t,"order",this._getDrawingOrder(r,t).indexOf(c)],[r,t,"order",0]);E.push(h),o.includes(a[0])||o.push(a[0])});const W=E.reduce(F.type.compose,null);return{undo:F.type.invertWithDoc(W,this.drawingManagerData),redo:W,unitId:r,subUnitId:t,objects:{...e,drawingIds:o}}}_getDrawingCount(e,r){return this.getDrawingOrder(e,r).length||0}_getOrderFromSearchParams(e,r,t,n=!1){return t.map(u=>{const o=this._hasDrawingOrder({unitId:e,subUnitId:r,drawingId:u});return{drawingId:u,zIndex:o}}).sort(n===!1?Y.sortRules:Y.sortRulesByDesc)}_hasDrawingOrder(e){if(e==null)return-1;const{unitId:r,subUnitId:t,drawingId:n}=e;return this._establishDrawingMap(r,t),this._getDrawingOrder(r,t).indexOf(n)}_getCurrentBySearch(e){var u,o,a;if(e==null)return;const{unitId:r,subUnitId:t,drawingId:n}=e;return(a=(o=(u=this.drawingManagerData[r])==null?void 0:u[t])==null?void 0:o.data)==null?void 0:a[n]}_getOldBySearch(e){var u,o,a;if(e==null)return;const{unitId:r,subUnitId:t,drawingId:n}=e;return(a=(o=(u=this._oldDrawingManagerData[r])==null?void 0:u[t])==null?void 0:o.data)==null?void 0:a[n]}_establishDrawingMap(e,r,t){var n;return this.drawingManagerData[e]||(this.drawingManagerData[e]={}),this.drawingManagerData[e][r]||(this.drawingManagerData[e][r]={data:{},order:[]}),t==null?null:(n=this.drawingManagerData[e][r].data)==null?void 0:n[t]}_addByParam(e){const{unitId:r,subUnitId:t,drawingId:n}=e;this._establishDrawingMap(r,t,n);const u=F.insertOp([r,t,"data",n],e),o=F.insertOp([r,t,"order",this._getDrawingOrder(r,t).length],n),a=[u,o].reduce(F.type.compose,null),E=F.type.invertWithDoc(a,this.drawingManagerData);return{op:a,invertOp:E}}_removeByParam(e){if(e==null)return{op:[],invertOp:[]};const{unitId:r,subUnitId:t,drawingId:n}=e;if(this._establishDrawingMap(r,t,n)==null)return{op:[],invertOp:[]};const o=F.removeOp([r,t,"data",n],!0),a=F.removeOp([r,t,"order",this._getDrawingOrder(r,t).indexOf(n)],!0),E=[o,a].reduce(F.type.compose,null),W=F.type.invertWithDoc(E,this.drawingManagerData);return{op:E,invertOp:W}}_updateByParam(e){const{unitId:r,subUnitId:t,drawingId:n}=e,u=this._establishDrawingMap(r,t,n);if(u==null)return{op:[],invertOp:[]};const a=this._getUpdateParamCompareOp(e,u).reduce(F.type.compose,null),E=F.type.invertWithDoc(a,this.drawingManagerData);return{op:a,invertOp:E}}_getUpdateParamCompareOp(e,r){const{unitId:t,subUnitId:n,drawingId:u}=e,o=[];return Object.keys(e).forEach(a=>{const E=e[a],W=r[a];W!==E&&o.push(F.replaceOp([t,n,"data",u,a],W,E))}),o}_getDrawingData(e,r){var t,n;return((n=(t=this.drawingManagerData[e])==null?void 0:t[r])==null?void 0:n.data)||{}}_getDrawingOrder(e,r){var t,n;return((n=(t=this.drawingManagerData[e])==null?void 0:t[r])==null?void 0:n.order)||[]}getDrawingVisible(){return this._visible}getDrawingEditable(){return this._editable}setDrawingVisible(e){this._visible=e}setDrawingEditable(e){this._editable=e}}class St extends Tt{}function qt({unitId:l,subUnitId:e,drawingId:r},t){return typeof t=="number"?`${l}#-#${e}#-#${r}#-#${t}`:`${l}#-#${e}#-#${r}`}const xt=async l=>new Promise((e,r)=>{const t=new Image;t.src=l,t.onload=()=>{e({width:t.width,height:t.height,image:t})},t.onerror=n=>{r(n)}}),Ht="drawing.config",Pt={},At=Y.createIdentifier("univer.drawing-manager.service");class Mt{constructor(){K(this,"_waitCount",0);K(this,"_change$",new ge.Subject);K(this,"change$",this._change$);K(this,"_imageSourceCache",new Map)}setWaitCount(e){this._waitCount=e,this._change$.next(e)}getImageSourceCache(e,r){if(r===Y.ImageSourceType.BASE64){const t=new Image;return t.src=e,t}return this._imageSourceCache.get(e)}addImageSourceCache(e,r,t){r===Y.ImageSourceType.BASE64||t==null||this._imageSourceCache.set(e,t)}async getImage(e){return Promise.resolve(e)}async saveImage(e){return new Promise((r,t)=>{if(!gt.includes(e.type)){t(new Error(Y.ImageUploadStatusType.ERROR_IMAGE_TYPE)),this._decreaseWaiting();return}if(e.size>5242880){t(new Error(Y.ImageUploadStatusType.ERROR_EXCEED_SIZE)),this._decreaseWaiting();return}const n=new FileReader;n.readAsDataURL(e),n.onload=u=>{var E;const o=(E=u.target)==null?void 0:E.result;if(o==null){t(new Error(Y.ImageUploadStatusType.ERROR_IMAGE)),this._decreaseWaiting();return}const a=Y.Tools.generateRandomId(6);r({imageId:a,imageSourceType:Y.ImageSourceType.BASE64,source:o,base64Cache:o,status:Y.ImageUploadStatusType.SUCCUSS}),this._decreaseWaiting()}})}_decreaseWaiting(){this._waitCount-=1,this._change$.next(this._waitCount)}}var Kt=Object.defineProperty,Ft=Object.getOwnPropertyDescriptor,Vt=(l,e,r,t)=>{for(var n=t>1?void 0:t?Ft(e,r):e,u=l.length-1,o;u>=0;u--)(o=l[u])&&(n=(t?o(e,r,n):o(n))||n);return t&&n&&Kt(e,r,n),n},Ut=(l,e)=>(r,t)=>e(r,t,l);const zt="UNIVER_DRAWING_PLUGIN";ne.UniverDrawingPlugin=(ct=class extends Y.Plugin{constructor(e=Pt,r,t){super(),this._config=e,this._injector=r,this._configService=t;const{...n}=Y.merge({},Pt,this._config);this._configService.setConfig(Ht,n)}onStarting(){this._initDependencies()}_initDependencies(){var t;const e=[[Y.IImageIoService,{useClass:Mt}],[At,{useClass:St}]];Y.mergeOverrideWithDependencies(e,(t=this._config)==null?void 0:t.override).forEach(n=>this._injector.add(n))}},K(ct,"pluginName",zt),ct),ne.UniverDrawingPlugin=Vt([Ut(1,Y.Inject(Y.Injector)),Ut(2,Y.IConfigService)],ne.UniverDrawingPlugin),Object.defineProperty(ne,"IImageIoService",{enumerable:!0,get:()=>Y.IImageIoService}),Object.defineProperty(ne,"ImageSourceType",{enumerable:!0,get:()=>Y.ImageSourceType}),Object.defineProperty(ne,"ImageUploadStatusType",{enumerable:!0,get:()=>Y.ImageUploadStatusType}),ne.DRAWING_IMAGE_ALLOW_IMAGE_LIST=gt,ne.DRAWING_IMAGE_ALLOW_SIZE=5242880,ne.DRAWING_IMAGE_COUNT_LIMIT=10,ne.DRAWING_IMAGE_HEIGHT_LIMIT=500,ne.DRAWING_IMAGE_WIDTH_LIMIT=500,ne.DrawingManagerService=St,ne.IDrawingManagerService=At,ne.ImageIoService=Mt,ne.UnitDrawingService=Tt,ne.getDrawingShapeKeyByDrawingSearch=qt,ne.getImageSize=xt,Object.defineProperty(ne,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/drawing",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "author": "DreamNum <developer@univer.ai>",
@@ -47,27 +47,14 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "ot-json1": "^1.0.2",
50
- "@univerjs/core": "0.5.2"
50
+ "@univerjs/core": "0.5.3"
51
51
  },
52
52
  "devDependencies": {
53
53
  "rxjs": "^7.8.1",
54
54
  "typescript": "^5.7.2",
55
- "vite": "^6.0.3",
55
+ "vite": "^6.0.6",
56
56
  "vitest": "^2.1.8",
57
- "@univerjs-infra/shared": "0.5.2"
58
- },
59
- "space": {
60
- ".": {
61
- "import": "./lib/es/index.js",
62
- "require": "./lib/cjs/index.js",
63
- "types": "./lib/types/index.d.ts"
64
- },
65
- "./*": {
66
- "import": "./lib/es/*",
67
- "require": "./lib/cjs/*",
68
- "types": "./lib/types/index.d.ts"
69
- },
70
- "./lib/*": "./lib/*"
57
+ "@univerjs-infra/shared": "0.5.3"
71
58
  },
72
59
  "scripts": {
73
60
  "test": "vitest run",