Visual editor - handling enter/+ when in the middle of node,
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 5 May 2013 20:25:12 +0000 (22:25 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 5 May 2013 20:25:12 +0000 (22:25 +0200)
modules/visualEditor.js

index 012f7a9..1c61ef0 100644 (file)
@@ -106,6 +106,12 @@ rng.modules.visualEditor = function(sandbox) {
                 anchor = anchor.parent();\r
                 todel.remove();\r
             }\r
                 anchor = anchor.parent();\r
                 todel.remove();\r
             }\r
+            if(anchorOffset > 0 && anchorOffset < anchor.text().length) {\r
+                if(wlxmlTag === null && wlxmlClass === null) {\r
+                    return this.splitWithNewNode(anchor);\r
+                }\r
+                return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
+            }\r
             var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));\r
             if(anchorOffset === 0)\r
                 anchor.before(newNode)\r
             var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));\r
             if(anchorOffset === 0)\r
                 anchor.before(newNode)\r
@@ -115,7 +121,6 @@ rng.modules.visualEditor = function(sandbox) {
             isDirty = true;\r
         },\r
         wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
             isDirty = true;\r
         },\r
         wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
-            \r
             var selection = window.getSelection();\r
             if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {\r
                 var startOffset = selection.anchorOffset;\r
             var selection = window.getSelection();\r
             if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {\r
                 var startOffset = selection.anchorOffset;\r
@@ -130,7 +135,7 @@ rng.modules.visualEditor = function(sandbox) {
                 var suffix = node.data.substr(endOffset);\r
                 var core = node.data.substr(startOffset, endOffset - startOffset);\r
                 var newNode = this._createNode(wlxmlTag, wlxmlClass);\r
                 var suffix = node.data.substr(endOffset);\r
                 var core = node.data.substr(startOffset, endOffset - startOffset);\r
                 var newNode = this._createNode(wlxmlTag, wlxmlClass);\r
-                newNode.text(core);\r
+                newNode.text(core || 'test');\r
                 $(node).replaceWith(newNode);\r
                 newNode.before(prefix);\r
                 newNode.after(suffix);\r
                 $(node).replaceWith(newNode);\r
                 newNode.before(prefix);\r
                 newNode.after(suffix);\r
@@ -138,6 +143,31 @@ rng.modules.visualEditor = function(sandbox) {
                 isDirty = true;\r
             }\r
         },\r
                 isDirty = true;\r
             }\r
         },\r
+        splitWithNewNode: function(node) {\r
+            var selection = window.getSelection();\r
+            if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {\r
+                var startOffset = selection.anchorOffset;\r
+                var endOffset = selection.focusOffset;\r
+                if(startOffset > endOffset) {\r
+                    var tmp = startOffset;\r
+                    startOffset = endOffset;\r
+                    endOffset = tmp;\r
+                }\r
+                var anchor = selection.anchorNode;\r
+                var prefix = anchor.data.substr(0, startOffset);\r
+                var suffix = anchor.data.substr(endOffset);\r
+                var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
+                var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
+                var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
+                prefixNode.text(prefix);\r
+                suffixNode.text(suffix);\r
+                node.replaceWith(newNode);\r
+                newNode.before(prefixNode);\r
+                newNode.after(suffixNode);\r
+                mediator.nodeCreated(newNode);\r
+                isDirty = true;\r
+            }\r
+        },\r
         getMetaData: function() {\r
             var toret = {};\r
             this.metaTable.find('tr').each(function() {\r
         getMetaData: function() {\r
             var toret = {};\r
             this.metaTable.find('tr').each(function() {\r