Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[dist] add codemirror-client.js

  • Loading branch information...
commit 54a2cfd3d4ba4698ed2a9e0202bb338aa9fad292 1 parent 1e336d9
Tim Baumann authored April 14, 2012

Showing 2 changed files with 5 additions and 3 deletions. Show diff stats Hide diff stats

  1. 6  Makefile
  2. 2  dist/ot-min.js
6  Makefile
... ...
@@ -1,2 +1,4 @@
1  
-dist/ot-min.js: lib/operation.js lib/client.js lib/codemirror-operation.js
2  
-	cat lib/operation.js lib/client.js lib/codemirror-operation.js | uglifyjs -nc > dist/ot-min.js
  1
+dist/ot-min.js: lib/operation.js lib/client.js lib/codemirror-operation.js lib/codemirror-client.js
  2
+	cat lib/operation.js lib/client.js lib/codemirror-operation.js lib/codemirror-client.js | uglifyjs -nc > dist/ot-min.js
  3
+
  4
+.PHONY: dist/ot-min.js
2  dist/ot-min.js
... ...
@@ -1 +1 @@
1  
-if(typeof ot=="undefined")var ot={};ot.Operation=function(){function a(a,c,e){b(typeof a=="number"&&a>=0,"the first parameter to the the parent revision number of the document"),this.revision=a,this.id=c||d(),b(this.id&&typeof this.id=="string","not a valid id: "+this.id),this.meta=e||{},this.ops=[],this.baseLength=0,this.targetLength=0}function b(a,b){if(!a)throw new Error(b||"assertion error")}function c(a){return Math.floor(Math.random()*a)}function d(){var a="",b=16;while(b--)a+=c(16).toString(16);return a}return a.prototype.retain=function(a){b(typeof a=="number"&&a>=0);if(a===0)return this;this.baseLength+=a,this.targetLength+=a;var c=this.ops[this.ops.length-1];return c&&c.retain?c.retain+=a:this.ops.push({retain:a}),this},a.prototype.insert=function(a){b(typeof a=="string");if(a==="")return this;this.targetLength+=a.length;var c=this.ops[this.ops.length-1];return c&&c.insert?c.insert+=a:this.ops.push({insert:a}),this},a.prototype.delete=function(a){b(typeof a=="string");if(a==="")return this;this.baseLength+=a.length;var c=this.ops[this.ops.length-1];return c&&c.delete?c.delete+=a:this.ops.push({"delete":a}),this},a.prototype.toString=function(){var a=Array.prototype.map||function(a){var b=this,c=[];for(var d=0,e=b.length;d<e;d++)c[d]=a(b[d]);return c};return a.call(this.ops,function(a){return a.retain?"retain "+a.retain:a.insert?"insert '"+a.insert+"'":"delete '"+a.delete+"'"}).join(", ")},a.fromJSON=function(c){b(c.id);var d=new a(c.revision,c.id,c.meta);b(typeof d.meta=="object");var e=c.ops;for(var f=0,g=e.length;f<g;f++){var h=e[f];if(h.retain)d.retain(h.retain);else if(h.insert)d.insert(h.insert);else{if(!h.delete)throw new Error("unknown operation: "+JSON.stringify(h));d.delete(h.delete)}}return b(d.baseLength===c.baseLength,"baseLengths don't match"),b(d.targetLength===c.targetLength,"targetLengths don't match"),d},a.prototype.apply=function(a){var b=this;if(a.length!==b.baseLength)throw new Error("The operation's base length must be equal to the string's length.");var c=[],d=0,e=0,f=this.ops;for(var g=0,h=f.length;g<h;g++){var i=f[g];if(i.retain){if(e+i.retain>a.length)throw new Error("Operation can't retain more characters than are left in the string.");c[d++]=a.slice(e,e+i.retain),e+=i.retain}else if(i.insert)c[d++]=i.insert;else{if(i.delete!==a.slice(e,e+i.delete.length))throw new Error("The deleted string and the next characters in the string don't match.");e+=i.delete.length}}if(e!==a.length)throw new Error("The operation didn't operate on the whole string.");return c.join("")},a.prototype.invert=function(){var b=new a(this.revision+1),c=this.ops;for(var d=0,e=c.length;d<e;d++){var f=c[d];f.retain?b.retain(f.retain):f.insert?b.delete(f.insert):b.insert(f.delete)}return b},a.prototype.compose=function(b){var c=this;if(c.targetLength!==b.baseLength)throw new Error("The base length of the second operation has to be the target length of the first operation");if(c.revision+1!==b.revision)throw new Error("The second operations revision must be one more than the first operations revision");var d=new a(c.revision,undefined,c.meta),e=c.ops,f=b.ops,g=0,h=0,i=e[g++],j=f[h++];for(;;){var k=i&&(i.retain||(i.insert||i.delete).length),l=j&&(j.retain||(j.insert||j.delete).length),m=Math.min(k,l);if(typeof i=="undefined"&&typeof j=="undefined")break;if(typeof i=="undefined"){if(!j.insert)throw new Error("Successive operations can only insert new characters at the end of the string.");d.insert(j.insert),j=f[h++]}else if(typeof j=="undefined"){if(!i.delete)throw new Error("The first operation can only delete at the end of operation 2.");d.delete(i.delete),i=e[g++]}else if(i.retain&&j.retain)d.retain(m),k>l?(i={retain:k-l},j=f[h++]):k===l?(i=e[g++],j=f[h++]):(i=e[g++],j={retain:l-k});else if(i.insert&&j.delete){if(i.insert.slice(0,m)!==j.delete.slice(0,m))throw new Error("Successive operations must delete what has been inserted before.");k>l?(i={insert:i.insert.slice(l)},j=f[h++]):k===l?(i=e[g++],j=f[h++]):(i=e[g++],j={"delete":j.delete.slice(k)})}else if(i.insert&&j.retain)k>l?(d.insert(i.insert.slice(0,l)),i={insert:i.insert.slice(l)},j=f[h++]):k===l?(d.insert(i.insert),i=e[g++],j=f[h++]):(d.insert(i.insert),i=e[g++],j={retain:l-k});else if(i.retain&&j.delete)k>l?(d.delete(j.delete),i={retain:k-l},j=f[h++]):k===l?(d.delete(j.delete),i=e[g++],j=f[h++]):(d.delete(j.delete.slice(0,k)),i=e[g++],j={"delete":j.delete.slice(k)});else if(i.delete)d.delete(i.delete),i=e[g++];else{if(!j.insert)throw new Error("This shouldn't happen: op1: "+JSON.stringify(i)+", op2: "+JSON.stringify(j));d.insert(j.insert),j=f[h++]}}return d},a.transform=function(b,c){if(b.baseLength!==c.baseLength)throw new Error("Both operations have to have the same base length");if(b.revision!==c.revision)throw new Error("Both operations have to have the same revision");var d=new a(c.revision+1,b.id,b.meta),e=new a(b.revision+1,c.id,c.meta),f=b.ops,g=c.ops,h=0,i=0,j=f[h++],k=g[i++];for(;;){var l=j&&(j.retain||(j.insert||j.delete).length),m=k&&(k.retain||(k.insert||k.delete).length),n=Math.min(l,m);if(typeof j=="undefined"&&typeof k=="undefined")break;if(j&&j.insert)d.insert(j.insert),e.retain(j.insert.length),j=f[h++];else if(k&&k.insert)d.retain(k.insert.length),e.insert(k.insert),k=g[i++];else if(j.retain&&k.retain)d.retain(n),e.retain(n),l>m?(j={retain:l-m},k=g[i++]):l===m?(j=f[h++],k=g[i++]):(j=f[h++],k={retain:m-l});else if(j.delete&&k.delete){if(j.delete.slice(0,n)!==k.delete.slice(0,n))throw new Error("When two concurrent operations delete text at the same position, they must delete the same text");l>m?(j={"delete":j.delete.slice(m)},k=g[i++]):l===m?(j=f[h++],k=g[i++]):(j=f[h++],k={"delete":k.delete.slice(l)})}else if(j.delete&&k.retain)d.delete(j.delete.slice(0,n)),l>m?(j={"delete":j.delete.slice(m)},k=g[i++]):l===m?(j=f[h++],k=g[i++]):(j=f[h++],k={retain:k.retain-l});else{if(!j.retain||!k.delete)throw new Error("The two operations aren't compatible");e.delete(k.delete.slice(0,n)),l>m?(j={retain:j.retain-m},k=g[i++]):l===m?(j=f[h++],k=g[i++]):(j=f[h++],k={"delete":k.delete.slice(l)})}}return[d,e]},a}(),typeof module=="object"&&(module.exports=ot.Operation);if(typeof ot=="undefined")var ot={};ot.Client=function(a){function d(a){f(typeof a=="number"&&a>=0),this.serverRevision=a,this.state="synchronized"}function e(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c])}function f(a,b){if(!a)throw new Error(b||"assertion error")}var b=a.ot?a.ot.Operation:require("./operation"),c={callMethodForState:function(a){var b=Array.prototype.slice.call(arguments,1);return this.states[this.state][a].apply(this,b)},transitionTo:function(a){var b=Array.prototype.slice.call(arguments,1);this.states[this.state].exit.apply(this,[]),this.states[this.state=a].enter.apply(this,b)}};return e(d.prototype,c),d.prototype.createOperation=function(){return new b(this.callMethodForState("newRevision"))},d.prototype.applyClient=function(a){return this.callMethodForState("applyClient",a)},d.prototype.applyServer=function(a){f(a.revision===this.serverRevision),this.callMethodForState("applyServer",a),this.serverRevision++},d.prototype.sendOperation=function(a){throw new Error("sendOperation must be defined in child class")},d.prototype.applyOperation=function(a){throw new Error("applyOperation must be defined in child class")},d.prototype.states={"synchronized":{enter:function(){},exit:function(){},applyClient:function(a){this.sendOperation(a),this.transitionTo("awaitingConfirm",a)},applyServer:function(a){this.applyOperation(a)},newRevision:function(){return this.serverRevision}},awaitingConfirm:{enter:function(a){this.outstanding=a},exit:function(){delete this.outstanding},applyClient:function(a){f(a.revision===this.serverRevision+1),this.transitionTo("awaitingWithBuffer",this.outstanding,a)},applyServer:function(a){if(a.id===this.outstanding.id)this.transitionTo("synchronized");else{var c=b.transform(this.outstanding,a);this.outstanding=c[0],this.applyOperation(c[1])}},newRevision:function(){return this.serverRevision+1}},awaitingWithBuffer:{enter:function(a,b){this.outstanding=a,this.buffer=b},exit:function(){delete this.outstanding,delete this.buffer},applyClient:function(a){f(a.revision===this.serverRevision+2),this.buffer=this.buffer.compose(a)},applyServer:function(a){if(a.id===this.outstanding.id)this.sendOperation(this.buffer),this.transitionTo("awaitingConfirm",this.buffer);else{var c=b.transform(this.outstanding,a);this.outstanding=c[0];var d=c[1],e=b.transform(this.buffer,d);this.buffer=e[0],this.applyOperation(e[1])}},newRevision:function(){return this.serverRevision+2}}},d}(this),typeof module=="object"&&(module.exports=ot.Client),function(){function a(a,b){if(!a)throw new Error(b||"assertion error")}ot.Operation.prototype.fromCodeMirrorChange=function(a,b){function e(a){var b=a.line,c=a.ch,e=0;for(var f=0;f<a.line;f++)e+=d[f].length+1;return e+=c,e}function f(){var a=0;for(var b=0,c=d.length;b<c;b++)a+=d[b].length;return a+d.length-1}function g(a,b){if(a.line===b.line)return d[a.line].slice(a.ch,b.ch);var c=d[a.line].slice(a.ch)+"\n";for(var e=a.line+1;e<b.line;e++)c+=d[e]+"\n";return c+=d[b.line].slice(0,b.ch),c}function h(a,b,c){var e=a.slice(0),f=d[b.line].slice(0,b.ch),g=d[c.line].slice(c.ch);e[0]=f+e[0],e[e.length-1]+=g,e.unshift(c.line-b.line+1),e.unshift(b.line),d.splice.apply(d,e)}function i(a,b){var c=e(b.from),d=e(b.to),i=f();a.retain(c),a.delete(g(b.from,b.to)),a.insert(b.text.join("\n")),a.retain(i-d),h(b.text,b.from,b.to)}var c=this,d=b.split("\n");i(c,a);for(;;){a=a.next;if(!a)break;var j=new ot.Operation(c.revision+1);i(j,a),c=c.compose(j)}return c},ot.Operation.prototype.applyToCodeMirror=function(b){var c=this;b.operation(function(){var d=c.ops,e=0;for(var f=0,g=d.length;f<g;f++){var h=d[f];if(h.retain)e+=h.retain;else if(h.insert)b.replaceRange(h.insert,b.posFromIndex(e)),e+=h.insert.length;else if(h.delete){var i=b.posFromIndex(e),j=b.posFromIndex(e+h.delete.length);a(b.getRange(i,j)===h.delete),b.replaceRange("",i,j)}}a(e===b.getValue().length)})}}();
  1
+if(typeof ot=="undefined")var ot={};ot.Operation=function(){function a(a,c,e){b(typeof a=="number"&&a>=0,"the first parameter to the the parent revision number of the document"),this.revision=a,this.id=c||d(),b(this.id&&typeof this.id=="string","not a valid id: "+this.id),this.meta=e||{},this.ops=[],this.baseLength=0,this.targetLength=0}function b(a,b){if(!a)throw new Error(b||"assertion error")}function c(a){return Math.floor(Math.random()*a)}function d(){var a="",b=16;while(b--)a+=c(16).toString(16);return a}return a.prototype.retain=function(a){b(typeof a=="number"&&a>=0);if(a===0)return this;this.baseLength+=a,this.targetLength+=a;var c=this.ops[this.ops.length-1];return c&&c.retain?c.retain+=a:this.ops.push({retain:a}),this},a.prototype.insert=function(a){b(typeof a=="string");if(a==="")return this;this.targetLength+=a.length;var c=this.ops[this.ops.length-1];return c&&c.insert?c.insert+=a:this.ops.push({insert:a}),this},a.prototype.delete=function(a){b(typeof a=="string");if(a==="")return this;this.baseLength+=a.length;var c=this.ops[this.ops.length-1];return c&&c.delete?c.delete+=a:this.ops.push({"delete":a}),this},a.prototype.toString=function(){var a=Array.prototype.map||function(a){var b=this,c=[];for(var d=0,e=b.length;d<e;d++)c[d]=a(b[d]);return c};return a.call(this.ops,function(a){return a.retain?"retain "+a.retain:a.insert?"insert '"+a.insert+"'":"delete '"+a.delete+"'"}).join(", ")},a.fromJSON=function(c){b(c.id);var d=new a(c.revision,c.id,c.meta);b(typeof d.meta=="object");var e=c.ops;for(var f=0,g=e.length;f<g;f++){var h=e[f];if(h.retain)d.retain(h.retain);else if(h.insert)d.insert(h.insert);else{if(!h.delete)throw new Error("unknown operation: "+JSON.stringify(h));d.delete(h.delete)}}return b(d.baseLength===c.baseLength,"baseLengths don't match"),b(d.targetLength===c.targetLength,"targetLengths don't match"),d},a.prototype.apply=function(a){var b=this;if(a.length!==b.baseLength)throw new Error("The operation's base length must be equal to the string's length.");var c=[],d=0,e=0,f=this.ops;for(var g=0,h=f.length;g<h;g++){var i=f[g];if(i.retain){if(e+i.retain>a.length)throw new Error("Operation can't retain more characters than are left in the string.");c[d++]=a.slice(e,e+i.retain),e+=i.retain}else if(i.insert)c[d++]=i.insert;else{if(i.delete!==a.slice(e,e+i.delete.length))throw new Error("The deleted string and the next characters in the string don't match.");e+=i.delete.length}}if(e!==a.length)throw new Error("The operation didn't operate on the whole string.");return c.join("")},a.prototype.invert=function(){var b=new a(this.revision+1),c=this.ops;for(var d=0,e=c.length;d<e;d++){var f=c[d];f.retain?b.retain(f.retain):f.insert?b.delete(f.insert):b.insert(f.delete)}return b},a.prototype.compose=function(b){var c=this;if(c.targetLength!==b.baseLength)throw new Error("The base length of the second operation has to be the target length of the first operation");if(c.revision+1!==b.revision)throw new Error("The second operations revision must be one more than the first operations revision");var d=new a(c.revision,undefined,c.meta),e=c.ops,f=b.ops,g=0,h=0,i=e[g++],j=f[h++];for(;;){var k=i&&(i.retain||(i.insert||i.delete).length),l=j&&(j.retain||(j.insert||j.delete).length),m=Math.min(k,l);if(typeof i=="undefined"&&typeof j=="undefined")break;if(typeof i=="undefined"){if(!j.insert)throw new Error("Successive operations can only insert new characters at the end of the string.");d.insert(j.insert),j=f[h++]}else if(typeof j=="undefined"){if(!i.delete)throw new Error("The first operation can only delete at the end of operation 2.");d.delete(i.delete),i=e[g++]}else if(i.retain&&j.retain)d.retain(m),k>l?(i={retain:k-l},j=f[h++]):k===l?(i=e[g++],j=f[h++]):(i=e[g++],j={retain:l-k});else if(i.insert&&j.delete){if(i.insert.slice(0,m)!==j.delete.slice(0,m))throw new Error("Successive operations must delete what has been inserted before.");k>l?(i={insert:i.insert.slice(l)},j=f[h++]):k===l?(i=e[g++],j=f[h++]):(i=e[g++],j={"delete":j.delete.slice(k)})}else if(i.insert&&j.retain)k>l?(d.insert(i.insert.slice(0,l)),i={insert:i.insert.slice(l)},j=f[h++]):k===l?(d.insert(i.insert),i=e[g++],j=f[h++]):(d.insert(i.insert),i=e[g++],j={retain:l-k});else if(i.retain&&j.delete)k>l?(d.delete(j.delete),i={retain:k-l},j=f[h++]):k===l?(d.delete(j.delete),i=e[g++],j=f[h++]):(d.delete(j.delete.slice(0,k)),i=e[g++],j={"delete":j.delete.slice(k)});else if(i.delete)d.delete(i.delete),i=e[g++];else{if(!j.insert)throw new Error("This shouldn't happen: op1: "+JSON.stringify(i)+", op2: "+JSON.stringify(j));d.insert(j.insert),j=f[h++]}}return d},a.transform=function(b,c){if(b.baseLength!==c.baseLength)throw new Error("Both operations have to have the same base length");if(b.revision!==c.revision)throw new Error("Both operations have to have the same revision");var d=new a(c.revision+1,b.id,b.meta),e=new a(b.revision+1,c.id,c.meta),f=b.ops,g=c.ops,h=0,i=0,j=f[h++],k=g[i++];for(;;){var l=j&&(j.retain||(j.insert||j.delete).length),m=k&&(k.retain||(k.insert||k.delete).length),n=Math.min(l,m);if(typeof j=="undefined"&&typeof k=="undefined")break;if(j&&j.insert)d.insert(j.insert),e.retain(j.insert.length),j=f[h++];else if(k&&k.insert)d.retain(k.insert.length),e.insert(k.insert),k=g[i++];else if(j.retain&&k.retain)d.retain(n),e.retain(n),l>m?(j={retain:l-m},k=g[i++]):l===m?(j=f[h++],k=g[i++]):(j=f[h++],k={retain:m-l});else if(j.delete&&k.delete){if(j.delete.slice(0,n)!==k.delete.slice(0,n))throw new Error("When two concurrent operations delete text at the same position, they must delete the same text");l>m?(j={"delete":j.delete.slice(m)},k=g[i++]):l===m?(j=f[h++],k=g[i++]):(j=f[h++],k={"delete":k.delete.slice(l)})}else if(j.delete&&k.retain)d.delete(j.delete.slice(0,n)),l>m?(j={"delete":j.delete.slice(m)},k=g[i++]):l===m?(j=f[h++],k=g[i++]):(j=f[h++],k={retain:k.retain-l});else{if(!j.retain||!k.delete)throw new Error("The two operations aren't compatible");e.delete(k.delete.slice(0,n)),l>m?(j={retain:j.retain-m},k=g[i++]):l===m?(j=f[h++],k=g[i++]):(j=f[h++],k={"delete":k.delete.slice(l)})}}return[d,e]},a}(),typeof module=="object"&&(module.exports=ot.Operation);if(typeof ot=="undefined")var ot={};ot.Client=function(a){function d(a){f(typeof a=="number"&&a>=0),this.serverRevision=a,this.state="synchronized"}function e(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c])}function f(a,b){if(!a)throw new Error(b||"assertion error")}var b=a.ot?a.ot.Operation:require("./operation"),c={callMethodForState:function(a){var b=Array.prototype.slice.call(arguments,1);return this.states[this.state][a].apply(this,b)},transitionTo:function(a){var b=Array.prototype.slice.call(arguments,1);this.states[this.state].exit.apply(this,[]),this.states[this.state=a].enter.apply(this,b)}};return e(d.prototype,c),d.prototype.createOperation=function(){return new b(this.callMethodForState("newRevision"))},d.prototype.applyClient=function(a){return this.callMethodForState("applyClient",a)},d.prototype.applyServer=function(a){f(a.revision===this.serverRevision),this.callMethodForState("applyServer",a),this.serverRevision++},d.prototype.sendOperation=function(a){throw new Error("sendOperation must be defined in child class")},d.prototype.applyOperation=function(a){throw new Error("applyOperation must be defined in child class")},d.prototype.states={"synchronized":{enter:function(){},exit:function(){},applyClient:function(a){this.sendOperation(a),this.transitionTo("awaitingConfirm",a)},applyServer:function(a){this.applyOperation(a)},newRevision:function(){return this.serverRevision}},awaitingConfirm:{enter:function(a){this.outstanding=a},exit:function(){delete this.outstanding},applyClient:function(a){f(a.revision===this.serverRevision+1),this.transitionTo("awaitingWithBuffer",this.outstanding,a)},applyServer:function(a){if(a.id===this.outstanding.id)this.transitionTo("synchronized");else{var c=b.transform(this.outstanding,a);this.outstanding=c[0],this.applyOperation(c[1])}},newRevision:function(){return this.serverRevision+1}},awaitingWithBuffer:{enter:function(a,b){this.outstanding=a,this.buffer=b},exit:function(){delete this.outstanding,delete this.buffer},applyClient:function(a){f(a.revision===this.serverRevision+2),this.buffer=this.buffer.compose(a)},applyServer:function(a){if(a.id===this.outstanding.id)this.sendOperation(this.buffer),this.transitionTo("awaitingConfirm",this.buffer);else{var c=b.transform(this.outstanding,a);this.outstanding=c[0];var d=c[1],e=b.transform(this.buffer,d);this.buffer=e[0],this.applyOperation(e[1])}},newRevision:function(){return this.serverRevision+2}}},d}(this),typeof module=="object"&&(module.exports=ot.Client),function(){function a(a,b){if(!a)throw new Error(b||"assertion error")}ot.Operation.prototype.fromCodeMirrorChange=function(a,b){function e(a){var b=a.line,c=a.ch,e=0;for(var f=0;f<a.line;f++)e+=d[f].length+1;return e+=c,e}function f(){var a=0;for(var b=0,c=d.length;b<c;b++)a+=d[b].length;return a+d.length-1}function g(a,b){if(a.line===b.line)return d[a.line].slice(a.ch,b.ch);var c=d[a.line].slice(a.ch)+"\n";for(var e=a.line+1;e<b.line;e++)c+=d[e]+"\n";return c+=d[b.line].slice(0,b.ch),c}function h(a,b,c){var e=a.slice(0),f=d[b.line].slice(0,b.ch),g=d[c.line].slice(c.ch);e[0]=f+e[0],e[e.length-1]+=g,e.unshift(c.line-b.line+1),e.unshift(b.line),d.splice.apply(d,e)}function i(a,b){var c=e(b.from),d=e(b.to),i=f();a.retain(c),a.delete(g(b.from,b.to)),a.insert(b.text.join("\n")),a.retain(i-d),h(b.text,b.from,b.to)}var c=this,d=b.split("\n");i(c,a);for(;;){a=a.next;if(!a)break;var j=new ot.Operation(c.revision+1);i(j,a),c=c.compose(j)}return c},ot.Operation.prototype.applyToCodeMirror=function(b){var c=this;b.operation(function(){var d=c.ops,e=0;for(var f=0,g=d.length;f<g;f++){var h=d[f];if(h.retain)e+=h.retain;else if(h.insert)b.replaceRange(h.insert,b.posFromIndex(e)),e+=h.insert.length;else if(h.delete){var i=b.posFromIndex(e),j=b.posFromIndex(e+h.delete.length);a(b.getRange(i,j)===h.delete),b.replaceRange("",i,j)}}a(e===b.getValue().length)})}}(),ot.CodeMirrorClient=function(){function c(b,c,d,e,f){a.call(this,b),this.cm=c,this.socket=d,this.name=e,this.users=f||{},this.fromServer=!1,this.oldValue=c.getValue(),this.unredo=!1,this.undoStack=[],this.redoStack=[],this.initializeSocket(),this.initializeCodeMirror(),this.initializeUsers(),this.onCodeMirrorCursorActivity()}function d(a){function b(a){var b=a.ops;return b.length===0||b.length===1&&!!b[0].retain}while(a.length>0){var c=a[a.length-1];if(!b(c))break;a.pop()}}function f(a){var b=a.ops;if(b[0].retain){var c=b[0].retain;return b[1].insert?c+b[1].insert.length:c}return b[0].insert?b[0].insert.length:0}function g(a){var b=document.createElement("div");b.className="other-user";var c=document.createElement("pre");return c.innerHTML="&nbsp;",b.appendChild(c),b.appendChild(document.createTextNode(a)),b}function h(a,b){function c(){}c.prototype=b.prototype,a.prototype=new c,a.prototype.constructor=a}function i(a){a.parentNode&&a.parentNode.removeChild(a)}var a=ot.Client,b=ot.Operation;h(c,a),c.prototype.applyClient=function(b){b.meta.cursor=this.cursor,b.meta.selectionEnd=this.selectionEnd,clearTimeout(this.sendCursorTimeout),a.prototype.applyClient.call(this,b)},c.prototype.applyServer=function(b){var c=this.outstanding&&this.outstanding.id===b.id;a.prototype.applyServer.call(this,b);if(!c){var d=b.meta;this.updateUserCursor(d.name,d.cursor,d.selectionEnd),this.transformUnredoStack(this.undoStack,b),this.transformUnredoStack(this.redoStack,b)}},c.prototype.initializeSocket=function(){var a=this;this.socket.on("user_joined",function(b){a.onUserJoined(b)}).on("user_left",function(b){a.onUserLeft(b)}).on("operation",function(c){var d=b.fromJSON(c);console.log("Operation from server by user "+d.meta.name+":",d),a.applyServer(d)}).on("cursor",function(b){a.updateUserCursor(b.name,b.cursor,b.selectionEnd)})},c.prototype.onUserJoined=function(a){console.log("User joined: ",a),this.users[a.name]=a,this.initializeUser(a)},c.prototype.onUserLeft=function(a){console.log("User disconnected: "+a.name),i(this.users[a.name].el),delete this.users[a.name]},c.prototype.initializeCodeMirror=function(){var a=this.cm,b=this,c=a.getOption("onChange");a.setOption("onChange",function(d,e){b.onCodeMirrorChange(e),c&&c.call(this,a,e)});var d=a.getOption("onCursorActivity");a.setOption("onCursorActivity",function(c){b.onCodeMirrorCursorActivity(),d&&d.call(this,a)}),a.undo=function(){b.undo()},a.redo=function(){b.redo()}},c.prototype.initializeUsers=function(){var a=this.users;for(var b in a)a.hasOwnProperty(b)&&(a[b].name=b,this.initializeUser(a[b]))},c.prototype.initializeUser=function(a){a.el=g(a.name),this.updateUserElementPosition(a),this.updateUserMark(a)};var e=20;return c.prototype.unredoHelper=function(a,b){d(a);if(a.length===0)return;var c=a.pop();c.revision=this.createOperation().revision,this.unredo=!0,c.applyToCodeMirror(this.cm),this.applyClient(c),b.push(c.invert());var e=this.cm.posFromIndex(f(c));this.cm.setCursor(e)},c.prototype.transformUnredoStack=function(a,c){d(a);for(var e=a.length-1;e>=0;e--){a[e].revision=c.revision;var f=b.transform(a[e],c);a[e]=f[0],c=f[1]}},c.prototype.addOperationToUndo=function(a){function b(a,b){var c=a.ops;switch(c.length){case 0:return!0;case 1:return!!b(c[0]);case 2:return!!(c[0].retain&&b(c[1])||b(c[0])&&c[1].retain);case 3:return!!(c[0].retain&&b(c[1])&&c[2].retain);default:return!1}}function c(a){return b(a,function(a){return a.insert})}function d(a){return b(a,function(a){return a.delete})}function f(a,b){if(c(a)&&c(b))return c(a.compose(b));if(d(a)&&d(b)){var e=a.ops[0],f=b.ops;return e.retain?f[0].delete?e.retain===f[0].delete.length:e.retain===f[0].retain+f[1].delete.length:!1}return!1}if(this.undoStack.length===0)this.undoStack.push(a);else{var g=this.undoStack[this.undoStack.length-1];g.revision=a.revision+1;if(f(a,g)){var h=a.compose(g);this.undoStack[this.undoStack.length-1]=h}else this.undoStack.push(a),this.undoStack.length>e&&this.undoStack.shift()}this.redoStack.length>0&&(this.redoStack=[])},c.prototype.undo=function(){this.unredoHelper(this.undoStack,this.redoStack)},c.prototype.redo=function(){this.unredoHelper(this.redoStack,this.undoStack)},c.prototype.updateUserCursor=function(a,b,c){console.log(a+" moved his/her cursor: "+b);var d=this.users[a];d.cursor=b,d.selectionEnd=c,this.updateUserElementPosition(d),this.updateUserMark(d)},c.prototype.updateUserElementPosition=function(a){var b=cm.posFromIndex(a.cursor);i(a.el),this.cm.addWidget(b,a.el,!1)},c.prototype.updateUserMark=function(a){a.mark&&(a.mark.clear(),delete a.mark);if(a.selectionEnd!==a.cursor){var b=Math.min(a.cursor,a.selectionEnd),c=Math.max(a.cursor,a.selectionEnd),d=cm.posFromIndex(b),e=cm.posFromIndex(c);a.mark=this.cm.markText(d,e,"other-user-selection")}},c.prototype.onCodeMirrorChange=function(a){var b=this.cm;try{if(!this.fromServer&&!this.unredo){var c=this.createOperation().fromCodeMirrorChange(a,this.oldValue);this.addOperationToUndo(c.invert()),this.applyClient(c)}}finally{this.fromServer=!1,this.unredo=!1,this.oldValue=b.getValue()}},c.prototype.onCodeMirrorCursorActivity=function(){function b(a,b){return a.line===b.line&&a.ch===b.ch}var a=this.cm,c=a.getCursor(),d=a.indexFromPos(c),e;if(a.somethingSelected()){var f=a.getCursor(!0),g=b(c,f)?a.getCursor(!1):f;e=a.indexFromPos(g)}else e=d;this.cursor=d,this.selectionEnd=e;if(this.state==="awaitingWithBuffer")this.buffer.meta.cursor=d,this.buffer.meta.selectionEnd=e;else{var h=this;clearTimeout(this.sendCursorTimeout),this.sendCursorTimeout=setTimeout(function(){h.socket.emit("cursor",{cursor:d,selectionEnd:e})},50)}},c.prototype.sendOperation=function(a){this.socket.emit("operation",a)},c.prototype.applyOperation=function(a){this.fromServer=!0,a.applyToCodeMirror(this.cm)},c}();

0 notes on commit 54a2cfd

Please sign in to comment.
Something went wrong with that request. Please try again.