editor: canvas - stop using thrown away nodeMoved event, use 'move' flag instead
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 9 May 2014 14:12:27 +0000 (16:12 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 28 May 2014 07:58:37 +0000 (09:58 +0200)
This approach always rerenders text nodes...

src/editor/modules/documentCanvas/canvas/genericElement.js
src/editor/modules/documentCanvas/canvas/utils.js
src/editor/modules/documentCanvas/canvas/wlxmlListener.js

index a2601f8..44615f5 100644 (file)
@@ -103,20 +103,19 @@ $.extend(generic, {
             referenceAction = 'after';
         }
       
-        if(event.type === 'nodeMoved') {
-            actionArg = utils.getElementForNode(event.meta.node, event.meta.parent);
-            if(actionArg.sameNode(referenceElement)) {
+        if(event.meta.move) {
+            /* Let's check if this node had its own canvas element and it's accessible. */
+            actionArg = utils.getElementForElementRootNode(event.meta.node);
+            if(actionArg && actionArg.sameNode(referenceElement)) {
                 referenceElement = this.children()[nodeIndex];
             }
-        } else {
+        }
+        if(!actionArg) {
             actionArg = event.meta.node;
         }
 
         referenceElement[referenceAction](actionArg);
     },
-    onNodeMoved: function(event) {
-        return this.onNodeAdded.call(this, event);
-    },
     onNodeDetached: function(event) {
         if(event.meta.node.sameNode(this)) {
             this.detach();
index 130d51c..1df3f82 100644 (file)
@@ -20,6 +20,24 @@ var nearestInDocumentOrder = function(selector, direction, element) {
     return null;
 };
 
+var getElementForElementRootNode = function(node, withParent) {
+    if(node.nodeType === Node.TEXT_NODE) {
+        return _getElementForRootTextNode(node, withParent);
+    }
+    return node.getData('canvasElement');
+};
+
+var _getElementForRootTextNode = function(textNode, withParent) {
+    var parentElement = getElementForNode(withParent || textNode.parent()),
+        toret;
+    parentElement.children().some(function(child) {
+        if(child.wlxmlNode.sameNode(textNode)) {
+            toret = child;
+            return true;
+        }
+    });
+    return toret;
+};
 
 var getElementForNode = function(node, withParent) {
     if(node.nodeType === Node.TEXT_NODE) {
@@ -62,7 +80,8 @@ return {
         ZWS: '\u200B'
     },
     getElementForNode: getElementForNode,
-    getElementForDetachedNode: getElementForDetachedNode
+    getElementForDetachedNode: getElementForDetachedNode,
+    getElementForElementRootNode: getElementForElementRootNode
 };
 
 });
index 760208f..02dc657 100644 (file)
@@ -69,10 +69,6 @@ var handlers = {
 
         containingElement.handle(event);
     },
-    nodeMoved: function(event) {
-        return handlers.nodeAdded.call(this, event); //
-        //
-    },
     nodeDetached: function(event) {
         var element = utils.getElementForDetachedNode(event.meta.node, event.meta.parent);
         element.handle(event);