From: Aleksander Ɓukasz Date: Sun, 5 May 2013 20:25:12 +0000 (+0200) Subject: Visual editor - handling enter/+ when in the middle of node, X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/66aae866400efc99684f51486f8ce1ebb7f59404?ds=inline Visual editor - handling enter/+ when in the middle of node, --- diff --git a/modules/visualEditor.js b/modules/visualEditor.js index 012f7a9..1c61ef0 100644 --- a/modules/visualEditor.js +++ b/modules/visualEditor.js @@ -106,6 +106,12 @@ rng.modules.visualEditor = function(sandbox) { anchor = anchor.parent(); todel.remove(); } + if(anchorOffset > 0 && anchorOffset < anchor.text().length) { + if(wlxmlTag === null && wlxmlClass === null) { + return this.splitWithNewNode(anchor); + } + return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass); + } var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class')); if(anchorOffset === 0) anchor.before(newNode) @@ -115,7 +121,6 @@ rng.modules.visualEditor = function(sandbox) { isDirty = true; }, wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) { - var selection = window.getSelection(); if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) { var startOffset = selection.anchorOffset; @@ -130,7 +135,7 @@ rng.modules.visualEditor = function(sandbox) { var suffix = node.data.substr(endOffset); var core = node.data.substr(startOffset, endOffset - startOffset); var newNode = this._createNode(wlxmlTag, wlxmlClass); - newNode.text(core); + newNode.text(core || 'test'); $(node).replaceWith(newNode); newNode.before(prefix); newNode.after(suffix); @@ -138,6 +143,31 @@ rng.modules.visualEditor = function(sandbox) { isDirty = true; } }, + splitWithNewNode: function(node) { + var selection = window.getSelection(); + if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) { + var startOffset = selection.anchorOffset; + var endOffset = selection.focusOffset; + if(startOffset > endOffset) { + var tmp = startOffset; + startOffset = endOffset; + endOffset = tmp; + } + var anchor = selection.anchorNode; + var prefix = anchor.data.substr(0, startOffset); + var suffix = anchor.data.substr(endOffset); + var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class')); + var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class')); + var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class')); + prefixNode.text(prefix); + suffixNode.text(suffix); + node.replaceWith(newNode); + newNode.before(prefixNode); + newNode.after(suffixNode); + mediator.nodeCreated(newNode); + isDirty = true; + } + }, getMetaData: function() { var toret = {}; this.metaTable.find('tr').each(function() {