Files
hedgedoc-hedgeagent/public/vendor/ot/ot.min.js
2026-03-24 21:12:41 +01:00

2 lines
27 KiB
JavaScript

var ot;function hex2rgb(t){if(3==(t="#"==t[0]?t.substr(1):t).length){var e=t;t="";for(var e=/^([a-f0-9])([a-f0-9])([a-f0-9])$/i.exec(e).slice(1),n=0;n<3;n++)t+=e[n]+e[n]}var o=/^([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i.exec(t).slice(1);return{red:parseInt(o[0],16),green:parseInt(o[1],16),blue:parseInt(o[2],16)}}(ot=void 0===ot?{}:ot).TextOperation=(()=>{function l(){if(!this||this.constructor!==l)return new l;this.ops=[],this.baseLength=0,this.targetLength=0}l.prototype.equals=function(t){if(this.baseLength!==t.baseLength)return!1;if(this.targetLength!==t.targetLength)return!1;if(this.ops.length!==t.ops.length)return!1;for(var e=0;e<this.ops.length;e++)if(this.ops[e]!==t.ops[e])return!1;return!0};var u=l.isRetain=function(t){return"number"==typeof t&&0<t},f=l.isInsert=function(t){return"string"==typeof t},g=l.isDelete=function(t){return"number"==typeof t&&t<0};function i(t){var e=t.ops,n=l.isRetain;switch(e.length){case 1:return e[0];case 2:return n(e[0])?e[1]:n(e[1])?e[0]:null;case 3:if(n(e[0])&&n(e[2]))return e[1]}return null}function r(t){return u(t.ops[0])?t.ops[0]:0}return l.prototype.retain=function(t){if("number"!=typeof t)throw new Error("retain expects an integer");return 0!==t&&(this.baseLength+=t,this.targetLength+=t,u(this.ops[this.ops.length-1])?this.ops[this.ops.length-1]+=t:this.ops.push(t)),this},l.prototype.insert=function(t){if("string"!=typeof t)throw new Error("insert expects a string");var e;return""!==t&&(this.targetLength+=t.length,e=this.ops,f(e[e.length-1])?e[e.length-1]+=t:g(e[e.length-1])?f(e[e.length-2])?e[e.length-2]+=t:(e[e.length]=e[e.length-1],e[e.length-2]=t):e.push(t)),this},l.prototype.delete=function(t){if("number"!=typeof(t="string"==typeof t?t.length:t))throw new Error("delete expects an integer or a string");return 0!==t&&(this.baseLength-=t=0<t?-t:t,g(this.ops[this.ops.length-1])?this.ops[this.ops.length-1]+=t:this.ops.push(t)),this},l.prototype.isNoop=function(){return 0===this.ops.length||1===this.ops.length&&u(this.ops[0])},l.prototype.toString=function(){return(Array.prototype.map||function(t){for(var e=[],n=0,o=this.length;n<o;n++)e[n]=t(this[n]);return e}).call(this.ops,function(t){return u(t)?"retain "+t:f(t)?"insert '"+t+"'":"delete "+-t}).join(", ")},l.prototype.toJSON=function(){return this.ops},l.fromJSON=function(t){for(var e=new l,n=0,o=t.length;n<o;n++){var i=t[n];if(u(i))e.retain(i);else if(f(i))e.insert(i);else{if(!g(i))throw new Error("unknown operation: "+JSON.stringify(i));e.delete(i)}}return e},l.prototype.apply=function(t){if(t.length!==this.baseLength)throw new Error("The operation's base length must be equal to the string's length.");for(var e=[],n=0,o=0,i=this.ops,r=0,s=i.length;r<s;r++){var a=i[r];if(u(a)){if(o+a>t.length)throw new Error("Operation can't retain more characters than are left in the string.");e[n++]=t.slice(o,o+a),o+=a}else f(a)?e[n++]=a:o-=a}if(o!==t.length)throw new Error("The operation didn't operate on the whole string.");return e.join("")},l.prototype.invert=function(t){for(var e=0,n=new l,o=this.ops,i=0,r=o.length;i<r;i++){var s=o[i];u(s)?(n.retain(s),e+=s):f(s)?n.delete(s.length):(n.insert(t.slice(e,e-s)),e-=s)}return n},l.prototype.compose=function(t){if(this.targetLength!==t.baseLength)throw new Error("The base length of the second operation has to be the target length of the first operation");for(var e=new l,n=this.ops,o=t.ops,i=0,r=0,s=n[i++],a=o[r++];void 0!==s||void 0!==a;)if(g(s))e.delete(s),s=n[i++];else if(f(a))e.insert(a),a=o[r++];else{if(void 0===s)throw new Error("Cannot compose operations: first operation is too short.");if(void 0===a)throw new Error("Cannot compose operations: first operation is too long.");if(u(s)&&u(a))a<s?(e.retain(a),s-=a,a=o[r++]):s===a?(e.retain(s),s=n[i++],a=o[r++]):(e.retain(s),a-=s,s=n[i++]);else if(f(s)&&g(a))s.length>-a?(s=s.slice(-a),a=o[r++]):s.length===-a?(s=n[i++],a=o[r++]):(a+=s.length,s=n[i++]);else if(f(s)&&u(a))s.length>a?(e.insert(s.slice(0,a)),s=s.slice(a),a=o[r++]):s.length===a?(e.insert(s),s=n[i++],a=o[r++]):(e.insert(s),a-=s.length,s=n[i++]);else{if(!u(s)||!g(a))throw new Error("This shouldn't happen: op1: "+JSON.stringify(s)+", op2: "+JSON.stringify(a));-a<s?(e.delete(a),s+=a,a=o[r++]):s===-a?(e.delete(a),s=n[i++],a=o[r++]):(e.delete(s),a+=s,s=n[i++])}}return e},l.prototype.shouldBeComposedWith=function(t){var e,n,o;return!!(this.isNoop()||t.isNoop()||(e=r(this),n=r(t),o=i(this),t=i(t),o&&t&&(f(o)&&f(t)?e+o.length===n:g(o)&&g(t)&&(n-t===e||e===n))))},l.prototype.shouldBeComposedWithInverted=function(t){var e,n,o;return!!(this.isNoop()||t.isNoop()||(e=r(this),n=r(t),o=i(this),t=i(t),o&&t&&(f(o)&&f(t)?e+o.length===n||e===n:g(o)&&g(t)&&n-t===e)))},l.transform=function(t,e){if(t.baseLength!==e.baseLength)throw new Error("Both operations have to have the same base length");for(var n,o=new l,i=new l,r=t.ops,s=e.ops,a=0,p=0,h=r[a++],c=s[p++];void 0!==h||void 0!==c;)if(f(h))o.insert(h),i.retain(h.length),h=r[a++];else if(f(c))o.retain(c.length),i.insert(c),c=s[p++];else{if(void 0===h)throw new Error("Cannot compose operations: first operation is too short.");if(void 0===c)throw new Error("Cannot compose operations: first operation is too long.");if(u(h)&&u(c))c<h?(h-=n=c,c=s[p++]):h===c?(n=c,h=r[a++],c=s[p++]):(c-=n=h,h=r[a++]),o.retain(n),i.retain(n);else if(g(h)&&g(c))-c<-h?(h-=c,c=s[p++]):h===c?(h=r[a++],c=s[p++]):(c-=h,h=r[a++]);else if(g(h)&&u(c))c<-h?(h+=n=c,c=s[p++]):-h===c?(n=c,h=r[a++],c=s[p++]):(n=-h,c+=h,h=r[a++]),o.delete(n);else{if(!u(h)||!g(c))throw new Error("The two operations aren't compatible");-c<h?(n=-c,h+=c,c=s[p++]):h===-c?(n=h,h=r[a++],c=s[p++]):(c+=n=h,h=r[a++]),i.delete(n)}}return[o,i]},l})(),"object"==typeof module&&(module.exports=ot.TextOperation),(ot=void 0===ot?{}:ot).Selection=(t=>{var s=t.ot?t.ot.TextOperation:require("./text-operation");function i(t,e){this.anchor=t,this.head=e}function r(t){this.ranges=t||[]}return i.fromJSON=function(t){return new i(t.anchor,t.head)},i.prototype.equals=function(t){return this.anchor===t.anchor&&this.head===t.head},i.prototype.isEmpty=function(){return this.anchor===this.head},i.prototype.transform=function(r){function t(t){for(var e=t,n=r.ops,o=0,i=r.ops.length;o<i&&(s.isRetain(n[o])?t-=n[o]:s.isInsert(n[o])?e+=n[o].length:(e-=Math.min(t,-n[o]),t+=n[o]),!(t<0));o++);return e}var e=t(this.anchor);return this.anchor===this.head?new i(e,e):new i(e,t(this.head))},r.Range=i,r.createCursor=function(t){return new r([new i(t,t)])},r.fromJSON=function(t){for(var e=t.ranges||t,n=0,o=[];n<e.length;n++)o[n]=i.fromJSON(e[n]);return new r(o)},r.prototype.equals=function(t){if(this.position!==t.position)return!1;if(this.ranges.length!==t.ranges.length)return!1;for(var e=0;e<this.ranges.length;e++)if(!this.ranges[e].equals(t.ranges[e]))return!1;return!0},r.prototype.somethingSelected=function(){for(var t=0;t<this.ranges.length;t++)if(!this.ranges[t].isEmpty())return!0;return!1},r.prototype.compose=function(t){return t},r.prototype.transform=function(t){for(var e=0,n=[];e<this.ranges.length;e++)n[e]=this.ranges[e].transform(t);return new r(n)},r})(this),"object"==typeof module&&(module.exports=ot.Selection),(ot=void 0===ot?{}:ot).WrappedOperation=(()=>{function o(t,e){this.wrapped=t,this.meta=e}function n(t,e){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])}function i(t,e){return t&&"object"==typeof t&&"function"==typeof t.transform?t.transform(e):t}return o.prototype.apply=function(){return this.wrapped.apply.apply(this.wrapped,arguments)},o.prototype.invert=function(){var t=this.meta;return new o(this.wrapped.invert.apply(this.wrapped,arguments),t&&"object"==typeof t&&"function"==typeof t.invert?t.invert.apply(t,arguments):t)},o.prototype.compose=function(t){return new o(this.wrapped.compose(t.wrapped),(e=this.meta,t=t.meta,e&&"object"==typeof e?"function"==typeof e.compose?e.compose(t):(n(e,e={}),n(t,e),e):t));var e},o.transform=function(t,e){var n=(0,t.wrapped.constructor.transform)(t.wrapped,e.wrapped);return[new o(n[0],i(t.meta,e.wrapped)),new o(n[1],i(e.meta,t.wrapped))]},o})(),"object"==typeof module&&(module.exports=ot.WrappedOperation),(ot=void 0===ot?{}:ot).UndoManager=(()=>{var e="normal",o="undoing",i="redoing";function t(t){this.maxItems=t||50,this.state=e,this.dontCompose=!1,this.undoStack=[],this.redoStack=[]}function n(t,e){for(var n=[],o=e.constructor,i=t.length-1;0<=i;i--){var r=o.transform(t[i],e);"function"==typeof r[0].isNoop&&r[0].isNoop()||n.push(r[0]),e=r[1]}return n.reverse()}return t.prototype.add=function(t,e){var n;this.state===o?(this.redoStack.push(t),this.dontCompose=!0):this.state===i?(this.undoStack.push(t),this.dontCompose=!0):(n=this.undoStack,!this.dontCompose&&e&&0<n.length?n.push(t.compose(n.pop())):(n.push(t),n.length>this.maxItems&&n.shift()),this.dontCompose=!1,this.redoStack=[])},t.prototype.transform=function(t){this.undoStack=n(this.undoStack,t),this.redoStack=n(this.redoStack,t)},t.prototype.performUndo=function(t){if(this.state=o,0===this.undoStack.length)throw new Error("undo not possible");t(this.undoStack.pop()),this.state=e},t.prototype.performRedo=function(t){if(this.state=i,0===this.redoStack.length)throw new Error("redo not possible");t(this.redoStack.pop()),this.state=e},t.prototype.canUndo=function(){return 0!==this.undoStack.length},t.prototype.canRedo=function(){return 0!==this.redoStack.length},t.prototype.isUndoing=function(){return this.state===o},t.prototype.isRedoing=function(){return this.state===i},t})(),"object"==typeof module&&(module.exports=ot.UndoManager),(ot=void 0===ot?{}:ot).Client=(()=>{function t(t){this.revision=t,this.setState(p)}function e(){}t.prototype.setState=function(t){this.state=t},t.prototype.applyClient=function(t){this.setState(this.state.applyClient(this,t))},t.prototype.applyServer=function(t,e){this.setState(this.state.applyServer(this,t,e))},t.prototype.applyOperations=function(t,e){this.setState(this.state.applyOperations(this,t,e))},t.prototype.serverAck=function(t){this.setState(this.state.serverAck(this,t))},t.prototype.serverReconnect=function(){"function"==typeof this.state.resend&&this.state.resend(this)},t.prototype.transformSelection=function(t){return this.state.transformSelection(t)},t.prototype.sendOperation=function(t,e){throw new Error("sendOperation must be defined in child class")},t.prototype.applyOperation=function(t){throw new Error("applyOperation must be defined in child class")},(t.Synchronized=e).prototype.applyClient=function(t,e){return t.sendOperation(t.revision,e),new c(e)},e.prototype.applyServer=function(t,e,n){if(1<e-t.revision)throw new Error("Invalid revision.");return t.revision=e,t.applyOperation(n),this},e.prototype.serverAck=function(t,e){throw new Error("There is no pending operation.")},e.prototype.transformSelection=function(t){return t};var p=new e;function c(t){this.outstanding=t}function o(t,e){this.outstanding=t,this.buffer=e}function n(t,e,n){this.acknowlaged=t,this.client=e,this.revision=n,this.pendingOps=[]}function i(t,e,n,o){this.acknowlaged=t,this.buffer=e,this.client=n,this.revision=o,this.pendingOps=[]}return(t.AwaitingConfirm=c).prototype.applyClient=function(t,e){return new o(this.outstanding,e)},c.prototype.applyServer=function(t,e,n){if(1<e-t.revision)throw new Error("Invalid revision.");t.revision=e;e=n.constructor.transform(this.outstanding,n);return t.applyOperation(e[1]),new c(e[0])},c.prototype.serverAck=function(t,e){return 1<e-t.revision?new n(this.outstanding,t,e).getOperations():(t.revision=e,p)},c.prototype.transformSelection=function(t){return t.transform(this.outstanding)},c.prototype.resend=function(t){t.sendOperation(t.revision,this.outstanding)},(t.AwaitingWithBuffer=o).prototype.applyClient=function(t,e){e=this.buffer.compose(e);return new o(this.outstanding,e)},o.prototype.applyServer=function(t,e,n){if(1<e-t.revision)throw new Error("Invalid revision.");t.revision=e;e=n.constructor.transform,n=e(this.outstanding,n),e=e(this.buffer,n[1]);return t.applyOperation(e[1]),new o(n[0],e[0])},o.prototype.serverAck=function(t,e){return 1<e-t.revision?new i(this.outstanding,this.buffer,t,e).getOperations():(t.revision=e,t.sendOperation(t.revision,this.buffer),new c(this.buffer))},o.prototype.transformSelection=function(t){return t.transform(this.outstanding).transform(this.buffer)},o.prototype.resend=function(t){t.sendOperation(t.revision,this.outstanding)},(t.Stale=n).prototype.applyClient=function(t,e){e=new i(this.acknowlaged,e,t,this.revision);return e.pendingOps=this.pendingOps,e},n.prototype.applyServer=function(t,e,n){return this.pendingOps.push({revision:e,operation:n}),this},n.prototype.applyOperations=function(t,e,n){for(var o=this.acknowlaged.constructor.transform,i=0;i<n.length;i++){var r=ot.TextOperation.fromJSON(n[i]),r=o(this.acknowlaged,r);t.applyOperation(r[1]),this.acknowlaged=r[0]}t.revision=this.revision;for(var s=0;s<this.pendingOps.length;s++){var a=this.pendingOps[s];t.revision=a.revision,t.applyOperation(a.operation)}return p},n.prototype.serverAck=function(t,e){throw new Error("There is no pending operation.")},n.prototype.transformSelection=function(t){return t},n.prototype.getOperations=function(){return this.client.getOperations(this.client.revision,this.revision-1),this},(t.StaleWithBuffer=i).prototype.applyClient=function(t,e){e=this.buffer.compose(e),e=new i(this.acknowlaged,e,t,this.revision);return e.pendingOps=this.pendingOps,e},i.prototype.applyServer=function(t,e,n){return this.pendingOps.push({revision:e,operation:n}),this},i.prototype.applyOperations=function(t,e,n){for(var o=this.acknowlaged.constructor.transform,i=0;i<n.length;i++){var r=ot.TextOperation.fromJSON(n[i]),r=o(this.acknowlaged,r),s=o(this.buffer,r[1]);t.applyOperation(s[1]),this.acknowlaged=r[0],this.buffer=s[0]}t.revision=this.revision;for(var a=0;a<this.pendingOps.length;a++){var p=this.pendingOps[a],h=o(this.buffer,p.operation);t.applyOperation(h[1]),this.buffer=h[0],t.revision=p.revision}return t.sendOperation(t.revision,this.buffer),new c(this.buffer)},i.prototype.serverAck=function(t,e){throw new Error("There is no pending operation.")},i.prototype.transformSelection=function(t){return t},i.prototype.getOperations=function(){return this.client.getOperations(this.client.revision,this.revision-1),this},t})(),"object"==typeof module&&(module.exports=ot.Client),ot.CodeMirrorAdapter=(()=>{var f=ot.TextOperation,i=ot.Selection;function n(t){this.cm=t,this.ignoreNextChange=!1,this.changeInProgress=!1,this.selectionChanged=!1,a(this,"onChanges"),a(this,"onChange"),a(this,"onCursorActivity"),a(this,"onFocus"),a(this,"onBlur"),t.on("changes",this.onChanges),t.on("change",this.onChange),t.on("cursorActivity",this.onCursorActivity),t.on("focus",this.onFocus),t.on("blur",this.onBlur)}function o(t,e){return t.line<e.line?-1:e.line<t.line?1:t.ch<e.ch?-1:e.ch<t.ch?1:0}function g(t,e){return o(t,e)<=0}n.prototype.detach=function(){this.cm.off("changes",this.onChanges),this.cm.off("change",this.onChange),this.cm.off("cursorActivity",this.onCursorActivity),this.cm.off("focus",this.onFocus),this.cm.off("blur",this.onBlur)},n.operationFromCodeMirrorChange=n.operationFromCodeMirrorChanges=function(t,e){var n,o=(n=e).indexFromPos({line:n.lastLine(),ch:0})+n.getLine(n.lastLine()).length,i=(new f).retain(o),r=(new f).retain(o),s=function(t){return e.indexFromPos(t)};function a(t){if(0===t.length)return 0;for(var e=0,n=0;n<t.length;n++)e+=t[n].length;return e+t.length-1}function p(n,o){return function(t){var e;return g(t,o.from)?n(t):g(o.to,t)?n({line:t.line+o.text.length-1-(o.to.line-o.from.line),ch:o.to.line<t.line?t.ch:o.text.length<=1?t.ch-(o.to.ch-o.from.ch)+a(o.text):t.ch-o.to.ch+(e=o.text)[e.length-1].length})+a(o.removed)-a(o.text):o.from.line===t.line?n(o.from)+t.ch-o.from.ch:n(o.from)+a(o.removed.slice(0,t.line-o.from.line))+1+t.ch}}for(var h=t.length-1;0<=h;h--){var c=t[h],l=(s=p(s,c))(c.from),u=o-l-a(c.text),i=(new f).retain(l).delete(a(c.removed)).insert(c.text.join("\n")).retain(u).compose(i),r=r.compose((new f).retain(l).delete(a(c.text)).insert(c.removed.join("\n")).retain(u));o+=a(c.removed)-a(c.text)}return[i,r]},n.applyOperationToCodeMirror=function(s,a){a.operation(function(){for(var t=s.ops,e=0,n=0,o=t.length;n<o;n++){var i,r=t[n];f.isRetain(r)?e+=r:f.isInsert(r)?(a.replaceRange(r,a.posFromIndex(e),null,"ignoreHistory"),e+=r.length):f.isDelete(r)&&(i=a.posFromIndex(e),r=a.posFromIndex(e-r),a.replaceRange("",i,r,"ignoreHistory"))}})},n.prototype.registerCallbacks=function(t){this.callbacks=t},n.prototype.onChange=function(){this.changeInProgress=!0},n.prototype.onChanges=function(t,e){this.ignoreNextChange||(e=n.operationFromCodeMirrorChanges(e,this.cm),this.trigger("change",e[0],e[1])),this.selectionChanged&&this.trigger("selectionChange"),this.changeInProgress=!1,this.ignoreNextChange=!1},n.prototype.onCursorActivity=n.prototype.onFocus=function(){this.changeInProgress?this.selectionChanged=!0:this.trigger("selectionChange")},n.prototype.onBlur=function(){this.cm.somethingSelected()||this.trigger("blur")},n.prototype.getValue=function(){return this.cm.getValue()},n.prototype.getSelection=function(){for(var t=this.cm,e=t.listSelections(),n=[],o=0;o<e.length;o++)n[o]=new i.Range(t.indexFromPos(e[o].anchor),t.indexFromPos(e[o].head));return new i(n)},n.prototype.setSelection=function(t){for(var e=[],n=0;t&&n<t.ranges.length;n++){var o=t.ranges[n];e[n]={anchor:this.cm.posFromIndex(o.anchor),head:this.cm.posFromIndex(o.head)}}this.cm.setSelections(e)},e={},t=document.createElement("style"),document.documentElement.getElementsByTagName("head")[0].appendChild(t),r=t.sheet;var e,t,r,s=function(t){e[t]||(e[t]=!0,r.insertRule(t,(r.cssRules||r.rules).length))};function a(t,e){var n=t[e];t[e]=function(){n.apply(t,arguments)}}return n.prototype.setOtherCursor=function(t,e,n){var t=this.cm.posFromIndex(t),o=(this.cm.cursorCoords(t),document.createElement("span"));return o.className="other-client",o.style.display="none",o.setAttribute("data-clientid",n),this.cm.setBookmark(t,{widget:o,insertLeft:!0})},n.prototype.setOtherSelectionRange=function(t,e,n){var o,i,r=/^#([0-9a-fA-F]{6})$/.exec(e);if(r)return e="."+(r="selection-"+r[1])+" { background: rgba("+(e=hex2rgb(e)).red+","+e.green+","+e.blue+",0.2); }",s(e),e=this.cm.posFromIndex(t.anchor),t=this.cm.posFromIndex(t.head),this.cm.markText(g(o=e,i=t)?o:i,g(o=e,i=t)?i:o,{className:r});throw new Error("only six-digit hex colors are allowed.")},n.prototype.setOtherSelection=function(t,e,n){for(var o=[],i=0;i<t.ranges.length;i++){var r=t.ranges[i];r.isEmpty()||(o[i]=this.setOtherSelectionRange(r,e,n))}return{clear:function(){for(var t=0;t<o.length;t++)o[t]&&o[t].clear()}}},n.prototype.trigger=function(t){var e=Array.prototype.slice.call(arguments,1),t=this.callbacks&&this.callbacks[t];t&&t.apply(this,e)},n.prototype.applyOperation=function(t){t.isNoop()||(this.ignoreNextChange=!0),n.applyOperationToCodeMirror(t,this.cm)},n.prototype.registerUndo=function(t){this.cm.undo=t},n.prototype.registerRedo=function(t){this.cm.redo=t},n})(),ot.SocketIOAdapter=(()=>{function t(t){this.socket=t;var i=this;t.on("client_left",function(t){i.trigger("client_left",t)}),t.on("set_name",function(t,e){i.trigger("set_name",t,e)}),t.on("set_color",function(t,e){i.trigger("set_color",t,e)}),t.on("ack",function(t){i.trigger("ack",t)}),t.on("operation",function(t,e,n,o){i.trigger("operation",e,n),i.trigger("selection",t,o)}),t.on("operations",function(t,e){i.trigger("operations",t,e)}),t.on("selection",function(t,e){i.trigger("selection",t,e)}),t.on("reconnect",function(){i.trigger("reconnect")})}return t.prototype.sendOperation=function(t,e,n){this.socket.emit("operation",t,e,n)},t.prototype.sendSelection=function(t){this.socket.emit("selection",t)},t.prototype.getOperations=function(t,e){this.socket.emit("get_operations",t,e)},t.prototype.registerCallbacks=function(t){this.callbacks=t},t.prototype.trigger=function(t){var e=Array.prototype.slice.call(arguments,1),t=this.callbacks&&this.callbacks[t];t&&t.apply(this,e)},t})(),ot.AjaxAdapter=(()=>{function t(t,e,n){"/"!==t[t.length-1]&&(t+="/"),this.path=t,this.ownUserName=e,this.majorRevision=n.major||0,this.minorRevision=n.minor||0,this.poll()}return t.prototype.renderRevisionPath=function(){return"revision/"+this.majorRevision+"-"+this.minorRevision},t.prototype.handleResponse=function(t){for(var e=t.operations,n=0;n<e.length;n++)e[n].user===this.ownUserName?this.trigger("ack"):this.trigger("operation",e[n].operation);0<e.length&&(this.majorRevision+=e.length,this.minorRevision=0);var o=t.events;if(o){for(n=0;n<o.length;n++){var i=o[n].user;if(i!==this.ownUserName)switch(o[n].event){case"joined":this.trigger("set_name",i,i);break;case"left":this.trigger("client_left",i);break;case"selection":this.trigger("selection",i,o[n].selection)}}this.minorRevision+=o.length}var r=t.users;r&&(delete r[this.ownUserName],this.trigger("clients",r)),t.revision&&(this.majorRevision=t.revision.major,this.minorRevision=t.revision.minor)},t.prototype.poll=function(){var e=this;$.ajax({url:this.path+this.renderRevisionPath(),type:"GET",dataType:"json",timeout:5e3,success:function(t){e.handleResponse(t),e.poll()},error:function(){setTimeout(function(){e.poll()},500)}})},t.prototype.sendOperation=function(t,e,n){if(t!==this.majorRevision)throw new Error("Revision numbers out of sync");var o=this;$.ajax({url:this.path+this.renderRevisionPath(),type:"POST",data:JSON.stringify({operation:e,selection:n}),contentType:"application/json",processData:!1,success:function(t){},error:function(){setTimeout(function(){o.sendOperation(t,e,n)},500)}})},t.prototype.sendSelection=function(t){$.ajax({url:this.path+this.renderRevisionPath()+"/selection",type:"POST",data:JSON.stringify(t),contentType:"application/json",processData:!1,timeout:1e3})},t.prototype.registerCallbacks=function(t){this.callbacks=t},t.prototype.trigger=function(t){var e=Array.prototype.slice.call(arguments,1),t=this.callbacks&&this.callbacks[t];t&&t.apply(this,e)},t})(),ot.EditorClient=(()=>{var t,r=ot.Client,s=ot.Selection,a=ot.UndoManager,p=ot.TextOperation,i=ot.WrappedOperation;function h(t,e){this.selectionBefore=t,this.selectionAfter=e}function e(t,e){this.clientId=t,this.selection=e}function n(t,e,n,o,i,r){this.id=t,this.listEl=e,this.editorAdapter=n,this.name=o,this.color=i,this.li=document.createElement("li"),o&&(this.li.textContent=o,this.listEl.appendChild(this.li)),i?this.setForceColor(i):this.setColor(o?f(o):Math.random()),r&&this.updateSelection(r)}function o(t,e,n,o){r.call(this,t),this.serverAdapter=n,this.editorAdapter=o,this.undoManager=new a,this.initializeClientList(),this.initializeClients(e);var i=this;this.editorAdapter.registerCallbacks({change:function(t,e){i.onChange(t,e)},selectionChange:function(){i.onSelectionChange()},blur:function(){i.onBlur()}}),this.editorAdapter.registerUndo(function(){i.undo()}),this.editorAdapter.registerRedo(function(){i.redo()}),this.serverAdapter.registerCallbacks({client_left:function(t){i.onClientLeft(t)},set_name:function(t,e){i.getClientObject(t).setName(e)},set_color:function(t,e){i.getClientObject(t).setForceColor(e)},ack:function(t){i.serverAck(t)},operation:function(t,e){i.applyServer(t,p.fromJSON(e))},operations:function(t,e){i.applyOperations(t,e)},selection:function(t,e){e?i.getClientObject(t).updateSelection(i.transformSelection(s.fromJSON(e))):i.getClientObject(t).removeSelection()},clients:function(t){var e,n;for(e in i.clients)i.clients.hasOwnProperty(e)&&!t.hasOwnProperty(e)&&i.onClientLeft(e);for(e in t)t.hasOwnProperty(e)&&(n=i.getClientObject(e),t[e].name&&n.setName(t[e].name),(n=t[e].selection)?i.clients[e].updateSelection(i.transformSelection(s.fromJSON(n))):i.clients[e].removeSelection())},reconnect:function(){i.serverReconnect()}})}function c(){}function l(t,e,n){function o(t){t=Math.round(255*t).toString(16);return 1===t.length?"0"+t:t}return"#"+o(t)+o(e)+o(n)}function u(t,e,n){var o,i;return 0===e?l(n,n,n):(i=2*n-(o=n<.5?n*(1+e):n+e-e*n),l((e=function(t){return t<0&&(t+=1),1<t&&--t,6*t<1?i+6*(o-i)*t:2*t<1?o:3*t<2?i+6*(o-i)*(2/3-t):i})(t+1/3),e(t),e(t-1/3)))}function f(t){for(var e=1,n=0;n<t.length;n++)e=17*(e+t.charCodeAt(n))%360;return e/360}return h.prototype.invert=function(){return new h(this.selectionAfter,this.selectionBefore)},h.prototype.compose=function(t){return new h(this.selectionBefore,t.selectionAfter)},h.prototype.transform=function(t){return new h(this.selectionBefore?this.selectionBefore.transform(t):null,this.selectionAfter?this.selectionAfter.transform(t):null)},e.fromJSON=function(t){return new e(t.clientId,t.selection&&s.fromJSON(t.selection))},e.prototype.transform=function(t){return new e(this.clientId,this.selection&&this.selection.transform(t))},n.prototype.setColor=function(t){this.hue=t,this.color=u(t,.75,.5),this.lightColor=u(t,.5,.9),this.li&&(this.li.style.color=this.color)},n.prototype.setForceColor=function(t){this.hue=null,this.color=t,this.lightColor=t,this.li&&(this.li.style.color=this.color)},n.prototype.setName=function(t){this.name!==t&&(this.name=t,this.li.textContent=t,this.li.parentNode||this.listEl.appendChild(this.li),this.setColor(f(t)))},n.prototype.updateSelection=function(t){this.removeSelection(),this.selection=t,this.mark=this.editorAdapter.setOtherSelection(t,t.position===t.selectionEnd?this.color:this.lightColor,this.id)},n.prototype.remove=function(){var t;this.li&&(t=this.li).parentNode&&t.parentNode.removeChild(t),this.removeSelection()},n.prototype.removeSelection=function(){this.mark&&(this.mark.clear(),this.mark=null)},t=o,c.prototype=r.prototype,t.prototype=new c,t.prototype.constructor=t,o.prototype.addClient=function(t,e){this.clients[t]=new n(t,this.clientListEl,this.editorAdapter,e.name||t,e.color||null,e.selection?s.fromJSON(e.selection):null)},o.prototype.initializeClients=function(t){for(var e in this.clients={},t)t.hasOwnProperty(e)&&this.addClient(e,t[e])},o.prototype.getClientObject=function(t){var e=this.clients[t];return e||(this.clients[t]=new n(t,this.clientListEl,this.editorAdapter))},o.prototype.onClientLeft=function(t){var e=this.clients[t];e&&(e.remove(),delete this.clients[t])},o.prototype.initializeClientList=function(){this.clientListEl=document.createElement("ul")},o.prototype.applyUnredo=function(t){this.undoManager.add(t.invert(this.editorAdapter.getValue())),this.editorAdapter.applyOperation(t.wrapped),this.selection=t.meta.selectionAfter,this.editorAdapter.setSelection(this.selection),this.applyClient(t.wrapped)},o.prototype.undo=function(){var e=this;this.undoManager.canUndo()&&this.undoManager.performUndo(function(t){e.applyUnredo(t)})},o.prototype.redo=function(){var e=this;this.undoManager.canRedo()&&this.undoManager.performRedo(function(t){e.applyUnredo(t)})},o.prototype.onChange=function(t,e){var n=this.selection,o=(this.updateSelection(),new h(n,this.selection)),o=(new i(t,o),0<this.undoManager.undoStack.length&&e.shouldBeComposedWithInverted((o=this.undoManager.undoStack)[o.length-1].wrapped)),n=new h(this.selection,n);this.undoManager.add(new i(e,n),o),this.applyClient(t)},o.prototype.updateSelection=function(){this.selection=this.editorAdapter.getSelection()},o.prototype.onSelectionChange=function(){var t=this.selection;this.updateSelection(),t&&this.selection.equals(t)||this.sendSelection(this.selection)},o.prototype.onBlur=function(){this.selection=null,this.sendSelection(null)},o.prototype.sendSelection=function(t){this.state instanceof r.AwaitingWithBuffer||this.serverAdapter.sendSelection(t)},o.prototype.sendOperation=function(t,e){this.serverAdapter.sendOperation(t,e.toJSON(),this.selection)},o.prototype.getOperations=function(t,e){this.serverAdapter.getOperations(t,e)},o.prototype.applyOperation=function(t){this.editorAdapter.applyOperation(t),this.updateSelection(),this.undoManager.transform(new i(t,null))},o})();