forked from josephg/ShareJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-tp2.js
More file actions
1 lines (1 loc) · 5.75 KB
/
text-tp2.js
File metadata and controls
1 lines (1 loc) · 5.75 KB
1
(function(){var e,t,n,r,i,s,o,u=!0,a=window.sharejs,f={name:"text-tp2",tp2:!0,create:function(){return{charLength:0,totalLength:0,positionCache:[],data:[]}},serialize:function(e){if(!e.data)throw new Error("invalid doc snapshot");return e.data},deserialize:function(e){var t,n,r,i=f.create();i.data=e;for(n=0,r=e.length;n<r;n++)t=e[n],typeof t=="string"?(i.charLength+=t.length,i.totalLength+=t.length):i.totalLength+=t;return i}},l=function(e){var t,n,r,i,s;if(!Array.isArray(e))throw new Error("Op must be an array of components");n=null,s=[];for(r=0,i=e.length;r<i;r++){t=e[r];if(typeof t=="object")if(t.i!==void 0){if(!(typeof t.i=="string"&&t.i.length>0||typeof t.i=="number"&&t.i>0))throw new Error("Inserts must insert a string or a +ive number")}else{if(t.d===void 0)throw new Error("Operation component must define .i or .d");if(!(typeof t.d=="number"&&t.d>0))throw new Error("Deletes must be a +ive number")}else{if(typeof t!="number")throw new Error("Op components must be objects or numbers");if(!(t>0))throw new Error("Skip components must be a positive number");if(typeof n=="number")throw new Error("Adjacent skip components should be combined")}s.push(n=t)}return s};f._takeDoc=s=function(e,t,n,r){var i,s,o;if(t.index>=e.data.length)throw new Error("Operation goes past the end of the document");return i=e.data[t.index],s=typeof i=="string"?n!==void 0?i.slice(t.offset,t.offset+n):i.slice(t.offset):n===void 0||r?i-t.offset:Math.min(n,i-t.offset),o=s.length||s,(i.length||i)-t.offset>o?t.offset+=o:(t.index++,t.offset=0),s},f._appendDoc=t=function(e,t){var n;if(t===0||t==="")return;typeof t=="string"?(e.charLength+=t.length,e.totalLength+=t.length):e.totalLength+=t,n=e.data,n.length===0?n.push(t):typeof n[n.length-1]==typeof t?n[n.length-1]+=t:n.push(t)},f.apply=function(e,n){var r,i,o,u,a,c,h;if(e.totalLength===void 0||e.charLength===void 0||e.data.length===void 0)throw new Error("Snapshot is invalid");l(n),i=f.create(),u={index:0,offset:0};for(c=0,h=n.length;c<h;c++){r=n[c];if(typeof r=="number"){a=r;while(a>0)o=s(e,u,a),t(i,o),a-=o.length||o}else if(r.i!==void 0)t(i,r.i);else if(r.d!==void 0){a=r.d;while(a>0)o=s(e,u,a),a-=o.length||o;t(i,r.d)}}return i},f._append=e=function(e,t){var n;if(t!==0&&t.i!==""&&t.i!==0&&t.d!==0)return e.length===0?e.push(t):(n=e[e.length-1],typeof t=="number"&&typeof n=="number"?e[e.length-1]+=t:t.i!==void 0&&n.i!=null&&typeof n.i==typeof t.i?n.i+=t.i:t.d!==void 0&&n.d!=null?n.d+=t.d:e.push(t))},i=function(e){var t=0,n=0,r=function(r,i){var s,o,u,a;return t===e.length?null:(u=e[t],typeof (o=u)=="number"||typeof (o=u.i)=="number"||(o=u.d)!==void 0?(r==null||o-n<=r||i&&u.i!==void 0?(s=o-n,++t,n=0):(n+=r,s=r),u.i!==void 0?{i:s}:u.d!==void 0?{d:s}:s):(r==null||u.i.length-n<=r||i?(a={i:u.i.slice(n)},++t,n=0):(a={i:u.i.slice(n,n+r)},n+=r),a))},i=function(){return e[t]};return[r,i]},r=function(e){return typeof e=="number"?e:typeof e.i=="string"?e.i.length:e.d||e.i},f.normalize=function(t){var n,r,i,s=[];for(r=0,i=t.length;r<i;r++)n=t[r],e(s,n);return s},o=function(t,n,s,o){var u,a,f,c,h,p,d,v,m,g;l(t),l(n),c=[],m=i(t),p=m[0],h=m[1];for(d=0,v=n.length;d<v;d++){a=n[d],f=r(a);if(a.i!==void 0)if(s){if(o==="left")while(((g=h())!=null?g.i:void 0)!==void 0)e(c,p());e(c,f)}else while(f>0){u=p(f,!0);if(u===null)throw new Error("The transformed op is invalid");if(u.d!==void 0)throw new Error("The transformed op deletes locally inserted characters - it cannot be purged of the insert.");typeof u=="number"?f-=u:e(c,u)}else while(f>0){u=p(f,!0);if(u===null)throw new Error("The op traverses more elements than the document has");e(c,u),u.i||(f-=r(u))}}while(a=p()){if(a.i===void 0)throw new Error("Remaining fragments in the op: "+a);e(c,a)}return c},f.transform=function(e,t,n){if(n!=="left"&&n!=="right")throw new Error("side ("+n+") should be 'left' or 'right'");return o(e,t,!0,n)},f.prune=function(e,t){return o(e,t,!1)},f.compose=function(t,n){var s,o,u,a,f,c,h,p,d,v;if(t===null||t===void 0)return n;l(t),l(n),f=[],v=i(t),c=v[0],h=v[1];for(p=0,d=n.length;p<d;p++){u=n[p];if(typeof u=="number"){a=u;while(a>0){s=c(a);if(s===null)throw new Error("The op traverses more elements than the document has");e(f,s),a-=r(s)}}else if(u.i!==void 0)e(f,{i:u.i});else{a=u.d;while(a>0){s=c(a);if(s===null)throw new Error("The op traverses more elements than the document has");o=r(s),s.i!==void 0?e(f,{i:o}):e(f,{d:o}),a-=o}}}while(u=c()){if(u.i===void 0)throw new Error("Remaining fragments in op1: "+u);e(f,u)}return f},typeof u!="undefined"&&u!==null?a.types["text-tp2"]=f:module.exports=f,typeof u!="undefined"&&u!==null?f=a.types["text-tp2"]:f=require("./text-tp2"),s=f._takeDoc,e=f._append,n=function(t,n,r,i){var o,u=[];while((i===void 0||i>0)&&r.index<n.data.length)o=s(n,r,i,!0),i!==void 0&&typeof o=="string"&&(i-=o.length),u.push(e(t,o.length||o));return u},f.api={provides:{text:!0},getLength:function(){return this.snapshot.charLength},getText:function(){var e,t=function(){var t,n,r=this.snapshot.data,i=[];for(t=0,n=r.length;t<n;t++)e=r[t],typeof e=="string"&&i.push(e);return i}.call(this);return t.join("")},insert:function(t,r,i){var s,o;return t===void 0&&(t=0),o=[],s={index:0,offset:0},n(o,this.snapshot,s,t),e(o,{i:r}),n(o,this.snapshot,s),this.submitOp(o,i),o},del:function(t,r,i){var o,u=[],a={index:0,offset:0};n(u,this.snapshot,a,t);while(r>0)o=s(this.snapshot,a,r,!0),typeof o=="string"?(e(u,{d:o.length}),r-=o.length):e(u,o);return n(u,this.snapshot,a),this.submitOp(u,i),u},_register:function(){return this.on("remoteop",function(e,t){var n,r,i,o,u,a=0,f={index:0,offset:0};for(o=0,u=e.length;o<u;o++){n=e[o];if(typeof n=="number"){i=n;while(i>0)r=s(t,f,i),typeof r=="string"&&(a+=r.length),i-=r.length||r}else if(n.i!==void 0)typeof n.i=="string"&&(this.emit("insert",a,n.i),a+=n.i.length);else{i=n.d;while(i>0)r=s(t,f,i),typeof r=="string"&&this.emit("delete",a,r),i-=r.length||r}}})}}}).call(this)