Wrapping in node text with subnodes
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 28 Jun 2013 11:30:27 +0000 (13:30 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 28 Jun 2013 11:30:27 +0000 (13:30 +0200)
modules/documentCanvas/canvas.js
modules/documentCanvas/canvasManager.js
modules/documentCanvas/tests/canvas.test.js

index cafed1a..c71950d 100644 (file)
@@ -61,11 +61,37 @@ Canvas.prototype.nodeInsertAfter = function(options) {
     element.after(options.node.dom);\r
 };\r
 \r
+Canvas.prototype.nodeWrap2 = function(options) {\r
+    var container = $(this.content.find('#' + options.inside.getId()).get(0));\r
+    \r
+    var containerContent = container.contents();\r
+    var idx1 = Math.min.apply(Math, options.textNodeIdx);\r
+    var idx2 = Math.max.apply(Math, options.textNodeIdx);\r
+    var textNode1 = $(containerContent.get(idx1));\r
+    var textNode2 = $(containerContent.get(idx2));\r
+    \r
+    textNode1.after(options._with.dom);\r
+    textNode1.detach();\r
+    textNode2.detach();\r
+    \r
+    \r
+    options._with.dom.before(textNode1.text().substr(0, options.offsetStart));\r
+    options._with.dom.append(textNode1.text().substr(options.offsetStart));\r
+    for(var i = idx1 + 1; i < idx2; i++) {\r
+        options._with.dom.append(containerContent[i]);\r
+    }\r
+    options._with.dom.append(textNode2.text().substr(0, options.offsetEnd));\r
+    options._with.dom.after(textNode2.text().substr(options.offsetEnd));\r
+};\r
+\r
 Canvas.prototype.nodeWrap = function(options) {\r
     options = _.extend({textNodeIdx: 0}, options);\r
+    \r
+    if(options.textNodeIdx instanceof Array)\r
+        return this.nodeWrap2(options);\r
 \r
     var element = $(this.content.find('#' + options.inside.getId()).get(0));\r
-\r
+    \r
     var elementContents = element.contents();\r
     if(elementContents.length === 0 || \r
        elementContents.length - 1 < options.textNodeIdx || \r
index aa37ff2..b7e7212 100644 (file)
@@ -109,13 +109,20 @@ Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) {
             offsetStart = offsetEnd;\r
             offsetEnd = tmp;\r
         }\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
                               offsetEnd: offsetEnd,\r
-                              textNodeIdx: parent.contents().index($(selection.anchorNode))\r
+                              textNodeIdx: textNodeIdx\r
                             });\r
         this.selectNode(wrapper, {movecaret: 'end'});\r
     }\r
index 5e44dd9..12fb8a5 100644 (file)
@@ -69,6 +69,14 @@ define([
             assertDomEqual(c.getContent(), '<div wlxml-tag="header">Alice <span wlxml-tag="span">has a</span> <span wlxml-tag="aside">cat</span></div>');\r
         });\r
         \r
+        test('wrap text with nodes inside', function() {\r
+            var c = canvas.create('<div wlxml-tag="header">Alice has a <span wlxml-tag="span">small</span> cat</div>');\r
+            var header = c.findNodes({tag: 'header'})[0];\r
+            var wrapper = canvasNode.create({tag: 'aside'});\r
+            c.nodeWrap({inside: header, _with: wrapper, offsetStart: 6, offsetEnd: 4, textNodeIdx: [0,2]})\r
+            assertDomEqual(c.getContent(), '<div wlxml-tag="header">Alice <span wlxml-tag="aside">has a <span wlxml-tag="span">small</span> cat</span></div>');            \r
+        });\r
+        \r
         test('split node', function() {\r
             var c = canvas.create('<div wlxml-tag="section"><div wlxml-tag="header">Header 1</div></div>');\r
             var header = c.findNodes({tag: 'header'})[0];\r