diff options
author | matthewsotoudeh <matthewsot@outlook.com> | 2016-01-24 00:39:35 -0800 |
---|---|---|
committer | matthewsotoudeh <matthewsot@outlook.com> | 2016-01-24 00:39:35 -0800 |
commit | 5e5fadc8c1767bc3f9c2c465c3407339f250ec25 (patch) | |
tree | 59b893dc240e1ef3bed7e6c965617b71a9e9b5e1 | |
parent | 3e9f2f33d8a764a4dc05be4f4de7bf38e6148641 (diff) |
updated line.js to use ecrit-core
-rw-r--r-- | Line/README.md | 4 | ||||
-rw-r--r-- | Line/line.js | 104 | ||||
-rw-r--r-- | Line/line.old.js | 90 | ||||
-rw-r--r-- | gulpfile.js | 16 |
4 files changed, 122 insertions, 92 deletions
diff --git a/Line/README.md b/Line/README.md index 12119da..58278d4 100644 --- a/Line/README.md +++ b/Line/README.md @@ -1,4 +1,6 @@ Line ==== -A stripped-down test of the collision handling system.
\ No newline at end of file +A stripped-down test of the collision handling system. + +line.old.js has minimal dependency on ecrit-core (and was used in early testing), whereas line.js is essentially just a wrapper to make testing ecrit-core easier.
\ No newline at end of file diff --git a/Line/line.js b/Line/line.js index 35437ff..14419f6 100644 --- a/Line/line.js +++ b/Line/line.js @@ -1,90 +1,28 @@ var Line = function (text) { - this.text = ""; - this.deferred = []; - this.history = new ecrit.NodeHistory(); - - this._applyTransformation({ - remove: false, - index: 0, - content: text, - timestamp: 0 + this.doc = new ecrit.Document(); + var para = new ecrit.Paragraph(this.doc, "p-id-1"); + + this.doc.applyTransformation({ + "affectsId": "root", + "timestamp": (new Date()).getTime(), + "action": "insertNode", + "node": para, + "lastApplied": -1 }); - this.history.push({ - remove: false, - index: 0, - content: text, - timestamp: 0 + + this.doc.getChildNodeById("p-id-1").applyTransformation({ + "affectsId": "p-id-1", + "timestamp": (new Date()).getTime(), + "action": "insertNode", + "node": new ecrit.TextSpan(para, "ts-id-1", { text: text }), + "lastApplied": -1 }); -}; -Line.prototype._applyTransformation = function (transformation) { - if (transformation.remove) { - var newStr = this.text.substring(0, transformation.index); - newStr += this.text.substring((transformation.index + transformation.content.length)); - this.text = newStr; - return; - } else { - this.text = this.text.slice(0, transformation.index) + transformation.content + this.text.slice(transformation.index); - return; - } -}; - -Line.prototype._undo = function (transformation) { - transformation.remove = !transformation.remove; - this._applyTransformation(transformation); - transformation.remove = !transformation.remove; + this.__defineGetter__("text", function(){ + return this.doc.getChildNodeById("ts-id-1").text; + }); }; -Line.prototype.applyTransformation = function (transformation, verbose, clone) { - cv = verbose === true ? console.log : function () { }; - if (clone !== false) { - transformation = JSON.parse(JSON.stringify(transformation)); - } - - var reference = this.history.withTimestamp(transformation.lastApplied); - if (reference === null) { - this.deferred.push(transformation); - return; - } - - var U = this.history.afterTimestamp(transformation.timestamp); - for (var i = (U.length - 1); i >= 0; i--) { - this.history.splice(this.history.indexOf(U[i]), 1); - this._undo(U[i]); - } - - var E = this.history.betweenTimestamps(transformation.lastApplied, transformation.timestamp); - var D = 0; - for (var i = 0; i < E.length; i++) { - var toCheck = E[i]; - if (toCheck.index < transformation.index) { - D += toCheck.remove ? (-1 * toCheck.content.length) : toCheck.content.length; - } - } - var initialIndex = transformation.index; - transformation.index += D; - - cv(transformation.index); - this._applyTransformation(transformation); - - this.history.push(transformation); - - for (var i = 0; i < U.length; i++) { - var toApply = U[i]; - if (toApply.index > initialIndex) { - toApply.index += D + transformation.content.length; - } - this._applyTransformation(toApply); - this.history.push(toApply); - } - - for (var i = 0; i < this.deferred.length; i++) { - if (this.deferred[i].lastApplied === transformation.timestamp) { - cv(this.deferred[i].index); - this.applyTransformation(this.deferred[i], verbose); - cv(this.text); - this.deferred.splice(i, 1); - i--; - } - } +Line.prototype.applyTransformation = function (transformation) { + this.doc.getChildNodeById("ts-id-1").applyTransformation(new ecrit.Transformation(transformation)); };
\ No newline at end of file diff --git a/Line/line.old.js b/Line/line.old.js new file mode 100644 index 0000000..35437ff --- /dev/null +++ b/Line/line.old.js @@ -0,0 +1,90 @@ +var Line = function (text) { + this.text = ""; + this.deferred = []; + this.history = new ecrit.NodeHistory(); + + this._applyTransformation({ + remove: false, + index: 0, + content: text, + timestamp: 0 + }); + this.history.push({ + remove: false, + index: 0, + content: text, + timestamp: 0 + }); +}; + +Line.prototype._applyTransformation = function (transformation) { + if (transformation.remove) { + var newStr = this.text.substring(0, transformation.index); + newStr += this.text.substring((transformation.index + transformation.content.length)); + this.text = newStr; + return; + } else { + this.text = this.text.slice(0, transformation.index) + transformation.content + this.text.slice(transformation.index); + return; + } +}; + +Line.prototype._undo = function (transformation) { + transformation.remove = !transformation.remove; + this._applyTransformation(transformation); + transformation.remove = !transformation.remove; +}; + +Line.prototype.applyTransformation = function (transformation, verbose, clone) { + cv = verbose === true ? console.log : function () { }; + if (clone !== false) { + transformation = JSON.parse(JSON.stringify(transformation)); + } + + var reference = this.history.withTimestamp(transformation.lastApplied); + if (reference === null) { + this.deferred.push(transformation); + return; + } + + var U = this.history.afterTimestamp(transformation.timestamp); + for (var i = (U.length - 1); i >= 0; i--) { + this.history.splice(this.history.indexOf(U[i]), 1); + this._undo(U[i]); + } + + var E = this.history.betweenTimestamps(transformation.lastApplied, transformation.timestamp); + var D = 0; + for (var i = 0; i < E.length; i++) { + var toCheck = E[i]; + if (toCheck.index < transformation.index) { + D += toCheck.remove ? (-1 * toCheck.content.length) : toCheck.content.length; + } + } + var initialIndex = transformation.index; + transformation.index += D; + + cv(transformation.index); + this._applyTransformation(transformation); + + this.history.push(transformation); + + for (var i = 0; i < U.length; i++) { + var toApply = U[i]; + if (toApply.index > initialIndex) { + toApply.index += D + transformation.content.length; + } + this._applyTransformation(toApply); + this.history.push(toApply); + } + + for (var i = 0; i < this.deferred.length; i++) { + if (this.deferred[i].lastApplied === transformation.timestamp) { + cv(this.deferred[i].index); + this.applyTransformation(this.deferred[i], verbose); + cv(this.text); + this.deferred.splice(i, 1); + i--; + } + } +};
\ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index 36ad254..1d4863f 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -90,27 +90,27 @@ gulp.task('simple-test', function () { var C = new sandbox.Line("xyz123"); var ATransform = { - remove: false, - content: "abc", + action: "insertText", + text: "abc", index: 0, timestamp: 300, - lastApplied: 0 + lastApplied: -1 }; var BTransform = { - remove: false, - content: "hello", + action: "insertText", + text: "hello", index: 6, timestamp: 500, lastApplied: 100 }; var CTransform = { - remove: false, - content: "aaa", + action: "insertText", + text: "aaa", index: 1, timestamp: 100, - lastApplied: 0 + lastApplied: -1 }; // A tests |