Fixing wrapping nodes with subnodes
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 28 Jun 2013 12:32:07 +0000 (14:32 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 28 Jun 2013 12:32:07 +0000 (14:32 +0200)
canvas.nodeWrap was given wrong arguments when text selected on the
left of first subnode was longer than the text selected on the right
of last subnode

modules/documentCanvas/canvasManager.js

index b7e7212..aff3b46 100644 (file)
@@ -102,22 +102,27 @@ Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) {
     if(selection.getRangeAt(0).collapsed) {\r
     \r
     } else {\r
+        var textNodeIdx;\r
+        var parent = $(selection.anchorNode).parent();\r
         var offsetStart = selection.anchorOffset;\r
         var offsetEnd = selection.focusOffset;\r
-        if(offsetStart > offsetEnd) {\r
-            var tmp = offsetStart;\r
-            offsetStart = offsetEnd;\r
-            offsetEnd = tmp;\r
-        }\r
         \r
+        if(selection.anchorNode === selection.focusNode) {\r
+            if(offsetStart > offsetEnd) {\r
+                var tmp = offsetStart;\r
+                offsetStart = offsetEnd;\r
+                offsetEnd = tmp;\r
+            }\r
+            textNodeIdx = parent.contents().index($(selection.anchorNode));\r
+        } else {\r
+            if(parent.contents().index($(selection.anchorNode)) > parent.contents().index($(selection.focusNode))) {\r
+                offsetStart = selection.focusOffset;\r
+                offsetEnd = selection.anchorOffset;\r
+            }\r
+            textNodeIdx = [parent.contents().index($(selection.anchorNode)), parent.contents().index($(selection.focusNode))];\r
+        }\r
         \r
         var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass});\r
-        var parent = $(selection.anchorNode).parent();\r
-        \r
-        var textNodeIdx = parent.contents().index($(selection.anchorNode));\r
-        if(selection.anchorNode != selection.focusNode) {\r
-            textNodeIdx = [textNodeIdx, parent.contents().index($(selection.focusNode))];\r
-        }\r
         this.canvas.nodeWrap({inside: canvasNode.create(parent),\r
                               _with: wrapper,\r
                               offsetStart: offsetStart,\r