UNPKG

pharaoh

Version:

ABANDONED. A desktop app for the Pharaoh live programming tool.

14 lines 91.9 kB
/*! * Firepad is an open-source, collaborative code and text editor. It was designed * to be embedded inside larger applications. Since it uses Firebase as a backend, * it requires no server-side code and can be added to any web app simply by * including a couple JavaScript files. * * Firepad 1.3.0 * http://www.firepad.io/ * License: MIT * Copyright: 2014 Firebase * With code from ot.js (Copyright 2012-2013 Tim Baumann) */ !function(a,b,c){"undefined"!=typeof module&&module.exports?module.exports=b():"function"==typeof c.define&&c.define.amd?define(b):c[a]=b()}("Firepad",function(){var a=a||{};a.utils={},a.utils.makeEventEmitter=function(a,b){a.prototype.allowedEvents_=b,a.prototype.on=function(a,b,c){this.validateEventType_(a),this.eventListeners_=this.eventListeners_||{},this.eventListeners_[a]=this.eventListeners_[a]||[],this.eventListeners_[a].push({callback:b,context:c})},a.prototype.off=function(a,b){this.validateEventType_(a),this.eventListeners_=this.eventListeners_||{};for(var c=this.eventListeners_[a]||[],d=0;d<c.length;d++)if(c[d].callback===b)return void c.splice(d,1)},a.prototype.trigger=function(a){this.eventListeners_=this.eventListeners_||{};for(var b=this.eventListeners_[a]||[],c=0;c<b.length;c++)b[c].callback.apply(b[c].context,Array.prototype.slice.call(arguments,1))},a.prototype.validateEventType_=function(a){if(this.allowedEvents_){for(var b=!1,c=0;c<this.allowedEvents_.length;c++)if(this.allowedEvents_[c]===a){b=!0;break}if(!b)throw new Error('Unknown event "'+a+'"')}}},a.utils.elt=function(b,c,d){var e=document.createElement(b);if("string"==typeof c)a.utils.setTextContent(e,c);else if(c)for(var f=0;f<c.length;++f)e.appendChild(c[f]);for(var g in d||{})e.setAttribute(g,d[g]);return e},a.utils.setTextContent=function(a,b){a.innerHTML="",a.appendChild(document.createTextNode(b))},a.utils.on=function(a,b,c,d){a.addEventListener?a.addEventListener(b,c,d||!1):a.attachEvent&&a.attachEvent("on"+b,c)},a.utils.off=function(a,b,c,d){a.removeEventListener?a.removeEventListener(b,c,d||!1):a.detachEvent&&a.detachEvent("on"+b,c)},a.utils.preventDefault=function(a){a.preventDefault?a.preventDefault():a.returnValue=!1},a.utils.stopPropagation=function(a){a.stopPropagation?a.stopPropagation():a.cancelBubble=!0},a.utils.stopEvent=function(b){a.utils.preventDefault(b),a.utils.stopPropagation(b)},a.utils.stopEventAnd=function(b){return function(c){return b(c),a.utils.stopEvent(c),!1}},a.utils.trim=function(a){return a.replace(/^\s+/g,"").replace(/\s+$/g,"")},a.utils.stringEndsWith=function(a,b){for(var c="string"==typeof b?[b]:b,d=0;d<c.length;d++){var b=c[d];if(-1!==a.indexOf(b,a.length-b.length))return!0}return!1},a.utils.assert=function(a,b){if(!a)throw new Error(b||"assertion error")},a.utils.log=function(){if("undefined"!=typeof console&&"undefined"!=typeof console.log){for(var a=["Firepad:"],b=0;b<arguments.length;b++)a.push(arguments[b]);console.log.apply(console,a)}};var a=a||{};a.Span=function(){function a(a,b){this.pos=a,this.length=b}return a.prototype.end=function(){return this.pos+this.length},a}();var a=a||{};a.TextOp=function(){function b(a){this.type=a,this.chars=null,this.text=null,this.attributes=null,"insert"===a?(this.text=arguments[1],c.assert("string"==typeof this.text),this.attributes=arguments[2]||{},c.assert("object"==typeof this.attributes)):"delete"===a?(this.chars=arguments[1],c.assert("number"==typeof this.chars)):"retain"===a&&(this.chars=arguments[1],c.assert("number"==typeof this.chars),this.attributes=arguments[2]||{},c.assert("object"==typeof this.attributes))}var c=a.utils;return b.prototype.isInsert=function(){return"insert"===this.type},b.prototype.isDelete=function(){return"delete"===this.type},b.prototype.isRetain=function(){return"retain"===this.type},b.prototype.equals=function(a){return this.type===a.type&&this.text===a.text&&this.chars===a.chars&&this.attributesEqual(a.attributes)},b.prototype.attributesEqual=function(a){for(var b in this.attributes)if(this.attributes[b]!==a[b])return!1;for(b in a)if(this.attributes[b]!==a[b])return!1;return!0},b.prototype.hasEmptyAttributes=function(){var a=!0;for(var b in this.attributes){a=!1;break}return a},b}();var a=a||{};a.TextOperation=function(){"use strict";function b(){return this&&this.constructor===b?(this.ops=[],this.baseLength=0,void(this.targetLength=0)):new b}function c(a){var b=a.ops;switch(b.length){case 1:return b[0];case 2:return b[0].isRetain()?b[1]:b[1].isRetain()?b[0]:null;case 3:if(b[0].isRetain()&&b[2].isRetain())return b[1]}return null}function d(a){return a.ops[0].isRetain()?a.ops[0].chars:0}var e=a.TextOp,f=a.utils;return b.prototype.equals=function(a){if(this.baseLength!==a.baseLength)return!1;if(this.targetLength!==a.targetLength)return!1;if(this.ops.length!==a.ops.length)return!1;for(var b=0;b<this.ops.length;b++)if(!this.ops[b].equals(a.ops[b]))return!1;return!0},b.prototype.retain=function(a,b){if("number"!=typeof a||0>a)throw new Error("retain expects a positive integer.");if(0===a)return this;this.baseLength+=a,this.targetLength+=a,b=b||{};var c=this.ops.length>0?this.ops[this.ops.length-1]:null;return c&&c.isRetain()&&c.attributesEqual(b)?c.chars+=a:this.ops.push(new e("retain",a,b)),this},b.prototype.insert=function(a,b){if("string"!=typeof a)throw new Error("insert expects a string");if(""===a)return this;b=b||{},this.targetLength+=a.length;var c=this.ops.length>0?this.ops[this.ops.length-1]:null,d=this.ops.length>1?this.ops[this.ops.length-2]:null;return c&&c.isInsert()&&c.attributesEqual(b)?c.text+=a:c&&c.isDelete()?d&&d.isInsert()&&d.attributesEqual(b)?d.text+=a:(this.ops[this.ops.length-1]=new e("insert",a,b),this.ops.push(c)):this.ops.push(new e("insert",a,b)),this},b.prototype["delete"]=function(a){if("string"==typeof a&&(a=a.length),"number"!=typeof a||0>a)throw new Error("delete expects a positive integer or a string");if(0===a)return this;this.baseLength+=a;var b=this.ops.length>0?this.ops[this.ops.length-1]:null;return b&&b.isDelete()?b.chars+=a:this.ops.push(new e("delete",a)),this},b.prototype.isNoop=function(){return 0===this.ops.length||1===this.ops.length&&this.ops[0].isRetain()&&this.ops[0].hasEmptyAttributes()},b.prototype.clone=function(){for(var a=new b,c=0;c<this.ops.length;c++)this.ops[c].isRetain()?a.retain(this.ops[c].chars,this.ops[c].attributes):this.ops[c].isInsert()?a.insert(this.ops[c].text,this.ops[c].attributes):a["delete"](this.ops[c].chars);return a},b.prototype.toString=function(){var a=Array.prototype.map||function(a){for(var b=this,c=[],d=0,e=b.length;e>d;d++)c[d]=a(b[d]);return c};return a.call(this.ops,function(a){return a.isRetain()?"retain "+a.chars:a.isInsert()?"insert '"+a.text+"'":"delete "+a.chars}).join(", ")},b.prototype.toJSON=function(){for(var a=[],b=0;b<this.ops.length;b++)this.ops[b].hasEmptyAttributes()||a.push(this.ops[b].attributes),"retain"===this.ops[b].type?a.push(this.ops[b].chars):"insert"===this.ops[b].type?a.push(this.ops[b].text):"delete"===this.ops[b].type&&a.push(-this.ops[b].chars);return 0===a.length&&a.push(0),a},b.fromJSON=function(a){for(var c=new b,d=0,e=a.length;e>d;d++){var g=a[d],h={};"object"==typeof g&&(h=g,d++,g=a[d]),"number"==typeof g?g>0?c.retain(g,h):c["delete"](-g):(f.assert("string"==typeof g),c.insert(g,h))}return c},b.prototype.apply=function(a,b,c){var d=this;if(b=b||[],c=c||[],a.length!==d.baseLength)throw new Error("The operation's base length must be equal to the string's length.");for(var e,g,h=[],i=0,j=0,k=this.ops,l=0,m=k.length;m>l;l++){var n=k[l];if(n.isRetain()){if(j+n.chars>a.length)throw new Error("Operation can't retain more characters than are left in the string.");for(h[i++]=a.slice(j,j+n.chars),e=0;e<n.chars;e++){var o=b[j+e]||{},p={};for(g in o)p[g]=o[g],f.assert(p[g]!==!1);for(g in n.attributes)n.attributes[g]===!1?delete p[g]:p[g]=n.attributes[g],f.assert(p[g]!==!1);c.push(p)}j+=n.chars}else if(n.isInsert())for(h[i++]=n.text,e=0;e<n.text.length;e++){var q={};for(g in n.attributes)q[g]=n.attributes[g],f.assert(q[g]!==!1);c.push(q)}else j+=n.chars}if(j!==a.length)throw new Error("The operation didn't operate on the whole string.");var r=h.join("");return f.assert(r.length===c.length),r},b.prototype.invert=function(a){for(var c=0,d=new b,e=this.ops,f=0,g=e.length;g>f;f++){var h=e[f];h.isRetain()?(d.retain(h.chars),c+=h.chars):h.isInsert()?d["delete"](h.text.length):(d.insert(a.slice(c,c+h.chars)),c+=h.chars)}return d},b.prototype.compose=function(a){function c(a,b,c){var d,e={};for(d in a)e[d]=a[d];for(d in b)c&&b[d]===!1?delete e[d]:e[d]=b[d];return e}var d=this;if(d.targetLength!==a.baseLength)throw new Error("The base length of the second operation has to be the target length of the first operation");for(var e,f=new b,g=d.clone().ops,h=a.clone().ops,i=0,j=0,k=g[i++],l=h[j++];;){if("undefined"==typeof k&&"undefined"==typeof l)break;if(k&&k.isDelete())f["delete"](k.chars),k=g[i++];else if(l&&l.isInsert())f.insert(l.text,l.attributes),l=h[j++];else{if("undefined"==typeof k)throw new Error("Cannot compose operations: first operation is too short.");if("undefined"==typeof l)throw new Error("Cannot compose operations: first operation is too long.");if(k.isRetain()&&l.isRetain())e=c(k.attributes,l.attributes),k.chars>l.chars?(f.retain(l.chars,e),k.chars-=l.chars,l=h[j++]):k.chars===l.chars?(f.retain(k.chars,e),k=g[i++],l=h[j++]):(f.retain(k.chars,e),l.chars-=k.chars,k=g[i++]);else if(k.isInsert()&&l.isDelete())k.text.length>l.chars?(k.text=k.text.slice(l.chars),l=h[j++]):k.text.length===l.chars?(k=g[i++],l=h[j++]):(l.chars-=k.text.length,k=g[i++]);else if(k.isInsert()&&l.isRetain())e=c(k.attributes,l.attributes,!0),k.text.length>l.chars?(f.insert(k.text.slice(0,l.chars),e),k.text=k.text.slice(l.chars),l=h[j++]):k.text.length===l.chars?(f.insert(k.text,e),k=g[i++],l=h[j++]):(f.insert(k.text,e),l.chars-=k.text.length,k=g[i++]);else{if(!k.isRetain()||!l.isDelete())throw new Error("This shouldn't happen: op1: "+JSON.stringify(k)+", op2: "+JSON.stringify(l));k.chars>l.chars?(f["delete"](l.chars),k.chars-=l.chars,l=h[j++]):k.chars===l.chars?(f["delete"](l.chars),k=g[i++],l=h[j++]):(f["delete"](k.chars),l.chars-=k.chars,k=g[i++])}}}return f},b.prototype.shouldBeComposedWith=function(a){if(this.isNoop()||a.isNoop())return!0;var b=d(this),e=d(a),f=c(this),g=c(a);return f&&g?f.isInsert()&&g.isInsert()?b+f.text.length===e:f.isDelete()&&g.isDelete()?e+g.chars===b||b===e:!1:!1},b.prototype.shouldBeComposedWithInverted=function(a){if(this.isNoop()||a.isNoop())return!0;var b=d(this),e=d(a),f=c(this),g=c(a);return f&&g?f.isInsert()&&g.isInsert()?b+f.text.length===e||b===e:f.isDelete()&&g.isDelete()?e+g.chars===b:!1:!1},b.transformAttributes=function(a,b){var c,d={},e={},g={};for(c in a)g[c]=!0;for(c in b)g[c]=!0;for(c in g){var h=a[c],i=b[c];f.assert(null!=h||null!=i),null==h?e[c]=i:null==i?d[c]=h:h===i||(d[c]=h)}return[d,e]},b.transform=function(a,c){if(a.baseLength!==c.baseLength)throw new Error("Both operations have to have the same base length");for(var d=new b,e=new b,f=a.clone().ops,g=c.clone().ops,h=0,i=0,j=f[h++],k=g[i++];;){if("undefined"==typeof j&&"undefined"==typeof k)break;if(j&&j.isInsert())d.insert(j.text,j.attributes),e.retain(j.text.length),j=f[h++];else if(k&&k.isInsert())d.retain(k.text.length),e.insert(k.text,k.attributes),k=g[i++];else{if("undefined"==typeof j)throw new Error("Cannot transform operations: first operation is too short.");if("undefined"==typeof k)throw new Error("Cannot transform operations: first operation is too long.");var l;if(j.isRetain()&&k.isRetain()){var m=b.transformAttributes(j.attributes,k.attributes);j.chars>k.chars?(l=k.chars,j.chars-=k.chars,k=g[i++]):j.chars===k.chars?(l=k.chars,j=f[h++],k=g[i++]):(l=j.chars,k.chars-=j.chars,j=f[h++]),d.retain(l,m[0]),e.retain(l,m[1])}else if(j.isDelete()&&k.isDelete())j.chars>k.chars?(j.chars-=k.chars,k=g[i++]):j.chars===k.chars?(j=f[h++],k=g[i++]):(k.chars-=j.chars,j=f[h++]);else if(j.isDelete()&&k.isRetain())j.chars>k.chars?(l=k.chars,j.chars-=k.chars,k=g[i++]):j.chars===k.chars?(l=k.chars,j=f[h++],k=g[i++]):(l=j.chars,k.chars-=j.chars,j=f[h++]),d["delete"](l);else{if(!j.isRetain()||!k.isDelete())throw new Error("The two operations aren't compatible");j.chars>k.chars?(l=k.chars,j.chars-=k.chars,k=g[i++]):j.chars===k.chars?(l=j.chars,j=f[h++],k=g[i++]):(l=j.chars,k.chars-=j.chars,j=f[h++]),e["delete"](l)}}}return[d,e]},b}();var a=a||{};a.AnnotationList=function(){function b(a,b){if(!a)throw new Error("AnnotationList assertion failed"+(b?": "+b:""))}function c(a,b){this.pos=a,this.length=b.length,this.annotation=b.annotation,this.attachedObject_=b.attachedObject}function d(a,b){this.pos=a,this.length=b.length,this.annotation=b.annotation,this.node_=b}function e(a){this.head_=new f(0,h),this.changeHandler_=a}function f(a,b){this.length=a,this.annotation=b,this.attachedObject=null,this.next=null}var g=a.Span;c.prototype.getAttachedObject=function(){return this.attachedObject_},d.prototype.attachObject=function(a){this.node_.attachedObject=a};var h={equals:function(){return!1}};return e.prototype.insertAnnotatedSpan=function(a,c){this.wrapOperation_(new g(a.pos,0),function(d,e){b(!e||null===e.next);var g=new f(a.length,c);if(e){b(a.pos>d&&a.pos<d+e.length);var i=new f(0,h);return i.next=new f(a.pos-d,e.annotation),i.next.next=g,g.next=new f(d+e.length-a.pos,e.annotation),i.next}return g})},e.prototype.removeSpan=function(a){0!==a.length&&this.wrapOperation_(a,function(c,d){b(null!==d);var e=new f(0,h),g=e;for(a.pos>c&&(g.next=new f(a.pos-c,d.annotation),g=g.next);a.end()>c+d.length;)c+=d.length,d=d.next;var i=c+d.length-a.end();return i>0&&(g.next=new f(i,d.annotation)),e.next})},e.prototype.updateSpan=function(a,c){0!==a.length&&this.wrapOperation_(a,function(d,e){b(null!==e);var g=new f(0,h),i=g,j=d,k=a.pos-j;for(b(k<e.length),k>0&&(i.next=new f(k,e.annotation),i=i.next,j+=i.length);null!==e&&a.end()>=d+e.length;){var l=d+e.length-j;i.next=new f(l,c(e.annotation,l)),i=i.next,d+=e.length,e=e.next,j=d}var m=a.end()-j;return m>0&&(b(m<e.length),i.next=new f(m,c(e.annotation,m)),i=i.next,j+=i.length,i.next=new f(d+e.length-j,e.annotation)),g.next})},e.prototype.wrapOperation_=function(a,b){if(a.pos<0)throw new Error("Span start cannot be negative.");var e,g=[],h=[],i=this.getAffectedNodes_(a);null!==i.start?(e=i.end.next,i.end.next=null):e=i.succ;var j=b(i.startPos,i.start),k=!1,l=!1;if(j){this.mergeNodesWithSameAnnotations_(j);var m;for(i.pred&&i.pred.annotation.equals(j.annotation)?(k=!0,j.length+=i.pred.length,i.beforePred.next=j,m=i.predPos):(i.beforeStart.next=j,m=i.startPos);j.next;)h.push(new d(m,j)),m+=j.length,j=j.next;i.succ&&i.succ.annotation.equals(j.annotation)?(j.length+=i.succ.length,l=!0,j.next=i.succ.next):j.next=e,h.push(new d(m,j))}else i.pred&&i.succ&&i.pred.annotation.equals(i.succ.annotation)?(k=!0,l=!0,j=new f(i.pred.length+i.succ.length,i.pred.annotation),i.beforePred.next=j,j.next=i.succ.next,h.push(new d(i.startPos-i.pred.length,j))):i.beforeStart.next=e;k&&g.push(new c(i.predPos,i.pred));for(var n=i.startPos,o=i.start;null!==o;)g.push(new c(n,o)),n+=o.length,o=o.next;l&&g.push(new c(n,i.succ)),this.changeHandler_(g,h)},e.prototype.getAffectedNodes_=function(a){for(var b={},c=null,d=this.head_,e=d.next,f=0;null!==e&&a.pos>=f+e.length;)f+=e.length,c=d,d=e,e=e.next;if(null===e&&(0!==a.length||a.pos!==f))throw new Error("Span start exceeds the bounds of the AnnotationList.");for(b.startPos=f,0===a.length&&a.pos===f?b.start=null:b.start=e,b.beforeStart=d,f===a.pos&&f>0?(b.pred=d,b.predPos=f-d.length,b.beforePred=c):b.pred=null;null!==e&&a.end()>f;)f+=e.length,d=e,e=e.next;if(a.end()>f)throw new Error("Span end exceeds the bounds of the AnnotationList.");return 0===a.length&&a.end()===f?b.end=null:b.end=d,b.succ=f===a.end()?e:null,b},e.prototype.mergeNodesWithSameAnnotations_=function(a){if(a)for(var b=null,c=a;c;)b&&b.annotation.equals(c.annotation)?(b.length+=c.length,b.next=c.next):b=c,c=c.next},e.prototype.forEach=function(a){for(var b=this.head_.next;null!==b;)a(b.length,b.annotation,b.attachedObject),b=b.next},e.prototype.getAnnotatedSpansForPos=function(a){for(var b=0,d=this.head_.next,e=null;null!==d&&b+d.length<=a;)b+=d.length,e=d,d=d.next;if(null===d&&b!==a)throw new Error("pos exceeds the bounds of the AnnotationList");var f=[];return b===a&&e&&f.push(new c(b-e.length,e)),d&&f.push(new c(b,d)),f},e.prototype.getAnnotatedSpansForSpan=function(a){if(0===a.length)return[];for(var b=[],c=this.getAffectedNodes_(a),d=c.startPos,e=c.start;null!==e&&d<a.end();){var f=Math.max(d,a.pos),h=Math.min(d+e.length,a.end()),i=new g(f,h-f);i.annotation=e.annotation,b.push(i),d+=e.length,e=e.next}return b},e.prototype.count=function(){for(var a=0,c=this.head_.next,d=null;null!==c;)d&&b(!d.annotation.equals(c.annotation)),d=c,c=c.next,a++;return a},f.prototype.clone=function(){var a=new f(this.spanLength,this.annotation);return a.next=this.next,a},e}();var a=a||{};a.Cursor=function(){"use strict";function a(a,b){this.position=a,this.selectionEnd=b}return a.fromJSON=function(b){return new a(b.position,b.selectionEnd)},a.prototype.equals=function(a){return this.position===a.position&&this.selectionEnd===a.selectionEnd},a.prototype.compose=function(a){return a},a.prototype.transform=function(b){function c(a){for(var c=a,d=b.ops,e=0,f=b.ops.length;f>e&&(d[e].isRetain()?a-=d[e].chars:d[e].isInsert()?c+=d[e].text.length:(c-=Math.min(a,d[e].chars),a-=d[e].chars),!(0>a));e++);return c}var d=c(this.position);return this.position===this.selectionEnd?new a(d,d):new a(d,c(this.selectionEnd))},a}();var a=a||{};a.FirebaseAdapter=function(b){function c(a,b,c){this.ref_=a,this.ready_=!1,this.firebaseCallbacks_=[],this.zombie_=!1,this.document_=new g,this.revision_=0,this.pendingReceivedRevisions_={};var d=this;b?(this.setUserId(b),this.setColor(c),this.firebaseOn_(a.root().child(".info/connected"),"value",function(a){a.val()===!0&&d.initializeUserData_()},this),this.on("ready",function(){d.monitorCursors_()})):this.userId_=a.push().key(),setTimeout(function(){d.monitorHistory_()},0)}function d(a,b){if(!a)throw new Error(b||"assertion error")}function e(a){if(0===a)return"A0";for(var b="";a>0;){var c=a%j.length;b=j[c]+b,a-=c,a/=j.length}var d=j[b.length+9];return d+b}function f(a){d(a.length>0&&a[0]===j[a.length+8]);for(var b=0,c=1;c<a.length;c++)b*=j.length,b+=j.indexOf(a[c]);return b}"function"==typeof require&&"function"!=typeof Firebase&&(Firebase=require("firebase"));var g=a.TextOperation,h=a.utils,i=100;h.makeEventEmitter(c,["ready","cursor","operation","ack","retry"]),c.prototype.dispose=function(){var a=this;return this.ready_?(this.removeFirebaseCallbacks_(),this.userRef_&&(this.userRef_.child("cursor").remove(),this.userRef_.child("color").remove()),this.ref_=null,this.document_=null,void(this.zombie_=!0)):void this.on("ready",function(){a.dispose()})},c.prototype.setUserId=function(a){this.userRef_&&(this.userRef_.child("cursor").remove(),this.userRef_.child("cursor").onDisconnect().cancel(),this.userRef_.child("color").remove(),this.userRef_.child("color").onDisconnect().cancel()),this.userId_=a,this.userRef_=this.ref_.child("users").child(a),this.initializeUserData_()},c.prototype.isHistoryEmpty=function(){return d(this.ready_,"Not ready yet."),0===this.revision_},c.prototype.sendOperation=function(a,b){function c(a,d){f.ref_.child("history").child(a).transaction(function(a){return null===a?d:void 0},function(e,g,i){if(e){if("disconnect"!==e.message)throw h.log("Transaction failure!",e),e;f.sent_&&f.sent_.id===a?setTimeout(function(){c(a,d)},0):b&&b(e,!1)}else b&&b(null,g)},!1)}var f=this;if(!this.ready_)return void this.on("ready",function(){f.trigger("retry")});d(this.document_.targetLength===a.baseLength,"sendOperation() called with invalid operation.");var g=e(this.revision_);this.sent_={id:g,op:a},c(g,{a:f.userId_,o:a.toJSON(),t:Firebase.ServerValue.TIMESTAMP})},c.prototype.sendCursor=function(a){this.userRef_.child("cursor").set(a),this.cursor_=a},c.prototype.setColor=function(a){this.userRef_.child("color").set(a),this.color_=a},c.prototype.getDocument=function(){return this.document_},c.prototype.registerCallbacks=function(a){for(var b in a)this.on(b,a[b])},c.prototype.initializeUserData_=function(){this.userRef_.child("cursor").onDisconnect().remove(),this.userRef_.child("color").onDisconnect().remove(),this.sendCursor(this.cursor_||null),this.setColor(this.color_||null)},c.prototype.monitorCursors_=function(){function a(a){var b=a.key(),d=a.val();c.trigger("cursor",b,d.cursor,d.color)}var b=this.ref_.child("users"),c=this;this.firebaseOn_(b,"child_added",a),this.firebaseOn_(b,"child_changed",a),this.firebaseOn_(b,"child_removed",function(a){var b=a.key();c.trigger("cursor",b,null)})},c.prototype.monitorHistory_=function(){var a=this;this.ref_.child("checkpoint").once("value",function(b){if(!a.zombie_){var c=b.child("id").val(),d=b.child("o").val(),e=b.child("a").val();null!=d&&null!=c&&null!==e?(a.pendingReceivedRevisions_[c]={o:d,a:e},a.checkpointRevision_=f(c),a.monitorHistoryStartingAt_(a.checkpointRevision_+1)):(a.checkpointRevision_=0,a.monitorHistoryStartingAt_(a.checkpointRevision_))}})},c.prototype.monitorHistoryStartingAt_=function(a){var b=this.ref_.child("history").startAt(null,e(a)),c=this;setTimeout(function(){c.firebaseOn_(b,"child_added",function(a){var b=a.key();c.pendingReceivedRevisions_[b]=a.val(),c.ready_&&c.handlePendingReceivedRevisions_()}),b.once("value",function(){c.handleInitialRevisions_()})},0)},c.prototype.handleInitialRevisions_=function(){d(!this.ready_,"Should not be called multiple times."),this.revision_=this.checkpointRevision_;for(var a=e(this.revision_),b=this.pendingReceivedRevisions_;null!=b[a];){var c=this.parseRevision_(b[a]);c?this.document_=this.document_.compose(c.operation):h.log("Invalid operation.",this.ref_.toString(),a,b[a]),delete b[a],this.revision_++,a=e(this.revision_)}this.trigger("operation",this.document_),this.ready_=!0;var f=this;setTimeout(function(){f.trigger("ready")},0)},c.prototype.handlePendingReceivedRevisions_=function(){for(var a=this.pendingReceivedRevisions_,b=e(this.revision_),c=!1;null!=a[b];){this.revision_++;var d=this.parseRevision_(a[b]);d?(this.document_=this.document_.compose(d.operation),this.sent_&&b===this.sent_.id?this.sent_.op.equals(d.operation)&&d.author===this.userId_?(this.revision_%i===0&&this.saveCheckpoint_(),this.sent_=null,this.trigger("ack")):(c=!0,this.trigger("operation",d.operation)):this.trigger("operation",d.operation)):h.log("Invalid operation.",this.ref_.toString(),b,a[b]),delete a[b],b=e(this.revision_)}c&&(this.sent_=null,this.trigger("retry"))},c.prototype.parseRevision_=function(a){if("object"!=typeof a)return null;if("string"!=typeof a.a||"object"!=typeof a.o)return null;var b=null;try{b=g.fromJSON(a.o)}catch(c){return null}return b.baseLength!==this.document_.targetLength?null:{author:a.a,operation:b}},c.prototype.saveCheckpoint_=function(){this.ref_.child("checkpoint").set({a:this.userId_,o:this.document_.toJSON(),id:e(this.revision_-1)})},c.prototype.firebaseOn_=function(a,b,c,d){return this.firebaseCallbacks_.push({ref:a,eventType:b,callback:c,context:d}),a.on(b,c,d),c},c.prototype.firebaseOff_=function(a,b,c,d){a.off(b,c,d);for(var e=0;e<this.firebaseCallbacks_.length;e++){var f=this.firebaseCallbacks_[e];if(f.ref===a&&f.eventType===b&&f.callback===c&&f.context===d){this.firebaseCallbacks_.splice(e,1);break}}},c.prototype.removeFirebaseCallbacks_=function(){for(var a=0;a<this.firebaseCallbacks_.length;a++){var b=this.firebaseCallbacks_[a];b.ref.off(b.eventType,b.callback,b.context)}this.firebaseCallbacks_=[]};var j="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";return c}();var a=a||{};a.RichTextToolbar=function(b){function c(a){this.imageInsertionUI=a,this.element_=this.makeElement_()}var d=a.utils;return d.makeEventEmitter(c,["bold","italic","underline","strike","font","font-size","color","left","center","right","unordered-list","ordered-list","todo-list","indent-increase","indent-decrease","undo","redo","insert-image"]),c.prototype.element=function(){return this.element_},c.prototype.makeButton_=function(a,b){var c=this;b=b||a;var e=d.elt("a",[d.elt("span","",{"class":"firepad-tb-"+b})],{"class":"firepad-btn"});return d.on(e,"click",d.stopEventAnd(function(){c.trigger(a)})),e},c.prototype.makeElement_=function(){var a=this,b=this.makeFontDropdown_(),c=this.makeFontSizeDropdown_(),e=this.makeColorDropdown_(),f=[d.elt("div",[b],{"class":"firepad-btn-group"}),d.elt("div",[c],{"class":"firepad-btn-group"}),d.elt("div",[e],{"class":"firepad-btn-group"}),d.elt("div",[a.makeButton_("bold"),a.makeButton_("italic"),a.makeButton_("underline"),a.makeButton_("strike","strikethrough")],{"class":"firepad-btn-group"}),d.elt("div",[a.makeButton_("unordered-list","list-2"),a.makeButton_("ordered-list","numbered-list"),a.makeButton_("todo-list","list")],{"class":"firepad-btn-group"}),d.elt("div",[a.makeButton_("indent-decrease"),a.makeButton_("indent-increase")],{"class":"firepad-btn-group"}),d.elt("div",[a.makeButton_("left","paragraph-left"),a.makeButton_("center","paragraph-center"),a.makeButton_("right","paragraph-right")],{"class":"firepad-btn-group"}),d.elt("div",[a.makeButton_("undo"),a.makeButton_("redo")],{"class":"firepad-btn-group"})];a.imageInsertionUI&&f.push(d.elt("div",[a.makeButton_("insert-image")],{"class":"firepad-btn-group"}));var g=d.elt("div",f,{"class":"firepad-toolbar-wrapper"}),h=d.elt("div",null,{"class":"firepad-toolbar"});return h.appendChild(g),h},c.prototype.makeFontDropdown_=function(){for(var a=["Arial","Comic Sans MS","Courier New","Impact","Times New Roman","Verdana"],b=[],c=0;c<a.length;c++){var e=d.elt("span",a[c]);e.setAttribute("style","font-family:"+a[c]),b.push({content:e,value:a[c]})}return this.makeDropdown_("Font","font",b)},c.prototype.makeFontSizeDropdown_=function(){for(var a=[9,10,12,14,18,24,32,42],b=[],c=0;c<a.length;c++){var e=d.elt("span",a[c].toString());e.setAttribute("style","font-size:"+a[c]+"px; line-height:"+(a[c]-6)+"px;"),b.push({content:e,value:a[c]})}return this.makeDropdown_("Size","font-size",b,"px")},c.prototype.makeColorDropdown_=function(){for(var a=["black","red","green","blue","yellow","cyan","magenta","grey"],b=[],c=0;c<a.length;c++){var e=d.elt("div");e.className="firepad-color-dropdown-item",e.setAttribute("style","background-color:"+a[c]),b.push({content:e,value:a[c]})}return this.makeDropdown_("Color","color",b)},c.prototype.makeDropdown_=function(a,b,c,e){function f(){l||(k.style.display="block",d.on(document,"click",g,!0),l=!0)}function g(){l&&(k.style.display="",d.off(document,"click",g,!0),l=!1),m=!0,setTimeout(function(){m=!1},0)}function h(a,c){"object"!=typeof a&&(a=document.createTextNode(String(a)));var f=d.elt("a",[a]);d.on(f,"click",d.stopEventAnd(function(){g(),i.trigger(b,c+e)})),k.appendChild(f)}e=e||"";var i=this,j=d.elt("a",a+" ▾",{"class":"firepad-btn firepad-dropdown"}),k=d.elt("ul",[],{"class":"firepad-dropdown-menu"});j.appendChild(k);for(var l=!1,m=!1,n=0;n<c.length;n++){var o=c[n].content,p=c[n].value;h(o,p)}return d.on(j,"click",d.stopEventAnd(function(){m||f()})),j},c}();var a=a||{};a.WrappedOperation=function(a){"use strict";function b(a,b){this.wrapped=a,this.meta=b}function c(a,b){for(var c in a)a.hasOwnProperty(c)&&(b[c]=a[c])}function d(a,b){if(a&&"object"==typeof a){if("function"==typeof a.compose)return a.compose(b);var d={};return c(a,d),c(b,d),d}return b}function e(a,b){return a&&"object"==typeof a&&"function"==typeof a.transform?a.transform(b):a}return b.prototype.apply=function(){return this.wrapped.apply.apply(this.wrapped,arguments)},b.prototype.invert=function(){var a=this.meta;return new b(this.wrapped.invert.apply(this.wrapped,arguments),a&&"object"==typeof a&&"function"==typeof a.invert?a.invert.apply(a,arguments):a)},b.prototype.compose=function(a){return new b(this.wrapped.compose(a.wrapped),d(this.meta,a.meta))},b.transform=function(a,c){var d=a.wrapped.constructor.transform,f=d(a.wrapped,c.wrapped);return[new b(f[0],e(a.meta,c.wrapped)),new b(f[1],e(c.meta,a.wrapped))]},b}();var a=a||{};a.UndoManager=function(){"use strict";function a(a){this.maxItems=a||50,this.state=c,this.dontCompose=!1,this.undoStack=[],this.redoStack=[]}function b(a,b){for(var c=[],d=b.constructor,e=a.length-1;e>=0;e--){var f=d.transform(a[e],b);"function"==typeof f[0].isNoop&&f[0].isNoop()||c.push(f[0]),b=f[1]}return c.reverse()}var c="normal",d="undoing",e="redoing";return a.prototype.add=function(a,b){if(this.state===d)this.redoStack.push(a),this.dontCompose=!0;else if(this.state===e)this.undoStack.push(a),this.dontCompose=!0;else{var c=this.undoStack;!this.dontCompose&&b&&c.length>0?c.push(a.compose(c.pop())):(c.push(a),c.length>this.maxItems&&c.shift()),this.dontCompose=!1,this.redoStack=[]}},a.prototype.transform=function(a){this.undoStack=b(this.undoStack,a),this.redoStack=b(this.redoStack,a)},a.prototype.performUndo=function(a){if(this.state=d,0===this.undoStack.length)throw new Error("undo not possible");a(this.undoStack.pop()),this.state=c},a.prototype.performRedo=function(a){if(this.state=e,0===this.redoStack.length)throw new Error("redo not possible");a(this.redoStack.pop()),this.state=c},a.prototype.canUndo=function(){return 0!==this.undoStack.length},a.prototype.canRedo=function(){return 0!==this.redoStack.length},a.prototype.isUndoing=function(){return this.state===d},a.prototype.isRedoing=function(){return this.state===e},a}();var a=a||{};a.Client=function(){"use strict";function a(){this.state=e}function b(){}function c(a){this.outstanding=a}function d(a,b){this.outstanding=a,this.buffer=b}a.prototype.setState=function(a){this.state=a},a.prototype.applyClient=function(a){this.setState(this.state.applyClient(this,a))},a.prototype.applyServer=function(a){this.setState(this.state.applyServer(this,a))},a.prototype.serverAck=function(){this.setState(this.state.serverAck(this))},a.prototype.serverRetry=function(){this.setState(this.state.serverRetry(this))},a.prototype.sendOperation=function(a){throw new Error("sendOperation must be defined in child class")},a.prototype.applyOperation=function(a){throw new Error("applyOperation must be defined in child class")},a.Synchronized=b,b.prototype.applyClient=function(a,b){return a.sendOperation(b),new c(b)},b.prototype.applyServer=function(a,b){return a.applyOperation(b),this},b.prototype.serverAck=function(a){throw new Error("There is no pending operation.")},b.prototype.serverRetry=function(a){throw new Error("There is no pending operation.")};var e=new b;return a.AwaitingConfirm=c,c.prototype.applyClient=function(a,b){return new d(this.outstanding,b)},c.prototype.applyServer=function(a,b){var d=b.constructor.transform(this.outstanding,b);return a.applyOperation(d[1]),new c(d[0])},c.prototype.serverAck=function(a){return e},c.prototype.serverRetry=function(a){return a.sendOperation(this.outstanding),this},a.AwaitingWithBuffer=d,d.prototype.applyClient=function(a,b){var c=this.buffer.compose(b);return new d(this.outstanding,c)},d.prototype.applyServer=function(a,b){var c=b.constructor.transform,e=c(this.outstanding,b),f=c(this.buffer,e[1]);return a.applyOperation(f[1]),new d(e[0],f[0])},d.prototype.serverRetry=function(a){var b=this.outstanding.compose(this.buffer);return a.sendOperation(b),new c(b)},d.prototype.serverAck=function(a){return a.sendOperation(this.buffer),new c(this.buffer)},a}();var a=a||{};a.EditorClient=function(){"use strict";function b(a,b){this.cursorBefore=a,this.cursorAfter=b}function c(a,b){this.id=a,this.editorAdapter=b,this.li=document.createElement("li")}function d(a,b){g.call(this),this.serverAdapter=a,this.editorAdapter=b,this.undoManager=new i,this.clients={};var c=this;this.editorAdapter.registerCallbacks({change:function(a,b){c.onChange(a,b)},cursorActivity:function(){c.onCursorActivity()},blur:function(){c.onBlur()},focus:function(){c.onFocus()}}),this.editorAdapter.registerUndo(function(){c.undo()}),this.editorAdapter.registerRedo(function(){c.redo()}),this.serverAdapter.registerCallbacks({ack:function(){c.serverAck(),c.focused&&c.state instanceof g.Synchronized&&(c.updateCursor(),c.sendCursor(c.cursor)),c.emitStatus()},retry:function(){c.serverRetry()},operation:function(a){c.applyServer(a)},cursor:function(a,b,d){if(c.serverAdapter.userId_!==a&&c.state instanceof g.Synchronized){var e=c.getClientObject(a);b?(d&&e.setColor(d),e.updateCursor(h.fromJSON(b))):e.removeCursor()}}})}function e(a,b){function c(){}c.prototype=b.prototype, a.prototype=new c,a.prototype.constructor=a}function f(a){return a[a.length-1]}var g=a.Client,h=a.Cursor,i=a.UndoManager,j=a.WrappedOperation;return b.prototype.invert=function(){return new b(this.cursorAfter,this.cursorBefore)},b.prototype.compose=function(a){return new b(this.cursorBefore,a.cursorAfter)},b.prototype.transform=function(a){return new b(this.cursorBefore?this.cursorBefore.transform(a):null,this.cursorAfter?this.cursorAfter.transform(a):null)},c.prototype.setColor=function(a){this.color=a},c.prototype.updateCursor=function(a){this.removeCursor(),this.cursor=a,this.mark=this.editorAdapter.setOtherCursor(a,this.color,this.id)},c.prototype.removeCursor=function(){this.mark&&this.mark.clear()},e(d,g),d.prototype.getClientObject=function(a){var b=this.clients[a];return b?b:this.clients[a]=new c(a,this.editorAdapter)},d.prototype.applyUnredo=function(a){this.undoManager.add(this.editorAdapter.invertOperation(a)),this.editorAdapter.applyOperation(a.wrapped),this.cursor=a.meta.cursorAfter,this.cursor&&this.editorAdapter.setCursor(this.cursor),this.applyClient(a.wrapped)},d.prototype.undo=function(){var a=this;this.undoManager.canUndo()&&this.undoManager.performUndo(function(b){a.applyUnredo(b)})},d.prototype.redo=function(){var a=this;this.undoManager.canRedo()&&this.undoManager.performRedo(function(b){a.applyUnredo(b)})},d.prototype.onChange=function(a,c){var d=this.cursor;this.updateCursor();var e=this.undoManager.undoStack.length>0&&c.shouldBeComposedWithInverted(f(this.undoManager.undoStack).wrapped),g=new b(this.cursor,d);this.undoManager.add(new j(c,g),e),this.applyClient(a)},d.prototype.updateCursor=function(){this.cursor=this.editorAdapter.getCursor()},d.prototype.onCursorActivity=function(){var a=this.cursor;this.updateCursor(),!this.focused||a&&this.cursor.equals(a)||this.sendCursor(this.cursor)},d.prototype.onBlur=function(){this.cursor=null,this.sendCursor(null),this.focused=!1},d.prototype.onFocus=function(){this.focused=!0,this.onCursorActivity()},d.prototype.sendCursor=function(a){this.state instanceof g.AwaitingWithBuffer||this.serverAdapter.sendCursor(a)},d.prototype.sendOperation=function(a){this.serverAdapter.sendOperation(a),this.emitStatus()},d.prototype.applyOperation=function(a){this.editorAdapter.applyOperation(a),this.updateCursor(),this.undoManager.transform(new j(a,null))},d.prototype.emitStatus=function(){var a=this;setTimeout(function(){a.trigger("synced",a.state instanceof g.Synchronized)},0)},d}(),a.utils.makeEventEmitter(a.EditorClient,["synced"]);var a,b=function(a,b){return function(){return a.apply(b,arguments)}},c=[].slice;("undefined"==typeof a||null===a)&&(a={}),a.ACEAdapter=function(){function d(a){this.onCursorActivity=b(this.onCursorActivity,this),this.onFocus=b(this.onFocus,this),this.onBlur=b(this.onBlur,this),this.onChange=b(this.onChange,this);var c;this.ace=a,this.aceSession=this.ace.getSession(),this.aceDoc=this.aceSession.getDocument(),this.aceDoc.setNewLineMode("unix"),this.grabDocumentState(),this.ace.on("change",this.onChange),this.ace.on("blur",this.onBlur),this.ace.on("focus",this.onFocus),this.aceSession.selection.on("changeCursor",this.onCursorActivity),null==this.aceRange&&(this.aceRange=(null!=(c=ace.require)?c:require)("ace/range").Range)}return d.prototype.ignoreChanges=!1,d.prototype.grabDocumentState=function(){return this.lastDocLines=this.aceDoc.getAllLines(),this.lastCursorRange=this.aceSession.selection.getRange()},d.prototype.detach=function(){return this.ace.removeListener("change",this.onChange),this.ace.removeListener("blur",this.onBlur),this.ace.removeListener("focus",this.onCursorActivity),this.aceSession.selection.removeListener("changeCursor",this.onCursorActivity)},d.prototype.onChange=function(a){var b;return this.ignoreChanges?void 0:(b=this.operationFromACEChange(a),this.trigger.apply(this,["change"].concat(c.call(b))),this.grabDocumentState())},d.prototype.onBlur=function(){return this.ace.selection.isEmpty()?this.trigger("blur"):void 0},d.prototype.onFocus=function(){return this.trigger("focus")},d.prototype.onCursorActivity=function(){var a=this;return setTimeout(function(){return a.trigger("cursorActivity")},0)},d.prototype.operationFromACEChange=function(b){var c,d,e,f,g,h,i,j;return b.data?(e=b.data,"insertLines"===(j=e.action)||"removeLines"===j?(i=e.lines.join("\n")+"\n",c=e.action.replace("Lines","")):(i=e.text.replace(this.aceDoc.getNewLineCharacter(),"\n"),c=e.action.replace("Text","")),h=this.indexFromPos(e.range.start)):(i=b.lines.join("\n"),h=this.indexFromPos(b.start)),g=this.lastDocLines.join("\n").length-h,"remove"===b.action&&(g-=i.length),f=(new a.TextOperation).retain(h).insert(i).retain(g),d=(new a.TextOperation).retain(h)["delete"](i).retain(g),"remove"===b.action?[d,f]:[f,d]},d.prototype.applyOperationToACE=function(a){var b,c,d,e,f,g,h,i;for(c=0,i=a.ops,g=0,h=i.length;h>g;g++)d=i[g],d.isRetain()?c+=d.chars:d.isInsert()?(this.aceDoc.insert(this.posFromIndex(c),d.text),c+=d.text.length):d.isDelete()&&(b=this.posFromIndex(c),f=this.posFromIndex(c+d.chars),e=this.aceRange.fromPoints(b,f),this.aceDoc.remove(e));return this.grabDocumentState()},d.prototype.posFromIndex=function(a){var b,c,d,e,f;for(f=this.aceDoc.$lines,c=d=0,e=f.length;e>d&&(b=f[c],!(a<=b.length));c=++d)a-=b.length+1;return{row:c,column:a}},d.prototype.indexFromPos=function(a,b){var c,d,e,f;for(null==b&&(b=this.lastDocLines),d=0,c=e=0,f=a.row;f>=0?f>e:e>f;c=f>=0?++e:--e)d+=this.lastDocLines[c].length+1;return d+=a.column},d.prototype.getValue=function(){return this.aceDoc.getValue()},d.prototype.getCursor=function(){var b,c,d,e,f,g;try{e=this.indexFromPos(this.aceSession.selection.getRange().start,this.aceDoc.$lines),d=this.indexFromPos(this.aceSession.selection.getRange().end,this.aceDoc.$lines)}catch(h){b=h;try{e=this.indexFromPos(this.lastCursorRange.start),d=this.indexFromPos(this.lastCursorRange.end)}catch(h){c=h,console.log("Couldn't figure out the cursor range:",c,"-- setting it to 0:0."),f=[0,0],e=f[0],d=f[1]}}return e>d&&(g=[d,e],e=g[0],d=g[1]),new a.Cursor(e,d)},d.prototype.setCursor=function(a){var b,c,d;return c=this.posFromIndex(a.position),b=this.posFromIndex(a.selectionEnd),a.position>a.selectionEnd&&(d=[b,c],c=d[0],b=d[1]),this.aceSession.selection.setSelectionRange(new this.aceRange(c.row,c.column,b.row,b.column))},d.prototype.setOtherCursor=function(a,b,c){var d,e,f,g,h,i,j,k,l=this;return null==this.otherCursors&&(this.otherCursors={}),f=this.otherCursors[c],f&&(f.start.detach(),f.end.detach(),this.aceSession.removeMarker(f.id)),j=this.posFromIndex(a.position),g=this.posFromIndex(a.selectionEnd),a.selectionEnd<a.position&&(k=[g,j],j=k[0],g=k[1]),d="other-client-selection-"+b.replace("#",""),h=a.position===a.selectionEnd,h&&(d=d.replace("selection","cursor")),e="."+d+" {\n position: absolute;\n background-color: "+(h?"transparent":b)+";\n border-left: 2px solid "+b+";\n}",this.addStyleRule(e),this.otherCursors[c]=f=new this.aceRange(j.row,j.column,g.row,g.column),i=this,f.clipRows=function(){var a;return a=i.aceRange.prototype.clipRows.apply(this,arguments),a.isEmpty=function(){return!1},a},f.start=this.aceDoc.createAnchor(f.start),f.end=this.aceDoc.createAnchor(f.end),f.id=this.aceSession.addMarker(f,d,"text"),{clear:function(){return f.start.detach(),f.end.detach(),l.aceSession.removeMarker(f.id)}}},d.prototype.addStyleRule=function(a){var b;if("undefined"!=typeof document&&null!==document&&(this.addedStyleRules||(this.addedStyleRules={},b=document.createElement("style"),document.documentElement.getElementsByTagName("head")[0].appendChild(b),this.addedStyleSheet=b.sheet),!this.addedStyleRules[a]))return this.addedStyleRules[a]=!0,this.addedStyleSheet.insertRule(a,0)},d.prototype.registerCallbacks=function(a){this.callbacks=a},d.prototype.trigger=function(){var a,b,d,e;return b=arguments[0],a=2<=arguments.length?c.call(arguments,1):[],null!=(d=this.callbacks)&&null!=(e=d[b])?e.apply(this,a):void 0},d.prototype.applyOperation=function(a){return a.isNoop()||(this.ignoreChanges=!0),this.applyOperationToACE(a),this.ignoreChanges=!1},d.prototype.registerUndo=function(a){return this.ace.undo=a},d.prototype.registerRedo=function(a){return this.ace.redo=a},d.prototype.invertOperation=function(a){return a.invert(this.getValue())},d}();var a=a||{};a.AttributeConstants={BOLD:"b",ITALIC:"i",UNDERLINE:"u",STRIKE:"s",FONT:"f",FONT_SIZE:"fs",COLOR:"c",BACKGROUND_COLOR:"bc",ENTITY_SENTINEL:"ent",LINE_SENTINEL:"l",LINE_INDENT:"li",LINE_ALIGN:"la",LIST_TYPE:"lt"},a.sentinelConstants={LINE_SENTINEL_CHARACTER:"",ENTITY_SENTINEL_CHARACTER:""};var a=a||{};a.EntityManager=function(){function b(){this.entities_={};var a=["src","alt","width","height","style","class"];this.register("img",{render:function(a){c.assert(a.src,"image entity should have 'src'!");for(var b=["src","alt","width","height","style","class"],d="<img ",e=0;e<b.length;e++){var f=b[e];f in a&&(d+=" "+f+'="'+a[f]+'"')}return d+=">"},fromElement:function(b){for(var c={},d=0;d<a.length;d++){var e=a[d];b.hasAttribute(e)&&(c[e]=b.getAttribute(e))}return c}})}var c=a.utils;return b.prototype.register=function(b,c){a.utils.assert(c.render,"Entity options should include a 'render' function!"),a.utils.assert(c.fromElement,"Entity options should include a 'fromElement' function!"),this.entities_[b]=c},b.prototype.renderToElement=function(a,b){return this.tryRenderToElement_(a,"render",b)},b.prototype.exportToElement=function(a){var b=this.tryRenderToElement_(a,"export")||this.tryRenderToElement_(a,"getHtml")||this.tryRenderToElement_(a,"render");return b.setAttribute("data-firepad-entity",a.type),b},b.prototype.updateElement=function(a,b){var c=a.type,d=a.info;this.entities_[c]&&"undefined"!=typeof this.entities_[c].update&&this.entities_[c].update(d,b)},b.prototype.fromElement=function(b){var c=b.getAttribute("data-firepad-entity");if(c||(c=b.nodeName.toLowerCase()),c&&this.entities_[c]){var d=this.entities_[c].fromElement(b);return new a.Entity(c,d)}},b.prototype.tryRenderToElement_=function(b,c,d){var e=b.type,f=b.info;if(this.entities_[e]&&this.entities_[e][c]){var g=a.document||window&&window.document,h=this.entities_[e][c](f,d,g);if(h){if("string"==typeof h){var i=(a.document||document).createElement("div");return i.innerHTML=h,i.childNodes[0]}if("object"==typeof h)return a.utils.assert("undefined"!=typeof h.nodeType,"Error rendering "+e+" entity. render() function must return an html string or a DOM element."),h}}},b.prototype.entitySupportsUpdate=function(a){return this.entities_[a]&&this.entities_[a].update},b}();var a=a||{};a.Entity=function(){function b(a,c){return this instanceof b?(this.type=a,void(this.info=c||{})):new b(a,c)}var c=a.AttributeConstants,d=c.ENTITY_SENTINEL,e=d+"_";return b.prototype.toAttributes=function(){var a={};a[d]=this.type;for(var b in this.info)a[e+b]=this.info[b];return a},b.fromAttributes=function(a){var c=a[d],f={};for(var g in a)0===g.indexOf(e)&&(f[g.substr(e.length)]=a[g]);return new b(c,f)},b}();var a=a||{};a.RichTextCodeMirror=function(){function b(a,b,c){this.codeMirror=a,this.options_=c||{},this.entityManager_=b,this.currentAttributes_=null;var d=this;this.annotationList_=new j(function(a,b){d.onAnnotationsChanged_(a,b)}),this.initAnnotationList_(),i(this,"onCodeMirrorBeforeChange_"),i(this,"onCodeMirrorChange_"),i(this,"onCursorActivity_"),parseInt(CodeMirror.version)>=4?this.codeMirror.on("changes",this.onCodeMirrorChange_):this.codeMirror.on("change",this.onCodeMirrorChange_),this.codeMirror.on("beforeChange",this.onCodeMirrorBeforeChange_),this.codeMirror.on("cursorActivity",this.onCursorActivity_),this.changeId_=0,this.outstandingChanges_={},this.dirtyLines_=[]}function c(a,b){return a.line-b.line||a.ch-b.ch}function d(a,b){return c(a,b)<=0}function e(a){return a[a.length-1]}function f(a){if(0===a.length)return 0;for(var b=0,c=0;c<a.length;c++)b+=a[c].length;return b+a.length-1}function g(a){this.attributes=a||{}}function h(a){for(var b in a)return!1;return!0}function i(a,b){var c=a[b];a[b]=function(){c.apply(a,arguments)}}var j=a.AnnotationList,k=a.Span,l=a.utils,m=a.AttributeConstants,n="cmrt-",o="cmrt-",p={c:"color",bc:"background-color",fs:"font-size",li:function(a){return"padding-left: "+40*a+"px"}},q={};l.makeEventEmitter(b,["change","attributesChange","newLine"]);var r=a.sentinelConstants.LINE_SENTINEL_CHARACTER,s=a.sentinelConstants.ENTITY_SENTINEL_CHARACTER;return b.prototype.detach=function(){this.codeMirror.off("beforeChange",this.onCodeMirrorBeforeChange_),this.codeMirror.off("change",this.onCodeMirrorChange_),this.codeMirror.off("changes",this.onCodeMirrorChange_),this.codeMirror.off("cursorActivity",this.onCursorActivity_),this.clearAnnotations_()},b.prototype.toggleAttribute=function(a,b){var c=b||!0;if(this.emptySelection_()){var d=this.getCurrentAttributes_();d[a]===c?delete d[a]:d[a]=c,this.currentAttributes_=d}else{var e=this.getCurrentAttributes_(),f=e[a]!==c?c:!1;this.setAttribute(a,f)}},b.prototype.setAttribute=function(a,b){var c=this.codeMirror;if(this.emptySelection_()){var d=this.getCurrentAttributes_();b===!1?delete d[a]:d[a]=b,this.currentAttributes_=d}else this.updateTextAttributes(c.indexFromPos(c.getCursor("start")),c.indexFromPos(c.getCursor("end")),function(c){b===!1?delete c[a]:c[a]=b}),this.updateCurrentAttributes_()},b.prototype.updateTextAttributes=function(a,b,c,d,e){var f=[],i=a,j=this;this.annotationList_.updateSpan(new k(a,b-a),function(a,b){var k={};for(var l in a.attributes)k[l]=a.attributes[l];(!k[m.LINE_SENTINEL]||e)&&c(k);var n={},o={};return j.computeChangedAttributes_(a.attributes,k,n,o),h(n)||f.push({start:i,end:i+b,attributes:n,attributesInverse:o,origin:d}),i+=b,new g(k)}),f.length>0&&this.trigger("attributesChange",this,f)},b.prototype.computeChangedAttributes_=function(a,b,c,d){var e,f={};for(e in a)f[e]=!0;for(e in b)f[e]=!0;for(e in f)e in b?e in a?a[e]!==b[e]&&(c[e]=b[e],d[e]=a[e]):(c[e]=b[e],d[e]=!1):(c[e]=!1,d[e]=a[e])},b.prototype.toggleLineAttribute=function(a,b){var c,d=this.getCurrentLineAttributes_();c=a in d&&d[a]===b?!1:b,this.setLineAttribute(a,c)},b.prototype.setLineAttribute=function(a,b){this.updateLineAttributesForSelection(function(c){b===!1?delete c[a]:c[a]=b})},b.prototype.updateLineAttributesForSelection=function(a){var b=this.codeMirror,c=b.getCursor("start"),d=b.getCursor("end"),e=c.line,f=d.line,g=b.getLine(f),h=this.areLineSentinelCharacters_(g.substr(0,d.ch));f>e&&h&&f--,this.updateLineAttributes(e,f,a)},b.prototype.updateLineAttributes=function(a,b,c){for(var d=a;b>=d;d++){var e=this.codeMirror.getLine(d),f=this.codeMirror.indexFromPos({line:d,ch:0});if(e[0]!==r){var g={};g[m.LINE_SENTINEL]=!0,c(g),this.insertText(f,r,g)}else this.updateTextAttributes(f,f+1,c,null,!0)}},b.prototype.replaceText=function(a,b,c,d,e){this.changeId_++;var f=o+this.changeId_;this.outstandingChanges_[f]={origOrigin:e,attributes:d};var g=this.codeMirror,h=g.posFromIndex(a),i="number"==typeof b?g.posFromIndex(b):null;g.replaceRange(c,h,i,f)},b.prototype.insertText=function(a,b,c,d){var e=this.codeMirror,f=e.getCursor(),g="RTCMADAPTER"==d&&!e.somethingSelected()&&a==e.indexFromPos(f);this.replaceText(a,null,b,c,d),g&&e.setCursor(f)},b.prototype.removeText=function(a,b,c){var d=this.codeMirror;d.replaceRange("",d.posFromIndex(a),d.posFromIndex(b),c)},b.prototype.insertEntityAtCursor=function(a,b,c){var d=this.codeMirror,e=d.indexFromPos(d.getCursor("head"));this.insertEntityAt(e,a,b,c)},b.prototype.insertEntityAt=function(b,c,d,e){this.codeMirror;this.insertEntity_(b,new a.Entity(c,d),e)},b.prototype.insertEntity_=function(a,b,c){this.replaceText(a,null,s,b.toAttributes(),c)},b.prototype.getAttributeSpans=function(a,b){for(var c=[],d=this.annotationList_.getAnnotatedSpansForSpan(new k(a,b-a)),e=0;e<d.length;e++)c.push({length:d[e].length,attributes:d[e].annotation.attributes});return c},b.prototype.end=function(){var a=this.codeMirror.lineCount()-1;return this.codeMirror.indexFromPos({line:a,ch:this.codeMirror.getLine(a).length})},b.prototype.getRange=function(a,b){var c=this.codeMirror.posFromIndex(a),d=this.codeMirror.posFromIndex(b);return this.codeMirror.getRange(c,d)},b.prototype.initAnnotationList_=function(){var a=this.end();0!==a&&this.annotationList_.insertAnnotatedSpan(new k(0,a),new g)},b.prototype.onAnnotationsChanged_=function(a,b){var c,d={};this.tryToUpdateEntitiesInPlace(a,b);for(var e=0;e<a.length;e++){var f=a[e].annotation.attributes;m.LINE_SENTINEL in f&&(d[this.codeMirror.posFromIndex(a[e].pos).line]=!0),c=a[e].getAttachedObject(),c&&c.clear()}for(e=0;e<b.length;e++){var g=b[e].annotation,h=m.LINE_SENTINEL in g.attributes,i=m.ENTITY_SENTINEL in g.attributes,j=this.codeMirror.posFromIndex(b[e].pos);if(h)d[j.line]=!0;else if(i)this.markEntity_(b[e]);else{var k=this.getClassNameForAttributes_(g.attributes);if(""!==k){var l=this.codeMirror.posFromIndex(b[e].pos+b[e].length);c=this.codeMirror.markText(j,l,{className:k}),b[e].attachObject(c)}}}for(var n in d)this.dirtyLines_.push(this.codeMirror.getLineHandle(Number(n))),this.queueLineMarking_()},b.prototype.tryToUpdateEntitiesInPlace=function(a,b){for(var c=a.length;c--;)for(var d=a[c],e=b.length;e--;){var f=b[e];if(d.pos==f.pos&&d.length==f.length&&d.annotation.attributes.ent&&d.annotation.attributes.ent==f.annotation.attributes.ent){var g=f.annotation.attributes.ent;if(this.entityManager_.entitySupportsUpdate(g)){a.splice(c,1),b.splice(e,1);var h=d.getAttachedObject();h.update(f.annotation.attributes),f.attachObject(h)}}}},b.prototype.queueLineMarking_=function(){if(null==this.lineMarkTimeout_){var a=this;this.lineMarkT