X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/9201c5810622e27a19de7e331046108d7110c46b..c0e11a8f6d0d9a4c030aa90fe91f0fc49cd05e0e:/modules/visualEditor.js
diff --git a/modules/visualEditor.js b/modules/visualEditor.js
index 70fc435..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 || ''}));
@@ -131,7 +146,7 @@ rng.modules.visualEditor = function(sandbox) {
view.node.on('change', '.rng-visualEditor-editPaneSelectionForm select', function(e) {
var target = $(e.target);
var selection = window.getSelection();
- if(selection.anchorNode.isSameNode(selection.focusNode) && selection.anchorNode.nodeType === Node.TEXT_NODE) {
+ if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
var startOffset = selection.anchorOffset;
var endOffset = selection.focusOffset;
if(startOffset > endOffset) {
@@ -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();
}
}