diff options
author | Matthew S <matthewsot@outlook.com> | 2015-07-03 04:01:17 -0500 |
---|---|---|
committer | Matthew S <matthewsot@outlook.com> | 2015-07-03 04:01:17 -0500 |
commit | d4229b0923b98bf079c47eeb2e84a5e3f2223235 (patch) | |
tree | 81e20b2a4157e8f196c2d8ceca007f8d7b8defdf | |
parent | 5e9b5d80549ebe979064e41954fc0fea9cc46cd1 (diff) |
paragraph methods
-rw-r--r-- | dist/ecrit.js | 87 | ||||
-rw-r--r-- | gulpfile.js | 8 | ||||
-rw-r--r-- | src/text/Document/Nodes/TextSpan.js | 5 |
3 files changed, 82 insertions, 18 deletions
diff --git a/dist/ecrit.js b/dist/ecrit.js index c7f9a36..89e66f7 100644 --- a/dist/ecrit.js +++ b/dist/ecrit.js @@ -171,20 +171,6 @@ ecrit.Document = function () { ecrit.Document.prototype = Object.create(ecrit.Node.prototype); ecrit.Document.prototype.constructor = ecrit.Document; -ecrit.Document.prototype._detectConflicts = function (transformation) { - var conflicts = []; - - for (var i = 0; i < this.history.length; i++) { - var compare = this.history[i]; - - if (compare.timestamp > transformation.timestamp && compare.affectsId === transformation.affectsId) { - conflicts.push(compare); - } - } - - return conflicts; -}; - ecrit.Document.prototype._applyTransformation = function (transformation) { switch (transformation.action) { case "insertNode": @@ -304,10 +290,79 @@ ecrit.Paragraph = function (parent, id, nodes) { }; ecrit.Paragraph.prototype = Object.create(ecrit.Node.prototype); -ecrit.TextSpan = function (id, options) { - this.id = id; + +ecrit.Paragraph.prototype._applyTransformation = function (transformation) { + switch (transformation.action) { + case "insertNode": + this.insertNode(transformation.node, transformation.afterId, transformation.beforeId); + return; + case "removeNode": + this.removeNode(transformation.node); + return; + } +}; + +/** + * Applies a transformation, deals with conflicting transformations, and adds the transformation to the history. + * @param {Transformation} transformation - The transformation to apply + */ +ecrit.Paragraph.prototype.applyTransformation = function (transformation, clone) { + if (clone !== false) { + /*var targetNode = transformation.targetNode; //prevents a circular dependency + + transformation.targetNode = {}; + var clonedTransformation = JSON.parse(JSON.stringify(transformation)); + clonedTransformation.targetNode = targetNode; + transformation.targetNode = targetNode;*/ + } + + var reference = this.history.withTimestamp(transformation.lastApplied); + if (transformation.lastApplied !== -1 && 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]; + //TODO: handle this? + } + /*var initialIndex = transformation.index; + transformation.index += D;*/ + + 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.text.length; + }*/ + this._applyTransformation(toApply); + this.history.push(toApply); + } + + for (var i = 0; i < this.deferred.length; i++) { + if (this.deferred[i].lastApplied === transformation.timestamp) { + this.applyTransformation(this.deferred[i]); + this.deferred.splice(i, 1); + i--; + } + } +}; +ecrit.TextSpan = function (parent, id, options) { this.text = options.text || ""; this.formatting = options.formatting || []; + + ecrit.Node.call(this, parent, id); }; ecrit.TextSpan.prototype = Object.create(ecrit.Node.prototype); diff --git a/gulpfile.js b/gulpfile.js index e99504c..c538271 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -41,6 +41,14 @@ gulp.task('test', function () { assert(doc.children.length === 1, "Doc child length"); assert(doc.getChildNodeById("p-id-1") === para, "getChildNodeById"); + + doc.getChildNodeById("p-id-1").applyTransformation({ + "affectsId": "p-id-1", + "timestamp": (new Date()).getTime(), + "action": "insertNode", + "node": new sandbox.ecrit.TextSpan(para, "ts-id-1"), + "lastApplied": -1 + }); }); gulp.task('simple-test', function () { diff --git a/src/text/Document/Nodes/TextSpan.js b/src/text/Document/Nodes/TextSpan.js index e0b52b6..9e5fbd0 100644 --- a/src/text/Document/Nodes/TextSpan.js +++ b/src/text/Document/Nodes/TextSpan.js @@ -1,7 +1,8 @@ -ecrit.TextSpan = function (id, options) { - this.id = id; +ecrit.TextSpan = function (parent, id, options) { this.text = options.text || ""; this.formatting = options.formatting || []; + + ecrit.Node.call(this, parent, id); }; ecrit.TextSpan.prototype = Object.create(ecrit.Node.prototype); |