diff options
author | matthewsotoudeh <matthewsot@outlook.com> | 2016-01-24 01:44:56 -0800 |
---|---|---|
committer | matthewsotoudeh <matthewsot@outlook.com> | 2016-01-24 01:44:56 -0800 |
commit | 765859af71c323576e1260ff6906a3c157f9bc8a (patch) | |
tree | b7c517c02cddf1dc72bfd4d0377e17b993039668 | |
parent | 70bb1dc9bc519996ca77674709fbcf449510b32d (diff) |
-rw-r--r-- | ecrit-core.js | 9 | ||||
-rw-r--r-- | ecrit-presentation.js | 21 | ||||
-rw-r--r-- | ecrit-test.js | 45 | ||||
-rw-r--r-- | index.html | 6 |
4 files changed, 66 insertions, 15 deletions
diff --git a/ecrit-core.js b/ecrit-core.js index 0a9cacb..bf56e1a 100644 --- a/ecrit-core.js +++ b/ecrit-core.js @@ -150,7 +150,7 @@ ecrit.Node.prototype.insertNode = function (node, afterId, beforeId) { * @param {Node} node - The node to remove */ ecrit.Node.prototype.removeNode = function (node) { - var foundNode = this.getChildNodebyId(node.id, false); + var foundNode = this.getChildNodeById(node.id); if (foundNode === null || foundNode === this) return; var index = this.children.indexOf(foundNode); @@ -330,13 +330,12 @@ ecrit.TextSpan.prototype._applyTransformation = function (transformation) { var newStr = this.text.substring(0, transformation.index); newStr += this.text.substring((transformation.index + transformation.text.length)); this.text = newStr; - this._emit("textModified", transformation); - return; + break; case "insertText": this.text = this.text.slice(0, transformation.index) + transformation.text + this.text.slice(transformation.index); - this._emit("textModified", transformation); - return; + break; } + this._emit("textModified", { node: this, transformation: transformation }); }; ecrit.TextSpan.prototype._undo = function (transformation) { diff --git a/ecrit-presentation.js b/ecrit-presentation.js index 27e8289..fa86bc8 100644 --- a/ecrit-presentation.js +++ b/ecrit-presentation.js @@ -1,11 +1,13 @@ ecrit.presentation = {}; -ecrit.presentation.Watcher = function (document, presentationDiv) { - document.on("nodeInserted", this.nodeInsertionHandler); - document.on("nodeRemoved", this.nodeRemovalHandler); +ecrit.presentation.Watcher = function (document) { + document.on("nodeInserted", this.nodeInsertionHandler.bind(this)); + document.on("nodeRemoved", this.nodeRemovalHandler.bind(this)); }; -ecrit.presentation.Watcher.prototype.nodeInsertionHander = function (data) { - data.node.on("nodeInserted", this.nodeInsertionHandler); +ecrit.presentation.Watcher.prototype.nodeInsertionHandler = function (data) { + data.node.on("nodeInserted", this.nodeInsertionHandler.bind(this)); + data.node.on("nodeRemoved", this.nodeRemovalHandler.bind(this)); + var parentEl = document.querySelector("[data-ecrit-id=\"" + data.node.parent.id + "\"]"); switch (data.node.type) { case "Paragraph": @@ -20,7 +22,7 @@ ecrit.presentation.Watcher.prototype.nodeInsertionHander = function (data) { } break; case "TextSpan": - data.node.on("textModified", this.textModifiedHandler); + data.node.on("textModified", this.textModifiedHandler.bind(this)); var newEl = document.createElement("span"); newEl.setAttribute("data-ecrit-id", data.node.id); newEl.textContent = data.node.text; @@ -35,6 +37,11 @@ ecrit.presentation.Watcher.prototype.nodeInsertionHander = function (data) { } }; -ecrit.presentation.Watcher.prototype.textModificationHandler = function (data) { +ecrit.presentation.Watcher.prototype.nodeRemovalHandler = function (data) { + document.querySelector("[data-ecrit-id=\"" + data.id + "\"]").remove(); +}; +ecrit.presentation.Watcher.prototype.textModifiedHandler = function (data) { + var el = document.querySelector("[data-ecrit-id=\"" + data.node.id + "\"]"); + el.textContent = data.node.text; };
\ No newline at end of file diff --git a/ecrit-test.js b/ecrit-test.js new file mode 100644 index 0000000..fe3390b --- /dev/null +++ b/ecrit-test.js @@ -0,0 +1,45 @@ +var doc = new ecrit.Document(); +var watcher = new ecrit.presentation.Watcher(doc); +var para = new ecrit.Paragraph(doc, "p-id-1"); + +doc.applyTransformation({ + "affectsId": "root", + "timestamp": 100, + "action": "insertNode", + "node": para, + "lastApplied": -1 +}); + +var textSpan = new ecrit.TextSpan(para, "ts-id-1", { text: "test text" }); + +doc.getChildNodeById("p-id-1").applyTransformation({ + "affectsId": "p-id-1", + "timestamp": 200, + "action": "insertNode", + "node": textSpan, + "lastApplied": -1 +}); + +doc.getChildNodeById("ts-id-1").applyTransformation(new ecrit.Transformation({ + action: "insertText", + text: "abc ", + index: 0, + timestamp: 300, + lastApplied: -1 +})); + +doc.getChildNodeById("ts-id-1").applyTransformation(new ecrit.Transformation({ + action: "insertText", + text: "hello there! ", + index: 4, + timestamp: 500, + lastApplied: 300 +})); + +// doc.applyTransformation({ +// "affectsId": "p-id-1", +// "timestamp": 100, +// "action": "removeNode", +// "node": doc.getChildNodeById("ts-id-1"), +// "lastApplied": 100 +// });
\ No newline at end of file @@ -4,8 +4,8 @@ </head> <body> <div id="presentation" data-ecrit-id="root"></div> - <script type="text/javascript" src="ecrit-core.js" /> - <script type="text/javascript" src="ecrit-presentation.js" /> - <script type="text/javascript" src="ecrit-test.js" /> + <script type="text/javascript" src="ecrit-core.js"></script> + <script type="text/javascript" src="ecrit-presentation.js"></script> + <script type="text/javascript" src="ecrit-test.js"></script> </body> </html>
\ No newline at end of file |