canvas: Fixing nodeMoved event handling
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 29 Nov 2013 10:27:19 +0000 (11:27 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 2 Dec 2013 13:50:54 +0000 (14:50 +0100)
src/editor/modules/documentCanvas/canvas/canvas.test.js
src/editor/modules/documentCanvas/canvas/wlxmlListener.js

index 98b7ffe..ceccda3 100644 (file)
@@ -60,6 +60,33 @@ describe('Handling changes to the document', function() {
     });
 });
 
     });
 });
 
+describe('Listening to document changes', function() {
+    it('Handling element node moved', function() {
+        var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
+            a = doc.root.contents()[0],
+            b = doc.root.contents()[1],
+            c = canvas.fromXMLDocument(doc);
+
+        a.before(b);
+        var sectionChildren = c.doc().children();
+        expect(sectionChildren.length).to.equal(2);
+        expect(sectionChildren[0].getWlxmlTag()).to.equal('b');
+        expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
+    });
+    it('Handling text node moved', function() {
+        var doc = getDocumentFromXML('<section><a></a>Alice</section>'),
+            a = doc.root.contents()[0],
+            textNode = doc.root.contents()[1],
+            c = canvas.fromXMLDocument(doc);
+
+        a.before(textNode);
+        var sectionChildren = c.doc().children();
+        expect(sectionChildren.length).to.equal(2);
+        expect(sectionChildren[0].getText()).to.equal('Alice');
+        expect(sectionChildren[1].getWlxmlTag()).to.equal('a');
+    });
+});
+
 describe('Cursor', function() {
 
     var getSelection;
 describe('Cursor', function() {
 
     var getSelection;
index 2dd61ff..c5deabf 100644 (file)
@@ -47,7 +47,7 @@ var handlers = {
         }
         var parentElement = utils.findCanvasElement(event.meta.node.parent()),
             nodeIndex = event.meta.node.getIndex(),
         }
         var parentElement = utils.findCanvasElement(event.meta.node.parent()),
             nodeIndex = event.meta.node.getIndex(),
-            referenceElement, referenceAction;
+            referenceElement, referenceAction, actionArg;
 
         if(nodeIndex === 0) {
             referenceElement = parentElement;
 
         if(nodeIndex === 0) {
             referenceElement = parentElement;
@@ -57,7 +57,8 @@ var handlers = {
             referenceAction = 'after';
         }
 
             referenceAction = 'after';
         }
 
-        referenceElement[referenceAction](event.meta.node);
+        actionArg = utils.findCanvasElement(event.meta.node) || event.meta.node;
+        referenceElement[referenceAction](actionArg);
     },
     nodeMoved: function(event) {
         return handlers.nodeAdded(event);
     },
     nodeMoved: function(event) {
         return handlers.nodeAdded(event);