/** * yjs - A framework for real-time p2p shared editing on any data * @version v13.0.0-24 * @license MIT */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.Y=e()}(this,function(){"use strict";function t(t,e){return e={exports:{}},t(e,e.exports),e.exports}function e(t,e){var n=e._id;if(void 0===n)e._integrate(t);else{if(t.ss.getState(n.user)>n.clock)return;e._integrate(t);var r=t._missingStructs.get(n.user);if(null!=r)for(var i=n.clock,o=i+e._length;i0&&(u+=" .. missing: "+l.map(v).join(", ")),n.push(u)}}function r(t,n,r){for(var i=t.readUint32(),o=0;o=0;d--){var g=h[d];_.has(g.user)||_.set(g.user,new Map);var v=_.get(g.user);v.has(g.clock)||v.set(g.clock,[]);(v=v.get(g.clock)).push(f)}}}}function i(t){for(var e=new Map,n=t.readUint32(),r=0;r0){var h=0,c=i[h],f=[];t.ds.iterate(new lt(r,0),new lt(r,Number.MAX_VALUE),function(t){for(;null!=c;){var e=0;if(t._id.clock+t.len<=c[0])break;c[0]=0;_--){var d=f[_];p(t,d[0],d[1],d[2])}for(;h0)for(var a=t.os.findNode(new lt(e,n));null!==a&&r>0&&a.val._id.equals(new lt(e,n));){var l=a.val;l._deleted||(l._splitAt(t,r),l._delete(t,i));var u=l._length;r-=u,n+=u,a=a.next()}}}function y(t,e,n){if(e!==t&&!e._deleted&&!t._transaction.newTypes.has(e)){var r=t._transaction.changedTypes,i=r.get(e);void 0===i&&(i=new Set,r.set(e,i)),i.add(n)}}function m(t,e,n,r){var i=e._id;n._id=new lt(i.user,i.clock+r),n._origin=e,n._left=e,n._right=e._right,null!==n._right&&(n._right._left=n),n._right_origin=e._right_origin,e._right=n,n._parent=e._parent,n._parentSub=e._parentSub,n._deleted=e._deleted;var o=new Set;o.add(e);for(var s=n._right;null!==s&&o.has(s._origin);)s._origin===e&&(s._origin=n),o.add(s),s=s._right;t.os.put(n)}function b(t,e){var n=void 0;do{n=e._right,e._right=null,e._right_origin=null,e._origin=e._left,e._integrate(t),e=n}while(null!==n)}function k(t,e){return e}function w(t){for(;null!==t&&t._deleted;)t=t._right;return t}function S(t,e,n){var r=t.insertDomElementsAfter(e,[n]);return r.length>0?r[0]:e}function E(t){var e=t._yxml,n=e._y,r=new Set(Array.prototype.map.call(t.childNodes,function(t){return t._yxml}).filter(function(t){return void 0!==t}));e.forEach(function(t,e){r.has(t)||t._delete(n)});for(var i=t.childNodes,o=i.length,s=null,a=w(e._start),l=0;l=e)return[n._id.user,n._id.clock+e-1];if(null===n._right)return[n._id.user,n._id.clock+n._length-1];n._deleted||(e-=n._length),n=n._right}return null}function U(t,e){if("startof"===e[0]){var n=void 0;return n=null===e[3]?new lt(e[1],e[2]):new Bt(e[3],e[4]),{type:t.os.get(n),offset:0}}var r=0,i=t.os.findNodeWithUpperBound(new lt(e[0],e[1])).val,o=i._parent;if(o._deleted)return null;for(i._deleted||(r=e[1]-i._id.clock+1),i=i._left;null!==i;)i._deleted||(r+=i._length),i=i._left;return{type:o,offset:r}}function B(t,e){if(null!==yt&&e){var n=yt.to,r=yt.from,i=yt.fromY,o=yt.toY,s=!1,a=pt.anchorNode,l=pt.anchorOffset,u=pt.focusNode,h=pt.focusOffset;if(null!==r){var c=U(i,r);null!==c&&(s=!0,a=c.type.getDom(),l=c.offset)}if(null!==n){var f=U(o,n);null!==f&&(u=f.type.getDom(),h=f.offset,s=!0)}s&&pt.setBaseAndExtent(a,l,u,h),yt=null,pt=null}}function x(t,e,n){if(t==e)return t?[[wt,t]]:[];(n<0||t.lengthe.length?t:e,i=t.length>e.length?e:t,o=r.indexOf(i);if(-1!=o)return n=[[kt,r.substring(0,o)],[wt,i],[kt,r.substring(o+i.length)]],t.length>e.length&&(n[0][0]=n[2][0]=bt),n;if(1==i.length)return[[bt,t],[kt,e]];var s=T(t,e);if(s){var a=s[0],l=s[1],u=s[2],h=s[3],c=s[4],f=x(a,u),_=x(l,h);return f.concat([[wt,c]],_)}return I(t,e)}function I(t,e){for(var n=t.length,r=e.length,i=Math.ceil((n+r)/2),o=i,s=2*i,a=new Array(s),l=new Array(s),u=0;un)_+=2;else if(b>r)f+=2;else if(c){var k=o+h-p;if(k>=0&&k=w)return L(t,e,y,b)}}}for(var S=-v+d;S<=v-g;S+=2){var w,k=o+S;w=S==-v||S!=v&&l[k-1]n)g+=2;else if(E>r)d+=2;else if(!c){var m=o+h-S;if(m>=0&&m=w)return L(t,e,y,b)}}}}return[[bt,t],[kt,e]]}function L(t,e,n,r){var i=t.substring(0,n),o=e.substring(0,r),s=t.substring(n),a=e.substring(r),l=x(i,o),u=x(s,a);return l.concat(u)}function N(t,e){if(!t||!e||t.charAt(0)!=e.charAt(0))return 0;for(var n=0,r=Math.min(t.length,e.length),i=r,o=0;n=t.length?[r,i,o,s,u]:null}var r=t.length>e.length?t:e,i=t.length>e.length?e:t;if(r.length<4||2*i.lengtha[4].length?s:a:s;var l,u,h,c;return t.length>e.length?(l=o[0],u=o[1],h=o[2],c=o[3]):(h=o[0],c=o[1],l=o[2],u=o[3]),[l,u,h,c,o[4]]}function P(t){t.push([wt,""]);for(var e,n=0,r=0,i=0,o="",s="";n1?(0!==r&&0!==i&&(e=N(s,o),0!==e&&(n-r-i>0&&t[n-r-i-1][0]==wt?t[n-r-i-1][1]+=s.substring(0,e):(t.splice(0,0,[wt,s.substring(0,e)]),n++),s=s.substring(e),o=o.substring(e)),0!==(e=V(s,o))&&(t[n][1]=s.substring(s.length-e)+t[n][1],s=s.substring(0,s.length-e),o=o.substring(0,o.length-e))),0===r?t.splice(n-i,r+i,[kt,s]):0===i?t.splice(n-r,r+i,[bt,o]):t.splice(n-r-i,r+i,[bt,o],[kt,s]),n=n-r-i+(r?1:0)+(i?1:0)+1):0!==n&&t[n-1][0]==wt?(t[n-1][1]+=t[n][1],t.splice(n,1)):n++,i=0,r=0,o="",s=""}""===t[t.length-1][1]&&t.pop();var a=!1;for(n=1;n0&&r.splice(i+2,0,[s[0],a]),F(r,i,3)}return t}function j(t){for(var e=!1,n=function(t){return t.charCodeAt(0)>=56320&&t.charCodeAt(0)<=57343},r=2;r=55296&&t.charCodeAt(t.length-1)<=56319}(t[r-2][1])&&t[r-1][0]===bt&&n(t[r-1][1])&&t[r][0]===kt&&n(t[r][1])&&(e=!0,t[r-1][1]=t[r-2][1].slice(-1)+t[r-1][1],t[r][1]=t[r-2][1].slice(-1)+t[r][1],t[r-2][1]=t[r-2][1].slice(0,-1));if(!e)return t;for(var i=[],r=0;r0&&i.push(t[r]);return i}function F(t,e,n){for(var r=e+n-1;r>=0&&r>=e-1;r--)if(r+1100)){var e=/^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(t);if(e){var n=parseFloat(e[1]);switch((e[2]||"ms").toLowerCase()){case"years":case"year":case"yrs":case"yr":case"y":return n*Mt;case"days":case"day":case"d":return n*Ct;case"hours":case"hour":case"hrs":case"hr":case"h":return n*Pt;case"minutes":case"minute":case"mins":case"min":case"m":return n*Tt;case"seconds":case"second":case"secs":case"sec":case"s":return n*Vt;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return n;default:return}}}}function $(t){return t>=Ct?Math.round(t/Ct)+"d":t>=Pt?Math.round(t/Pt)+"h":t>=Tt?Math.round(t/Tt)+"m":t>=Vt?Math.round(t/Vt)+"s":t+"ms"}function Q(t){return Z(t,Ct,"day")||Z(t,Pt,"hour")||Z(t,Tt,"minute")||Z(t,Vt,"second")||t+" ms"}function Z(t,e,n){if(!(t0))return n;if(n.gc){if(!((r=n._id.clock+n.len-t.clock)=o._id.clock)for(r=n._id.clock+n.len-o._id.clock;r>=0;){if(o.gc){n.len-=r,r>=o.len&&(r-=o.len)>0&&(this.put(n),this.markDeleted(new lt(o._id.user,o._id.clock+o.len),r));break}if(!(r>o.len)){n.len+=o.len-r,this.delete(o._id);break}var s=this.findNext(o._id);if(this.delete(o._id),null==s||n._id.user!==s._id.user)break;o=s,r=n._id.clock+n.len-o._id.clock}return this.put(n),n}}]),e}(at);String.fromCodePoint||function(){var t=function(){try{var t={},e=Object.defineProperty,n=e(t,t,t)&&e}catch(t){}return n}(),e=String.fromCharCode,n=Math.floor,r=function(t){var r,i,o=[],s=-1,a=arguments.length;if(!a)return"";for(var l="";++s1114111||n(u)!=u)throw RangeError("Invalid code point: "+u);u<=65535?o.push(u):(u-=65536,r=55296+(u>>10),i=u%1024+56320,o.push(r,i)),(s+1==a||o.length>16384)&&(l+=e.apply(null,o),o.length=0)}return l};t?t(String,"fromCodePoint",{value:r,configurable:!0,writable:!0}):String.fromCodePoint=r}(),String.prototype.codePointAt||function(){var t=function(){try{var t={},e=Object.defineProperty,n=e(t,t,t)&&e}catch(t){}return n}(),e=function(t){if(null==this)throw TypeError();var e=String(this),n=e.length,r=t?Number(t):0;if(r!=r&&(r=0),!(r<0||r>=n)){var i,o=e.charCodeAt(r);return o>=55296&&o<=56319&&n>r+1&&(i=e.charCodeAt(r+1))>=56320&&i<=57343?1024*(o-55296)+i-56320+65536:o}};t?t(String.prototype,"codePointAt",{value:e,configurable:!0,writable:!0}):String.prototype.codePointAt=e}();var ct=t(function(t){t.require;var e={isNotUTF8:function(t,n,r){try{e.getStringFromBytes(t,n,r,!0)}catch(t){return!0}return!1},getCharLength:function(t){return 240==(240&t)?4:224==(224&t)?3:192==(192&t)?2:t==(127&t)?1:0},getCharCode:function(t,n,r){var i=0,o="";if(n=n||0,0==(r=r||e.getCharLength(t[n])))throw new Error(t[n].toString(2)+" is not a significative byte (offset:"+n+").");if(1===r)return t[n];if(o="00000000".slice(0,r)+1+"00000000".slice(r+1),t[n]&parseInt(o,2))throw Error("Index "+n+": A "+r+" bytes encoded char cannot encode the "+(r+1)+"th rank bit to 1.");for(o="0000".slice(0,r+1)+"11111111".slice(r+1),i+=(t[n]&parseInt(o,2))<<6*--r;r;){if(128!=(128&t[n+1])||64==(64&t[n+1]))throw Error("Index "+(n+1)+': Next bytes of encoded char must begin with a "10" bit sequence.');i+=(63&t[++n])<<6*--r}return i},getStringFromBytes:function(t,n,r,i){var o,s=[];for(n|=0,r="number"==typeof r?r:t.byteLength||t.length;nr){if(i)throw Error("Index "+n+": Found a "+o+" bytes encoded char declaration but only "+(r-n)+" bytes are available.")}else s.push(String.fromCodePoint(e.getCharCode(t,n,o,i)));n+=o-1}return s.join("")},getBytesForCharCode:function(t){if(t<128)return 1;if(t<2048)return 2;if(t<65536)return 3;if(t<2097152)return 4;throw new Error("CharCode "+t+" cannot be encoded with UTF8.")},setBytesFromCharCode:function(t,n,r,i){if(t|=0,n=n||[],r|=0,1==(i=i||e.getBytesForCharCode(t)))n[r]=t;else for(n[r++]=(parseInt("1111".slice(0,i),2)<<8-i)+(t>>>6*--i);i>0;)n[r++]=t>>>6*--i&63|128;return n},setBytesFromString:function(t,n,r,i,o){t=t||"",n=n||[],r|=0,i="number"==typeof i?i:n.byteLength||1/0;for(var s=0,a=t.length;si)throw new Error('Not enought bytes to encode the char "'+t[s]+'" at the offset "'+r+'".');e.setBytesFromCharCode(t[s].codePointAt(0),n,r,l,o),r+=l}return n}};t.exports=e}),ft=function(){function t(e){if(tt(this,t),e instanceof ArrayBuffer)this.uint8arr=new Uint8Array(e);else{if(!(e instanceof Uint8Array||"undefined"!=typeof Buffer&&e instanceof Buffer))throw new Error("Expected an ArrayBuffer or Uint8Array!");this.uint8arr=e}this.pos=0}return et(t,[{key:"clone",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.pos,n=new t(this.uint8arr);return n.pos=e,n}},{key:"skip8",value:function(){this.pos++}},{key:"readUint8",value:function(){return this.uint8arr[this.pos++]}},{key:"readUint32",value:function(){var t=this.uint8arr[this.pos]+(this.uint8arr[this.pos+1]<<8)+(this.uint8arr[this.pos+2]<<16)+(this.uint8arr[this.pos+3]<<24);return this.pos+=4,t}},{key:"peekUint8",value:function(){return this.uint8arr[this.pos]}},{key:"readVarUint",value:function(){for(var t=0,e=0;;){var n=this.uint8arr[this.pos++];if(t|=(127&n)<>>0;if(e>35)throw new Error("Integer out of range!")}}},{key:"readVarString",value:function(){for(var t=this.readVarUint(),e=new Array(t),n=0;n>>8&255)}},{key:"setUint16",value:function(t,e){this.data[t]=255&e,this.data[t+1]=e>>>8&255}},{key:"writeUint32",value:function(t){for(var e=0;e<4;e++)this.data.push(255&t),t>>>=8}},{key:"setUint32",value:function(t,e){for(var n=0;n<4;n++)this.data[t+n]=255&e,e>>>=8}},{key:"writeVarUint",value:function(t){for(;t>=128;)this.data.push(128|127&t),t>>>=7;this.data.push(127&t)}},{key:"writeVarString",value:function(t){var e=ct.setBytesFromString(t),n=e.length;this.writeVarUint(n);for(var r=0;r1&&void 0!==arguments[1]&&arguments[1])t.connector.broadcastStruct(this);else{var e=this._targetID;p(t,e.user,e.clock,this._length)}null!==t.persistence&&t.persistence.saveOperations(this)}},{key:"_logString",value:function(){return"Delete - target: "+v(this._targetID)+", len: "+this._length}}]),Delete}(),gt=function t(e){tt(this,t),this.y=e,this.newTypes=new Set,this.changedTypes=new Map,this.deletedStructs=new Set,this.beforeState=new Map,this.changedParentTypes=new Map},Item=function(){function Item(){tt(this,Item),this._id=null,this._origin=null,this._left=null,this._right=null,this._right_origin=null,this._parent=null,this._parentSub=null, this._deleted=!1}return et(Item,[{key:"_copy",value:function(){var t=new this.constructor;return t._origin=this._left,t._left=this._left,t._right=this,t._right_origin=this,t._parent=this._parent,t._parentSub=this._parentSub,t}},{key:"_splitAt",value:function(t,e){return 0===e?this:this._right}},{key:"_delete",value:function(t){var e=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(this._deleted=!0,t.ds.markDeleted(this._id,this._length),e){var n=new Delete;n._targetID=this._id,n._length=this._length,n._integrate(t,!0)}y(t,this._parent,this._parentSub),t._transaction.deletedStructs.add(this)}},{key:"_beforeChange",value:function(){}},{key:"_integrate",value:function(t){var e=this._parent,n=this._id,r=null===n?0:t.ss.getState(n.user);if(null===n)this._id=t.ss.getNextID(this._length);else if(n.user===Ut);else{if(n.clock=this._length)return this._right;var n=new ItemJSON;return n._content=this._content.splice(e),m(t,this,n,e),n}},{key:"_length",get:function(){return this._content.length}}]),ItemJSON}(Item),YEvent=function(){function YEvent(t){tt(this,YEvent),this.target=t,this._path=null}return et(YEvent,[{key:"path",get:function(){if(null!==this._path)return this._path;for(var t=[],e=this.target,n=e._y;e._parent!==n;){var r=e._parent;if(null!==e._parentSub)t.push(e._parentSub);else{var i=!0,o=!1,s=void 0;try{for(var a,l=r[Symbol.iterator]();!(i=(a=l.next()).done);i=!0){var u=ot(a.value,2),h=u[0];if(u[1]===e){t.push(h);break}}}catch(t){o=!0,s=t}finally{try{!i&&l.return&&l.return()}finally{if(o)throw s}}}e=r}return this._path=t,t}}]),YEvent}(),YArrayEvent=function(t){function YArrayEvent(t,e){tt(this,YArrayEvent);var n=it(this,(YArrayEvent.__proto__||Object.getPrototypeOf(YArrayEvent)).call(this,t));return n.remote=e,n}return rt(YArrayEvent,t),YArrayEvent}(YEvent),YArray=function(t){function YArray(){return tt(this,YArray),it(this,(YArray.__proto__||Object.getPrototypeOf(YArray)).apply(this,arguments))}return rt(YArray,t),et(YArray,[{key:"_callObserver",value:function(t,e){this._callEventHandler(new YArrayEvent(this,e))}},{key:"get",value:function(t){for(var e=this._start;null!==e;){if(!e._deleted){if(t1&&void 0!==arguments[1]?arguments[1]:1;if(this._y.transact(function(){for(var r=e._start,i=0;null!==r&&n>0;){if(!r._deleted)if(i<=t&&t0)throw new Error("Delete exceeds the range of the YArray")}},{key:"insertAfter",value:function(t,e){var n=this;this._transact(function(r){var i=void 0;i=null===t?n._start:t._right;for(var o=null,s=0;si)throw new Error("Position exceeds array range!");this.insertAfter(n,e)}},{key:"_logString",value:function(){var t=null!==this._left?this._left._lastId:null,e=null!==this._origin?this._origin._lastId:null;return"YArray(id:"+v(this._id)+",start:"+v(this._start)+",left:"+v(t)+",origin:"+v(e)+",right:"+v(this._right)+",parent:"+v(this._parent)+",parentSub:"+v(this._parentSub)+")"}},{key:"length",get:function(){for(var t=0,e=this._start;null!==e;)e._deleted||(t+=e._length),e=e._right;return t}}]),YArray}(Type),YMapEvent=function(t){function YMapEvent(t,e,n){tt(this,YMapEvent);var r=it(this,(YMapEvent.__proto__||Object.getPrototypeOf(YMapEvent)).call(this,t));return r.keysChanged=e,r.remote=n,r}return rt(YMapEvent,t),YMapEvent}(YEvent),YMap=function(t){function YMap(){return tt(this,YMap),it(this,(YMap.__proto__||Object.getPrototypeOf(YMap)).apply(this,arguments))}return rt(YMap,t),et(YMap,[{key:"_callObserver",value:function(t,e){this._callEventHandler(new YMapEvent(this,t,e))}},{key:"toJSON",value:function(){var t={},e=!0,n=!1,r=void 0;try{for(var i,o=this._map[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var s=ot(i.value,2),a=s[0],l=s[1];if(!l._deleted){var u=void 0;u=l instanceof Type?void 0!==l.toJSON?l.toJSON():l.toString():l._content[0],t[a]=u}}}catch(t){n=!0,r=t}finally{try{!e&&o.return&&o.return()}finally{if(n)throw r}}return t}},{key:"keys",value:function(){var t=[],e=!0,n=!1,r=void 0;try{for(var i,o=this._map[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var s=ot(i.value,2),a=s[0];s[1]._deleted||t.push(a)}}catch(t){n=!0,r=t}finally{try{!e&&o.return&&o.return()}finally{if(n)throw r}}return t}},{key:"delete",value:function(t){var e=this;this._transact(function(n){var r=e._map.get(t);null!==n&&void 0!==r&&r._delete(n)})}},{key:"set",value:function(t,e){var n=this;return this._transact(function(r){var i=n._map.get(t)||null;if(null!==i){if(i instanceof ItemJSON&&i._content[0]===e)return e;null!==r&&i._delete(r)}var o=void 0;"function"==typeof e?(o=new e,e=o):e instanceof Item?o=e:(o=new ItemJSON,o._content=[e]),o._right=i,o._right_origin=i,o._parent=n,o._parentSub=t,null!==r?o._integrate(r):n._map.set(t,o)}),e}},{key:"get",value:function(t){var e=this._map.get(t);if(void 0!==e&&!e._deleted)return e instanceof Type?e:e._content[e._content.length-1]}},{key:"has",value:function(t){var e=this._map.get(t);return void 0!==e&&!e._deleted}},{key:"_logString",value:function(){var t=null!==this._left?this._left._lastId:null,e=null!==this._origin?this._origin._lastId:null;return"YMap(id:"+v(this._id)+",mapSize:"+this._map.size+",left:"+v(t)+",origin:"+v(e)+",right:"+v(this._right)+",parent:"+v(this._parent)+",parentSub:"+v(this._parentSub)+")"}}]),YMap}(Type),ItemString=function(t){function ItemString(){tt(this,ItemString);var t=it(this,(ItemString.__proto__||Object.getPrototypeOf(ItemString)).call(this));return t._content=null,t}return rt(ItemString,t),et(ItemString,[{key:"_copy",value:function(){var t=nt(ItemString.prototype.__proto__||Object.getPrototypeOf(ItemString.prototype),"_copy",this).call(this);return t._content=this._content,t}},{key:"_fromBinary",value:function(t,e){var n=nt(ItemString.prototype.__proto__||Object.getPrototypeOf(ItemString.prototype),"_fromBinary",this).call(this,t,e);return this._content=e.readVarString(),n}},{key:"_toBinary",value:function(t){nt(ItemString.prototype.__proto__||Object.getPrototypeOf(ItemString.prototype),"_toBinary",this).call(this,t),t.writeVarString(this._content)}},{key:"_logString",value:function(){var t=null!==this._left?this._left._lastId:null,e=null!==this._origin?this._origin._lastId:null;return"ItemJSON(id:"+v(this._id)+",content:"+JSON.stringify(this._content)+",left:"+v(t)+",origin:"+v(e)+",right:"+v(this._right)+",parent:"+v(this._parent)+",parentSub:"+v(this._parentSub)+")"}},{key:"_splitAt",value:function(t,e){if(0===e)return this;if(e>=this._length)return this._right;var n=new ItemString;return n._content=this._content.slice(e),this._content=this._content.slice(0,e),m(t,this,n,e),n}},{key:"_length",get:function(){return this._content.length}}]),ItemString}(Item),YText=function(t){function YText(t){tt(this,YText);var e=it(this,(YText.__proto__||Object.getPrototypeOf(YText)).call(this));if("string"==typeof t){var n=new ItemString;n._parent=e,n._content=t,e._start=n}return e}return rt(YText,t),et(YText,[{key:"toString",value:function(){for(var t=[],e=this._start;null!==e;)e._deleted||t.push(e._content),e=e._right;return t.join("")}},{key:"insert",value:function(t,e){var n=this;this._transact(function(r){for(var i=null,o=n._start,s=0;null!==o;){if(s<=t&&ts)throw new Error("Position exceeds array range!");var l=new ItemString;l._origin=i,l._left=i,l._right=o,l._right_origin=o,l._parent=n,l._content=e,null!==r?l._integrate(n._y):null===i?n._start=l:i._right=l})}},{key:"_logString",value:function(){var t=null!==this._left?this._left._lastId:null,e=null!==this._origin?this._origin._lastId:null;return"YText(id:"+v(this._id)+",start:"+v(this._start)+",left:"+v(t)+",origin:"+v(e)+",right:"+v(this._right)+",parent:"+v(this._parent)+",parentSub:"+v(this._parentSub)+")"}}]),YText}(YArray),YXmlText=function(t){function YXmlText(t){tt(this,YXmlText);var e=null,n=null;null!=t&&(null!=t.nodeType&&t.nodeType===t.TEXT_NODE?(e=t,n=e.nodeValue):"string"==typeof t&&(n=t));var r=it(this,(YXmlText.__proto__||Object.getPrototypeOf(YXmlText)).call(this,n));return r._dom=null,r._domObserver=null,r._domObserverListener=null,r._scrollElement=null,null!==e&&r._setDom(t),r}return rt(YXmlText,t),et(YXmlText,[{key:"setDomFilter",value:function(){}},{key:"enableSmartScrolling",value:function(t){this._scrollElement=t}},{key:"_setDom",value:function(t){null!=this._dom&&this._unbindFromDom(),null!=t._yxml&&t._yxml._unbindFromDom(),this._dom=t,t._yxml=this}},{key:"getDom",value:function(t){if(t=t||document,null===this._dom){var e=t.createTextNode(this.toString());return this._setDom(e),e}return this._dom}},{key:"_delete",value:function(t,e){this._unbindFromDom(),nt(YXmlText.prototype.__proto__||Object.getPrototypeOf(YXmlText.prototype),"_delete",this).call(this,t,e)}},{key:"_unbindFromDom",value:function(){null!=this._domObserver&&(this._domObserver.disconnect(),this._domObserver=null),null!=this._dom&&(this._dom._yxml=null,this._dom=null)}}]),YXmlText}(YText),pt=null,yt=null,mt=void 0;mt="undefined"!=typeof getSelection?function(t,e){if(e){yt={from:null,to:null,fromY:null,toY:null},pt=getSelection();var n=pt.anchorNode;if(null!==n&&null!=n._yxml){var r=n._yxml;yt.from=A(r,pt.anchorOffset),yt.fromY=r._y}var i=pt.focusNode;if(null!==i&&null!=i._yxml){var o=i._yxml;yt.to=A(o,pt.focusOffset),yt.toY=o._y}}}:function(){};var YXmlEvent=function(t){function YXmlEvent(t,e,n){tt(this,YXmlEvent);var r=it(this,(YXmlEvent.__proto__||Object.getPrototypeOf(YXmlEvent)).call(this,t));return r.childListChanged=!1,r.attributesChanged=new Set,r.remote=n,e.forEach(function(t){null===t?r.childListChanged=!0:r.attributesChanged.add(t)}),r}return rt(YXmlEvent,t),YXmlEvent}(YEvent),bt=-1,kt=1,wt=0,St=x;St.INSERT=kt,St.DELETE=bt,St.EQUAL=wt;var Et=St,YXmlFragment=function(t){function YXmlFragment(){tt(this,YXmlFragment);var t=it(this,(YXmlFragment.__proto__||Object.getPrototypeOf(YXmlFragment)).call(this));t._dom=null,t._domFilter=k,t._domObserver=null;var e=!0;return t._mutualExclude=function(n){if(e){e=!1;try{n()}catch(t){console.error(t)}t._domObserver.takeRecords(),e=!0}},t}return rt(YXmlFragment,t),et(YXmlFragment,[{key:"enableSmartScrolling",value:function(t){this._scrollElement=t,this.forEach(function(e){e.enableSmartScrolling(t)})}},{key:"setDomFilter",value:function(t){this._domFilter=t,this.forEach(function(e){e.setDomFilter(t)})}},{key:"_callObserver",value:function(t,e){this._callEventHandler(new YXmlEvent(this,t,e))}},{key:"toString",value:function(){return this.map(function(t){return t.toString()}).join("")}},{key:"_delete",value:function(t,e){this._unbindFromDom(),nt(YXmlFragment.prototype.__proto__||Object.getPrototypeOf(YXmlFragment.prototype),"_delete",this).call(this,t,e)}},{key:"_unbindFromDom",value:function(){null!=this._domObserver&&(this._domObserver.disconnect(),this._domObserver=null),null!=this._dom&&(this._dom._yxml=null,this._dom=null)}},{key:"insertDomElementsAfter",value:function(t,e){var n=R(this,e);return this.insertAfter(t,n),n}},{key:"insertDomElements",value:function(t,e){var n=R(this,e);return this.insert(t,n),n}},{key:"getDom",value:function(){return this._dom}},{key:"bindToDom",value:function(t){if(null!=this._dom&&this._unbindFromDom(),null!=t._yxml&&t._yxml._unbindFromDom(),null==MutationObserver)throw new Error("Not able to bind to a DOM element, because MutationObserver is not available!");t.innerHTML="",this._dom=t,t._yxml=this,this.forEach(function(e){t.insertBefore(e.getDom(),null)}),this._bindToDom(t)}},{key:"_bindToDom",value:function(t){var e=this;if(null!==this._parent&&null==this._parent._dom&&"undefined"!=typeof MutationObserver)return this._y.on("beforeTransaction",function(){e._domObserverListener(e._domObserver.takeRecords())}),this._y.on("beforeTransaction",mt),this._y.on("afterTransaction",B),this.observeDeep(O.bind(this)),this._domObserverListener=function(t){e._mutualExclude(function(){e._y.transact(function(){var n=new Set;t.forEach(function(t){var r=t.target,i=r._yxml;if(null!=i)switch(t.type){case"characterData":for(var o=Et(i.toString(),r.nodeValue),s=0,a=0;a0&&e.constructor!==YXmlFragment){var h=r.getAttribute(u);i.getAttribute(u)!==h&&(null==h?i.removeAttribute(u):i.setAttribute(u,h))}break;case"childList":n.add(t.target)}});var r=!0,i=!1,o=void 0;try{for(var s,a=n[Symbol.iterator]();!(r=(s=a.next()).done);r=!0){var l=s.value;null!=l._yxml&&E(l)}}catch(t){i=!0,o=t}finally{try{!r&&a.return&&a.return()}finally{if(i)throw o}}})})},this._domObserver=new MutationObserver(this._domObserverListener),this._domObserver.observe(t,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),t}},{key:"_logString",value:function(){var t=null!==this._left?this._left._lastId:null,e=null!==this._origin?this._origin._lastId:null;return"YXml(id:"+v(this._id)+",left:"+v(t)+",origin:"+v(e)+",right:"+this._right+",parent:"+v(this._parent)+",parentSub:"+this._parentSub+")"}}]),YXmlFragment}(YArray),YXmlElement=function(t){function YXmlElement(t,e){tt(this,YXmlElement);var n=it(this,(YXmlElement.__proto__||Object.getPrototypeOf(YXmlElement)).call(this));return n.nodeName=null,n._scrollElement=null,"string"==typeof t?n.nodeName=t.toUpperCase():null!=t&&null!=t.nodeType&&t.nodeType===t.ELEMENT_NODE?(n.nodeName=t.nodeName,n._setDom(t)):n.nodeName="UNDEFINED","function"==typeof e&&(n._domFilter=e),n}return rt(YXmlElement,t),et(YXmlElement,[{key:"_copy",value:function(t){var e=nt(YXmlElement.prototype.__proto__||Object.getPrototypeOf(YXmlElement.prototype),"_copy",this).call(this,t);return e.nodeName=this.nodeName,e}},{key:"_setDom",value:function(t){if(null!=this._dom)throw new Error("Only call this method if you know what you are doing ;)");if(null!=t._yxml)throw new Error("Already bound to an YXml type");this._dom=t,t._yxml=this;for(var e=[],n=0;n0?" "+e.join(" "):"")+">"+nt(YXmlElement.prototype.__proto__||Object.getPrototypeOf(YXmlElement.prototype),"toString",this).call(this)+""}},{key:"removeAttribute",value:function(){return YMap.prototype.delete.apply(this,arguments)}},{key:"setAttribute",value:function(){return YMap.prototype.set.apply(this,arguments)}},{key:"getAttribute",value:function(){return YMap.prototype.get.apply(this,arguments)}},{key:"getAttributes",value:function(){var t={},e=!0,n=!1,r=void 0;try{for(var i,o=this._map[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){var s=ot(i.value,2),a=s[0],l=s[1];t[a]=l._content[0]}}catch(t){n=!0,r=t}finally{try{!e&&o.return&&o.return()}finally{if(n)throw r}}return t}},{key:"getDom",value:function(t){t=t||document;var e=this._dom;if(null==e){e=t.createElement(this.nodeName),this._dom=e,e._yxml=this;var n=this.getAttributes();for(var r in n)e.setAttribute(r,n[r]);this.forEach(function(n){e.appendChild(n.getDom(t))}),this._bindToDom(e)}return e}}]),YXmlElement}(YXmlFragment),Ot=new Map,At=new Map;J(0,ItemJSON),J(1,ItemString),J(2,Delete),J(3,YArray),J(4,YMap),J(5,YText),J(6,YXmlFragment),J(7,YXmlElement),J(8,YXmlText);var Ut=16777215,Bt=function(){function t(e,n){tt(this,t),this.user=Ut,this.name=e,this.type=z(n)}return et(t,[{key:"equals",value:function(t){return null!==t&&t.user===this.user&&t.name===this.name&&t.type===this.type}},{key:"lessThan",value:function(t){return this.user1?e-1:0),r=1;r0)){var r=new Lt(t);n._undoing?n._redoBuffer.push(r):(n._undoBuffer.push(r),n._redoing||(n._redoBuffer=[]))}})}return et(t,[{key:"undo",value:function(){this._undoing=!0,this._applyReverseOperation(this._undoBuffer),this._undoing=!1}},{key:"redo",value:function(){this._redoing=!0,this._applyReverseOperation(this._redoBuffer),this._redoing=!1}},{key:"_applyReverseOperation",value:function(t){var e=this;this.y.transact(function(){for(var n=!1;!n&&t.length>0;){var r=t.pop();e.y.os.getItemCleanStart(r.fromState),e.y.os.getItemCleanEnd(r.toState),e.y.os.iterate(r.fromState,r.toState,function(t){!t._deleted&&X(e.y,t,e._scope)&&(n=!0,t._delete(e.y))});var i=!0,o=!1,s=void 0;try{for(var a,l=r.deletedStructs[Symbol.iterator]();!(i=(a=l.next()).done);i=!0){var u=a.value;X(e.y,u,e._scope)&&u._parent!==e.y&&!u._parent._deleted&&(u._parent._id.user!==e.y.userID||u._parent._id.clockr.fromState.clock)&&(n=!0,u=u._copy(r.deletedStructs),u._integrate(e.y))}}catch(t){o=!0,s=t}finally{try{!i&&l.return&&l.return()}finally{if(o)throw s}}}})}}]),t}(),Vt=1e3,Tt=60*Vt,Pt=60*Tt,Ct=24*Pt,Mt=365.25*Ct,jt=function(t,e){e=e||{};var n=void 0===t?"undefined":K(t);if("string"===n&&t.length>0)return q(t);if("number"===n&&!1===isNaN(t))return e.long?Q(t):$(t);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(t))},Ft=t(function(t,e){function n(t){var n,r=0;for(n in t)r=(r<<5)-r+t.charCodeAt(n),r|=0;return e.colors[Math.abs(r)%e.colors.length]}function r(t){function r(){if(r.enabled){var t=r,n=+new Date,i=n-(l||n);t.diff=i,t.prev=l,t.curr=n,l=n;for(var o=new Array(arguments.length),s=0;s=31||"undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/))}function r(t){var n=this.useColors;if(t[0]=(n?"%c":"")+this.namespace+(n?" %c":" ")+t[0]+(n?"%c ":" ")+"+"+e.humanize(this.diff),n){var r="color: "+this.color;t.splice(1,0,r,"color: inherit");var i=0,o=0;t[0].replace(/%[a-zA-Z%]/g,function(t){"%%"!==t&&(i++,"%c"===t&&(o=i))}),t.splice(o,0,r)}}function i(){return"object"===("undefined"==typeof console?"undefined":K(console))&&console.log&&Function.prototype.apply.call(console.log,console,arguments)}function o(t){try{null==t?e.storage.removeItem("debug"):e.storage.debug=t}catch(t){}}function s(){var t;try{t=e.storage.debug}catch(t){}return!t&&"undefined"!=typeof process&&"env"in process&&(t=process.env.DEBUG),t}e=t.exports=Ft,e.log=i,e.formatArgs=r,e.save=o,e.load=s,e.useColors=n,e.storage="undefined"!=typeof chrome&&void 0!==chrome.storage?chrome.storage.local:function(){try{return window.localStorage}catch(t){}}(),e.colors=["lightseagreen","forestgreen","goldenrod","dodgerblue","darkorchid","crimson"],e.formatters.j=function(t){try{return JSON.stringify(t)}catch(t){return"[UnexpectedJSONParseError]: "+t.message}},e.enable(s())}),Yt=function(){function t(e,n){if(tt(this,t),this.y=e,this.opts=n,null==n.role||"master"===n.role)this.role="master";else{if("slave"!==n.role)throw new Error("Role must be either 'master' or 'slave'!");this.role="slave"}this.log=Rt("y:connector"),this.logMessage=Rt("y:connector-message"),this._forwardAppliedStructs=n.forwardAppliedOperations||!1,this.role=n.role,this.connections=new Map,this.isSynced=!1,this.userEventListeners=[],this.whenSyncedListeners=[],this.currentSyncTarget=null,this.debug=!0===n.debug,this.broadcastBuffer=new dt,this.broadcastBufferSize=0,this.protocolVersion=11,this.authInfo=n.auth||null,this.checkAuth=n.checkAuth||function(){return Promise.resolve("write")},null==n.maxBufferLength?this.maxBufferLength=-1:this.maxBufferLength=n.maxBufferLength}return et(t,[{key:"reconnect",value:function(){this.log("reconnecting..")}},{key:"disconnect",value:function(){return this.log("discronnecting.."),this.connections=new Map,this.isSynced=!1,this.currentSyncTarget=null,this.whenSyncedListeners=[],Promise.resolve()}},{key:"onUserEvent",value:function(t){this.userEventListeners.push(t)}},{key:"removeUserEventListener",value:function(t){this.userEventListeners=this.userEventListeners.filter(function(e){return t!==e})}},{key:"userLeft",value:function(t){if(this.connections.has(t)){this.log("%s: User left %s",this.y.userID,t),this.connections.delete(t),this._setSyncedWith(null);var e=!0,n=!1,r=void 0;try{for(var i,o=this.userEventListeners[Symbol.iterator]();!(e=(i=o.next()).done);e=!0){(0,i.value)({action:"userLeft",user:t})}}catch(t){n=!0,r=t}finally{try{!e&&o.return&&o.return()}finally{if(n)throw r}}}}},{key:"userJoined",value:function(t,e,n){if(null==e)throw new Error("You must specify the role of the joined user!");if(this.connections.has(t))throw new Error("This user already joined!");this.log("%s: User joined %s",this.y.userID,t),this.connections.set(t,{uid:t,isSynced:!1,role:e,processAfterAuth:[],processAfterSync:[],auth:n||null,receivedSyncStep2:!1});var r={};r.promise=new Promise(function(t){r.resolve=t}),this.connections.get(t).syncStep2=r;var i=!0,o=!1,s=void 0;try{for(var a,l=this.userEventListeners[Symbol.iterator]();!(i=(a=l.next()).done);i=!0){(0,a.value)({action:"userJoined",user:t,role:e})}}catch(t){o=!0,s=t}finally{try{!i&&l.return&&l.return()}finally{if(o)throw s}}this._syncWithUser(t)}},{key:"whenSynced",value:function(t){this.isSynced?t():this.whenSyncedListeners.push(t)}},{key:"_syncWithUser",value:function(t){"slave"!==this.role&&u(this,t)}},{key:"_fireIsSyncedListeners",value:function(){if(!this.isSynced){this.isSynced=!0;var t=!0,e=!1,n=void 0;try{for(var r,i=this.whenSyncedListeners[Symbol.iterator]();!(t=(r=i.next()).done);t=!0){(0,r.value)()}}catch(t){e=!0,n=t}finally{try{!t&&i.return&&i.return()}finally{if(e)throw n}}this.whenSyncedListeners=[],this.y.emit("synced")}}},{key:"send",value:function(t,e){var n=this.y;if(!(e instanceof ArrayBuffer||e instanceof Uint8Array))throw new Error("Expected Message to be an ArrayBuffer or Uint8Array - don't use this method to send custom messages");this.log("User%s to User%s: Send '%y'",n.userID,t,e),this.logMessage("User%s to User%s: Send %Y",n.userID,t,[n,e])}},{key:"broadcast",value:function(t){var e=this.y;if(!(t instanceof ArrayBuffer||t instanceof Uint8Array))throw new Error("Expected Message to be an ArrayBuffer or Uint8Array - don't use this method to send custom messages");this.log("User%s: Broadcast '%y'",e.userID,t),this.logMessage("User%s: Broadcast: %Y",e.userID,[e,t])}},{key:"broadcastStruct",value:function(t){var e=this,n=0===this.broadcastBuffer.length;if(n&&(this.broadcastBuffer.writeVarString(this.y.room),this.broadcastBuffer.writeVarString("update"),this.broadcastBufferSize=0,this.broadcastBufferSizePos=this.broadcastBuffer.pos,this.broadcastBuffer.writeUint32(0)),this.broadcastBufferSize++,t._toBinary(this.broadcastBuffer),this.maxBufferLength>0&&this.broadcastBuffer.length>this.maxBufferLength){var r=this.broadcastBuffer;r.setUint32(this.broadcastBufferSizePos,this.broadcastBufferSize),this.broadcastBuffer=new dt,this.whenRemoteResponsive().then(function(){e.broadcast(r.createBuffer())})}else n&&setTimeout(function(){if(e.broadcastBuffer.length>0){var t=e.broadcastBuffer;t.setUint32(e.broadcastBufferSizePos,e.broadcastBufferSize),e.broadcast(t.createBuffer()),e.broadcastBuffer=new dt}},0)}},{key:"whenRemoteResponsive",value:function(){return new Promise(function(t){setTimeout(t,100)})}},{key:"receiveMessage",value:function(t,e,n){var r=this,i=this.y,o=i.userID;if(n=n||!1,!(e instanceof ArrayBuffer||e instanceof Uint8Array))return Promise.reject(new Error("Expected Message to be an ArrayBuffer or Uint8Array!"));if(t===o)return Promise.resolve();var s=new ft(e),a=new dt,l=s.readVarString();a.writeVarString(l);var u=s.readVarString(),h=this.connections.get(t);if(this.log("User%s from User%s: Receive '%s'",o,t,u),this.logMessage("User%s from User%s: Receive %Y",o,t,[i,e]),null==h&&!n)throw new Error("Received message from unknown peer!");if("sync step 1"===u||"sync step 2"===u){var c=s.readVarUint();if(null==h.auth)return h.processAfterAuth.push([u,h,s,a,t]),this.checkAuth(c,i,t).then(function(t){null==h.auth&&(h.auth=t,i.emit("userAuthenticated",{user:h.uid,auth:t}));var e=h.processAfterAuth;h.processAfterAuth=[],e.forEach(function(t){return r.computeMessage(t[0],t[1],t[2],t[3],t[4])})})}!n&&null==h.auth||"update"===u&&!h.isSynced?h.processAfterSync.push([u,h,s,a,t,!1]):this.computeMessage(u,h,s,a,t,n)}},{key:"computeMessage",value:function(t,e,n,i,o,s){if("sync step 1"!==t||"write"!==e.auth&&"read"!==e.auth){var a=this.y;a.transact(function(){if("sync step 2"===t&&"write"===e.auth)_(n,i,a,e,o);else{if("update"!==t||!s&&"write"!==e.auth)throw new Error("Unable to receive message");r(n,i,a,e,o)}},!0)}else c(n,i,this.y,e,o)}},{key:"_setSyncedWith",value:function(t){var e=this;if(null!=t){var n=this.connections.get(t);n.isSynced=!0;var r=n.processAfterSync;n.processAfterSync=[],r.forEach(function(t){e.computeMessage(t[0],t[1],t[2],t[3],t[4])})}var i=Array.from(this.connections.values());i.length>0&&i.every(function(t){return t.isSynced})&&this._fireIsSyncedListeners()}}]),t}();YXmlFragment._YXmlElement=YXmlElement;var Y=function(t){function Y(t){tt(this,Y);var e=it(this,(Y.__proto__||Object.getPrototypeOf(Y)).call(this));return e._opts=t,e.userID=null!=t._userID?t._userID:H(),e.share={},e.ds=new ht(e),e.os=new xt(e),e.ss=new Dt(e),e.connector=new Y[t.connector.name](e,t.connector),null!=t.persistence?(e.persistence=new Y[t.persistence.name](e,t.persistence),e.persistence.retrieveContent()):e.persistence=null,e.connected=!0,e._missingStructs=new Map,e._readyToIntegrate=[],e._transaction=null,e}return rt(Y,t),et(Y,[{key:"_beforeChange",value:function(){}},{key:"transact",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1],n=null===this._transaction;n&&(this.emit("beforeTransaction",this,e),this._transaction=new gt(this));try{t(this)}catch(t){console.error(t)}n&&(this._transaction.changedTypes.forEach(function(t,n){n._deleted||n._callObserver(t,e)}),this._transaction.changedParentTypes.forEach(function(t,e){e._deleted||(t=t.filter(function(t){return!t.target._deleted}),e._deepEventHandler.callEventListeners(t))}),this.emit("afterTransaction",this,e),this._transaction=null)}},{key:"define",value:function(t,e){var n=new Bt(t,e),r=this.os.get(n);if(null===r&&(r=new e,r._id=n,r._parent=this,r._integrate(this),void 0!==this.share[t]))throw new Error("Type is already defined with a different constructor!");return void 0===this.share[t]&&(this.share[t]=r),r}},{key:"get",value:function(t){return this.share[t]}},{key:"disconnect",value:function(){return this.connected?(this.connected=!1,this.connector.disconnect()):Promise.resolve()}},{key:"reconnect",value:function(){return this.connected?Promise.resolve():(this.connected=!0,this.connector.reconnect())}},{key:"destroy",value:function(){this.share=null,null!=this.connector.destroy?this.connector.destroy():this.connector.disconnect(),this.os=null,this.ds=null,this.ss=null}},{key:"whenSynced",value:function(){var t=this;return new Promise(function(e){t.once("synced",function(){e()})})}},{key:"_start",get:function(){return null},set:function(t){return null}},{key:"room",get:function(){return this._opts.connector.room}}]),Y}(It);return Y.extend=function(){for(var t=0;t