X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/6240d0b0afba6964a9ec696a531372822193b767..c0e11a8f6d0d9a4c030aa90fe91f0fc49cd05e0e:/modules/visualEditor.js?ds=sidebyside diff --git a/modules/visualEditor.js b/modules/visualEditor.js index 9cec971..f3c0a5c 100644 --- a/modules/visualEditor.js +++ b/modules/visualEditor.js @@ -93,12 +93,27 @@ rng.modules.visualEditor = function(sandbox) { this.currentNode = node; mediator.nodeSelected(node); }, - markFirstSelected: function() { + selectNode: function(node) { + view._markSelected(node); + var range = document.createRange(); + range.selectNodeContents(node[0]); + range.collapse(false); + + var selection = document.getSelection(); + selection.removeAllRanges() + selection.addRange(range); + }, + selectFirstNode: function() { var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() { return $(this).clone().children().remove().end().text().trim() !== ''; }).first(); + var node; if(firstNodeWithText.length) - $(firstNodeWithText[0]).click().focus(); + node = $(firstNodeWithText[0]) + else { + node = this.node.find('[wlxml-class|="p"]') + } + this.selectNode(node); }, _addMetaRow: function(key, value) { var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''})); @@ -143,7 +158,11 @@ rng.modules.visualEditor = function(sandbox) { var prefix = node.data.substr(0, startOffset); var suffix = node.data.substr(endOffset); var core = node.data.substr(startOffset, endOffset - startOffset); - $(node).replaceWith(prefix + '' + core + '' + suffix); + var newNode = $('' + core + ''); + $(node).replaceWith(newNode); + newNode.before(prefix); + newNode.after(suffix); + mediator.nodeCreated(newNode); isDirty = true; } }); @@ -168,6 +187,10 @@ rng.modules.visualEditor = function(sandbox) { getCurrentNode: function() { return view.currentNode; }, + nodeCreated: function(node) { + view.selectNode(node); + + }, nodeSelected: function(node) { sideBarView.updateEditPane(node); } @@ -202,7 +225,7 @@ rng.modules.visualEditor = function(sandbox) { onShowed: function() { if(!wasShownAlready) { wasShownAlready = true; - view.markFirstSelected(); + view.selectFirstNode(); } }