@thebase/ui
Version:
CDN-installable Owl and Bootstrap 5 UI component library.
2 lines (1 loc) • 23.6 kB
JavaScript
import{a as mt}from"./chunk-BJC3HW5B.js";import"./chunk-Y4CBBIL4.js";import{Ba as ne,D as Be,Da as E,Eb as se,Fa as oe,Fb as Te,Gb as y,Ia as lt,Ja as _e,Jb as x,Ka as Se,L as qe,N as $e,Q as Ze,Qa as ct,Qb as dt,S as Ge,Sa as re,Tb as ft,U as Qe,Ub as ut,V as et,Vb as le,W as z,Wa as at,a as He,b as G,ba as xe,bb as ie,c as Je,ca as tt,ea as nt,h as w,ha as ee,i as D,oa as be,pa as ot,ra as rt,u as We,v as Q,vb as we,wa as it,xa as te,ya as st}from"./chunk-VJBGANAS.js";import{Ka as ye,P as he,Q as Ue,R as Xe,T as Y,U as F,_a as je,d as R,e as me,h as De,i as Ee,jb as Le,pa as Ie,pb as ze,qa as pe,ra as ge,ta as Ke,ya as Z}from"./chunk-XNCXGIZE.js";import{a as M,d as L}from"./chunk-OQJVATGH.js";import"./chunk-ILE44FN6.js";import{f as p,g as A,h as C,i as j}from"./chunk-23NN352E.js";var Dt=/[\uD800-\uDBFF]/,Et=/[\uDC00-\uDFFF]/,Ut=(t,e)=>{let o=0,n=0;for(;o<t.length&&o<e.length&&t[o]===e[o];)o++;for(o>0&&Dt.test(t[o-1])&&o--;n+o<t.length&&n+o<e.length&&t[t.length-n-1]===e[e.length-n-1];)n++;return n>0&&Et.test(t[t.length-n])&&n--,{index:o,remove:t.length-o-n,insert:e.slice(o,e.length-n)}},ht=Ut;var h=new F("y-sync"),b=new F("y-undo"),O=new F("yjs-cursor");var _=(t,e)=>t>>>e|t<<32-e,It=t=>_(t,2)^_(t,13)^_(t,22),Kt=t=>_(t,6)^_(t,11)^_(t,25),jt=t=>_(t,7)^_(t,18)^t>>>3,Lt=t=>_(t,17)^_(t,19)^t>>>10,zt=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),Ht=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),Pe=class{constructor(){let e=new ArrayBuffer(320);this._H=new Uint32Array(e,0,8),this._H.set(Ht),this._W=new Uint32Array(e,64,64)}_updateHash(){let e=this._H,o=this._W;for(let a=16;a<64;a++)o[a]=Lt(o[a-2])+o[a-7]+jt(o[a-15])+o[a-16];let n=e[0],r=e[1],c=e[2],i=e[3],l=e[4],d=e[5],s=e[6],f=e[7];for(let a=0,u,m;a<64;a++)u=f+Kt(l)+(l&d^~l&s)+zt[a]+o[a]>>>0,m=It(n)+(n&r^n&c^r&c)>>>0,f=s,s=d,d=l,l=i+u>>>0,i=c,c=r,r=n,n=u+m>>>0;e[0]+=n,e[1]+=r,e[2]+=c,e[3]+=i,e[4]+=l,e[5]+=d,e[6]+=s,e[7]+=f}digest(e){let o=0;for(;o+56<=e.length;){let i=0;for(;i<16&&o+3<e.length;i++)this._W[i]=e[o++]<<24|e[o++]<<16|e[o++]<<8|e[o++];if(o%64!==0){for(this._W.fill(0,i,16);o<e.length;)this._W[i]|=e[o]<<(3-o%4)*8,o++;this._W[i]|=128<<(3-o%4)*8}this._updateHash()}let n=o%64!==0;this._W.fill(0,0,16);let r=0;for(;o<e.length;r++)for(let i=3;i>=0&&o<e.length;i--)this._W[r]|=e[o++]<<i*8;n||(this._W[r-(o%4===0?0:1)]|=128<<(3-o%4)*8),this._W[14]=e.byteLength/536870912,this._W[15]=e.byteLength*8,this._updateHash();let c=new Uint8Array(32);for(let i=0;i<this._H.length;i++)for(let l=0;l<4;l++)c[i*4+l]=this._H[i]>>>(3-l)*8;return c}},pt=t=>new Pe().digest(t);var Wt=t=>{for(let o=6;o<t.length;o++)t[o%6]=t[o%6]^t[o];return t.slice(0,6)},gt=t=>Ge(Wt(pt(Qe(t))));var _t=()=>({mapping:new Map,isOMark:new Map}),H=(t,e)=>e===void 0?!t.deleted:e.sv.has(t.id.client)&&e.sv.get(t.id.client)>t.id.clock&&!tt(e.ds,t.id),Qt=[{light:"#ecd44433",dark:"#ecd444"}],en=(t,e,o)=>{if(!t.has(o)){if(t.size<e.length){let n=Je();t.forEach(r=>n.add(r)),e=e.filter(r=>!n.has(r))}t.set(o,Be(e))}return t.get(o)},Ae=(t,{colors:e=Qt,colorMapping:o=new Map,permanentUserData:n=null,onFirstRender:r=()=>{},mapping:c}={})=>{let i=!1,l=new ce(t,c),d=new Y({props:{editable:s=>{let f=h.getState(s);return f.snapshot==null&&f.prevSnapshot==null}},key:h,state:{init:(s,f)=>({type:t,doc:t.doc,binding:l,snapshot:null,prevSnapshot:null,isChangeOrigin:!1,isUndoRedoOperation:!1,addToHistory:!0,colors:e,colorMapping:o,permanentUserData:n}),apply:(s,f)=>{let a=s.getMeta(h);if(a!==void 0){f=Object.assign({},f);for(let u in a)f[u]=a[u]}return f.addToHistory=s.getMeta("addToHistory")!==!1,f.isChangeOrigin=a!==void 0&&!!a.isChangeOrigin,f.isUndoRedoOperation=a!==void 0&&!!a.isChangeOrigin&&!!a.isUndoRedoOperation,l.prosemirrorView!==null&&a!==void 0&&(a.snapshot!=null||a.prevSnapshot!=null)&&z(0,()=>{l.prosemirrorView!=null&&(a.restore==null?l._renderSnapshot(a.snapshot,a.prevSnapshot,f):(l._renderSnapshot(a.snapshot,a.snapshot,f),delete f.restore,delete f.snapshot,delete f.prevSnapshot,l.mux(()=>{l._prosemirrorChanged(l.prosemirrorView.state.doc)})))}),f}},view:s=>(l.initView(s),c==null&&l._forceRerender(),r(),{update:()=>{let f=d.getState(s.state);if(f.snapshot==null&&f.prevSnapshot==null&&(i||s.state.doc.content.findDiffStart(s.state.doc.type.createAndFill().content)!==null)){if(i=!0,f.addToHistory===!1&&!f.isChangeOrigin){let a=b.getState(s.state),u=a&&a.undoManager;u&&u.stopCapturing()}l.mux(()=>{f.doc.transact(a=>{a.meta.set("addToHistory",f.addToHistory),l._prosemirrorChanged(s.state.doc)},h)})}},destroy:()=>{l.destroy()}})});return d},tn=(t,e,o)=>{if(e!==null&&e.anchor!==null&&e.head!==null)if(e.type==="all")t.setSelection(new Xe(t.doc));else if(e.type==="node"){let n=T(o.doc,o.type,e.anchor,o.mapping);t.setSelection(Ue.create(t.doc,n))}else{let n=T(o.doc,o.type,e.anchor,o.mapping),r=T(o.doc,o.type,e.head,o.mapping);if(n!==null&&r!==null){let c=he.between(t.doc.resolve(n),t.doc.resolve(r));t.setSelection(c)}}},J=(t,e)=>({type:e.selection.jsonID,anchor:v(e.selection.anchor,t.type,t.mapping),head:v(e.selection.head,t.type,t.mapping)}),ce=class{constructor(e,o=new Map){this.type=e,this.prosemirrorView=null,this.mux=mt(),this.mapping=o,this.isOMark=new Map,this._observeFunction=this._typeChanged.bind(this),this.doc=e.doc,this.beforeTransactionSelection=null,this.beforeAllTransactions=()=>{this.beforeTransactionSelection===null&&this.prosemirrorView!=null&&(this.beforeTransactionSelection=J(this,this.prosemirrorView.state))},this.afterAllTransactions=()=>{this.beforeTransactionSelection=null},this._domSelectionInView=null}get _tr(){return this.prosemirrorView.state.tr.setMeta("addToHistory",!1)}_isLocalCursorInView(){return this.prosemirrorView.hasFocus()?(Ze&&this._domSelectionInView===null&&(z(0,()=>{this._domSelectionInView=null}),this._domSelectionInView=this._isDomSelectionInView()),this._domSelectionInView):!1}_isDomSelectionInView(){let e=this.prosemirrorView._root.getSelection();if(e==null||e.anchorNode==null)return!1;let o=this.prosemirrorView._root.createRange();o.setStart(e.anchorNode,e.anchorOffset),o.setEnd(e.focusNode,e.focusOffset),o.getClientRects().length===0&&o.startContainer&&o.collapsed&&o.selectNodeContents(o.startContainer);let r=o.getBoundingClientRect(),c=et.documentElement;return r.bottom>=0&&r.right>=0&&r.left<=(window.innerWidth||c.clientWidth||0)&&r.top<=(window.innerHeight||c.clientHeight||0)}renderSnapshot(e,o){o||(o=ct(nt(),new Map)),this.prosemirrorView.dispatch(this._tr.setMeta(h,{snapshot:e,prevSnapshot:o}))}unrenderSnapshot(){this.mapping.clear(),this.mux(()=>{let e=this.type.toArray().map(n=>U(n,this.prosemirrorView.state.schema,this)).filter(n=>n!==null),o=this._tr.replace(0,this.prosemirrorView.state.doc.content.size,new L(M.from(e),0,0));o.setMeta(h,{snapshot:null,prevSnapshot:null}),this.prosemirrorView.dispatch(o)})}_forceRerender(){this.mapping.clear(),this.mux(()=>{let e=this.beforeTransactionSelection!==null?null:this.prosemirrorView.state.selection,o=this.type.toArray().map(r=>U(r,this.prosemirrorView.state.schema,this)).filter(r=>r!==null),n=this._tr.replace(0,this.prosemirrorView.state.doc.content.size,new L(M.from(o),0,0));if(e){let r=w(D(e.anchor,0),n.doc.content.size),c=w(D(e.head,0),n.doc.content.size);n.setSelection(he.create(n.doc,r,c))}this.prosemirrorView.dispatch(n.setMeta(h,{isChangeOrigin:!0,binding:this}))})}_renderSnapshot(e,o,n){let r=this.doc,c=this.type;if(e||(e=re(this.doc)),e instanceof Uint8Array||o instanceof Uint8Array)if((!(e instanceof Uint8Array)||!(o instanceof Uint8Array))&&Q(),r=new ee({gc:!1}),be(r,o),o=re(r),be(r,e),e=re(r),c._item===null){let i=Array.from(this.doc.share.keys()).find(l=>this.doc.share.get(l)===this.type);c=r.getXmlFragment(i)}else{let i=r.store.clients.get(c._item.id.client)??[],l=at(i,c._item.id.clock);c=i[l].content.type}this.mapping.clear(),this.mux(()=>{r.transact(i=>{let l=n.permanentUserData;l&&l.dss.forEach(a=>{xe(i,a,u=>{})});let d=(a,u)=>{let m=a==="added"?l.getUserByClientId(u.client):l.getUserByDeletedId(u);return{user:m,type:a,color:en(n.colorMapping,n.colors,m)}},s=we(c,new Se(o.ds,e.sv)).map(a=>!a._item.deleted||H(a._item,e)||H(a._item,o)?U(a,this.prosemirrorView.state.schema,{mapping:new Map,isOMark:new Map},e,o,d):null).filter(a=>a!==null),f=this._tr.replace(0,this.prosemirrorView.state.doc.content.size,new L(M.from(s),0,0));this.prosemirrorView.dispatch(f.setMeta(h,{isChangeOrigin:!0}))},h)})}_typeChanged(e,o){if(this.prosemirrorView==null)return;let n=h.getState(this.prosemirrorView.state);if(e.length===0||n.snapshot!=null||n.prevSnapshot!=null){this.renderSnapshot(n.snapshot,n.prevSnapshot);return}this.mux(()=>{let r=(l,d)=>this.mapping.delete(d);xe(o,o.deleteSet,l=>{if(l.constructor===le){let d=l.content.type;d&&this.mapping.delete(d)}}),o.changed.forEach(r),o.changedParentTypes.forEach(r);let c=this.type.toArray().map(l=>St(l,this.prosemirrorView.state.schema,this)).filter(l=>l!==null),i=this._tr.replace(0,this.prosemirrorView.state.doc.content.size,new L(M.from(c),0,0));tn(i,this.beforeTransactionSelection,this),i=i.setMeta(h,{isChangeOrigin:!0,isUndoRedoOperation:o.origin instanceof ie}),this.beforeTransactionSelection!==null&&this._isLocalCursorInView()&&i.scrollIntoView(),this.prosemirrorView.dispatch(i)})}_prosemirrorChanged(e){this.doc.transact(()=>{X(this.doc,this.type,e,this),this.beforeTransactionSelection=J(this,this.prosemirrorView.state)},h)}initView(e){this.prosemirrorView!=null&&this.destroy(),this.prosemirrorView=e,this.doc.on("beforeAllTransactions",this.beforeAllTransactions),this.doc.on("afterAllTransactions",this.afterAllTransactions),this.type.observeDeep(this._observeFunction)}destroy(){this.prosemirrorView!=null&&(this.prosemirrorView=null,this.type.unobserveDeep(this._observeFunction),this.doc.off("beforeAllTransactions",this.beforeAllTransactions),this.doc.off("afterAllTransactions",this.afterAllTransactions))}},St=(t,e,o,n,r,c)=>{let i=o.mapping.get(t);if(i===void 0){if(t instanceof y)return U(t,e,o,n,r,c);throw We()}return i},U=(t,e,o,n,r,c)=>{let i=[],l=d=>{if(d instanceof y){let s=St(d,e,o,n,r,c);s!==null&&i.push(s)}else{let s=d._item.right?.content?.type;s instanceof se&&!s._item.deleted&&s._item.id.client===s.doc.clientID&&(d.applyDelta([{retain:d.length},...s.toDelta()]),s.doc.transact(a=>{s._item.delete(a)}));let f=nn(d,e,o,n,r,c);f!==null&&f.forEach(a=>{a!==null&&i.push(a)})}};n===void 0||r===void 0?t.toArray().forEach(l):we(t,new Se(r.ds,n.sv)).forEach(l);try{let d=t.getAttributes(n);n!==void 0&&(H(t._item,n)?H(t._item,r)||(d.ychange=c?c("added",t._item.id):{type:"added"}):d.ychange=c?c("removed",t._item.id):{type:"removed"});let s=e.node(t.nodeName,d,i);return o.mapping.set(t,s),s}catch{return t.doc.transact(s=>{t._item.delete(s)},h),o.mapping.delete(t),null}},nn=(t,e,o,n,r,c)=>{let i=[],l=t.toDelta(n,r,c);try{for(let d=0;d<l.length;d++){let s=l[d];i.push(e.text(s.insert,an(s.attributes,e)))}}catch{return t.doc.transact(s=>{t._item.delete(s)},h),null}return i},on=(t,e)=>{let o=new x,n=t.map(r=>({insert:r.text,attributes:Tt(r.marks,e)}));return o.applyDelta(n),e.mapping.set(o,t),o},rn=(t,e)=>{let o=new y(t.type.name);for(let n in t.attrs){let r=t.attrs[n];r!==null&&n!=="ychange"&&o.setAttribute(n,r)}return o.insert(0,de(t).map(n=>Ce(n,e))),e.mapping.set(o,t),o},Ce=(t,e)=>t instanceof Array?on(t,e):rn(t,e),yt=t=>typeof t=="object"&&t!==null,Me=(t,e)=>{let o=Object.keys(t).filter(r=>t[r]!==null),n=o.length===(e==null?0:Object.keys(e).filter(r=>e[r]!==null).length);for(let r=0;r<o.length&&n;r++){let c=o[r],i=t[c],l=e[c];n=c==="ychange"||i===l||yt(i)&&yt(l)&&Me(i,l)}return n},de=t=>{let e=t.content.content,o=[];for(let n=0;n<e.length;n++){let r=e[n];if(r.isText){let c=[];for(let i=e[n];n<e.length&&i.isText;i=e[++n])c.push(i);n--,o.push(c)}else o.push(r)}return o},wt=(t,e)=>{let o=t.toDelta();return o.length===e.length&&o.every((n,r)=>n.insert===e[r].text&&qe(n.attributes||{}).length===e[r].marks.length&&$e(n.attributes,(c,i)=>{let l=Oe(i),d=e[r].marks;return Me(c,d.find(s=>s.type.name===l)?.attrs)}))},W=(t,e)=>{if(t instanceof y&&!(e instanceof Array)&&Ye(t,e)){let o=de(e);return t._length===o.length&&Me(t.getAttributes(),e.attrs)&&t.toArray().every((n,r)=>W(n,o[r]))}return t instanceof x&&e instanceof Array&&wt(t,e)},ae=(t,e)=>t===e||t instanceof Array&&e instanceof Array&&t.length===e.length&&t.every((o,n)=>e[n]===o),xt=(t,e,o)=>{let n=t.toArray(),r=de(e),c=r.length,i=n.length,l=w(i,c),d=0,s=0,f=!1;for(;d<l;d++){let a=n[d],u=r[d];if(ae(o.mapping.get(a),u))f=!0;else if(!W(a,u))break}for(;d+s<l;s++){let a=n[i-s-1],u=r[c-s-1];if(ae(o.mapping.get(a),u))f=!0;else if(!W(a,u))break}return{equalityFactor:d+s,foundMappedChild:f}},sn=t=>{let e="",o=t._start,n={};for(;o!==null;)o.deleted||(o.countable&&o.content instanceof ft?e+=o.content.str:o.content instanceof dt&&(n[o.content.key]=null)),o=o.right;return{str:e,nAttrs:n}},ln=(t,e,o)=>{o.mapping.set(t,e);let{nAttrs:n,str:r}=sn(t),c=e.map(s=>({insert:s.text,attributes:Object.assign({},n,Tt(s.marks,o))})),{insert:i,remove:l,index:d}=ht(r,c.map(s=>s.insert).join(""));t.delete(d,l),t.insert(d,i),t.applyDelta(c.map(s=>({retain:s.insert.length,attributes:s.attributes})))},cn=/(.*)(--[a-zA-Z0-9+/=]{8})$/,Oe=t=>cn.exec(t)?.[1]??t,an=(t,e)=>{let o=[];for(let n in t)o.push(e.mark(Oe(n),t[n]));return o},Tt=(t,e)=>{let o={};return t.forEach(n=>{if(n.type.name!=="ychange"){let r=G(e.isOMark,n.type,()=>!n.type.excludes(n.type));o[r?`${n.type.name}--${gt(n.toJSON())}`:n.type.name]=n.attrs}}),o},X=(t,e,o,n)=>{if(e instanceof y&&e.nodeName!==o.type.name)throw new Error("node name mismatch!");if(n.mapping.set(e,o),e instanceof y){let a=e.getAttributes(),u=o.attrs;for(let m in u)u[m]!==null?a[m]!==u[m]&&m!=="ychange"&&e.setAttribute(m,u[m]):e.removeAttribute(m);for(let m in a)u[m]===void 0&&e.removeAttribute(m)}let r=de(o),c=r.length,i=e.toArray(),l=i.length,d=w(c,l),s=0,f=0;for(;s<d;s++){let a=i[s],u=r[s];if(!ae(n.mapping.get(a),u))if(W(a,u))n.mapping.set(a,u);else break}for(;f+s<d;f++){let a=i[l-f-1],u=r[c-f-1];if(!ae(n.mapping.get(a),u))if(W(a,u))n.mapping.set(a,u);else break}t.transact(()=>{for(;l-s-f>0&&c-s-f>0;){let u=i[s],m=r[s],V=i[l-f-1],K=r[c-f-1];if(u instanceof x&&m instanceof Array)wt(u,m)||ln(u,m,n),s+=1;else{let q=u instanceof y&&Ye(u,m),$=V instanceof y&&Ye(V,K);if(q&&$){let fe=xt(u,m,n),ue=xt(V,K,n);fe.foundMappedChild&&!ue.foundMappedChild?$=!1:!fe.foundMappedChild&&ue.foundMappedChild||fe.equalityFactor<ue.equalityFactor?q=!1:$=!1}q?(X(t,u,m,n),s+=1):$?(X(t,V,K,n),f+=1):(n.mapping.delete(e.get(s)),e.delete(s,1),e.insert(s,[Ce(m,n)]),s+=1)}}let a=l-s-f;if(l===1&&c===0&&i[0]instanceof x?(n.mapping.delete(i[0]),i[0].delete(0,i[0].length)):a>0&&(e.slice(s,s+a).forEach(u=>n.mapping.delete(u)),e.delete(s,a)),s+f<c){let u=[];for(let m=s;m<c-f;m++)u.push(Ce(r[m],n));e.insert(s,u)}},h)},Ye=(t,e)=>!(e instanceof Array)&&t.nodeName===e.type.name;var B=null,dn=()=>{let t=B;B=null,t.forEach((e,o)=>{let n=o.state.tr,r=h.getState(o.state);r&&r.binding&&!r.binding.isDestroyed&&(e.forEach((c,i)=>{n.setMeta(i,c)}),o.dispatch(n))})},ve=(t,e,o)=>{B||(B=new Map,z(0,dn)),G(B,t,He).set(e,o)},v=(t,e,o)=>{if(t===0)return oe(e,0,e.length===0?-1:0);let n=e._first===null?null:e._first.content.type;for(;n!==null&&e!==n;){if(n instanceof x){if(n._length>=t)return oe(n,t,e.length===0?-1:0);if(t-=n._length,n._item!==null&&n._item.next!==null)n=n._item.next.content.type;else{do n=n._item===null?null:n._item.parent,t--;while(n!==e&&n!==null&&n._item!==null&&n._item.next===null);n!==null&&n!==e&&(n=n._item===null?null:n._item.next.content.type)}}else{let r=(o.get(n)||{nodeSize:0}).nodeSize;if(n._first!==null&&t<r)n=n._first.content.type,t--;else{if(t===1&&n._length===0&&r>1)return new ne(n._item===null?null:n._item.id,n._item===null?te(n):null,null);if(t-=r,n._item!==null&&n._item.next!==null)n=n._item.next.content.type;else{if(t===0)return n=n._item===null?n:n._item.parent,new ne(n._item===null?null:n._item.id,n._item===null?te(n):null,null);do n=n._item.parent,t--;while(n!==e&&n._item.next===null);n!==e&&(n=n._item.next.content.type)}}}if(n===null)throw Q();if(t===0&&n.constructor!==x&&n!==e)return fn(n._item.parent,n._item)}return oe(e,e._length,e.length===0?-1:0)},fn=(t,e)=>{let o=null,n=null;return t._item===null?n=te(t):o=it(t._item.id.client,t._item.id.clock),new ne(o,n,e.id)},T=(t,e,o,n)=>{let r=lt(o,t);if(r===null||r.type!==e&&!st(e,r.type._item))return null;let c=r.type,i=0;if(c.constructor===x)i=r.index;else if(c._item===null||!c._item.deleted){let l=c._first,d=0;for(;d<c._length&&d<r.index&&l!==null;){if(!l.deleted){let s=l.content.type;d++,s instanceof x?i+=s._length:i+=n.get(s).nodeSize}l=l.right}i+=1}for(;c!==e&&c._item!==null;){let l=c._item.parent;if(l._item===null||!l._item.deleted){i+=1;let d=l._first;for(;d!==null;){let s=d.content.type;if(s===c)break;d.deleted||(s instanceof x?i+=s._length:i+=n.get(s).nodeSize),d=d.right}}c=l}return i-1},Pt=(t,e)=>{let o=t.toArray().map(n=>U(n,e,_t())).filter(n=>n!==null);return M.fromArray(o)},Ne=(t,e)=>e.topNodeType.create(null,Pt(t,e));function ke(t,e="prosemirror"){let o=new ee,n=o.get(e,Te);return n.doc?(Ct(t,n),n.doc):o}function Ct(t,e){let o=e||new Te,n=o.doc?o.doc:{transact:r=>r(void 0)};return X(n,o,t,{mapping:new Map,isOMark:new Map}),o}var un=(t,e,o)=>t!==e,mn=t=>{let e=document.createElement("span");e.classList.add("ProseMirror-yjs-cursor"),e.setAttribute("style",`border-color: ${t.color}`);let o=document.createElement("div");o.setAttribute("style",`background-color: ${t.color}`),o.insertBefore(document.createTextNode(t.name),null);let n=document.createTextNode("\u2060"),r=document.createTextNode("\u2060");return e.insertBefore(n,null),e.insertBefore(o,null),e.insertBefore(r,null),e},hn=t=>({style:`background-color: ${t.color}70`,class:"ProseMirror-yjs-selection"}),pn=/^#[0-9a-fA-F]{6}$/,Yt=(t,e,o,n,r)=>{let c=h.getState(t),i=c.doc,l=[];return c.snapshot!=null||c.prevSnapshot!=null||c.binding.mapping.size===0?ge.create(t.doc,[]):(e.getStates().forEach((d,s)=>{if(o(i.clientID,s,d)&&d.cursor!=null){let f=d.user||{};f.color==null?f.color="#ffa500":pn.test(f.color)||console.warn("A user uses an unsupported color format",f),f.name==null&&(f.name=`User: ${s}`);let a=T(i,c.type,E(d.cursor.anchor),c.binding.mapping),u=T(i,c.type,E(d.cursor.head),c.binding.mapping);if(a!==null&&u!==null){let m=D(t.doc.content.size-1,0);a=w(a,m),u=w(u,m),l.push(pe.widget(u,()=>n(f,s),{key:s+"",side:10}));let V=w(a,u),K=D(a,u);l.push(pe.inline(V,K,r(f,s),{inclusiveEnd:!0,inclusiveStart:!1}))}}}),ge.create(t.doc,l))},At=(t,{awarenessStateFilter:e=un,cursorBuilder:o=mn,selectionBuilder:n=hn,getSelection:r=i=>i.selection}={},c="cursor")=>new Y({key:O,state:{init(i,l){return Yt(l,t,e,o,n)},apply(i,l,d,s){let f=h.getState(s),a=i.getMeta(O);return f&&f.isChangeOrigin||a&&a.awarenessUpdated?Yt(s,t,e,o,n):l.map(i.mapping,i.doc)}},props:{decorations:i=>O.getState(i)},view:i=>{let l=()=>{i.docView&&ve(i,O,{awarenessUpdated:!0})},d=()=>{let s=h.getState(i.state),f=t.getLocalState()||{};if(i.hasFocus()){let a=r(i.state),u=v(a.anchor,s.type,s.binding.mapping),m=v(a.head,s.type,s.binding.mapping);(f.cursor==null||!_e(E(f.cursor.anchor),u)||!_e(E(f.cursor.head),m))&&t.setLocalStateField(c,{anchor:u,head:m})}else f.cursor!=null&&T(s.doc,s.type,E(f.cursor.anchor),s.binding.mapping)!==null&&t.setLocalStateField(c,null)};return t.on("change",l),i.dom.addEventListener("focusin",d),i.dom.addEventListener("focusout",d),{update:d,destroy:()=>{i.dom.removeEventListener("focusin",d),i.dom.removeEventListener("focusout",d),t.off("change",l),t.setLocalStateField(c,null)}}}});var Mt=t=>b.getState(t)?.undoManager?.undo()!=null,Ve=t=>b.getState(t)?.undoManager?.redo()!=null;var gn=new Set(["paragraph"]),yn=(t,e)=>!(t instanceof le)||!(t.content instanceof ut)||!(t.content.type instanceof se||t.content.type instanceof y&&e.has(t.content.type.nodeName))||t.content.type._length===0,Ot=({protectedNodes:t=gn,trackedOrigins:e=[],undoManager:o=null}={})=>new Y({key:b,state:{init:(n,r)=>{let c=h.getState(r),i=o||new ie(c.type,{trackedOrigins:new Set([h].concat(e)),deleteFilter:l=>yn(l,t),captureTransaction:l=>l.meta.get("addToHistory")!==!1});return{undoManager:i,prevSel:null,hasUndoOps:i.undoStack.length>0,hasRedoOps:i.redoStack.length>0}},apply:(n,r,c,i)=>{let l=h.getState(i).binding,d=r.undoManager,s=d.undoStack.length>0,f=d.redoStack.length>0;return l?{undoManager:d,prevSel:J(l,c),hasUndoOps:s,hasRedoOps:f}:s!==r.hasUndoOps||f!==r.hasRedoOps?Object.assign({},r,{hasUndoOps:d.undoStack.length>0,hasRedoOps:d.redoStack.length>0}):r}},view:n=>{let r=h.getState(n.state),c=b.getState(n.state).undoManager;return c.on("stack-item-added",({stackItem:i})=>{let l=r.binding;l&&i.meta.set(l,b.getState(n.state).prevSel)}),c.on("stack-item-popped",({stackItem:i})=>{let l=r.binding;l&&(l.beforeTransactionSelection=i.meta.get(l)||l.beforeTransactionSelection)}),{destroy:()=>{c.destroy()}}}});var kt=new F("MILKDOWN_COLLAB_KEYMAP"),xn=[kt,h,O,b],N,S,I,g,k,P,Rt,Ft,Fe,Nt,Vt=(Nt=class{constructor(){A(this,P);A(this,N,{});A(this,S,null);A(this,I,null);A(this,g,null);A(this,k,!1)}bindCtx(t){return C(this,g,t),this}bindDoc(t){return C(this,S,t.getXmlFragment("prosemirror")),this}bindXmlFragment(t){return C(this,S,t),this}setOptions(t){return C(this,N,t),this}mergeOptions(t){return Object.assign(p(this,N),t),this}setAwareness(t){return C(this,I,t),this}applyTemplate(t,e){if(!p(this,g))throw R();if(!p(this,S))throw me();let o=e||(i=>i.textContent.length===0),n=j(this,P,Rt).call(this,t),r=p(this,g).get(ye),c=Ne(p(this,S),r);if(n&&o(c,n)){let i=p(this,S);i.delete(0,i.length);let l=ke(n),d=rt(l);i.doc&&ot(i.doc,d),l.destroy()}return this}connect(){if(!p(this,g))throw R();if(p(this,k))return;let t=p(this,g).get(Z),e=j(this,P,Ft).call(this),o=t.concat(e);return j(this,P,Fe).call(this,o),C(this,k,!0),this}disconnect(){if(!p(this,g))throw R();if(!p(this,k))return this;let t=p(this,g).get(Z).filter(e=>!e.spec.key||!xn.includes(e.spec.key));return j(this,P,Fe).call(this,t),C(this,k,!1),this}},N=new WeakMap,S=new WeakMap,I=new WeakMap,g=new WeakMap,k=new WeakMap,P=new WeakSet,Rt=function(t){if(!p(this,g))throw R();let e=p(this,g).get(ye),o=p(this,g).get(je);return Le(t,o,e)},Ft=function(){if(!p(this,S))throw me();let{ySyncOpts:t,yUndoOpts:e}=p(this,N),o=[Ae(p(this,S),t),Ot(e),new Y({key:kt,props:{handleKeyDown:Ie({"Mod-z":Mt,"Mod-y":Ve,"Mod-Shift-z":Ve})}})];if(p(this,I)){let{yCursorOpts:n,yCursorStateField:r}=p(this,N);o.push(At(p(this,I),n,r))}return o},Fe=function(t){if(!p(this,g))throw R();p(this,g).set(Z,t);let e=p(this,g).get(Ke),o=e.state.reconfigure({plugins:t});e.updateState(o)},Nt),vt=De(new Vt,"collabServiceCtx"),Re=Ee("CollabReady"),bn=t=>{let e=new Vt;return t.inject(vt,e).record(Re),async()=>(await t.wait(ze),e.bindCtx(t),t.done(Re),()=>{t.remove(vt).clearTimer(Re)})};bn.meta={package:"@milkdown/plugin-collab",displayName:"Collab"};export{kt as CollabKeymapPluginKey,Re as CollabReady,Vt as CollabService,bn as collab,vt as collabServiceCtx};