Refactoring
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 28 Jun 2013 13:47:48 +0000 (15:47 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 28 Jun 2013 13:47:48 +0000 (15:47 +0200)
modules/documentCanvas/canvasManager.js

index aff3b46..201394e 100644 (file)
@@ -97,15 +97,17 @@ Manager.prototype.selectNode = function(cnode, options) {
 };\r
 \r
 Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) {\r
-    var selection = window.getSelection();\r
-\r
-    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
+    var selection = window.getSelection(),\r
+        $anchorNode = $(selection.anchorNode),\r
+        $focusNode = $(selection.focusNode);\r
+        \r
+        \r
+    if(!selection.isCollapsed && $anchorNode.parent()[0] === $focusNode.parent()[0]) {\r
+        var textNodeIdx,\r
+            parent = $anchorNode.parent(),\r
+            parentContents = parent.contents(),\r
+            offsetStart = selection.anchorOffset,\r
+            offsetEnd = selection.focusOffset;\r
         \r
         if(selection.anchorNode === selection.focusNode) {\r
             if(offsetStart > offsetEnd) {\r
@@ -113,13 +115,13 @@ Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) {
                 offsetStart = offsetEnd;\r
                 offsetEnd = tmp;\r
             }\r
-            textNodeIdx = parent.contents().index($(selection.anchorNode));\r
+            textNodeIdx = parentContents.index($anchorNode);\r
         } else {\r
-            if(parent.contents().index($(selection.anchorNode)) > parent.contents().index($(selection.focusNode))) {\r
+            if(parentContents.index($anchorNode) > parentContents.index($focusNode)) {\r
                 offsetStart = selection.focusOffset;\r
                 offsetEnd = selection.anchorOffset;\r
             }\r
-            textNodeIdx = [parent.contents().index($(selection.anchorNode)), parent.contents().index($(selection.focusNode))];\r
+            textNodeIdx = [parentContents.index($anchorNode), parentContents.index($focusNode)];\r
         }\r
         \r
         var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass});\r