playhtml 0.1.2

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.
@@ -0,0 +1,6 @@
1
+ (function(v,_){typeof exports=="object"&&typeof module<"u"?_(exports):typeof define=="function"&&define.amd?define(["exports"],_):(v=typeof globalThis<"u"?globalThis:v||self,_(v.playhtml={}))})(this,function(v){"use strict";var Oo=Object.defineProperty;var Ro=(v,_,j)=>_ in v?Oo(v,_,{enumerable:!0,configurable:!0,writable:!0,value:j}):v[_]=j;var m=(v,_,j)=>(Ro(v,typeof _!="symbol"?_+"":_,j),j);const _=()=>new Map,j=n=>{const t=_();return n.forEach((e,s)=>{t.set(s,e)}),t},$=(n,t,e)=>{let s=n.get(t);return s===void 0&&n.set(t,s=e()),s},Bs=(n,t)=>{const e=[];for(const[s,r]of n)e.push(t(r,s));return e},Hs=(n,t)=>{for(const[e,s]of n)if(t(s,e))return!0;return!1},tt=()=>new Set,de=n=>n[n.length-1],Fs=(n,t)=>{for(let e=0;e<t.length;e++)n.push(t[e])},z=Array.from,js=Array.isArray,$s=String.fromCharCode,Ys=n=>n.toLowerCase(),Xs=/^\s*/g,Js=n=>n.replace(Xs,""),Gs=/([A-Z])/g,on=(n,t)=>Js(n.replace(Gs,e=>`${t}${Ys(e)}`)),Ws=n=>{const t=unescape(encodeURIComponent(n)),e=t.length,s=new Uint8Array(e);for(let r=0;r<e;r++)s[r]=t.codePointAt(r);return s},Dt=typeof TextEncoder<"u"?new TextEncoder:null,zs=Dt?n=>Dt.encode(n):Ws;let Et=typeof TextDecoder>"u"?null:new TextDecoder("utf-8",{fatal:!0,ignoreBOM:!0});Et&&Et.decode(new Uint8Array).length===1&&(Et=null);const ln=n=>n===void 0?null:n;class Ks{constructor(){this.map=new Map}setItem(t,e){this.map.set(t,e)}getItem(t){return this.map.get(t)}}let cn=new Ks,fe=!0;try{typeof localStorage<"u"&&(cn=localStorage,fe=!1)}catch{}const hn=cn,qs=n=>fe||addEventListener("storage",n),Qs=n=>fe||removeEventListener("storage",n),Zs=Object.assign,Ps=Object.keys,tr=(n,t)=>{for(const e in n)t(n[e],e)},er=(n,t)=>{const e=[];for(const s in n)e.push(t(n[s],s));return e},$t=n=>Ps(n).length,nr=n=>{for(const t in n)return!1;return!0},sr=(n,t)=>{for(const e in n)if(!t(n[e],e))return!1;return!0},an=(n,t)=>Object.prototype.hasOwnProperty.call(n,t),rr=(n,t)=>n===t||$t(n)===$t(t)&&sr(n,(e,s)=>(e!==void 0||an(t,s))&&t[s]===e),ge=(n,t,e=0)=>{try{for(;e<n.length;e++)n[e](...t)}finally{e<n.length&&ge(n,t,e+1)}},ir=n=>n,or=(n,t)=>n===t,It=(n,t)=>{if(n==null||t==null)return or(n,t);if(n.constructor!==t.constructor)return!1;if(n===t)return!0;switch(n.constructor){case ArrayBuffer:n=new Uint8Array(n),t=new Uint8Array(t);case Uint8Array:{if(n.byteLength!==t.byteLength)return!1;for(let e=0;e<n.length;e++)if(n[e]!==t[e])return!1;break}case Set:{if(n.size!==t.size)return!1;for(const e of n)if(!t.has(e))return!1;break}case Map:{if(n.size!==t.size)return!1;for(const e of n.keys())if(!t.has(e)||!It(n.get(e),t.get(e)))return!1;break}case Object:if($t(n)!==$t(t))return!1;for(const e in n)if(!an(n,e)||!It(n[e],t[e]))return!1;break;case Array:if(n.length!==t.length)return!1;for(let e=0;e<n.length;e++)if(!It(n[e],t[e]))return!1;break;default:return!1}return!0},lr=(n,t)=>t.includes(n),ut=typeof process<"u"&&process.release&&/node|io\.js/.test(process.release.name),un=typeof window<"u"&&typeof document<"u"&&!ut;let N;const cr=()=>{if(N===void 0)if(ut){N=_();const n=process.argv;let t=null;for(let e=0;e<n.length;e++){const s=n[e];s[0]==="-"?(t!==null&&N.set(t,""),t=s):t!==null&&(N.set(t,s),t=null)}t!==null&&N.set(t,"")}else typeof location=="object"?(N=_(),(location.search||"?").slice(1).split("&").forEach(n=>{if(n.length!==0){const[t,e]=n.split("=");N.set(`--${on(t,"-")}`,e),N.set(`-${on(t,"-")}`,e)}})):N=_();return N},pe=n=>cr().has(n),we=n=>ln(ut?process.env[n.toUpperCase()]:hn.getItem(n));(n=>pe("--"+n)||we(n)!==null)("production");const dn=ut&&lr(process.env.FORCE_COLOR,["true","1","2"]),hr=!pe("no-colors")&&(!ut||process.stdout.isTTY||dn)&&(!ut||pe("color")||dn||we("COLORTERM")!==null||(we("TERM")||"").includes("color")),Y=Math.floor,Yt=Math.abs,me=(n,t)=>n<t?n:t,et=(n,t)=>n>t?n:t,ar=Math.pow,fn=n=>n!==0?n<0:1/n<0,gn=1,pn=2,ye=4,ke=8,At=32,X=64,U=128,Xt=31,be=63,nt=127,ur=2147483647,wn=Number.MAX_SAFE_INTEGER,dr=Number.isInteger||(n=>typeof n=="number"&&isFinite(n)&&Y(n)===n);class Lt{constructor(){this.cpos=0,this.cbuf=new Uint8Array(100),this.bufs=[]}}const x=()=>new Lt,_e=n=>{let t=n.cpos;for(let e=0;e<n.bufs.length;e++)t+=n.bufs[e].length;return t},S=n=>{const t=new Uint8Array(_e(n));let e=0;for(let s=0;s<n.bufs.length;s++){const r=n.bufs[s];t.set(r,e),e+=r.length}return t.set(Me(n.cbuf.buffer,0,n.cpos),e),t},fr=(n,t)=>{const e=n.cbuf.length;e-n.cpos<t&&(n.bufs.push(Me(n.cbuf.buffer,0,n.cpos)),n.cbuf=new Uint8Array(et(e,t)*2),n.cpos=0)},I=(n,t)=>{const e=n.cbuf.length;n.cpos===e&&(n.bufs.push(n.cbuf),n.cbuf=new Uint8Array(e*2),n.cpos=0),n.cbuf[n.cpos++]=t},Se=I,p=(n,t)=>{for(;t>nt;)I(n,U|nt&t),t=Y(t/128);I(n,nt&t)},Ce=(n,t)=>{const e=fn(t);for(e&&(t=-t),I(n,(t>be?U:0)|(e?X:0)|be&t),t=Y(t/64);t>0;)I(n,(t>nt?U:0)|nt&t),t=Y(t/128)},De=new Uint8Array(3e4),gr=De.length/3,pr=(n,t)=>{if(t.length<gr){const e=Dt.encodeInto(t,De).written||0;p(n,e);for(let s=0;s<e;s++)I(n,De[s])}else C(n,zs(t))},wr=(n,t)=>{const e=unescape(encodeURIComponent(t)),s=e.length;p(n,s);for(let r=0;r<s;r++)I(n,e.codePointAt(r))},st=Dt&&Dt.encodeInto?pr:wr,Jt=(n,t)=>{const e=n.cbuf.length,s=n.cpos,r=me(e-s,t.length),i=t.length-r;n.cbuf.set(t.subarray(0,r),s),n.cpos+=r,i>0&&(n.bufs.push(n.cbuf),n.cbuf=new Uint8Array(et(e*2,i)),n.cbuf.set(t.subarray(r)),n.cpos=i)},C=(n,t)=>{p(n,t.byteLength),Jt(n,t)},Ee=(n,t)=>{fr(n,t);const e=new DataView(n.cbuf.buffer,n.cpos,t);return n.cpos+=t,e},mr=(n,t)=>Ee(n,4).setFloat32(0,t,!1),yr=(n,t)=>Ee(n,8).setFloat64(0,t,!1),kr=(n,t)=>Ee(n,8).setBigInt64(0,t,!1),mn=new DataView(new ArrayBuffer(4)),br=n=>(mn.setFloat32(0,n),mn.getFloat32(0)===n),xt=(n,t)=>{switch(typeof t){case"string":I(n,119),st(n,t);break;case"number":dr(t)&&Yt(t)<=ur?(I(n,125),Ce(n,t)):br(t)?(I(n,124),mr(n,t)):(I(n,123),yr(n,t));break;case"bigint":I(n,122),kr(n,t);break;case"object":if(t===null)I(n,126);else if(js(t)){I(n,117),p(n,t.length);for(let e=0;e<t.length;e++)xt(n,t[e])}else if(t instanceof Uint8Array)I(n,116),C(n,t);else{I(n,118);const e=Object.keys(t);p(n,e.length);for(let s=0;s<e.length;s++){const r=e[s];st(n,r),xt(n,t[r])}}break;case"boolean":I(n,t?120:121);break;default:I(n,127)}};class yn extends Lt{constructor(t){super(),this.w=t,this.s=null,this.count=0}write(t){this.s===t?this.count++:(this.count>0&&p(this,this.count-1),this.count=1,this.w(this,t),this.s=t)}}const kn=n=>{n.count>0&&(Ce(n.encoder,n.count===1?n.s:-n.s),n.count>1&&p(n.encoder,n.count-2))};class Gt{constructor(){this.encoder=new Lt,this.s=0,this.count=0}write(t){this.s===t?this.count++:(kn(this),this.count=1,this.s=t)}toUint8Array(){return kn(this),S(this.encoder)}}const bn=n=>{if(n.count>0){const t=n.diff*2+(n.count===1?0:1);Ce(n.encoder,t),n.count>1&&p(n.encoder,n.count-2)}};class Ie{constructor(){this.encoder=new Lt,this.s=0,this.count=0,this.diff=0}write(t){this.diff===t-this.s?(this.s=t,this.count++):(bn(this),this.count=1,this.diff=t-this.s,this.s=t)}toUint8Array(){return bn(this),S(this.encoder)}}class _r{constructor(){this.sarr=[],this.s="",this.lensE=new Gt}write(t){this.s+=t,this.s.length>19&&(this.sarr.push(this.s),this.s=""),this.lensE.write(t.length)}toUint8Array(){const t=new Lt;return this.sarr.push(this.s),this.s="",st(t,this.sarr.join("")),Jt(t,this.lensE.toUint8Array()),S(t)}}const dt=n=>new Error(n),V=()=>{throw dt("Method unimplemented")},B=()=>{throw dt("Unexpected case")},_n=dt("Unexpected end of array"),Sn=dt("Integer out of Range");class Wt{constructor(t){this.arr=t,this.pos=0}}const K=n=>new Wt(n),Sr=n=>n.pos!==n.arr.length,Cr=(n,t)=>{const e=Me(n.arr.buffer,n.pos+n.arr.byteOffset,t);return n.pos+=t,e},M=n=>Cr(n,w(n)),ft=n=>n.arr[n.pos++],w=n=>{let t=0,e=1;const s=n.arr.length;for(;n.pos<s;){const r=n.arr[n.pos++];if(t=t+(r&nt)*e,e*=128,r<U)return t;if(t>wn)throw Sn}throw _n},Ae=n=>{let t=n.arr[n.pos++],e=t&be,s=64;const r=(t&X)>0?-1:1;if(!(t&U))return r*e;const i=n.arr.length;for(;n.pos<i;){if(t=n.arr[n.pos++],e=e+(t&nt)*s,s*=128,t<U)return r*e;if(e>wn)throw Sn}throw _n},q=Et?n=>Et.decode(M(n)):n=>{let t=w(n);if(t===0)return"";{let e=String.fromCodePoint(ft(n));if(--t<100)for(;t--;)e+=String.fromCodePoint(ft(n));else for(;t>0;){const s=t<1e4?t:1e4,r=n.arr.subarray(n.pos,n.pos+s);n.pos+=s,e+=String.fromCodePoint.apply(null,r),t-=s}return decodeURIComponent(escape(e))}},Le=(n,t)=>{const e=new DataView(n.arr.buffer,n.arr.byteOffset+n.pos,t);return n.pos+=t,e},Dr=[n=>{},n=>null,Ae,n=>Le(n,4).getFloat32(0,!1),n=>Le(n,8).getFloat64(0,!1),n=>Le(n,8).getBigInt64(0,!1),n=>!1,n=>!0,q,n=>{const t=w(n),e={};for(let s=0;s<t;s++){const r=q(n);e[r]=Mt(n)}return e},n=>{const t=w(n),e=[];for(let s=0;s<t;s++)e.push(Mt(n));return e},M],Mt=n=>Dr[127-ft(n)](n);class Cn extends Wt{constructor(t,e){super(t),this.reader=e,this.s=null,this.count=0}read(){return this.count===0&&(this.s=this.reader(this),Sr(this)?this.count=w(this)+1:this.count=-1),this.count--,this.s}}class zt extends Wt{constructor(t){super(t),this.s=0,this.count=0}read(){if(this.count===0){this.s=Ae(this);const t=fn(this.s);this.count=1,t&&(this.s=-this.s,this.count=w(this)+2)}return this.count--,this.s}}class xe extends Wt{constructor(t){super(t),this.s=0,this.count=0,this.diff=0}read(){if(this.count===0){const t=Ae(this),e=t&1;this.diff=Y(t/2),this.count=1,e&&(this.count=w(this)+2)}return this.s+=this.diff,this.count--,this.s}}class Er{constructor(t){this.decoder=new zt(t),this.str=q(this.decoder),this.spos=0}read(){const t=this.spos+this.decoder.read(),e=this.str.slice(this.spos,t);return this.spos=t,e}}const Dn=n=>new Uint8Array(n),Me=(n,t,e)=>new Uint8Array(n,t,e),Ir=n=>new Uint8Array(n),Ar=n=>{let t="";for(let e=0;e<n.byteLength;e++)t+=$s(n[e]);return btoa(t)},Lr=n=>Buffer.from(n.buffer,n.byteOffset,n.byteLength).toString("base64"),xr=n=>{const t=atob(n),e=Dn(t.length);for(let s=0;s<t.length;s++)e[s]=t.charCodeAt(s);return e},Mr=n=>{const t=Buffer.from(n,"base64");return new Uint8Array(t.buffer,t.byteOffset,t.byteLength)},Ur=un?Ar:Lr,vr=un?xr:Mr,Tr=n=>{const t=Dn(n.byteLength);return t.set(n),t},En=new Map;class Or{constructor(t){this.room=t,this.onmessage=null,this._onChange=e=>e.key===t&&this.onmessage!==null&&this.onmessage({data:vr(e.newValue||"")}),qs(this._onChange)}postMessage(t){hn.setItem(this.room,Ur(Ir(t)))}close(){Qs(this._onChange)}}const Rr=typeof BroadcastChannel>"u"?Or:BroadcastChannel,Ue=n=>$(En,n,()=>{const t=tt(),e=new Rr(n);return e.onmessage=s=>t.forEach(r=>r(s.data,"broadcastchannel")),{bc:e,subs:t}}),Nr=(n,t)=>(Ue(n).subs.add(t),t),Vr=(n,t)=>{const e=Ue(n),s=e.subs.delete(t);return s&&e.subs.size===0&&(e.bc.close(),En.delete(n)),s},gt=(n,t,e=null)=>{const s=Ue(n);s.bc.postMessage(t),s.subs.forEach(r=>r(t,e))},rt=Date.now;class ve{constructor(){this._observers=_()}on(t,e){$(this._observers,t,tt).add(e)}once(t,e){const s=(...r)=>{this.off(t,s),e(...r)};this.on(t,s)}off(t,e){const s=this._observers.get(t);s!==void 0&&(s.delete(e),s.size===0&&this._observers.delete(t))}emit(t,e){return z((this._observers.get(t)||_()).values()).forEach(s=>s(...e))}destroy(){this._observers=_()}}const Br=crypto.getRandomValues.bind(crypto),In=()=>Br(new Uint32Array(1))[0],Hr=[1e7]+-1e3+-4e3+-8e3+-1e11,Fr=()=>Hr.replace(/[018]/g,n=>(n^In()&15>>n/4).toString(16)),An=n=>new Promise(n);Promise.all.bind(Promise);class jr{constructor(t,e){this.left=t,this.right=e}}const J=(n,t)=>new jr(n,t);typeof DOMParser<"u"&&new DOMParser;const $r=n=>Bs(n,(t,e)=>`${e}:${t};`).join(""),G=Symbol,Ln=G(),xn=G(),Yr=G(),Xr=G(),Jr=G(),Mn=G(),Gr=G(),Un=G(),Wr=G(),zr=n=>{const t=[];let e=0;for(;e<n.length;e++){const s=n[e];s.constructor===String||s.constructor===Number||s.constructor===Object&&t.push(JSON.stringify(s))}return t},Kr={[Ln]:J("font-weight","bold"),[xn]:J("font-weight","normal"),[Yr]:J("color","blue"),[Jr]:J("color","green"),[Xr]:J("color","grey"),[Mn]:J("color","red"),[Gr]:J("color","purple"),[Un]:J("color","orange"),[Wr]:J("color","black")},qr=hr?n=>{const t=[],e=[],s=_();let r=[],i=0;for(;i<n.length;i++){const o=n[i],l=Kr[o];if(l!==void 0)s.set(l.left,l.right);else if(o.constructor===String||o.constructor===Number){const c=$r(s);i>0||c.length>0?(t.push("%c"+o),e.push(c)):t.push(o)}else break}for(i>0&&(r=e,r.unshift(t.join("")));i<n.length;i++){const o=n[i];o instanceof Symbol||r.push(o)}return r}:zr,Qr=(...n)=>{console.log(...qr(n)),Zr.forEach(t=>t.print(n))},Zr=tt(),vn=n=>({[Symbol.iterator](){return this},next:n}),Pr=(n,t)=>vn(()=>{let e;do e=n.next();while(!e.done&&!t(e.value));return e}),Te=(n,t)=>vn(()=>{const{done:e,value:s}=n.next();return{done:e,value:e?void 0:t(s)}});class Oe{constructor(t,e){this.clock=t,this.len=e}}class Ut{constructor(){this.clients=new Map}}const Tn=(n,t,e)=>t.clients.forEach((s,r)=>{const i=n.doc.store.clients.get(r);for(let o=0;o<s.length;o++){const l=s[o];Qn(n,i,l.clock,l.len,e)}}),ti=(n,t)=>{let e=0,s=n.length-1;for(;e<=s;){const r=Y((e+s)/2),i=n[r],o=i.clock;if(o<=t){if(t<o+i.len)return r;e=r+1}else s=r-1}return null},On=(n,t)=>{const e=n.clients.get(t.client);return e!==void 0&&ti(e,t.clock)!==null},Re=n=>{n.clients.forEach(t=>{t.sort((r,i)=>r.clock-i.clock);let e,s;for(e=1,s=1;e<t.length;e++){const r=t[s-1],i=t[e];r.clock+r.len>=i.clock?r.len=et(r.len,i.clock+i.len-r.clock):(s<e&&(t[s]=i),s++)}t.length=s})},ei=n=>{const t=new Ut;for(let e=0;e<n.length;e++)n[e].clients.forEach((s,r)=>{if(!t.clients.has(r)){const i=s.slice();for(let o=e+1;o<n.length;o++)Fs(i,n[o].clients.get(r)||[]);t.clients.set(r,i)}});return Re(t),t},Kt=(n,t,e,s)=>{$(n.clients,t,()=>[]).push(new Oe(e,s))},ni=()=>new Ut,si=n=>{const t=ni();return n.clients.forEach((e,s)=>{const r=[];for(let i=0;i<e.length;i++){const o=e[i];if(o.deleted){const l=o.id.clock;let c=o.length;if(i+1<e.length)for(let a=e[i+1];i+1<e.length&&a.deleted;a=e[++i+1])c+=a.length;r.push(new Oe(l,c))}}r.length>0&&t.clients.set(s,r)}),t},pt=(n,t)=>{p(n.restEncoder,t.clients.size),z(t.clients.entries()).sort((e,s)=>s[0]-e[0]).forEach(([e,s])=>{n.resetDsCurVal(),p(n.restEncoder,e);const r=s.length;p(n.restEncoder,r);for(let i=0;i<r;i++){const o=s[i];n.writeDsClock(o.clock),n.writeDsLen(o.len)}})},Ne=n=>{const t=new Ut,e=w(n.restDecoder);for(let s=0;s<e;s++){n.resetDsCurVal();const r=w(n.restDecoder),i=w(n.restDecoder);if(i>0){const o=$(t.clients,r,()=>[]);for(let l=0;l<i;l++)o.push(new Oe(n.readDsClock(),n.readDsLen()))}}return t},Rn=(n,t,e)=>{const s=new Ut,r=w(n.restDecoder);for(let i=0;i<r;i++){n.resetDsCurVal();const o=w(n.restDecoder),l=w(n.restDecoder),c=e.clients.get(o)||[],a=A(e,o);for(let h=0;h<l;h++){const u=n.readDsClock(),d=u+n.readDsLen();if(u<a){a<d&&Kt(s,o,a,d-a);let f=H(c,u),g=c[f];for(!g.deleted&&g.id.clock<u&&(c.splice(f+1,0,ae(t,g,u-g.id.clock)),f++);f<c.length&&(g=c[f++],g.id.clock<d);)g.deleted||(d<g.id.clock+g.length&&c.splice(f,0,ae(t,g,d-g.id.clock)),g.delete(t))}else Kt(s,o,u,d-u)}}if(s.clients.size>0){const i=new it;return p(i.restEncoder,0),pt(i,s),i.toUint8Array()}return null},Nn=In;class wt extends ve{constructor({guid:t=Fr(),collectionid:e=null,gc:s=!0,gcFilter:r=()=>!0,meta:i=null,autoLoad:o=!1,shouldLoad:l=!0}={}){super(),this.gc=s,this.gcFilter=r,this.clientID=Nn(),this.guid=t,this.collectionid=e,this.share=new Map,this.store=new zn,this._transaction=null,this._transactionCleanups=[],this.subdocs=new Set,this._item=null,this.shouldLoad=l,this.autoLoad=o,this.meta=i,this.isLoaded=!1,this.isSynced=!1,this.whenLoaded=An(a=>{this.on("load",()=>{this.isLoaded=!0,a(this)})});const c=()=>An(a=>{const h=u=>{(u===void 0||u===!0)&&(this.off("sync",h),a())};this.on("sync",h)});this.on("sync",a=>{a===!1&&this.isSynced&&(this.whenSynced=c()),this.isSynced=a===void 0||a===!0,this.isLoaded||this.emit("load",[])}),this.whenSynced=c()}load(){const t=this._item;t!==null&&!this.shouldLoad&&b(t.parent.doc,e=>{e.subdocsLoaded.add(this)},null,!0),this.shouldLoad=!0}getSubdocs(){return this.subdocs}getSubdocGuids(){return new Set(z(this.subdocs).map(t=>t.guid))}transact(t,e=null){return b(this,t,e)}get(t,e=L){const s=$(this.share,t,()=>{const i=new e;return i._integrate(this,null),i}),r=s.constructor;if(e!==L&&r!==e)if(r===L){const i=new e;i._map=s._map,s._map.forEach(o=>{for(;o!==null;o=o.left)o.parent=i}),i._start=s._start;for(let o=i._start;o!==null;o=o.right)o.parent=i;return i._length=s._length,this.share.set(t,i),i._integrate(this,null),i}else throw new Error(`Type with the name ${t} has already been defined with a different constructor`);return s}getArray(t=""){return this.get(t,kt)}getText(t=""){return this.get(t,St)}getMap(t=""){return this.get(t,bt)}getXmlFragment(t=""){return this.get(t,ot)}toJSON(){const t={};return this.share.forEach((e,s)=>{t[s]=e.toJSON()}),t}destroy(){z(this.subdocs).forEach(e=>e.destroy());const t=this._item;if(t!==null){this._item=null;const e=t.content;e.doc=new wt({guid:this.guid,...e.opts,shouldLoad:!1}),e.doc._item=t,b(t.parent.doc,s=>{const r=e.doc;t.deleted||s.subdocsAdded.add(r),s.subdocsRemoved.add(this)},null,!0)}this.emit("destroyed",[!0]),this.emit("destroy",[this]),super.destroy()}on(t,e){super.on(t,e)}off(t,e){super.off(t,e)}}class Vn{constructor(t){this.restDecoder=t}resetDsCurVal(){}readDsClock(){return w(this.restDecoder)}readDsLen(){return w(this.restDecoder)}}class Bn extends Vn{readLeftID(){return y(w(this.restDecoder),w(this.restDecoder))}readRightID(){return y(w(this.restDecoder),w(this.restDecoder))}readClient(){return w(this.restDecoder)}readInfo(){return ft(this.restDecoder)}readString(){return q(this.restDecoder)}readParentInfo(){return w(this.restDecoder)===1}readTypeRef(){return w(this.restDecoder)}readLen(){return w(this.restDecoder)}readAny(){return Mt(this.restDecoder)}readBuf(){return Tr(M(this.restDecoder))}readJSON(){return JSON.parse(q(this.restDecoder))}readKey(){return q(this.restDecoder)}}class ri{constructor(t){this.dsCurrVal=0,this.restDecoder=t}resetDsCurVal(){this.dsCurrVal=0}readDsClock(){return this.dsCurrVal+=w(this.restDecoder),this.dsCurrVal}readDsLen(){const t=w(this.restDecoder)+1;return this.dsCurrVal+=t,t}}class mt extends ri{constructor(t){super(t),this.keys=[],w(t),this.keyClockDecoder=new xe(M(t)),this.clientDecoder=new zt(M(t)),this.leftClockDecoder=new xe(M(t)),this.rightClockDecoder=new xe(M(t)),this.infoDecoder=new Cn(M(t),ft),this.stringDecoder=new Er(M(t)),this.parentInfoDecoder=new Cn(M(t),ft),this.typeRefDecoder=new zt(M(t)),this.lenDecoder=new zt(M(t))}readLeftID(){return new yt(this.clientDecoder.read(),this.leftClockDecoder.read())}readRightID(){return new yt(this.clientDecoder.read(),this.rightClockDecoder.read())}readClient(){return this.clientDecoder.read()}readInfo(){return this.infoDecoder.read()}readString(){return this.stringDecoder.read()}readParentInfo(){return this.parentInfoDecoder.read()===1}readTypeRef(){return this.typeRefDecoder.read()}readLen(){return this.lenDecoder.read()}readAny(){return Mt(this.restDecoder)}readBuf(){return M(this.restDecoder)}readJSON(){return Mt(this.restDecoder)}readKey(){const t=this.keyClockDecoder.read();if(t<this.keys.length)return this.keys[t];{const e=this.stringDecoder.read();return this.keys.push(e),e}}}class Hn{constructor(){this.restEncoder=x()}toUint8Array(){return S(this.restEncoder)}resetDsCurVal(){}writeDsClock(t){p(this.restEncoder,t)}writeDsLen(t){p(this.restEncoder,t)}}class vt extends Hn{writeLeftID(t){p(this.restEncoder,t.client),p(this.restEncoder,t.clock)}writeRightID(t){p(this.restEncoder,t.client),p(this.restEncoder,t.clock)}writeClient(t){p(this.restEncoder,t)}writeInfo(t){Se(this.restEncoder,t)}writeString(t){st(this.restEncoder,t)}writeParentInfo(t){p(this.restEncoder,t?1:0)}writeTypeRef(t){p(this.restEncoder,t)}writeLen(t){p(this.restEncoder,t)}writeAny(t){xt(this.restEncoder,t)}writeBuf(t){C(this.restEncoder,t)}writeJSON(t){st(this.restEncoder,JSON.stringify(t))}writeKey(t){st(this.restEncoder,t)}}class Fn{constructor(){this.restEncoder=x(),this.dsCurrVal=0}toUint8Array(){return S(this.restEncoder)}resetDsCurVal(){this.dsCurrVal=0}writeDsClock(t){const e=t-this.dsCurrVal;this.dsCurrVal=t,p(this.restEncoder,e)}writeDsLen(t){t===0&&B(),p(this.restEncoder,t-1),this.dsCurrVal+=t}}class it extends Fn{constructor(){super(),this.keyMap=new Map,this.keyClock=0,this.keyClockEncoder=new Ie,this.clientEncoder=new Gt,this.leftClockEncoder=new Ie,this.rightClockEncoder=new Ie,this.infoEncoder=new yn(Se),this.stringEncoder=new _r,this.parentInfoEncoder=new yn(Se),this.typeRefEncoder=new Gt,this.lenEncoder=new Gt}toUint8Array(){const t=x();return p(t,0),C(t,this.keyClockEncoder.toUint8Array()),C(t,this.clientEncoder.toUint8Array()),C(t,this.leftClockEncoder.toUint8Array()),C(t,this.rightClockEncoder.toUint8Array()),C(t,S(this.infoEncoder)),C(t,this.stringEncoder.toUint8Array()),C(t,S(this.parentInfoEncoder)),C(t,this.typeRefEncoder.toUint8Array()),C(t,this.lenEncoder.toUint8Array()),Jt(t,S(this.restEncoder)),S(t)}writeLeftID(t){this.clientEncoder.write(t.client),this.leftClockEncoder.write(t.clock)}writeRightID(t){this.clientEncoder.write(t.client),this.rightClockEncoder.write(t.clock)}writeClient(t){this.clientEncoder.write(t)}writeInfo(t){this.infoEncoder.write(t)}writeString(t){this.stringEncoder.write(t)}writeParentInfo(t){this.parentInfoEncoder.write(t?1:0)}writeTypeRef(t){this.typeRefEncoder.write(t)}writeLen(t){this.lenEncoder.write(t)}writeAny(t){xt(this.restEncoder,t)}writeBuf(t){C(this.restEncoder,t)}writeJSON(t){xt(this.restEncoder,t)}writeKey(t){const e=this.keyMap.get(t);e===void 0?(this.keyClockEncoder.write(this.keyClock++),this.stringEncoder.write(t)):this.keyClockEncoder.write(e)}}const ii=(n,t,e,s)=>{s=et(s,t[0].id.clock);const r=H(t,s);p(n.restEncoder,t.length-r),n.writeClient(e),p(n.restEncoder,s);const i=t[r];i.write(n,s-i.id.clock);for(let o=r+1;o<t.length;o++)t[o].write(n,0)},Ve=(n,t,e)=>{const s=new Map;e.forEach((r,i)=>{A(t,i)>r&&s.set(i,r)}),Qt(t).forEach((r,i)=>{e.has(i)||s.set(i,0)}),p(n.restEncoder,s.size),z(s.entries()).sort((r,i)=>i[0]-r[0]).forEach(([r,i])=>{ii(n,t.clients.get(r),r,i)})},oi=(n,t)=>{const e=_(),s=w(n.restDecoder);for(let r=0;r<s;r++){const i=w(n.restDecoder),o=new Array(i),l=n.readClient();let c=w(n.restDecoder);e.set(l,{i:0,refs:o});for(let a=0;a<i;a++){const h=n.readInfo();switch(Xt&h){case 0:{const u=n.readLen();o[a]=new T(y(l,c),u),c+=u;break}case 10:{const u=w(n.restDecoder);o[a]=new O(y(l,c),u),c+=u;break}default:{const u=(h&(X|U))===0,d=new E(y(l,c),null,(h&U)===U?n.readLeftID():null,null,(h&X)===X?n.readRightID():null,u?n.readParentInfo()?t.get(n.readString()):n.readLeftID():null,u&&(h&At)===At?n.readString():null,_s(n,h));o[a]=d,c+=d.length}}}}return e},li=(n,t,e)=>{const s=[];let r=z(e.keys()).sort((f,g)=>f-g);if(r.length===0)return null;const i=()=>{if(r.length===0)return null;let f=e.get(r[r.length-1]);for(;f.refs.length===f.i;)if(r.pop(),r.length>0)f=e.get(r[r.length-1]);else return null;return f};let o=i();if(o===null&&s.length===0)return null;const l=new zn,c=new Map,a=(f,g)=>{const k=c.get(f);(k==null||k>g)&&c.set(f,g)};let h=o.refs[o.i++];const u=new Map,d=()=>{for(const f of s){const g=f.id.client,k=e.get(g);k?(k.i--,l.clients.set(g,k.refs.slice(k.i)),e.delete(g),k.i=0,k.refs=[]):l.clients.set(g,[f]),r=r.filter(R=>R!==g)}s.length=0};for(;;){if(h.constructor!==O){const g=$(u,h.id.client,()=>A(t,h.id.client))-h.id.clock;if(g<0)s.push(h),a(h.id.client,h.id.clock-1),d();else{const k=h.getMissing(n,t);if(k!==null){s.push(h);const R=e.get(k)||{refs:[],i:0};if(R.refs.length===R.i)a(k,A(t,k)),d();else{h=R.refs[R.i++];continue}}else(g===0||g<h.length)&&(h.integrate(n,g),u.set(h.id.client,h.id.clock+h.length))}}if(s.length>0)h=s.pop();else if(o!==null&&o.i<o.refs.length)h=o.refs[o.i++];else{if(o=i(),o===null)break;h=o.refs[o.i++]}}if(l.clients.size>0){const f=new it;return Ve(f,l,new Map),p(f.restEncoder,0),{missing:c,update:f.toUint8Array()}}return null},ci=(n,t)=>Ve(n,t.doc.store,t.beforeState),hi=(n,t,e,s=new mt(n))=>b(t,r=>{r.local=!1;let i=!1;const o=r.doc,l=o.store,c=oi(s,o),a=li(r,l,c),h=l.pendingStructs;if(h){for(const[d,f]of h.missing)if(f<A(l,d)){i=!0;break}if(a){for(const[d,f]of a.missing){const g=h.missing.get(d);(g==null||g>f)&&h.missing.set(d,f)}h.update=Pt([h.update,a.update])}}else l.pendingStructs=a;const u=Rn(s,r,l);if(l.pendingDs){const d=new mt(K(l.pendingDs));w(d.restDecoder);const f=Rn(d,r,l);u&&f?l.pendingDs=Pt([u,f]):l.pendingDs=u||f}else l.pendingDs=u;if(i){const d=l.pendingStructs.update;l.pendingStructs=null,jn(r.doc,d)}},e,!1),jn=(n,t,e,s=mt)=>{const r=K(t);hi(r,n,e,new s(r))},ai=(n,t,e)=>jn(n,t,e,Bn),ui=(n,t,e=new Map)=>{Ve(n,t.store,e),pt(n,si(t.store))},di=(n,t=new Uint8Array([0]),e=new it)=>{const s=$n(t);ui(e,n,s);const r=[e.toUint8Array()];if(n.store.pendingDs&&r.push(n.store.pendingDs),n.store.pendingStructs&&r.push(Ai(n.store.pendingStructs.update,t)),r.length>1){if(e.constructor===vt)return Ei(r.map((i,o)=>o===0?i:xi(i)));if(e.constructor===it)return Pt(r)}return r[0]},fi=(n,t)=>di(n,t,new vt),gi=n=>{const t=new Map,e=w(n.restDecoder);for(let s=0;s<e;s++){const r=w(n.restDecoder),i=w(n.restDecoder);t.set(r,i)}return t},$n=n=>gi(new Vn(K(n))),Yn=(n,t)=>(p(n.restEncoder,t.size),z(t.entries()).sort((e,s)=>s[0]-e[0]).forEach(([e,s])=>{p(n.restEncoder,e),p(n.restEncoder,s)}),n),pi=(n,t)=>Yn(n,Qt(t.store)),wi=(n,t=new Fn)=>(n instanceof Map?Yn(t,n):pi(t,n),t.toUint8Array()),mi=n=>wi(n,new Hn);class yi{constructor(){this.l=[]}}const Xn=()=>new yi,Jn=(n,t)=>n.l.push(t),Gn=(n,t)=>{const e=n.l,s=e.length;n.l=e.filter(r=>t!==r),s===n.l.length&&console.error("[yjs] Tried to remove event handler that doesn't exist.")},Wn=(n,t,e)=>ge(n.l,[t,e]);class yt{constructor(t,e){this.client=t,this.clock=e}}const qt=(n,t)=>n===t||n!==null&&t!==null&&n.client===t.client&&n.clock===t.clock,y=(n,t)=>new yt(n,t),ki=n=>{for(const[t,e]of n.doc.share.entries())if(e===n)return t;throw B()},Tt=(n,t)=>t===void 0?!n.deleted:t.sv.has(n.id.client)&&(t.sv.get(n.id.client)||0)>n.id.clock&&!On(t.ds,n.id),Be=(n,t)=>{const e=$(n.meta,Be,tt),s=n.doc.store;e.has(t)||(t.sv.forEach((r,i)=>{r<A(s,i)&&Q(n,y(i,r))}),Tn(n,t.ds,r=>{}),e.add(t))};class zn{constructor(){this.clients=new Map,this.pendingStructs=null,this.pendingDs=null}}const Qt=n=>{const t=new Map;return n.clients.forEach((e,s)=>{const r=e[e.length-1];t.set(s,r.id.clock+r.length)}),t},A=(n,t)=>{const e=n.clients.get(t);if(e===void 0)return 0;const s=e[e.length-1];return s.id.clock+s.length},Kn=(n,t)=>{let e=n.clients.get(t.id.client);if(e===void 0)e=[],n.clients.set(t.id.client,e);else{const s=e[e.length-1];if(s.id.clock+s.length!==t.id.clock)throw B()}e.push(t)},H=(n,t)=>{let e=0,s=n.length-1,r=n[s],i=r.id.clock;if(i===t)return s;let o=Y(t/(i+r.length-1)*s);for(;e<=s;){if(r=n[o],i=r.id.clock,i<=t){if(t<i+r.length)return o;e=o+1}else s=o-1;o=Y((e+s)/2)}throw B()},He=(n,t)=>{const e=n.clients.get(t.client);return e[H(e,t.clock)]},Fe=(n,t,e)=>{const s=H(t,e),r=t[s];return r.id.clock<e&&r instanceof E?(t.splice(s+1,0,ae(n,r,e-r.id.clock)),s+1):s},Q=(n,t)=>{const e=n.doc.store.clients.get(t.client);return e[Fe(n,e,t.clock)]},qn=(n,t,e)=>{const s=t.clients.get(e.client),r=H(s,e.clock),i=s[r];return e.clock!==i.id.clock+i.length-1&&i.constructor!==T&&s.splice(r+1,0,ae(n,i,e.clock-i.id.clock+1)),i},bi=(n,t,e)=>{const s=n.clients.get(t.id.client);s[H(s,t.id.clock)]=e},Qn=(n,t,e,s,r)=>{if(s===0)return;const i=e+s;let o=Fe(n,t,e),l;do l=t[o++],i<l.id.clock+l.length&&Fe(n,t,i),r(l);while(o<t.length&&t[o].id.clock<i)};class _i{constructor(t,e,s){this.doc=t,this.deleteSet=new Ut,this.beforeState=Qt(t.store),this.afterState=new Map,this.changed=new Map,this.changedParentTypes=new Map,this._mergeStructs=[],this.origin=e,this.meta=new Map,this.local=s,this.subdocsAdded=new Set,this.subdocsRemoved=new Set,this.subdocsLoaded=new Set,this._needFormattingCleanup=!1}}const Zn=(n,t)=>t.deleteSet.clients.size===0&&!Hs(t.afterState,(e,s)=>t.beforeState.get(s)!==e)?!1:(Re(t.deleteSet),ci(n,t),pt(n,t.deleteSet),!0),Pn=(n,t,e)=>{const s=t._item;(s===null||s.id.clock<(n.beforeState.get(s.id.client)||0)&&!s.deleted)&&$(n.changed,t,tt).add(e)},Zt=(n,t)=>{const e=n[t-1],s=n[t];e.deleted===s.deleted&&e.constructor===s.constructor&&e.mergeWith(s)&&(n.splice(t,1),s instanceof E&&s.parentSub!==null&&s.parent._map.get(s.parentSub)===s&&s.parent._map.set(s.parentSub,e))},Si=(n,t,e)=>{for(const[s,r]of n.clients.entries()){const i=t.clients.get(s);for(let o=r.length-1;o>=0;o--){const l=r[o],c=l.clock+l.len;for(let a=H(i,l.clock),h=i[a];a<i.length&&h.id.clock<c;h=i[++a]){const u=i[a];if(l.clock+l.len<=u.id.clock)break;u instanceof E&&u.deleted&&!u.keep&&e(u)&&u.gc(t,!1)}}}},Ci=(n,t)=>{n.clients.forEach((e,s)=>{const r=t.clients.get(s);for(let i=e.length-1;i>=0;i--){const o=e[i],l=me(r.length-1,1+H(r,o.clock+o.len-1));for(let c=l,a=r[c];c>0&&a.id.clock>=o.clock;a=r[--c])Zt(r,c)}})},ts=(n,t)=>{if(t<n.length){const e=n[t],s=e.doc,r=s.store,i=e.deleteSet,o=e._mergeStructs;try{Re(i),e.afterState=Qt(e.doc.store),s.emit("beforeObserverCalls",[e,s]);const l=[];e.changed.forEach((c,a)=>l.push(()=>{(a._item===null||!a._item.deleted)&&a._callObserver(e,c)})),l.push(()=>{e.changedParentTypes.forEach((c,a)=>{a._dEH.l.length>0&&(a._item===null||!a._item.deleted)&&(c=c.filter(h=>h.target._item===null||!h.target._item.deleted),c.forEach(h=>{h.currentTarget=a,h._path=null}),c.sort((h,u)=>h.path.length-u.path.length),Wn(a._dEH,c,e))})}),l.push(()=>s.emit("afterTransaction",[e,s])),ge(l,[]),e._needFormattingCleanup&&$i(e)}finally{s.gc&&Si(i,r,s.gcFilter),Ci(i,r),e.afterState.forEach((h,u)=>{const d=e.beforeState.get(u)||0;if(d!==h){const f=r.clients.get(u),g=et(H(f,d),1);for(let k=f.length-1;k>=g;k--)Zt(f,k)}});for(let h=0;h<o.length;h++){const{client:u,clock:d}=o[h].id,f=r.clients.get(u),g=H(f,d);g+1<f.length&&Zt(f,g+1),g>0&&Zt(f,g)}if(!e.local&&e.afterState.get(s.clientID)!==e.beforeState.get(s.clientID)&&(Qr(Un,Ln,"[yjs] ",xn,Mn,"Changed the client-id because another client seems to be using it."),s.clientID=Nn()),s.emit("afterTransactionCleanup",[e,s]),s._observers.has("update")){const h=new vt;Zn(h,e)&&s.emit("update",[h.toUint8Array(),e.origin,s,e])}if(s._observers.has("updateV2")){const h=new it;Zn(h,e)&&s.emit("updateV2",[h.toUint8Array(),e.origin,s,e])}const{subdocsAdded:l,subdocsLoaded:c,subdocsRemoved:a}=e;(l.size>0||a.size>0||c.size>0)&&(l.forEach(h=>{h.clientID=s.clientID,h.collectionid==null&&(h.collectionid=s.collectionid),s.subdocs.add(h)}),a.forEach(h=>s.subdocs.delete(h)),s.emit("subdocs",[{loaded:c,added:l,removed:a},s,e]),a.forEach(h=>h.destroy())),n.length<=t+1?(s._transactionCleanups=[],s.emit("afterAllTransactions",[s,n])):ts(n,t+1)}}},b=(n,t,e=null,s=!0)=>{const r=n._transactionCleanups;let i=!1,o=null;n._transaction===null&&(i=!0,n._transaction=new _i(n,e,s),r.push(n._transaction),r.length===1&&n.emit("beforeAllTransactions",[n]),n.emit("beforeTransaction",[n._transaction,n]));try{o=t(n._transaction)}finally{if(i){const l=n._transaction===r[0];n._transaction=null,l&&ts(r,0)}}return o};function*Di(n){const t=w(n.restDecoder);for(let e=0;e<t;e++){const s=w(n.restDecoder),r=n.readClient();let i=w(n.restDecoder);for(let o=0;o<s;o++){const l=n.readInfo();if(l===10){const c=w(n.restDecoder);yield new O(y(r,i),c),i+=c}else if(Xt&l){const c=(l&(X|U))===0,a=new E(y(r,i),null,(l&U)===U?n.readLeftID():null,null,(l&X)===X?n.readRightID():null,c?n.readParentInfo()?n.readString():n.readLeftID():null,c&&(l&At)===At?n.readString():null,_s(n,l));yield a,i+=a.length}else{const c=n.readLen();yield new T(y(r,i),c),i+=c}}}}class je{constructor(t,e){this.gen=Di(t),this.curr=null,this.done=!1,this.filterSkips=e,this.next()}next(){do this.curr=this.gen.next().value||null;while(this.filterSkips&&this.curr!==null&&this.curr.constructor===O);return this.curr}}class $e{constructor(t){this.currClient=0,this.startClock=0,this.written=0,this.encoder=t,this.clientStructs=[]}}const Ei=n=>Pt(n,Bn,vt),Ii=(n,t)=>{if(n.constructor===T){const{client:e,clock:s}=n.id;return new T(y(e,s+t),n.length-t)}else if(n.constructor===O){const{client:e,clock:s}=n.id;return new O(y(e,s+t),n.length-t)}else{const e=n,{client:s,clock:r}=e.id;return new E(y(s,r+t),null,y(s,r+t-1),null,e.rightOrigin,e.parent,e.parentSub,e.content.splice(t))}},Pt=(n,t=mt,e=it)=>{if(n.length===1)return n[0];const s=n.map(h=>new t(K(h)));let r=s.map(h=>new je(h,!0)),i=null;const o=new e,l=new $e(o);for(;r=r.filter(d=>d.curr!==null),r.sort((d,f)=>{if(d.curr.id.client===f.curr.id.client){const g=d.curr.id.clock-f.curr.id.clock;return g===0?d.curr.constructor===f.curr.constructor?0:d.curr.constructor===O?1:-1:g}else return f.curr.id.client-d.curr.id.client}),r.length!==0;){const h=r[0],u=h.curr.id.client;if(i!==null){let d=h.curr,f=!1;for(;d!==null&&d.id.clock+d.length<=i.struct.id.clock+i.struct.length&&d.id.client>=i.struct.id.client;)d=h.next(),f=!0;if(d===null||d.id.client!==u||f&&d.id.clock>i.struct.id.clock+i.struct.length)continue;if(u!==i.struct.id.client)Z(l,i.struct,i.offset),i={struct:d,offset:0},h.next();else if(i.struct.id.clock+i.struct.length<d.id.clock)if(i.struct.constructor===O)i.struct.length=d.id.clock+d.length-i.struct.id.clock;else{Z(l,i.struct,i.offset);const g=d.id.clock-i.struct.id.clock-i.struct.length;i={struct:new O(y(u,i.struct.id.clock+i.struct.length),g),offset:0}}else{const g=i.struct.id.clock+i.struct.length-d.id.clock;g>0&&(i.struct.constructor===O?i.struct.length-=g:d=Ii(d,g)),i.struct.mergeWith(d)||(Z(l,i.struct,i.offset),i={struct:d,offset:0},h.next())}}else i={struct:h.curr,offset:0},h.next();for(let d=h.curr;d!==null&&d.id.client===u&&d.id.clock===i.struct.id.clock+i.struct.length&&d.constructor!==O;d=h.next())Z(l,i.struct,i.offset),i={struct:d,offset:0}}i!==null&&(Z(l,i.struct,i.offset),i=null),Ye(l);const c=s.map(h=>Ne(h)),a=ei(c);return pt(o,a),o.toUint8Array()},Ai=(n,t,e=mt,s=it)=>{const r=$n(t),i=new s,o=new $e(i),l=new e(K(n)),c=new je(l,!1);for(;c.curr;){const h=c.curr,u=h.id.client,d=r.get(u)||0;if(c.curr.constructor===O){c.next();continue}if(h.id.clock+h.length>d)for(Z(o,h,et(d-h.id.clock,0)),c.next();c.curr&&c.curr.id.client===u;)Z(o,c.curr,0),c.next();else for(;c.curr&&c.curr.id.client===u&&c.curr.id.clock+c.curr.length<=d;)c.next()}Ye(o);const a=Ne(l);return pt(i,a),i.toUint8Array()},es=n=>{n.written>0&&(n.clientStructs.push({written:n.written,restEncoder:S(n.encoder.restEncoder)}),n.encoder.restEncoder=x(),n.written=0)},Z=(n,t,e)=>{n.written>0&&n.currClient!==t.id.client&&es(n),n.written===0&&(n.currClient=t.id.client,n.encoder.writeClient(t.id.client),p(n.encoder.restEncoder,t.id.clock+e)),t.write(n.encoder,e),n.written++},Ye=n=>{es(n);const t=n.encoder.restEncoder;p(t,n.clientStructs.length);for(let e=0;e<n.clientStructs.length;e++){const s=n.clientStructs[e];p(t,s.written),Jt(t,s.restEncoder)}},Li=(n,t,e,s)=>{const r=new e(K(n)),i=new je(r,!1),o=new s,l=new $e(o);for(let a=i.curr;a!==null;a=i.next())Z(l,t(a),0);Ye(l);const c=Ne(r);return pt(o,c),o.toUint8Array()},xi=n=>Li(n,ir,mt,vt);class te{constructor(t,e){this.target=t,this.currentTarget=t,this.transaction=e,this._changes=null,this._keys=null,this._delta=null,this._path=null}get path(){return this._path||(this._path=Mi(this.currentTarget,this.target))}deletes(t){return On(this.transaction.deleteSet,t.id)}get keys(){if(this._keys===null){const t=new Map,e=this.target;this.transaction.changed.get(e).forEach(r=>{if(r!==null){const i=e._map.get(r);let o,l;if(this.adds(i)){let c=i.left;for(;c!==null&&this.adds(c);)c=c.left;if(this.deletes(i))if(c!==null&&this.deletes(c))o="delete",l=de(c.content.getContent());else return;else c!==null&&this.deletes(c)?(o="update",l=de(c.content.getContent())):(o="add",l=void 0)}else if(this.deletes(i))o="delete",l=de(i.content.getContent());else return;t.set(r,{action:o,oldValue:l})}}),this._keys=t}return this._keys}get delta(){return this.changes.delta}adds(t){return t.id.clock>=(this.transaction.beforeState.get(t.id.client)||0)}get changes(){let t=this._changes;if(t===null){const e=this.target,s=tt(),r=tt(),i=[];if(t={added:s,deleted:r,delta:i,keys:this.keys},this.transaction.changed.get(e).has(null)){let l=null;const c=()=>{l&&i.push(l)};for(let a=e._start;a!==null;a=a.right)a.deleted?this.deletes(a)&&!this.adds(a)&&((l===null||l.delete===void 0)&&(c(),l={delete:0}),l.delete+=a.length,r.add(a)):this.adds(a)?((l===null||l.insert===void 0)&&(c(),l={insert:[]}),l.insert=l.insert.concat(a.content.getContent()),s.add(a)):((l===null||l.retain===void 0)&&(c(),l={retain:0}),l.retain+=a.length);l!==null&&l.retain===void 0&&c()}this._changes=t}return t}}const Mi=(n,t)=>{const e=[];for(;t._item!==null&&t!==n;){if(t._item.parentSub!==null)e.unshift(t._item.parentSub);else{let s=0,r=t._item.parent._start;for(;r!==t._item&&r!==null;)r.deleted||s++,r=r.right;e.unshift(s)}t=t._item.parent}return e},ns=80;let Xe=0;class Ui{constructor(t,e){t.marker=!0,this.p=t,this.index=e,this.timestamp=Xe++}}const vi=n=>{n.timestamp=Xe++},ss=(n,t,e)=>{n.p.marker=!1,n.p=t,t.marker=!0,n.index=e,n.timestamp=Xe++},Ti=(n,t,e)=>{if(n.length>=ns){const s=n.reduce((r,i)=>r.timestamp<i.timestamp?r:i);return ss(s,t,e),s}else{const s=new Ui(t,e);return n.push(s),s}},ee=(n,t)=>{if(n._start===null||t===0||n._searchMarker===null)return null;const e=n._searchMarker.length===0?null:n._searchMarker.reduce((i,o)=>Yt(t-i.index)<Yt(t-o.index)?i:o);let s=n._start,r=0;for(e!==null&&(s=e.p,r=e.index,vi(e));s.right!==null&&r<t;){if(!s.deleted&&s.countable){if(t<r+s.length)break;r+=s.length}s=s.right}for(;s.left!==null&&r>t;)s=s.left,!s.deleted&&s.countable&&(r-=s.length);for(;s.left!==null&&s.left.id.client===s.id.client&&s.left.id.clock+s.left.length===s.id.clock;)s=s.left,!s.deleted&&s.countable&&(r-=s.length);return e!==null&&Yt(e.index-r)<s.parent.length/ns?(ss(e,s,r),e):Ti(n._searchMarker,s,r)},Ot=(n,t,e)=>{for(let s=n.length-1;s>=0;s--){const r=n[s];if(e>0){let i=r.p;for(i.marker=!1;i&&(i.deleted||!i.countable);)i=i.left,i&&!i.deleted&&i.countable&&(r.index-=i.length);if(i===null||i.marker===!0){n.splice(s,1);continue}r.p=i,i.marker=!0}(t<r.index||e>0&&t===r.index)&&(r.index=et(t,r.index+e))}},ne=(n,t,e)=>{const s=n,r=t.changedParentTypes;for(;$(r,n,()=>[]).push(e),n._item!==null;)n=n._item.parent;Wn(s._eH,e,t)};class L{constructor(){this._item=null,this._map=new Map,this._start=null,this.doc=null,this._length=0,this._eH=Xn(),this._dEH=Xn(),this._searchMarker=null}get parent(){return this._item?this._item.parent:null}_integrate(t,e){this.doc=t,this._item=e}_copy(){throw V()}clone(){throw V()}_write(t){}get _first(){let t=this._start;for(;t!==null&&t.deleted;)t=t.right;return t}_callObserver(t,e){!t.local&&this._searchMarker&&(this._searchMarker.length=0)}observe(t){Jn(this._eH,t)}observeDeep(t){Jn(this._dEH,t)}unobserve(t){Gn(this._eH,t)}unobserveDeep(t){Gn(this._dEH,t)}toJSON(){}}const rs=(n,t,e)=>{t<0&&(t=n._length+t),e<0&&(e=n._length+e);let s=e-t;const r=[];let i=n._start;for(;i!==null&&s>0;){if(i.countable&&!i.deleted){const o=i.content.getContent();if(o.length<=t)t-=o.length;else{for(let l=t;l<o.length&&s>0;l++)r.push(o[l]),s--;t=0}}i=i.right}return r},is=n=>{const t=[];let e=n._start;for(;e!==null;){if(e.countable&&!e.deleted){const s=e.content.getContent();for(let r=0;r<s.length;r++)t.push(s[r])}e=e.right}return t},Rt=(n,t)=>{let e=0,s=n._start;for(;s!==null;){if(s.countable&&!s.deleted){const r=s.content.getContent();for(let i=0;i<r.length;i++)t(r[i],e++,n)}s=s.right}},os=(n,t)=>{const e=[];return Rt(n,(s,r)=>{e.push(t(s,r,n))}),e},Oi=n=>{let t=n._start,e=null,s=0;return{[Symbol.iterator](){return this},next:()=>{if(e===null){for(;t!==null&&t.deleted;)t=t.right;if(t===null)return{done:!0,value:void 0};e=t.content.getContent(),s=0,t=t.right}const r=e[s++];return e.length<=s&&(e=null),{done:!1,value:r}}}},ls=(n,t)=>{const e=ee(n,t);let s=n._start;for(e!==null&&(s=e.p,t-=e.index);s!==null;s=s.right)if(!s.deleted&&s.countable){if(t<s.length)return s.content.getContent()[t];t-=s.length}},se=(n,t,e,s)=>{let r=e;const i=n.doc,o=i.clientID,l=i.store,c=e===null?t._start:e.right;let a=[];const h=()=>{a.length>0&&(r=new E(y(o,A(l,o)),r,r&&r.lastId,c,c&&c.id,t,null,new ct(a)),r.integrate(n,0),a=[])};s.forEach(u=>{if(u===null)a.push(u);else switch(u.constructor){case Number:case Object:case Boolean:case Array:case String:a.push(u);break;default:switch(h(),u.constructor){case Uint8Array:case ArrayBuffer:r=new E(y(o,A(l,o)),r,r&&r.lastId,c,c&&c.id,t,null,new Vt(new Uint8Array(u))),r.integrate(n,0);break;case wt:r=new E(y(o,A(l,o)),r,r&&r.lastId,c,c&&c.id,t,null,new Ht(u)),r.integrate(n,0);break;default:if(u instanceof L)r=new E(y(o,A(l,o)),r,r&&r.lastId,c,c&&c.id,t,null,new W(u)),r.integrate(n,0);else throw new Error("Unexpected content type in insert operation")}}}),h()},cs=dt("Length exceeded!"),hs=(n,t,e,s)=>{if(e>t._length)throw cs;if(e===0)return t._searchMarker&&Ot(t._searchMarker,e,s.length),se(n,t,null,s);const r=e,i=ee(t,e);let o=t._start;for(i!==null&&(o=i.p,e-=i.index,e===0&&(o=o.prev,e+=o&&o.countable&&!o.deleted?o.length:0));o!==null;o=o.right)if(!o.deleted&&o.countable){if(e<=o.length){e<o.length&&Q(n,y(o.id.client,o.id.clock+e));break}e-=o.length}return t._searchMarker&&Ot(t._searchMarker,r,s.length),se(n,t,o,s)},Ri=(n,t,e)=>{let r=(t._searchMarker||[]).reduce((i,o)=>o.index>i.index?o:i,{index:0,p:t._start}).p;if(r)for(;r.right;)r=r.right;return se(n,t,r,e)},as=(n,t,e,s)=>{if(s===0)return;const r=e,i=s,o=ee(t,e);let l=t._start;for(o!==null&&(l=o.p,e-=o.index);l!==null&&e>0;l=l.right)!l.deleted&&l.countable&&(e<l.length&&Q(n,y(l.id.client,l.id.clock+e)),e-=l.length);for(;s>0&&l!==null;)l.deleted||(s<l.length&&Q(n,y(l.id.client,l.id.clock+s)),l.delete(n),s-=l.length),l=l.right;if(s>0)throw cs;t._searchMarker&&Ot(t._searchMarker,r,-i+s)},re=(n,t,e)=>{const s=t._map.get(e);s!==void 0&&s.delete(n)},Je=(n,t,e,s)=>{const r=t._map.get(e)||null,i=n.doc,o=i.clientID;let l;if(s==null)l=new ct([s]);else switch(s.constructor){case Number:case Object:case Boolean:case Array:case String:l=new ct([s]);break;case Uint8Array:l=new Vt(s);break;case wt:l=new Ht(s);break;default:if(s instanceof L)l=new W(s);else throw new Error("Unexpected content type")}new E(y(o,A(i.store,o)),r,r&&r.lastId,null,null,t,e,l).integrate(n,0)},Ge=(n,t)=>{const e=n._map.get(t);return e!==void 0&&!e.deleted?e.content.getContent()[e.length-1]:void 0},us=n=>{const t={};return n._map.forEach((e,s)=>{e.deleted||(t[s]=e.content.getContent()[e.length-1])}),t},ds=(n,t)=>{const e=n._map.get(t);return e!==void 0&&!e.deleted},ie=n=>Pr(n.entries(),t=>!t[1].deleted);class Ni extends te{constructor(t,e){super(t,e),this._transaction=e}}class kt extends L{constructor(){super(),this._prelimContent=[],this._searchMarker=[]}static from(t){const e=new kt;return e.push(t),e}_integrate(t,e){super._integrate(t,e),this.insert(0,this._prelimContent),this._prelimContent=null}_copy(){return new kt}clone(){const t=new kt;return t.insert(0,this.toArray().map(e=>e instanceof L?e.clone():e)),t}get length(){return this._prelimContent===null?this._length:this._prelimContent.length}_callObserver(t,e){super._callObserver(t,e),ne(this,t,new Ni(this,t))}insert(t,e){this.doc!==null?b(this.doc,s=>{hs(s,this,t,e)}):this._prelimContent.splice(t,0,...e)}push(t){this.doc!==null?b(this.doc,e=>{Ri(e,this,t)}):this._prelimContent.push(...t)}unshift(t){this.insert(0,t)}delete(t,e=1){this.doc!==null?b(this.doc,s=>{as(s,this,t,e)}):this._prelimContent.splice(t,e)}get(t){return ls(this,t)}toArray(){return is(this)}slice(t=0,e=this.length){return rs(this,t,e)}toJSON(){return this.map(t=>t instanceof L?t.toJSON():t)}map(t){return os(this,t)}forEach(t){Rt(this,t)}[Symbol.iterator](){return Oi(this)}_write(t){t.writeTypeRef(oo)}}const Vi=n=>new kt;class Bi extends te{constructor(t,e,s){super(t,e),this.keysChanged=s}}class bt extends L{constructor(t){super(),this._prelimContent=null,t===void 0?this._prelimContent=new Map:this._prelimContent=new Map(t)}_integrate(t,e){super._integrate(t,e),this._prelimContent.forEach((s,r)=>{this.set(r,s)}),this._prelimContent=null}_copy(){return new bt}clone(){const t=new bt;return this.forEach((e,s)=>{t.set(s,e instanceof L?e.clone():e)}),t}_callObserver(t,e){ne(this,t,new Bi(this,t,e))}toJSON(){const t={};return this._map.forEach((e,s)=>{if(!e.deleted){const r=e.content.getContent()[e.length-1];t[s]=r instanceof L?r.toJSON():r}}),t}get size(){return[...ie(this._map)].length}keys(){return Te(ie(this._map),t=>t[0])}values(){return Te(ie(this._map),t=>t[1].content.getContent()[t[1].length-1])}entries(){return Te(ie(this._map),t=>[t[0],t[1].content.getContent()[t[1].length-1]])}forEach(t){this._map.forEach((e,s)=>{e.deleted||t(e.content.getContent()[e.length-1],s,this)})}[Symbol.iterator](){return this.entries()}delete(t){this.doc!==null?b(this.doc,e=>{re(e,this,t)}):this._prelimContent.delete(t)}set(t,e){return this.doc!==null?b(this.doc,s=>{Je(s,this,t,e)}):this._prelimContent.set(t,e),e}get(t){return Ge(this,t)}has(t){return ds(this,t)}clear(){this.doc!==null?b(this.doc,t=>{this.forEach(function(e,s,r){re(t,r,s)})}):this._prelimContent.clear()}_write(t){t.writeTypeRef(lo)}}const Hi=n=>new bt,P=(n,t)=>n===t||typeof n=="object"&&typeof t=="object"&&n&&t&&rr(n,t);class We{constructor(t,e,s,r){this.left=t,this.right=e,this.index=s,this.currentAttributes=r}forward(){switch(this.right===null&&B(),this.right.content.constructor){case D:this.right.deleted||_t(this.currentAttributes,this.right.content);break;default:this.right.deleted||(this.index+=this.right.length);break}this.left=this.right,this.right=this.right.right}}const fs=(n,t,e)=>{for(;t.right!==null&&e>0;){switch(t.right.content.constructor){case D:t.right.deleted||_t(t.currentAttributes,t.right.content);break;default:t.right.deleted||(e<t.right.length&&Q(n,y(t.right.id.client,t.right.id.clock+e)),t.index+=t.right.length,e-=t.right.length);break}t.left=t.right,t.right=t.right.right}return t},oe=(n,t,e)=>{const s=new Map,r=ee(t,e);if(r){const i=new We(r.p.left,r.p,r.index,s);return fs(n,i,e-r.index)}else{const i=new We(null,t._start,0,s);return fs(n,i,e)}},gs=(n,t,e,s)=>{for(;e.right!==null&&(e.right.deleted===!0||e.right.content.constructor===D&&P(s.get(e.right.content.key),e.right.content.value));)e.right.deleted||s.delete(e.right.content.key),e.forward();const r=n.doc,i=r.clientID;s.forEach((o,l)=>{const c=e.left,a=e.right,h=new E(y(i,A(r.store,i)),c,c&&c.lastId,a,a&&a.id,t,null,new D(l,o));h.integrate(n,0),e.right=h,e.forward()})},_t=(n,t)=>{const{key:e,value:s}=t;s===null?n.delete(e):n.set(e,s)},ps=(n,t)=>{for(;n.right!==null;){if(!(n.right.deleted||n.right.content.constructor===D&&P(t[n.right.content.key]||null,n.right.content.value)))break;n.forward()}},ws=(n,t,e,s)=>{const r=n.doc,i=r.clientID,o=new Map;for(const l in s){const c=s[l],a=e.currentAttributes.get(l)||null;if(!P(a,c)){o.set(l,a);const{left:h,right:u}=e;e.right=new E(y(i,A(r.store,i)),h,h&&h.lastId,u,u&&u.id,t,null,new D(l,c)),e.right.integrate(n,0),e.forward()}}return o},ze=(n,t,e,s,r)=>{e.currentAttributes.forEach((d,f)=>{r[f]===void 0&&(r[f]=null)});const i=n.doc,o=i.clientID;ps(e,r);const l=ws(n,t,e,r),c=s.constructor===String?new F(s):s instanceof L?new W(s):new lt(s);let{left:a,right:h,index:u}=e;t._searchMarker&&Ot(t._searchMarker,e.index,c.getLength()),h=new E(y(o,A(i.store,o)),a,a&&a.lastId,h,h&&h.id,t,null,c),h.integrate(n,0),e.right=h,e.index=u,e.forward(),gs(n,t,e,l)},ms=(n,t,e,s,r)=>{const i=n.doc,o=i.clientID;ps(e,r);const l=ws(n,t,e,r);t:for(;e.right!==null&&(s>0||l.size>0&&(e.right.deleted||e.right.content.constructor===D));){if(!e.right.deleted)switch(e.right.content.constructor){case D:{const{key:c,value:a}=e.right.content,h=r[c];if(h!==void 0){if(P(h,a))l.delete(c);else{if(s===0)break t;l.set(c,a)}e.right.delete(n)}else e.currentAttributes.set(c,a);break}default:s<e.right.length&&Q(n,y(e.right.id.client,e.right.id.clock+s)),s-=e.right.length;break}e.forward()}if(s>0){let c="";for(;s>0;s--)c+=`
2
+ `;e.right=new E(y(o,A(i.store,o)),e.left,e.left&&e.left.lastId,e.right,e.right&&e.right.id,t,null,new F(c)),e.right.integrate(n,0),e.forward()}gs(n,t,e,l)},ys=(n,t,e,s,r)=>{let i=t;const o=_();for(;i&&(!i.countable||i.deleted);){if(!i.deleted&&i.content.constructor===D){const a=i.content;o.set(a.key,a)}i=i.right}let l=0,c=!1;for(;t!==i;){if(e===t&&(c=!0),!t.deleted){const a=t.content;switch(a.constructor){case D:{const{key:h,value:u}=a,d=s.get(h)||null;(o.get(h)!==a||d===u)&&(t.delete(n),l++,!c&&(r.get(h)||null)===u&&d!==u&&(d===null?r.delete(h):r.set(h,d))),!c&&!t.deleted&&_t(r,a);break}}}t=t.right}return l},Fi=(n,t)=>{for(;t&&t.right&&(t.right.deleted||!t.right.countable);)t=t.right;const e=new Set;for(;t&&(t.deleted||!t.countable);){if(!t.deleted&&t.content.constructor===D){const s=t.content.key;e.has(s)?t.delete(n):e.add(s)}t=t.left}},ji=n=>{let t=0;return b(n.doc,e=>{let s=n._start,r=n._start,i=_();const o=j(i);for(;r;){if(r.deleted===!1)switch(r.content.constructor){case D:_t(o,r.content);break;default:t+=ys(e,s,r,i,o),i=j(o),s=r;break}r=r.right}}),t},$i=n=>{const t=new Set,e=n.doc;for(const[s,r]of n.afterState.entries()){const i=n.beforeState.get(s)||0;r!==i&&Qn(n,e.store.clients.get(s),i,r,o=>{!o.deleted&&o.content.constructor===D&&o.constructor!==T&&t.add(o.parent)})}b(e,s=>{Tn(n,n.deleteSet,r=>{if(r instanceof T||!r.parent._hasFormatting||t.has(r.parent))return;const i=r.parent;r.content.constructor===D?t.add(i):Fi(s,r)});for(const r of t)ji(r)})},ks=(n,t,e)=>{const s=e,r=j(t.currentAttributes),i=t.right;for(;e>0&&t.right!==null;){if(t.right.deleted===!1)switch(t.right.content.constructor){case W:case lt:case F:e<t.right.length&&Q(n,y(t.right.id.client,t.right.id.clock+e)),e-=t.right.length,t.right.delete(n);break}t.forward()}i&&ys(n,i,t.right,r,t.currentAttributes);const o=(t.left||t.right).parent;return o._searchMarker&&Ot(o._searchMarker,t.index,-s+e),t};class Yi extends te{constructor(t,e,s){super(t,e),this.childListChanged=!1,this.keysChanged=new Set,s.forEach(r=>{r===null?this.childListChanged=!0:this.keysChanged.add(r)})}get changes(){if(this._changes===null){const t={keys:this.keys,delta:this.delta,added:new Set,deleted:new Set};this._changes=t}return this._changes}get delta(){if(this._delta===null){const t=this.target.doc,e=[];b(t,s=>{const r=new Map,i=new Map;let o=this.target._start,l=null;const c={};let a="",h=0,u=0;const d=()=>{if(l!==null){let f=null;switch(l){case"delete":u>0&&(f={delete:u}),u=0;break;case"insert":(typeof a=="object"||a.length>0)&&(f={insert:a},r.size>0&&(f.attributes={},r.forEach((g,k)=>{g!==null&&(f.attributes[k]=g)}))),a="";break;case"retain":h>0&&(f={retain:h},nr(c)||(f.attributes=Zs({},c))),h=0;break}f&&e.push(f),l=null}};for(;o!==null;){switch(o.content.constructor){case W:case lt:this.adds(o)?this.deletes(o)||(d(),l="insert",a=o.content.getContent()[0],d()):this.deletes(o)?(l!=="delete"&&(d(),l="delete"),u+=1):o.deleted||(l!=="retain"&&(d(),l="retain"),h+=1);break;case F:this.adds(o)?this.deletes(o)||(l!=="insert"&&(d(),l="insert"),a+=o.content.str):this.deletes(o)?(l!=="delete"&&(d(),l="delete"),u+=o.length):o.deleted||(l!=="retain"&&(d(),l="retain"),h+=o.length);break;case D:{const{key:f,value:g}=o.content;if(this.adds(o)){if(!this.deletes(o)){const k=r.get(f)||null;P(k,g)?g!==null&&o.delete(s):(l==="retain"&&d(),P(g,i.get(f)||null)?delete c[f]:c[f]=g)}}else if(this.deletes(o)){i.set(f,g);const k=r.get(f)||null;P(k,g)||(l==="retain"&&d(),c[f]=k)}else if(!o.deleted){i.set(f,g);const k=c[f];k!==void 0&&(P(k,g)?k!==null&&o.delete(s):(l==="retain"&&d(),g===null?delete c[f]:c[f]=g))}o.deleted||(l==="insert"&&d(),_t(r,o.content));break}}o=o.right}for(d();e.length>0;){const f=e[e.length-1];if(f.retain!==void 0&&f.attributes===void 0)e.pop();else break}}),this._delta=e}return this._delta}}class St extends L{constructor(t){super(),this._pending=t!==void 0?[()=>this.insert(0,t)]:[],this._searchMarker=[],this._hasFormatting=!1}get length(){return this._length}_integrate(t,e){super._integrate(t,e);try{this._pending.forEach(s=>s())}catch(s){console.error(s)}this._pending=null}_copy(){return new St}clone(){const t=new St;return t.applyDelta(this.toDelta()),t}_callObserver(t,e){super._callObserver(t,e);const s=new Yi(this,t,e);ne(this,t,s),!t.local&&this._hasFormatting&&(t._needFormattingCleanup=!0)}toString(){let t="",e=this._start;for(;e!==null;)!e.deleted&&e.countable&&e.content.constructor===F&&(t+=e.content.str),e=e.right;return t}toJSON(){return this.toString()}applyDelta(t,{sanitize:e=!0}={}){this.doc!==null?b(this.doc,s=>{const r=new We(null,this._start,0,new Map);for(let i=0;i<t.length;i++){const o=t[i];if(o.insert!==void 0){const l=!e&&typeof o.insert=="string"&&i===t.length-1&&r.right===null&&o.insert.slice(-1)===`
3
+ `?o.insert.slice(0,-1):o.insert;(typeof l!="string"||l.length>0)&&ze(s,this,r,l,o.attributes||{})}else o.retain!==void 0?ms(s,this,r,o.retain,o.attributes||{}):o.delete!==void 0&&ks(s,r,o.delete)}}):this._pending.push(()=>this.applyDelta(t))}toDelta(t,e,s){const r=[],i=new Map,o=this.doc;let l="",c=this._start;function a(){if(l.length>0){const u={};let d=!1;i.forEach((g,k)=>{d=!0,u[k]=g});const f={insert:l};d&&(f.attributes=u),r.push(f),l=""}}const h=()=>{for(;c!==null;){if(Tt(c,t)||e!==void 0&&Tt(c,e))switch(c.content.constructor){case F:{const u=i.get("ychange");t!==void 0&&!Tt(c,t)?(u===void 0||u.user!==c.id.client||u.type!=="removed")&&(a(),i.set("ychange",s?s("removed",c.id):{type:"removed"})):e!==void 0&&!Tt(c,e)?(u===void 0||u.user!==c.id.client||u.type!=="added")&&(a(),i.set("ychange",s?s("added",c.id):{type:"added"})):u!==void 0&&(a(),i.delete("ychange")),l+=c.content.str;break}case W:case lt:{a();const u={insert:c.content.getContent()[0]};if(i.size>0){const d={};u.attributes=d,i.forEach((f,g)=>{d[g]=f})}r.push(u);break}case D:Tt(c,t)&&(a(),_t(i,c.content));break}c=c.right}a()};return t||e?b(o,u=>{t&&Be(u,t),e&&Be(u,e),h()},"cleanup"):h(),r}insert(t,e,s){if(e.length<=0)return;const r=this.doc;r!==null?b(r,i=>{const o=oe(i,this,t);s||(s={},o.currentAttributes.forEach((l,c)=>{s[c]=l})),ze(i,this,o,e,s)}):this._pending.push(()=>this.insert(t,e,s))}insertEmbed(t,e,s={}){const r=this.doc;r!==null?b(r,i=>{const o=oe(i,this,t);ze(i,this,o,e,s)}):this._pending.push(()=>this.insertEmbed(t,e,s))}delete(t,e){if(e===0)return;const s=this.doc;s!==null?b(s,r=>{ks(r,oe(r,this,t),e)}):this._pending.push(()=>this.delete(t,e))}format(t,e,s){if(e===0)return;const r=this.doc;r!==null?b(r,i=>{const o=oe(i,this,t);o.right!==null&&ms(i,this,o,e,s)}):this._pending.push(()=>this.format(t,e,s))}removeAttribute(t){this.doc!==null?b(this.doc,e=>{re(e,this,t)}):this._pending.push(()=>this.removeAttribute(t))}setAttribute(t,e){this.doc!==null?b(this.doc,s=>{Je(s,this,t,e)}):this._pending.push(()=>this.setAttribute(t,e))}getAttribute(t){return Ge(this,t)}getAttributes(){return us(this)}_write(t){t.writeTypeRef(co)}}const Xi=n=>new St;class Ke{constructor(t,e=()=>!0){this._filter=e,this._root=t,this._currentNode=t._start,this._firstCall=!0}[Symbol.iterator](){return this}next(){let t=this._currentNode,e=t&&t.content&&t.content.type;if(t!==null&&(!this._firstCall||t.deleted||!this._filter(e)))do if(e=t.content.type,!t.deleted&&(e.constructor===Nt||e.constructor===ot)&&e._start!==null)t=e._start;else for(;t!==null;)if(t.right!==null){t=t.right;break}else t.parent===this._root?t=null:t=t.parent._item;while(t!==null&&(t.deleted||!this._filter(t.content.type)));return this._firstCall=!1,t===null?{value:void 0,done:!0}:(this._currentNode=t,{value:t.content.type,done:!1})}}class ot extends L{constructor(){super(),this._prelimContent=[]}get firstChild(){const t=this._first;return t?t.content.getContent()[0]:null}_integrate(t,e){super._integrate(t,e),this.insert(0,this._prelimContent),this._prelimContent=null}_copy(){return new ot}clone(){const t=new ot;return t.insert(0,this.toArray().map(e=>e instanceof L?e.clone():e)),t}get length(){return this._prelimContent===null?this._length:this._prelimContent.length}createTreeWalker(t){return new Ke(this,t)}querySelector(t){t=t.toUpperCase();const s=new Ke(this,r=>r.nodeName&&r.nodeName.toUpperCase()===t).next();return s.done?null:s.value}querySelectorAll(t){return t=t.toUpperCase(),z(new Ke(this,e=>e.nodeName&&e.nodeName.toUpperCase()===t))}_callObserver(t,e){ne(this,t,new Wi(this,e,t))}toString(){return os(this,t=>t.toString()).join("")}toJSON(){return this.toString()}toDOM(t=document,e={},s){const r=t.createDocumentFragment();return s!==void 0&&s._createAssociation(r,this),Rt(this,i=>{r.insertBefore(i.toDOM(t,e,s),null)}),r}insert(t,e){this.doc!==null?b(this.doc,s=>{hs(s,this,t,e)}):this._prelimContent.splice(t,0,...e)}insertAfter(t,e){if(this.doc!==null)b(this.doc,s=>{const r=t&&t instanceof L?t._item:t;se(s,this,r,e)});else{const s=this._prelimContent,r=t===null?0:s.findIndex(i=>i===t)+1;if(r===0&&t!==null)throw dt("Reference item not found");s.splice(r,0,...e)}}delete(t,e=1){this.doc!==null?b(this.doc,s=>{as(s,this,t,e)}):this._prelimContent.splice(t,e)}toArray(){return is(this)}push(t){this.insert(this.length,t)}unshift(t){this.insert(0,t)}get(t){return ls(this,t)}slice(t=0,e=this.length){return rs(this,t,e)}forEach(t){Rt(this,t)}_write(t){t.writeTypeRef(ao)}}const Ji=n=>new ot;class Nt extends ot{constructor(t="UNDEFINED"){super(),this.nodeName=t,this._prelimAttrs=new Map}get nextSibling(){const t=this._item?this._item.next:null;return t?t.content.type:null}get prevSibling(){const t=this._item?this._item.prev:null;return t?t.content.type:null}_integrate(t,e){super._integrate(t,e),this._prelimAttrs.forEach((s,r)=>{this.setAttribute(r,s)}),this._prelimAttrs=null}_copy(){return new Nt(this.nodeName)}clone(){const t=new Nt(this.nodeName),e=this.getAttributes();return tr(e,(s,r)=>{typeof s=="string"&&t.setAttribute(r,s)}),t.insert(0,this.toArray().map(s=>s instanceof L?s.clone():s)),t}toString(){const t=this.getAttributes(),e=[],s=[];for(const l in t)s.push(l);s.sort();const r=s.length;for(let l=0;l<r;l++){const c=s[l];e.push(c+'="'+t[c]+'"')}const i=this.nodeName.toLocaleLowerCase(),o=e.length>0?" "+e.join(" "):"";return`<${i}${o}>${super.toString()}</${i}>`}removeAttribute(t){this.doc!==null?b(this.doc,e=>{re(e,this,t)}):this._prelimAttrs.delete(t)}setAttribute(t,e){this.doc!==null?b(this.doc,s=>{Je(s,this,t,e)}):this._prelimAttrs.set(t,e)}getAttribute(t){return Ge(this,t)}hasAttribute(t){return ds(this,t)}getAttributes(){return us(this)}toDOM(t=document,e={},s){const r=t.createElement(this.nodeName),i=this.getAttributes();for(const o in i){const l=i[o];typeof l=="string"&&r.setAttribute(o,l)}return Rt(this,o=>{r.appendChild(o.toDOM(t,e,s))}),s!==void 0&&s._createAssociation(r,this),r}_write(t){t.writeTypeRef(ho),t.writeKey(this.nodeName)}}const Gi=n=>new Nt(n.readKey());class Wi extends te{constructor(t,e,s){super(t,s),this.childListChanged=!1,this.attributesChanged=new Set,e.forEach(r=>{r===null?this.childListChanged=!0:this.attributesChanged.add(r)})}}class le extends bt{constructor(t){super(),this.hookName=t}_copy(){return new le(this.hookName)}clone(){const t=new le(this.hookName);return this.forEach((e,s)=>{t.set(s,e)}),t}toDOM(t=document,e={},s){const r=e[this.hookName];let i;return r!==void 0?i=r.createDom(this):i=document.createElement(this.hookName),i.setAttribute("data-yjs-hook",this.hookName),s!==void 0&&s._createAssociation(i,this),i}_write(t){t.writeTypeRef(uo),t.writeKey(this.hookName)}}const zi=n=>new le(n.readKey());class ce extends St{get nextSibling(){const t=this._item?this._item.next:null;return t?t.content.type:null}get prevSibling(){const t=this._item?this._item.prev:null;return t?t.content.type:null}_copy(){return new ce}clone(){const t=new ce;return t.applyDelta(this.toDelta()),t}toDOM(t=document,e,s){const r=t.createTextNode(this.toString());return s!==void 0&&s._createAssociation(r,this),r}toString(){return this.toDelta().map(t=>{const e=[];for(const r in t.attributes){const i=[];for(const o in t.attributes[r])i.push({key:o,value:t.attributes[r][o]});i.sort((o,l)=>o.key<l.key?-1:1),e.push({nodeName:r,attrs:i})}e.sort((r,i)=>r.nodeName<i.nodeName?-1:1);let s="";for(let r=0;r<e.length;r++){const i=e[r];s+=`<${i.nodeName}`;for(let o=0;o<i.attrs.length;o++){const l=i.attrs[o];s+=` ${l.key}="${l.value}"`}s+=">"}s+=t.insert;for(let r=e.length-1;r>=0;r--)s+=`</${e[r].nodeName}>`;return s}).join("")}toJSON(){return this.toString()}_write(t){t.writeTypeRef(fo)}}const Ki=n=>new ce;class qe{constructor(t,e){this.id=t,this.length=e}get deleted(){throw V()}mergeWith(t){return!1}write(t,e,s){throw V()}integrate(t,e){throw V()}}const qi=0;class T extends qe{get deleted(){return!0}delete(){}mergeWith(t){return this.constructor!==t.constructor?!1:(this.length+=t.length,!0)}integrate(t,e){e>0&&(this.id.clock+=e,this.length-=e),Kn(t.doc.store,this)}write(t,e){t.writeInfo(qi),t.writeLen(this.length-e)}getMissing(t,e){return null}}class Vt{constructor(t){this.content=t}getLength(){return 1}getContent(){return[this.content]}isCountable(){return!0}copy(){return new Vt(this.content)}splice(t){throw V()}mergeWith(t){return!1}integrate(t,e){}delete(t){}gc(t){}write(t,e){t.writeBuf(this.content)}getRef(){return 3}}const Qi=n=>new Vt(n.readBuf());class Bt{constructor(t){this.len=t}getLength(){return this.len}getContent(){return[]}isCountable(){return!1}copy(){return new Bt(this.len)}splice(t){const e=new Bt(this.len-t);return this.len=t,e}mergeWith(t){return this.len+=t.len,!0}integrate(t,e){Kt(t.deleteSet,e.id.client,e.id.clock,this.len),e.markDeleted()}delete(t){}gc(t){}write(t,e){t.writeLen(this.len-e)}getRef(){return 1}}const Zi=n=>new Bt(n.readLen()),bs=(n,t)=>new wt({guid:n,...t,shouldLoad:t.shouldLoad||t.autoLoad||!1});class Ht{constructor(t){t._item&&console.error("This document was already integrated as a sub-document. You should create a second instance instead with the same guid."),this.doc=t;const e={};this.opts=e,t.gc||(e.gc=!1),t.autoLoad&&(e.autoLoad=!0),t.meta!==null&&(e.meta=t.meta)}getLength(){return 1}getContent(){return[this.doc]}isCountable(){return!0}copy(){return new Ht(bs(this.doc.guid,this.opts))}splice(t){throw V()}mergeWith(t){return!1}integrate(t,e){this.doc._item=e,t.subdocsAdded.add(this.doc),this.doc.shouldLoad&&t.subdocsLoaded.add(this.doc)}delete(t){t.subdocsAdded.has(this.doc)?t.subdocsAdded.delete(this.doc):t.subdocsRemoved.add(this.doc)}gc(t){}write(t,e){t.writeString(this.doc.guid),t.writeAny(this.opts)}getRef(){return 9}}const Pi=n=>new Ht(bs(n.readString(),n.readAny()));class lt{constructor(t){this.embed=t}getLength(){return 1}getContent(){return[this.embed]}isCountable(){return!0}copy(){return new lt(this.embed)}splice(t){throw V()}mergeWith(t){return!1}integrate(t,e){}delete(t){}gc(t){}write(t,e){t.writeJSON(this.embed)}getRef(){return 5}}const to=n=>new lt(n.readJSON());class D{constructor(t,e){this.key=t,this.value=e}getLength(){return 1}getContent(){return[]}isCountable(){return!1}copy(){return new D(this.key,this.value)}splice(t){throw V()}mergeWith(t){return!1}integrate(t,e){const s=e.parent;s._searchMarker=null,s._hasFormatting=!0}delete(t){}gc(t){}write(t,e){t.writeKey(this.key),t.writeJSON(this.value)}getRef(){return 6}}const eo=n=>new D(n.readKey(),n.readJSON());class he{constructor(t){this.arr=t}getLength(){return this.arr.length}getContent(){return this.arr}isCountable(){return!0}copy(){return new he(this.arr)}splice(t){const e=new he(this.arr.slice(t));return this.arr=this.arr.slice(0,t),e}mergeWith(t){return this.arr=this.arr.concat(t.arr),!0}integrate(t,e){}delete(t){}gc(t){}write(t,e){const s=this.arr.length;t.writeLen(s-e);for(let r=e;r<s;r++){const i=this.arr[r];t.writeString(i===void 0?"undefined":JSON.stringify(i))}}getRef(){return 2}}const no=n=>{const t=n.readLen(),e=[];for(let s=0;s<t;s++){const r=n.readString();r==="undefined"?e.push(void 0):e.push(JSON.parse(r))}return new he(e)};class ct{constructor(t){this.arr=t}getLength(){return this.arr.length}getContent(){return this.arr}isCountable(){return!0}copy(){return new ct(this.arr)}splice(t){const e=new ct(this.arr.slice(t));return this.arr=this.arr.slice(0,t),e}mergeWith(t){return this.arr=this.arr.concat(t.arr),!0}integrate(t,e){}delete(t){}gc(t){}write(t,e){const s=this.arr.length;t.writeLen(s-e);for(let r=e;r<s;r++){const i=this.arr[r];t.writeAny(i)}}getRef(){return 8}}const so=n=>{const t=n.readLen(),e=[];for(let s=0;s<t;s++)e.push(n.readAny());return new ct(e)};class F{constructor(t){this.str=t}getLength(){return this.str.length}getContent(){return this.str.split("")}isCountable(){return!0}copy(){return new F(this.str)}splice(t){const e=new F(this.str.slice(t));this.str=this.str.slice(0,t);const s=this.str.charCodeAt(t-1);return s>=55296&&s<=56319&&(this.str=this.str.slice(0,t-1)+"�",e.str="�"+e.str.slice(1)),e}mergeWith(t){return this.str+=t.str,!0}integrate(t,e){}delete(t){}gc(t){}write(t,e){t.writeString(e===0?this.str:this.str.slice(e))}getRef(){return 4}}const ro=n=>new F(n.readString()),io=[Vi,Hi,Xi,Gi,Ji,zi,Ki],oo=0,lo=1,co=2,ho=3,ao=4,uo=5,fo=6;class W{constructor(t){this.type=t}getLength(){return 1}getContent(){return[this.type]}isCountable(){return!0}copy(){return new W(this.type._copy())}splice(t){throw V()}mergeWith(t){return!1}integrate(t,e){this.type._integrate(t.doc,e)}delete(t){let e=this.type._start;for(;e!==null;)e.deleted?t._mergeStructs.push(e):e.delete(t),e=e.right;this.type._map.forEach(s=>{s.deleted?t._mergeStructs.push(s):s.delete(t)}),t.changed.delete(this.type)}gc(t){let e=this.type._start;for(;e!==null;)e.gc(t,!0),e=e.right;this.type._start=null,this.type._map.forEach(s=>{for(;s!==null;)s.gc(t,!0),s=s.left}),this.type._map=new Map}write(t,e){this.type._write(t)}getRef(){return 7}}const go=n=>new W(io[n.readTypeRef()](n)),ae=(n,t,e)=>{const{client:s,clock:r}=t.id,i=new E(y(s,r+e),t,y(s,r+e-1),t.right,t.rightOrigin,t.parent,t.parentSub,t.content.splice(e));return t.deleted&&i.markDeleted(),t.keep&&(i.keep=!0),t.redone!==null&&(i.redone=y(t.redone.client,t.redone.clock+e)),t.right=i,i.right!==null&&(i.right.left=i),n._mergeStructs.push(i),i.parentSub!==null&&i.right===null&&i.parent._map.set(i.parentSub,i),t.length=e,i};class E extends qe{constructor(t,e,s,r,i,o,l,c){super(t,c.getLength()),this.origin=s,this.left=e,this.right=r,this.rightOrigin=i,this.parent=o,this.parentSub=l,this.redone=null,this.content=c,this.info=this.content.isCountable()?pn:0}set marker(t){(this.info&ke)>0!==t&&(this.info^=ke)}get marker(){return(this.info&ke)>0}get keep(){return(this.info&gn)>0}set keep(t){this.keep!==t&&(this.info^=gn)}get countable(){return(this.info&pn)>0}get deleted(){return(this.info&ye)>0}set deleted(t){this.deleted!==t&&(this.info^=ye)}markDeleted(){this.info|=ye}getMissing(t,e){if(this.origin&&this.origin.client!==this.id.client&&this.origin.clock>=A(e,this.origin.client))return this.origin.client;if(this.rightOrigin&&this.rightOrigin.client!==this.id.client&&this.rightOrigin.clock>=A(e,this.rightOrigin.client))return this.rightOrigin.client;if(this.parent&&this.parent.constructor===yt&&this.id.client!==this.parent.client&&this.parent.clock>=A(e,this.parent.client))return this.parent.client;if(this.origin&&(this.left=qn(t,e,this.origin),this.origin=this.left.lastId),this.rightOrigin&&(this.right=Q(t,this.rightOrigin),this.rightOrigin=this.right.id),(this.left&&this.left.constructor===T||this.right&&this.right.constructor===T)&&(this.parent=null),!this.parent)this.left&&this.left.constructor===E&&(this.parent=this.left.parent,this.parentSub=this.left.parentSub),this.right&&this.right.constructor===E&&(this.parent=this.right.parent,this.parentSub=this.right.parentSub);else if(this.parent.constructor===yt){const s=He(e,this.parent);s.constructor===T?this.parent=null:this.parent=s.content.type}return null}integrate(t,e){if(e>0&&(this.id.clock+=e,this.left=qn(t,t.doc.store,y(this.id.client,this.id.clock-1)),this.origin=this.left.lastId,this.content=this.content.splice(e),this.length-=e),this.parent){if(!this.left&&(!this.right||this.right.left!==null)||this.left&&this.left.right!==this.right){let s=this.left,r;if(s!==null)r=s.right;else if(this.parentSub!==null)for(r=this.parent._map.get(this.parentSub)||null;r!==null&&r.left!==null;)r=r.left;else r=this.parent._start;const i=new Set,o=new Set;for(;r!==null&&r!==this.right;){if(o.add(r),i.add(r),qt(this.origin,r.origin)){if(r.id.client<this.id.client)s=r,i.clear();else if(qt(this.rightOrigin,r.rightOrigin))break}else if(r.origin!==null&&o.has(He(t.doc.store,r.origin)))i.has(He(t.doc.store,r.origin))||(s=r,i.clear());else break;r=r.right}this.left=s}if(this.left!==null){const s=this.left.right;this.right=s,this.left.right=this}else{let s;if(this.parentSub!==null)for(s=this.parent._map.get(this.parentSub)||null;s!==null&&s.left!==null;)s=s.left;else s=this.parent._start,this.parent._start=this;this.right=s}this.right!==null?this.right.left=this:this.parentSub!==null&&(this.parent._map.set(this.parentSub,this),this.left!==null&&this.left.delete(t)),this.parentSub===null&&this.countable&&!this.deleted&&(this.parent._length+=this.length),Kn(t.doc.store,this),this.content.integrate(t,this),Pn(t,this.parent,this.parentSub),(this.parent._item!==null&&this.parent._item.deleted||this.parentSub!==null&&this.right!==null)&&this.delete(t)}else new T(this.id,this.length).integrate(t,0)}get next(){let t=this.right;for(;t!==null&&t.deleted;)t=t.right;return t}get prev(){let t=this.left;for(;t!==null&&t.deleted;)t=t.left;return t}get lastId(){return this.length===1?this.id:y(this.id.client,this.id.clock+this.length-1)}mergeWith(t){if(this.constructor===t.constructor&&qt(t.origin,this.lastId)&&this.right===t&&qt(this.rightOrigin,t.rightOrigin)&&this.id.client===t.id.client&&this.id.clock+this.length===t.id.clock&&this.deleted===t.deleted&&this.redone===null&&t.redone===null&&this.content.constructor===t.content.constructor&&this.content.mergeWith(t.content)){const e=this.parent._searchMarker;return e&&e.forEach(s=>{s.p===t&&(s.p=this,!this.deleted&&this.countable&&(s.index-=this.length))}),t.keep&&(this.keep=!0),this.right=t.right,this.right!==null&&(this.right.left=this),this.length+=t.length,!0}return!1}delete(t){if(!this.deleted){const e=this.parent;this.countable&&this.parentSub===null&&(e._length-=this.length),this.markDeleted(),Kt(t.deleteSet,this.id.client,this.id.clock,this.length),Pn(t,e,this.parentSub),this.content.delete(t)}}gc(t,e){if(!this.deleted)throw B();this.content.gc(t),e?bi(t,this,new T(this.id,this.length)):this.content=new Bt(this.length)}write(t,e){const s=e>0?y(this.id.client,this.id.clock+e-1):this.origin,r=this.rightOrigin,i=this.parentSub,o=this.content.getRef()&Xt|(s===null?0:U)|(r===null?0:X)|(i===null?0:At);if(t.writeInfo(o),s!==null&&t.writeLeftID(s),r!==null&&t.writeRightID(r),s===null&&r===null){const l=this.parent;if(l._item!==void 0){const c=l._item;if(c===null){const a=ki(l);t.writeParentInfo(!0),t.writeString(a)}else t.writeParentInfo(!1),t.writeLeftID(c.id)}else l.constructor===String?(t.writeParentInfo(!0),t.writeString(l)):l.constructor===yt?(t.writeParentInfo(!1),t.writeLeftID(l)):B();i!==null&&t.writeString(i)}this.content.write(t,e)}}const _s=(n,t)=>po[t&Xt](n),po=[()=>{B()},Zi,no,Qi,ro,to,eo,go,so,Pi,()=>{B()}],wo=10;class O extends qe{get deleted(){return!0}delete(){}mergeWith(t){return this.constructor!==t.constructor?!1:(this.length+=t.length,!0)}integrate(t,e){B()}write(t,e){t.writeInfo(wo),p(t.restEncoder,this.length-e)}getMissing(t,e){return null}}const Ss=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:{},Cs="__ $YJS$ __";Ss[Cs]===!0&&console.error("Yjs was already imported. This breaks constructor checks and will lead to issues! - https://github.com/yjs/yjs/issues/438"),Ss[Cs]=!0;const Ds=0,Qe=1,Es=2,Ze=(n,t)=>{p(n,Ds);const e=mi(t);C(n,e)},Is=(n,t,e)=>{p(n,Qe),C(n,fi(t,e))},mo=(n,t,e)=>Is(t,e,M(n)),As=(n,t,e)=>{try{ai(t,M(n),e)}catch(s){console.error("Caught error while handling a Yjs update",s)}},yo=(n,t)=>{p(n,Es),C(n,t)},ko=As,bo=(n,t,e,s)=>{const r=w(n);switch(r){case Ds:mo(n,t,e);break;case Qe:As(n,e,s);break;case Es:ko(n,e,s);break;default:throw new Error("Unknown message type")}return r},_o=0,So=(n,t,e)=>{switch(w(n)){case _o:e(t,q(n))}},Pe=3e4;class Co extends ve{constructor(t){super(),this.doc=t,this.clientID=t.clientID,this.states=new Map,this.meta=new Map,this._checkInterval=setInterval(()=>{const e=rt();this.getLocalState()!==null&&Pe/2<=e-this.meta.get(this.clientID).lastUpdated&&this.setLocalState(this.getLocalState());const s=[];this.meta.forEach((r,i)=>{i!==this.clientID&&Pe<=e-r.lastUpdated&&this.states.has(i)&&s.push(i)}),s.length>0&&tn(this,s,"timeout")},Y(Pe/10)),t.on("destroy",()=>{this.destroy()}),this.setLocalState({})}destroy(){this.emit("destroy",[this]),this.setLocalState(null),super.destroy(),clearInterval(this._checkInterval)}getLocalState(){return this.states.get(this.clientID)||null}setLocalState(t){const e=this.clientID,s=this.meta.get(e),r=s===void 0?0:s.clock+1,i=this.states.get(e);t===null?this.states.delete(e):this.states.set(e,t),this.meta.set(e,{clock:r,lastUpdated:rt()});const o=[],l=[],c=[],a=[];t===null?a.push(e):i==null?t!=null&&o.push(e):(l.push(e),It(i,t)||c.push(e)),(o.length>0||c.length>0||a.length>0)&&this.emit("change",[{added:o,updated:c,removed:a},"local"]),this.emit("update",[{added:o,updated:l,removed:a},"local"])}setLocalStateField(t,e){const s=this.getLocalState();s!==null&&this.setLocalState({...s,[t]:e})}getStates(){return this.states}}const tn=(n,t,e)=>{const s=[];for(let r=0;r<t.length;r++){const i=t[r];if(n.states.has(i)){if(n.states.delete(i),i===n.clientID){const o=n.meta.get(i);n.meta.set(i,{clock:o.clock+1,lastUpdated:rt()})}s.push(i)}}s.length>0&&(n.emit("change",[{added:[],updated:[],removed:s},e]),n.emit("update",[{added:[],updated:[],removed:s},e]))},Ft=(n,t,e=n.states)=>{const s=t.length,r=x();p(r,s);for(let i=0;i<s;i++){const o=t[i],l=e.get(o)||null,c=n.meta.get(o).clock;p(r,o),p(r,c),st(r,JSON.stringify(l))}return S(r)},Do=(n,t,e)=>{const s=K(t),r=rt(),i=[],o=[],l=[],c=[],a=w(s);for(let h=0;h<a;h++){const u=w(s);let d=w(s);const f=JSON.parse(q(s)),g=n.meta.get(u),k=n.states.get(u),R=g===void 0?0:g.clock;(R<d||R===d&&f===null&&n.states.has(u))&&(f===null?u===n.clientID&&n.getLocalState()!=null?d++:n.states.delete(u):n.states.set(u,f),n.meta.set(u,{clock:d,lastUpdated:r}),g===void 0&&f!==null?i.push(u):g!==void 0&&f===null?c.push(u):f!==null&&(It(f,k)||l.push(u),o.push(u)))}(i.length>0||l.length>0||c.length>0)&&n.emit("change",[{added:i,updated:l,removed:c},e]),(i.length>0||o.length>0||c.length>0)&&n.emit("update",[{added:i,updated:o,removed:c},e])},Eo=n=>er(n,(t,e)=>`${encodeURIComponent(e)}=${encodeURIComponent(t)}`).join("&");var ht=0,Ls=3,Ct=1,Io=2;function Ao(n,t){if(!n)throw new Error(t)}var jt=[];jt[ht]=(n,t,e,s,r)=>{p(n,ht);const i=bo(t,n,e.doc,e);s&&i===Qe&&!e.synced&&(e.synced=!0)},jt[Ls]=(n,t,e,s,r)=>{p(n,Ct),C(n,Ft(e.awareness,Array.from(e.awareness.getStates().keys())))},jt[Ct]=(n,t,e,s,r)=>{Do(e.awareness,M(t),e)},jt[Io]=(n,t,e,s,r)=>{So(t,e.doc,(i,o)=>Lo(e,o))};var xs=3e4;function Lo(n,t){console.warn(`Permission denied to access ${n.url}.
4
+ ${t}`)}function Ms(n,t,e){const s=K(t),r=x(),i=w(s),o=n.messageHandlers[i];return o?o(r,s,n,e,i):console.error("Unable to compute message"),r}function Us(n){if(n.shouldConnect&&n.ws===null){const t=new n._WS(n.url);t.binaryType="arraybuffer",n.ws=t,n.wsconnecting=!0,n.wsconnected=!1,n.synced=!1,t.onmessage=e=>{if(e.data==="ping"){t.send("pong");return}n.wsLastMessageReceived=rt();const s=Ms(n,new Uint8Array(e.data),!0);_e(s)>1&&t.send(S(s))},t.onerror=e=>{n.emit("connection-error",[e,n])},t.onclose=e=>{n.emit("connection-close",[e,n]),n.ws=null,n.wsconnecting=!1,n.wsconnected?(n.wsconnected=!1,n.synced=!1,tn(n.awareness,Array.from(n.awareness.getStates().keys()).filter(s=>s!==n.doc.clientID),n),n.emit("status",[{status:"disconnected"}])):n.wsUnsuccessfulReconnects++,setTimeout(Us,me(ar(2,n.wsUnsuccessfulReconnects)*100,n.maxBackoffTime),n)},t.onopen=()=>{n.wsLastMessageReceived=rt(),n.wsconnecting=!1,n.wsconnected=!0,n.wsUnsuccessfulReconnects=0,n.emit("status",[{status:"connected"}]);const e=x();if(p(e,ht),Ze(e,n.doc),t.send(S(e)),n.awareness.getLocalState()!==null){const s=x();p(s,Ct),C(s,Ft(n.awareness,[n.doc.clientID])),t.send(S(s))}},n.emit("status",[{status:"connecting"}])}}function en(n,t){const e=n.ws;n.wsconnected&&e&&e.readyState===e.OPEN&&e.send(t),n.bcconnected&&gt(n.bcChannel,t,n)}var xo=class extends ve{constructor(t,e,s,{connect:r=!0,awareness:i=new Co(s),params:o={},WebSocketPolyfill:l=WebSocket,resyncInterval:c=-1,maxBackoffTime:a=2500,disableBc:h=!1}={}){super();m(this,"maxBackoffTime");m(this,"bcChannel");m(this,"url");m(this,"roomname");m(this,"doc");m(this,"_WS");m(this,"awareness");m(this,"wsconnected");m(this,"wsconnecting");m(this,"bcconnected");m(this,"disableBc");m(this,"wsUnsuccessfulReconnects");m(this,"messageHandlers");m(this,"_synced");m(this,"ws");m(this,"wsLastMessageReceived");m(this,"shouldConnect");m(this,"_resyncInterval");m(this,"_bcSubscriber");m(this,"_updateHandler");m(this,"_awarenessUpdateHandler");m(this,"_unloadHandler");m(this,"_checkInterval");for(;t[t.length-1]==="/";)t=t.slice(0,t.length-1);const u=Eo(o);this.maxBackoffTime=a,this.bcChannel=t+"/"+e,this.url=t+"/"+e+(u.length===0?"":"?"+u),this.roomname=e,this.doc=s,this._WS=l,this.awareness=i,this.wsconnected=!1,this.wsconnecting=!1,this.bcconnected=!1,this.disableBc=h,this.wsUnsuccessfulReconnects=0,this.messageHandlers=jt.slice(),this._synced=!1,this.ws=null,this.wsLastMessageReceived=0,this.shouldConnect=r,this._resyncInterval=0,c>0&&(this._resyncInterval=setInterval(()=>{if(this.ws&&this.ws.readyState===WebSocket.OPEN){const d=x();p(d,ht),Ze(d,s),this.ws.send(S(d))}},c)),this._bcSubscriber=(d,f)=>{if(f!==this){const g=Ms(this,new Uint8Array(d),!1);_e(g)>1&&gt(this.bcChannel,S(g),this)}},this._updateHandler=(d,f)=>{if(f!==this){const g=x();p(g,ht),yo(g,d),en(this,S(g))}},this.doc.on("update",this._updateHandler),this._awarenessUpdateHandler=({added:d,updated:f,removed:g},k)=>{const R=d.concat(f).concat(g),rn=x();p(rn,Ct),C(rn,Ft(i,R)),en(this,S(rn))},this._unloadHandler=()=>{tn(this.awareness,[s.clientID],"window unload")},typeof window<"u"?window.addEventListener("unload",this._unloadHandler):typeof process<"u"&&process.on("exit",this._unloadHandler),i.on("update",this._awarenessUpdateHandler),this._checkInterval=setInterval(()=>{this.wsconnected&&xs<rt()-this.wsLastMessageReceived&&(Ao(this.ws!==null,"ws must not be null"),this.ws.close())},xs/10),r&&this.connect()}get synced(){return this._synced}set synced(t){this._synced!==t&&(this._synced=t,this.emit("synced",[t]),this.emit("sync",[t]))}destroy(){this._resyncInterval!==0&&clearInterval(this._resyncInterval),clearInterval(this._checkInterval),this.disconnect(),typeof window<"u"?window.removeEventListener("unload",this._unloadHandler):typeof process<"u"&&process.off("exit",this._unloadHandler),this.awareness.off("update",this._awarenessUpdateHandler),this.doc.off("update",this._updateHandler),super.destroy()}connectBc(){if(this.disableBc)return;this.bcconnected||(Nr(this.bcChannel,this._bcSubscriber),this.bcconnected=!0);const t=x();p(t,ht),Ze(t,this.doc),gt(this.bcChannel,S(t),this);const e=x();p(e,ht),Is(e,this.doc),gt(this.bcChannel,S(e),this);const s=x();p(s,Ls),gt(this.bcChannel,S(s),this);const r=x();p(r,Ct),C(r,Ft(this.awareness,[this.doc.clientID])),gt(this.bcChannel,S(r),this)}disconnectBc(){const t=x();p(t,Ct),C(t,Ft(this.awareness,[this.doc.clientID],new Map)),en(this,S(t)),this.bcconnected&&(Vr(this.bcChannel,this._bcSubscriber),this.bcconnected=!1)}disconnect(){this.shouldConnect=!1,this.disconnectBc(),this.ws!==null&&this.ws.close()}connect(){this.shouldConnect=!0,!this.wsconnected&&this.ws===null&&(Us(this),this.connectBc())}};function vs(){if(crypto.randomUUID)return crypto.randomUUID();let n=new Date().getTime(),t=typeof performance<"u"&&performance.now&&performance.now()*1e3||0;return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){let s=Math.random()*16;return n>0?(s=(n+s)%16|0,n=Math.floor(n/16)):(s=(t+s)%16|0,t=Math.floor(t/16)),(e==="x"?s:s&3|8).toString(16)})}var Mo=class extends xo{constructor(n,t,e,s={}){const r=`${n.startsWith("localhost:")||n.startsWith("127.0.0.1:")?"ws":"wss"}://${n}/party`;s.params===void 0?s.params={_pk:vs()}:s.params._pk=vs(),super(r,t,e,s)}};const Jo="";var at=(n=>(n.CanMove="can-move",n.CanSpin="can-spin",n.CanGrow="can-grow",n))(at||{});const Uo=(n,t=300)=>{let e;return function(...s){clearTimeout(e),e=setTimeout(()=>n.apply(this,s),t)}};class nn{constructor(t,e,s){m(this,"element");m(this,"_data");m(this,"onChange");m(this,"debouncedOnChange");this.element=t,this.onChange=s,this.debouncedOnChange=Uo(this.onChange,25),this._data=e,this.data=e}get data(){return this._data}set data(t){this._data=t,this.updateElement(t),this.debouncedOnChange(t)}}class Ts extends nn{constructor(e,s=0,r){super(e,s,r);m(this,"isDown");m(this,"startX");this.isDown=!1,this.startX=0,this.element.addEventListener("mousedown",i=>this.mouseDownHandler(i)),document.addEventListener("mousemove",i=>this.mouseMoveHandler(i)),document.addEventListener("mouseup",i=>this.mouseUpHandler(i))}updateElement(e){this.element.style.transform=`rotate(${e}deg)`}mouseDownHandler(e){this.isDown=!0,this.startX=e.pageX}mouseMoveHandler(e){if(!this.isDown)return;e.preventDefault();let s=Math.abs(e.pageX-this.startX)*2;e.pageX>this.startX?this.data+=s:e.pageX<this.startX&&(this.data-=s),this.startX=e.pageX}mouseUpHandler(e){this.isDown=!1}}class Os extends nn{constructor(e,s={x:0,y:0},r){super(e,s,r);m(this,"isDown");m(this,"startMouseX");m(this,"startMouseY");this.isDown=!1,this.startMouseX=0,this.startMouseY=0,this.element.addEventListener("mousedown",i=>this.mouseDownHandler(i)),document.addEventListener("mousemove",i=>this.mouseMoveHandler(i)),document.addEventListener("mouseup",i=>this.mouseUpHandler(i))}updateElement({x:e,y:s}){this.element.style.transform=`translate(${e}px, ${s}px)`}mouseDownHandler(e){this.isDown=!0,this.startMouseX=e.clientX,this.startMouseY=e.clientY}mouseMoveHandler(e){this.isDown&&(e.preventDefault(),this.data={x:this.data.x+e.clientX-this.startMouseX,y:this.data.y+e.clientY-this.startMouseY},this.startMouseX=e.clientX,this.startMouseY=e.clientY)}mouseUpHandler(e){this.isDown=!1}}class Rs extends nn{constructor(e,s=1,r,i=2){super(e,s,r);m(this,"maxScale");m(this,"originalCursor",`url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='44' height='53' viewport='0 0 100 100' style='fill:black;font-size:26px;'><text y='40%'>🚿</text></svg>")
5
+ 16 0,
6
+ auto`);m(this,"cutCursor",`url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='40' height='48' viewport='0 0 100 100' style='fill:black;font-size:24px;'><text y='50%'>✂️</text></svg>") 16 0,auto`);m(this,"isHovering",!1);this.maxScale=i,e.addEventListener("mouseenter",o=>this.mouseOverHandler(o)),e.addEventListener("mouseout",o=>{this.isHovering=!1}),e.addEventListener("click",o=>this.clickHandler(o)),document.addEventListener("keydown",o=>{this.isHovering&&this.mouseOverHandler(o)}),document.addEventListener("keyup",o=>{this.isHovering&&this.mouseOverHandler(o)})}updateElement(e){this.element.style.transform=`scale(${e})`}mouseOverHandler(e){if(this.isHovering=!0,e.altKey){if(this.data<=.5){this.element.style.cursor="not-allowed";return}this.element.style.cursor=this.cutCursor}else{if(this.data>=this.maxScale){this.element.style.cursor="not-allowed";return}this.element.style.cursor=this.originalCursor}}clickHandler(e){if(e.altKey){if(this.data<=.5)return;this.data-=.1}else{if(this.element.style.cursor=this.originalCursor,this.data>=this.maxScale)return;this.data+=.1}}}const vo=typeof PARTYKIT_HOST>"u"?"localhost:1999":PARTYKIT_HOST,ue=new wt,Ns=new Mo(vo,"yjs-demo",ue,{connect:!1});Ns.connect();function sn(n){return n.id}const Vs={[at.CanMove]:n=>{const t=ue.getMap(at.CanMove),e=new Map;function s(r,i){const o=t.get(r)||{x:0,y:0};o.x===i.x&&o.y===i.y||t.set(r,i)}for(const r of n){const i=sn(r),o=t.get(i);e.set(i,new Os(r,o,l=>s(i,l)))}t.observe(r=>{r.changes.keys.forEach((i,o)=>{if(i.action==="add")e.set(o,new Os(n[0],t.get(o),l=>s(o,l)));else if(i.action==="update"){const l=e.get(o);l.data=t.get(o)}})})},[at.CanSpin]:n=>{const t=ue.getMap(at.CanSpin),e=new Map;function s(r,i){t.get(r)!==i&&t.set(r,i)}for(const r of n){const i=sn(r),o=t.get(i)||0;e.set(i,new Ts(r,o,l=>s(i,l)))}t.observe(r=>{r.changes.keys.forEach((i,o)=>{if(i.action==="add")e.set(o,new Ts(n[0],t.get(o),l=>s(o,l)));else if(i.action==="update"){const l=e.get(o);l.data=t.get(o)}})})},[at.CanGrow]:n=>{const t=ue.getMap(at.CanGrow),e=new Map;function s(r,i){t.get(r)!==i&&t.set(r,i)}for(const r of n){const i=sn(r),o=t.get(i);e.set(i,new Rs(r,o,l=>s(i,l)))}t.observe(r=>{r.changes.keys.forEach((i,o)=>{if(i.action==="add")e.set(o,new Rs(n[0],t.get(o),l=>s(o,l)));else if(i.action==="update"){const l=e.get(o);l.data=t.get(o)}})})}};Ns.on("sync",n=>{n||console.error("Issue connecting to yjs...");for(const[t,e]of Object.entries(Vs)){const s=Array.from(document.querySelectorAll(`[${t}]`)).filter(To);e(s),s.forEach(r=>{r.classList.add("__playhtml-element"),r.classList.add(`__playhtml-${t}`)})}});function To(n){return n instanceof HTMLElement}v.TagData=Vs,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})});
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ onConnect(ws: import("partykit/server").PartyKitConnection, room: import("partykit/server").PartyKitRoom): Promise<void>;
3
+ };
4
+ export default _default;
package/dist/style.css ADDED
@@ -0,0 +1 @@
1
+ .__playhtml-element{cursor:pointer}.__playhtml-can-move{transition:transform .15s;will-change:transform}.__playhtml-can-spin,.__playhtml-can-grow{transition:transform .25s;will-change:transform}
package/dist/tags.d.ts ADDED
File without changes
@@ -0,0 +1,15 @@
1
+ import { GrowElement, MoveElement, SpinElement } from "./elements";
2
+ export type Position = {
3
+ x: number;
4
+ y: number;
5
+ };
6
+ export interface TagTypeToElement {
7
+ [TagType.CanMove]: MoveElement;
8
+ [TagType.CanSpin]: SpinElement;
9
+ [TagType.CanGrow]: GrowElement;
10
+ }
11
+ export declare enum TagType {
12
+ "CanMove" = "can-move",
13
+ "CanSpin" = "can-spin",
14
+ "CanGrow" = "can-grow"
15
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "playhtml",
3
+ "version": "0.1.2",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "main": "./dist/playhtml.umd.js",
7
+ "types": "./dist/main.d.ts",
8
+ "module": "./dist/playhtml.es.js",
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/playhtml.es.js",
15
+ "require": "./dist/playhtml.umd.js"
16
+ }
17
+ },
18
+ "scripts": {
19
+ "dev": "vite",
20
+ "build": "tsc && vite build",
21
+ "preview": "vite preview",
22
+ "publish-npm": "yarn run build && yarn publish"
23
+ },
24
+ "devDependencies": {
25
+ "@cloudflare/workers-types": "^4.20230518.0",
26
+ "@types/node": "^20.3.3",
27
+ "sass": "^1.62.1",
28
+ "typescript": "^5.0.2",
29
+ "vite": "^4.3.9",
30
+ "vite-plugin-dts": "^3.0.3"
31
+ },
32
+ "dependencies": {
33
+ "partykit": "^0.0.0-e7aa4cd",
34
+ "partysocket": "^0.0.0-e7aa4cd",
35
+ "y-partykit": "^0.0.0-e7aa4cd",
36
+ "yjs": "^13.6.6"
37
+ }
38
+ }