Adding jshint grunt task
[fnpeditor.git] / modules / documentCanvas / canvasManager.js
index 7e34e29..201394e 100644 (file)
@@ -97,23 +97,39 @@ Manager.prototype.selectNode = function(cnode, options) {
 };\r
 \r
 Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) {\r
 };\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 offsetStart = selection.anchorOffset;\r
-        var offsetEnd = selection.focusOffset;\r
-        if(offsetStart > offsetEnd) {\r
-            var tmp = offsetStart;\r
-            offsetStart = offsetEnd;\r
-            offsetEnd = tmp;\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
+                var tmp = offsetStart;\r
+                offsetStart = offsetEnd;\r
+                offsetEnd = tmp;\r
+            }\r
+            textNodeIdx = parentContents.index($anchorNode);\r
+        } else {\r
+            if(parentContents.index($anchorNode) > parentContents.index($focusNode)) {\r
+                offsetStart = selection.focusOffset;\r
+                offsetEnd = selection.anchorOffset;\r
+            }\r
+            textNodeIdx = [parentContents.index($anchorNode), parentContents.index($focusNode)];\r
         }\r
         }\r
+        \r
         var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass});\r
         var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass});\r
-        this.canvas.nodeWrap({inside: canvasNode.create($(selection.anchorNode).parent()),\r
+        this.canvas.nodeWrap({inside: canvasNode.create(parent),\r
                               _with: wrapper,\r
                               offsetStart: offsetStart,\r
                               _with: wrapper,\r
                               offsetStart: offsetStart,\r
-                              offsetEnd: offsetEnd\r
+                              offsetEnd: offsetEnd,\r
+                              textNodeIdx: textNodeIdx\r
                             });\r
         this.selectNode(wrapper, {movecaret: 'end'});\r
     }\r
                             });\r
         this.selectNode(wrapper, {movecaret: 'end'});\r
     }\r
@@ -159,6 +175,8 @@ Manager.prototype.selectFirstNode = function() {
 };\r
 \r
 Manager.prototype.movecaretToNode = function(nodeElement, where) {\r
 };\r
 \r
 Manager.prototype.movecaretToNode = function(nodeElement, where) {\r
+    if(!nodeElement.length)\r
+        return;\r
     var range = document.createRange();\r
     range.selectNodeContents(nodeElement[0]);\r
     \r
     var range = document.createRange();\r
     range.selectNodeContents(nodeElement[0]);\r
     \r
@@ -184,7 +202,7 @@ Manager.prototype.onEnterKey = function(e) {
 \r
     if(pos.isAtEnd) {\r
         newNode = canvasNode.create({tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')});\r
 \r
     if(pos.isAtEnd) {\r
         newNode = canvasNode.create({tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')});\r
-        this.canvas.nodeInsertAfter({node: newNode, after: canvas.getNodeById(pos.parentNode.attr('id'))});\r
+        this.canvas.nodeInsertAfter({node: newNode, after: this.canvas.getNodeById(pos.parentNode.attr('id'))});\r
     } else {\r
         newNode = this.canvas.nodeSplit({node: contextNode, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset});\r
     }\r
     } else {\r
         newNode = this.canvas.nodeSplit({node: contextNode, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset});\r
     }\r
@@ -215,17 +233,17 @@ Manager.prototype.command = function(command, meta) {
     \r
     if(command === 'createList') {\r
         var node = canvasNode.create(pos.parentNode);\r
     \r
     if(command === 'createList') {\r
         var node = canvasNode.create(pos.parentNode);\r
-        if(window.getSelection().getRangeAt().collapsed && this.canvas.nodeInsideList({node: node})) {\r
+        if(window.getSelection().getRangeAt(0).collapsed && this.canvas.nodeInsideList({node: node})) {\r
             this.canvas.listRemove({pointer: node});\r
             this.selectNode(node, {movecaret: 'end'});\r
             this.sandbox.publish('contentChanged');\r
         }\r
         else {\r
             this.canvas.listRemove({pointer: node});\r
             this.selectNode(node, {movecaret: 'end'});\r
             this.sandbox.publish('contentChanged');\r
         }\r
         else {\r
-            if(!this.canvas.nodeInsideList({node: node})) {\r
+            //if(!this.canvas.nodeInsideList({node: node})) {\r
                 this.canvas.listCreate({start: node, end: canvasNode.create(pos.focusNode), type: meta});\r
                 this.selectNode(node, {movecaret: 'end'});\r
                 this.sandbox.publish('contentChanged');\r
                 this.canvas.listCreate({start: node, end: canvasNode.create(pos.focusNode), type: meta});\r
                 this.selectNode(node, {movecaret: 'end'});\r
                 this.sandbox.publish('contentChanged');\r
-            }\r
+            //}\r
         }\r
     }\r
 \r
         }\r
     }\r
 \r