/** * yjs - A framework for real-time p2p shared editing on any data * @version v13.0.0-54 * @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){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 n(e,n){for(var r=n.readUint32(),i=0;i=0;d--){var _=u[d];f.has(_.user)||f.set(_.user,new Map);var v=f.get(_.user);v.has(_.clock)||v.set(_.clock,[]);(v=v.get(_.clock)).push(h)}}}}function r(t){for(var e=new Map,n=t.readUint32(),r=0;r0){var c=0,h=i[c],f=[];t.ds.iterate(new ot(r,0),new ot(r,Number.MAX_VALUE),function(t){for(;null!=h;){var e=0;if(t._id.clock+t.len<=h[0])break;h[0]=0;d--){var _=f[d];y(t,_[0],_[1],_[2])}for(;c0)for(var a=t.os.findNode(new ot(e,n));null!==a&&r>0&&a.val._id.equals(new ot(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 p(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 g(t,e,n,r){var i=e._id;n._id=new ot(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 m(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 b(t){for(;null!==t&&t._deleted;)t=t._right;return t}function w(t,e,n){var r=t.insertDomElementsAfter(e,[n]);return r.length>0?r[0]:e}function S(t){var e=t._yxml;if(e.constructor!==YXmlHook){var 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=b(e._start),l=0;le)return[n._id.user,n._id.clock+e];e-=n._length}n=n._right}return["endof",t._id.user,t._id.clock||null,t._id.name||null,t._id.type||null]}function U(t,e){if("endof"===e[0]){var n=void 0;n=null===e[3]?new ot(e[1],e[2]):new bt(e[3],e[4]);var r=t.os.get(n);return{type:r,offset:r.length}}var i=0,o=t.os.findNodeWithUpperBound(new ot(e[0],e[1])).val,s=o._parent;if(s._deleted)return null;for(o._deleted||(i=e[1]-o._id.clock),o=o._left;null!==o;)o._deleted||(i+=o._length),o=o._left;return{type:s,offset:i}}function A(t,e,n){if(null!==_t&&n){var r=_t.to,i=_t.from,o=_t.fromY,s=_t.toY,a=!1,l=dt.anchorNode,u=dt.anchorOffset,c=dt.focusNode,h=dt.focusOffset;if(null!==i){var f=U(o,i);if(null!==f){var d=f.type.getDom(),_=f.offset;d===l&&_===u||(l=d,u=_,a=!0)}}if(null!==r){var v=U(s,r);if(null!==v){var y=v.type.getDom(),p=v.offset;y===c&&p===h||(c=y,h=p,a=!0)}}a&&dt.setBaseAndExtent(l,u,c,h)}}function B(t,e){for(var n=0,r=0;n0;){var i=n.pop();null!==i.fromState&&(t.os.getItemCleanStart(i.fromState),t.os.getItemCleanEnd(i.toState),t.os.iterate(i.fromState,i.toState,function(n){for(;n._deleted&&null!==n._redone;)n=n._redone;!1===n._deleted&&P(t,n,e)&&(r=!0,n._delete(t))}));var o=!0,s=!1,a=void 0;try{for(var l,u=i.deletedStructs[Symbol.iterator]();!(o=(l=u.next()).done);o=!0){var c=l.value;P(t,c,e)&&c._parent!==t&&(c._id.user!==t.userID||null===i.fromState||c._id.clocki.toState.clock)&&(r=!0,c._redo(t))}}catch(t){s=!0,a=t}finally{try{!o&&u.return&&u.return()}finally{if(s)throw a}}}}),r}function M(t,e){return e={exports:{}},t(e,e.exports),e.exports}function C(t){if(t=String(t),!(t.length>100)){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*Tt;case"days":case"day":case"d":return n*Dt;case"hours":case"hour":case"hrs":case"hr":case"h":return n*xt;case"minutes":case"minute":case"mins":case"min":case"m":return n*Bt;case"seconds":case"second":case"secs":case"sec":case"s":return n*At;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return n;default:return}}}}function R(t){return t>=Dt?Math.round(t/Dt)+"d":t>=xt?Math.round(t/xt)+"h":t>=Bt?Math.round(t/Bt)+"m":t>=At?Math.round(t/At)+"s":t+"ms"}function F(t){return H(t,Dt,"day")||H(t,xt,"hour")||H(t,Bt,"minute")||H(t,At,"second")||t+" ms"}function H(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 ot(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}(it),lt=function(){function t(e){if(G(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 K(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=unescape(encodeURIComponent(t)),n=e.split("").map(function(t){return t.codePointAt()}),r=n.length;this.writeVarUint(r);for(var i=0;i1&&void 0!==arguments[1]&&arguments[1])null!==t.connector&&t.connector.broadcastStruct(this);else{var e=this._targetID;y(t,e.user,e.clock,this._length)}null!==t.persistence&&t.persistence.saveStruct(t,this)}},{key:"_logString",value:function(){return"Delete - target: "+v(this._targetID)+", len: "+this._length}}]),Delete}(),ht=function t(e){G(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(){G(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,this._redone=null}return K(Item,[{key:"_copy",value:function(){return new this.constructor}},{key:"_redo",value:function(t){if(null!==this._redone)return this._redone;var e=this._copy(),n=this._left,r=this,i=this._parent;if(!0===i._deleted&&null===i._redone&&i._redo(t),null!==i._redone){for(i=i._redone;null!==n;){if(null!==n._redone&&n._redone._parent===i){n=n._redone;break}n=n._left}for(;null!==r;)null!==r._redone&&r._redone._parent===i&&(r=r._redone),r=r._right}return e._origin=n,e._left=n,e._right=r,e._right_origin=r,e._parent=i,e._parentSub=this._parentSub,e._integrate(t),this._redone=e,e}},{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){this._deleted=!0,t.ds.markDeleted(this._id,this._length);var n=new Delete;n._targetID=this._id,n._length=this._length,e?n._integrate(t,!0):null!==t.persistence&&t.persistence.saveStruct(t,n),p(t,this._parent,this._parentSub),t._transaction.deletedStructs.add(this)}}},{key:"_beforeChange",value:function(){}},{key:"_integrate",value:function(t){t._transaction.newTypes.add(this);var e=this._parent,n=this._id,r=null===n?t.userID:n.user,i=t.ss.getState(r);if(null===n)this._id=t.ss.getNextID(this._length);else if(n.user===kt);else{if(n.clock=this._length)return this._right;var n=new ItemJSON;return n._content=this._content.splice(e),g(t,this,n,e),n}},{key:"_length",get:function(){return this._content.length}}]),ItemJSON}(Item),ItemString=function(t){function ItemString(){G(this,ItemString);var t=et(this,(ItemString.__proto__||Object.getPrototypeOf(ItemString)).call(this));return t._content=null,t}return tt(ItemString,t),K(ItemString,[{key:"_copy",value:function(){var t=Q(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=Q(ItemString.prototype.__proto__||Object.getPrototypeOf(ItemString.prototype),"_fromBinary",this).call(this,t,e);return this._content=e.readVarString(),n}},{key:"_toBinary",value:function(t){Q(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:"+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),g(t,this,n,e),n}},{key:"_length",get:function(){return this._content.length}}]),ItemString}(Item),YEvent=function(){function YEvent(t){G(this,YEvent),this.target=t,this.currentTarget=t}return K(YEvent,[{key:"path",get:function(){for(var t=[],e=this.target,n=e._y;e!==this.currentTarget&&e!==n;){var r=e._parent;if(null!==e._parentSub)t.unshift(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=nt(a.value,2),c=u[0];if(u[1]===e){t.unshift(c);break}}}catch(t){o=!0,s=t}finally{try{!i&&l.return&&l.return()}finally{if(o)throw s}}}e=r}return t}}]),YEvent}(),YArrayEvent=function(t){function YArrayEvent(t,e,n){G(this,YArrayEvent);var r=et(this,(YArrayEvent.__proto__||Object.getPrototypeOf(YArrayEvent)).call(this,t));return r.remote=e,r._transaction=n,r._addedElements=null,r}return tt(YArrayEvent,t),K(YArrayEvent,[{key:"addedElements",get:function(){if(null===this._addedElements){var t=this.target,e=this._transaction,n=new Set;e.newTypes.forEach(function(r){r._parent!==t||e.deletedStructs.has(r)||n.add(r)}),this._addedElements=n}return this._addedElements}},{key:"removedElements",get:function(){var t=this.target,e=this._transaction,n=new Set;return e.deletedStructs.forEach(function(r){r._parent!==t||e.newTypes.has(r)||n.add(r)}),n}}]),YArrayEvent}(YEvent),YArray=function(t){function YArray(){return G(this,YArray),et(this,(YArray.__proto__||Object.getPrototypeOf(YArray)).apply(this,arguments))}return tt(YArray,t),K(YArray,[{key:"_callObserver",value:function(t,e,n){this._callEventHandler(t,new YArrayEvent(this,n,t))}},{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:"push",value:function(t){for(var e=this._start,n=null;null!==e;)e._deleted||(n=e),e=e._right;this.insertAfter(n,t)}},{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:"+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){G(this,YMapEvent);var r=et(this,(YMapEvent.__proto__||Object.getPrototypeOf(YMapEvent)).call(this,t));return r.keysChanged=e,r.remote=n,r}return tt(YMapEvent,t),YMapEvent}(YEvent),YMap=function(t){function YMap(){return G(this,YMap),et(this,(YMap.__proto__||Object.getPrototypeOf(YMap)).apply(this,arguments))}return tt(YMap,t),K(YMap,[{key:"_callObserver",value:function(t,e,n){this._callEventHandler(t,new YMapEvent(this,e,n))}},{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=nt(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=nt(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.constructor===ItemJSON&&!i._deleted&&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:"+this._parentSub+")"}}]),YMap}(Type),YText=function(t){function YText(t){G(this,YText);var e=et(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 tt(YText,t),K(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;e.length<=0||this._transact(function(r){for(var i=null,o=n._start,s=0;null!==o;){var a=o._deleted?0:o._length-1;if(s<=t&&t<=s+a){var l=t-s;o=o._splitAt(n._y,l),i=o._left,s+=l;break}o._deleted||(s+=o._length),i=o,o=o._right}if(t>s)throw new Error("Position exceeds array range!");var u=new ItemString;u._origin=i,u._left=i,u._right=o,u._right_origin=o,u._parent=n,u._content=e,null!==r?u._integrate(n._y):null===i?n._start=u:i._right=u})}},{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:"+this._parentSub+")"}}]),YText}(YArray),dt=null,_t=null,vt=void 0;vt="undefined"!=typeof getSelection?function(t,e,n){if(n){_t={from:null,to:null,fromY:null,toY:null},dt=getSelection();var r=dt.anchorNode;if(null!==r&&null!=r._yxml){var i=r._yxml;_t.from=O(i,dt.anchorOffset),_t.fromY=i._y}var o=dt.focusNode;if(null!==o&&null!=o._yxml){var s=o._yxml;_t.to=O(s,dt.focusOffset),_t.toY=s._y}}}:function(){};var YXmlEvent=function(t){function YXmlEvent(t,e,n){G(this,YXmlEvent);var r=et(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 tt(YXmlEvent,t),YXmlEvent}(YEvent),yt=function(){function t(e,n){G(this,t),this._filter=n||function(){return!0},this._root=e,this._currentNode=e,this._firstCall=!0}return K(t,[{key:Symbol.iterator,value:function(){return this}},{key:"next",value:function(){var t=this._currentNode;if(this._firstCall&&(this._firstCall=!1,!t._deleted&&this._filter(t)))return{value:t,done:!1};do{if(t._deleted||t.constructor!==YXmlFragment._YXmlElement&&t.constructor!==YXmlFragment||null===t._start){for(;t!==this._root;){if(null!==t._right){t=t._right;break}t=t._parent}t===this._root&&(t=null)}else t=t._start;if(t===this._root)break}while(null!==t&&(t._deleted||!this._filter(t)));return this._currentNode=t,null===t?{done:!0}:{value:t,done:!1}}}]),t}(),YXmlFragment=function(t){function YXmlFragment(){G(this,YXmlFragment);var t=et(this,(YXmlFragment.__proto__||Object.getPrototypeOf(YXmlFragment)).call(this));t._dom=null,t._domFilter=k,t._domObserver=null;var e=!0;return t._mutualExclude=function(t){if(e){e=!1;try{t()}catch(t){console.error(t)}e=!0}},t}return tt(YXmlFragment,t),K(YXmlFragment,[{key:"createTreeWalker",value:function(t){return new yt(this,t)}},{key:"querySelector",value:function(t){t=t.toUpperCase();var e=new yt(this,function(e){return e.nodeName===t}),n=e.next();return n.done?null:n.value}},{key:"querySelectorAll",value:function(t){return t=t.toUpperCase(),Array.from(new yt(this,function(e){return e.nodeName===t}))}},{key:"enableSmartScrolling",value:function(t){this._scrollElement=t,this.forEach(function(e){e.enableSmartScrolling(t)})}},{key:"setDomFilter",value:function(t){var e=this;this._domFilter=t;var n=new Map;if(void 0!==this.getAttributes){var r=this.getAttributes();for(var i in r)n.set(i,r[i])}this._y.transact(function(){var r=e._domFilter(e.nodeName,new Map(n));null===r?e._delete(e._y):n.forEach(function(t,n){r.has(n)||e.removeAttribute(n)}),e.forEach(function(e){e.setDomFilter(t)})})}},{key:"_callObserver",value:function(t,e,n){this._callEventHandler(t,new YXmlEvent(this,e,n))}},{key:"toString",value:function(){return this.map(function(t){return t.toString()}).join("")}},{key:"_delete",value:function(t,e){this._unbindFromDom(),Q(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),void 0!==this._beforeTransactionHandler&&this._y.off("beforeTransaction",this._beforeTransactionHandler)}},{key:"insertDomElementsAfter",value:function(t,e,n){var r=x(this,e,n);return this.insertAfter(t,r),r}},{key:"insertDomElements",value:function(t,e,n){var r=x(this,e,n);return this.insert(t,r),r}},{key:"getDom",value:function(){return this._dom}},{key:"bindToDom",value:function(t,e){null!=this._dom&&this._unbindFromDom(),null!=t._yxml&&t._yxml._unbindFromDom(),t.innerHTML="",this.forEach(function(n){t.insertBefore(n.getDom(e),null)}),this._bindToDom(t,e)}},{key:"_bindToDom",value:function(t,e){var n=this;if(e=e||document,this._dom=t,t._yxml=this,null!==this._parent){this._y.on("beforeTransaction",vt),this._y.on("afterTransaction",A);var r=function(t){if(!t._deleted){for(var e=!1,r=t;r!==n._y;){if(r===n){e=!0;break}r=r._parent}if(e){var i=new Map;if(void 0!==t.getAttributes){var o=t.getAttributes();for(var s in o)i.set(s,o[s])}var a=n._domFilter(t.nodeName,new Map(i));null===a?t._delete(n._y):i.forEach(function(e,n){a.has(n)||t.removeAttribute(n)})}}};return this._y.on("beforeObserverCalls",function(t,e){e.changedTypes.forEach(function(t,e){(t.size>1||!t.has(null))&&r(e)}),e.newTypes.forEach(r)}),this.observeDeep(function(t){E.call(n,t,e)}),"undefined"!=typeof MutationObserver&&(this._beforeTransactionHandler=function(){n._domObserverListener(n._domObserver.takeRecords())},this._y.on("beforeTransaction",this._beforeTransactionHandler),this._domObserverListener=function(t){n._mutualExclude(function(){n._y.transact(function(){var e=new Set;t.forEach(function(t){var r=t.target,i=r._yxml;if(null!=i&&i.constructor!==YXmlHook)switch(t.type){case"characterData":var o=B(i.toString(),r.nodeValue);i.delete(o.pos,o.remove),i.insert(o.pos,o.insert);break;case"attributes":if(i.constructor===YXmlFragment)break;var s=t.attributeName,a=r.getAttribute(s),l=new Map;l.set(s,a),n._domFilter(r.nodeName,l).size>0&&i.constructor!==YXmlFragment&&i.getAttribute(s)!==a&&(null==a?i.removeAttribute(s):i.setAttribute(s,a));break;case"childList":e.add(t.target)}});var r=!0,i=!1,o=void 0;try{for(var s,a=e[Symbol.iterator]();!(r=(s=a.next()).done);r=!0){var l=s.value;void 0!==l.yOnChildrenChanged&&l.yOnChildrenChanged(),null!=l._yxml&&!1!==l._yxml&&S(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,n){G(this,YXmlElement);var r=et(this,(YXmlElement.__proto__||Object.getPrototypeOf(YXmlElement)).call(this));return r.nodeName=null,r._scrollElement=null,"string"==typeof t?r.nodeName=t.toUpperCase():null!=t&&null!=t.nodeType&&t.nodeType===t.ELEMENT_NODE?(r.nodeName=t.nodeName,r._setDom(t,n)):r.nodeName="UNDEFINED","function"==typeof e&&(r._domFilter=e),r}return tt(YXmlElement,t),K(YXmlElement,[{key:"_copy",value:function(){var t=Q(YXmlElement.prototype.__proto__||Object.getPrototypeOf(YXmlElement.prototype),"_copy",this).call(this);return t.nodeName=this.nodeName,t}},{key:"_setDom",value:function(t,e){var n=this;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");for(var r=new Map,i=0;i0?" "+e.join(" "):"")+">"+Q(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=nt(i.value,2),a=s[0],l=s[1];l._deleted||(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),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,t)}return e}}]),YXmlElement}(YXmlFragment),pt={},YXmlHook=function(t){function YXmlHook(t,e){G(this,YXmlHook);var n=et(this,(YXmlHook.__proto__||Object.getPrototypeOf(YXmlHook)).call(this));return n._dom=null,n.hookName=null,void 0!==t&&(n.hookName=t,n._dom=e,e._yjsHook=t,e._yxml=n,T(t).fillType(e,n)),n}return tt(YXmlHook,t),K(YXmlHook,[{key:"_copy",value:function(){var t=Q(YXmlHook.prototype.__proto__||Object.getPrototypeOf(YXmlHook.prototype),"_copy",this).call(this);return t.hookName=this.hookName,t}},{key:"getDom",value:function(t){if(t=t||document,null===this._dom){var e=T(this.hookName).createDom(this);this._dom=e,e._yxml=this,e._yjsHook=this.hookName}return this._dom}},{key:"_unbindFromDom",value:function(){this._dom._yxml=null,this._yxml=null}},{key:"_fromBinary",value:function(t,e){var n=Q(YXmlHook.prototype.__proto__||Object.getPrototypeOf(YXmlHook.prototype),"_fromBinary",this).call(this,t,e);return this.hookName=e.readVarString(),n}},{key:"_toBinary",value:function(t){Q(YXmlHook.prototype.__proto__||Object.getPrototypeOf(YXmlHook.prototype),"_toBinary",this).call(this,t),t.writeVarString(this.hookName)}},{key:"_integrate",value:function(t){if(null===this.hookName)throw new Error("hookName must be defined!");Q(YXmlHook.prototype.__proto__||Object.getPrototypeOf(YXmlHook.prototype),"_integrate",this).call(this,t)}},{key:"setDomFilter",value:function(){}},{key:"enableSmartScrolling",value:function(){}}]),YXmlHook}(YMap);YXmlHook.addHook=D;var YXmlText=function(t){function YXmlText(t){G(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=et(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 tt(YXmlText,t),K(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(),Q(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);YXmlFragment._YXmlElement=YXmlElement,YXmlFragment._YXmlHook=YXmlHook;var gt=new Map,mt=new Map;N(0,ItemJSON),N(1,ItemString),N(2,Delete),N(3,YArray),N(4,YMap),N(5,YText),N(6,YXmlFragment),N(7,YXmlElement),N(8,YXmlText),N(9,YXmlHook);var kt=16777215,bt=function(){function t(e,n){G(this,t),this.user=kt,this.name=e,this.type=L(n)}return K(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(e){return e.constructor!==t||(this.user1?e-1:0),r=1;r1&&void 0!==arguments[1]?arguments[1]:{};G(this,t),this.options=r,r.captureTimeout=null==r.captureTimeout?500:r.captureTimeout,this._undoBuffer=[],this._redoBuffer=[],this._scope=e,this._undoing=!1,this._redoing=!1,this._lastTransactionWasUndo=!1;var i=e._y;this.y=i,i.on("afterTransaction",function(t,i,o){if(!o&&i.changedParentTypes.has(e)){var s=new Ot(t,i);if(n._undoing)n._lastTransactionWasUndo=!0,n._redoBuffer.push(s);else{var a=n._undoBuffer.length>0?n._undoBuffer[n._undoBuffer.length-1]:null;!1===n._redoing&&!1===n._lastTransactionWasUndo&&null!==a&&s.created-a.created<=r.captureTimeout?(a.created=s.created,null!==s.toState&&(a.toState=s.toState,null===a.fromState&&(a.fromState=s.fromState)),s.deletedStructs.forEach(a.deletedStructs.add,a.deletedStructs)):(n._lastTransactionWasUndo=!1,n._undoBuffer.push(s)),n._redoing||(n._redoBuffer=[])}}})}return K(t,[{key:"undo",value:function(){this._undoing=!0;var t=j(this.y,this._scope,this._undoBuffer);return this._undoing=!1,t}},{key:"redo",value:function(){this._redoing=!0;var t=j(this.y,this._scope,this._redoBuffer);return this._redoing=!1,t}}]),t}(),At=1e3,Bt=60*At,xt=60*Bt,Dt=24*xt,Tt=365.25*Dt,Nt=function(t,e){e=e||{};var n=void 0===t?"undefined":Z(t);if("string"===n&&t.length>0)return C(t);if("number"===n&&!1===isNaN(t))return e.long?F(t):R(t);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(t))},It=M(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":Z(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=It,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())})),Vt=(Lt.log,Lt.formatArgs,Lt.save,Lt.load,Lt.useColors,Lt.storage,Lt.colors,function(){function t(e,n){if(G(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=Lt("y:connector"),this.logMessage=Lt("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 ct,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 K(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&&l(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._setContentReady(),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 ct,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 ct}},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 lt(e),a=new ct,l=s.readVarString();a.writeVarString(l);var u=s.readVarString(),c=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==c&&!n)throw new Error("Received message from unknown peer!");if("sync step 1"===u||"sync step 2"===u){var h=s.readVarUint();if(null==c.auth)return c.processAfterAuth.push([u,c,s,a,t]),this.checkAuth(h,i,t).then(function(t){null==c.auth&&(c.auth=t,i.emit("userAuthenticated",{user:c.uid,auth:t}));var e=c.processAfterAuth;c.processAfterAuth=[],e.forEach(function(t){return r.computeMessage(t[0],t[1],t[2],t[3],t[4])})})}!n&&null==c.auth||"update"===u&&!c.isSynced?c.processAfterSync.push([u,c,s,a,t,!1]):this.computeMessage(u,c,s,a,t,n)}},{key:"computeMessage",value:function(t,e,r,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)f(r,i,a,e,o);else{if("update"!==t||!s&&"write"!==e.auth)throw new Error("Unable to receive message");n(a,r)}},!0)}else c(r,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}()),Pt=function(){function t(e){G(this,t),this.opts=e,this.ys=new Map}return K(t,[{key:"_init",value:function(t){var e=this,n=this.ys.get(t);return void 0===n?(n=X(),n.mutualExclude=z(),this.ys.set(t,n),this.init(t).then(function(){return t.on("afterTransaction",function(t,n){var r=e.ys.get(t);if(r.len>0){r.buffer.setUint32(0,r.len),e.saveUpdate(t,r.buffer.createBuffer(),n);var i=X();for(var o in i)r[o]=i[o]}}),e.retrieve(t)}).then(function(){return Promise.resolve(n)})):Promise.resolve(n)}},{key:"deinit",value:function(t){this.ys.delete(t),t.persistence=null}},{key:"destroy",value:function(){this.ys=null}},{key:"removePersistedData",value:function(t){var e=this,n=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];this.ys.forEach(function(r,i){i.room===t&&(n?i.destroy():e.deinit(i))})}},{key:"saveUpdate",value:function(t){}},{key:"saveStruct",value:function(t,e){var n=this.ys.get(t);void 0!==n&&n.mutualExclude(function(){e._toBinary(n.buffer),n.len++})}},{key:"retrieve",value:function(t,e,r){var i=this.ys.get(t);void 0!==i&&i.mutualExclude(function(){t.transact(function(){if(null!=e&&W(t,new lt(new Uint8Array(e))),null!=r)for(var i=0;i1&&void 0!==arguments[1]&&arguments[1],n=null===this._transaction;n&&(this._transaction=new ht(this),this.emit("beforeTransaction",this,this._transaction,e));try{t(this)}catch(t){console.error(t)}if(n){this.emit("beforeObserverCalls",this,this._transaction,e);var r=this._transaction;this._transaction=null,r.changedTypes.forEach(function(t,n){n._deleted||n._callObserver(r,t,e)}),r.changedParentTypes.forEach(function(t,e){e._deleted||(t=t.filter(function(t){return!t.target._deleted}),t.forEach(function(t){t.currentTarget=e}),e._deepEventHandler.callEventListeners(r,t))}),this.emit("afterTransaction",this,r,e)}}},{key:"define",value:function(t,e){var n=new bt(t,e),r=this.os.get(n);if(void 0===this.share[t])this.share[t]=r;else if(this.share[t]!==r)throw new Error("Type is already defined with a different constructor");return 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(){Q(Y.prototype.__proto__||Object.getPrototypeOf(Y.prototype),"destroy",this).call(this),this.share=null,null!=this.connector&&(null!=this.connector.destroy?this.connector.destroy():this.connector.disconnect()),null!==this.persistence&&(this.persistence.deinit(this),this.persistence=null),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}}]),Y}(Et);return Y.extend=function(){for(var t=0;t