X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/b2cdac312af506bf41ad920ab6071119ab08b9d3..3c4f1543aaf955e96a526b4fe13688645e14e720:/modules/documentCanvas/documentCanvas.js diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js index 51acbf3..d8c199b 100644 --- a/modules/documentCanvas/documentCanvas.js +++ b/modules/documentCanvas/documentCanvas.js @@ -13,6 +13,7 @@ return function(sandbox) { var view = { node: $(_.template(template)()), currentNode: null, + shownAlready: false, setup: function() { var view = this; @@ -50,6 +51,16 @@ return function(sandbox) { view.insertNewNode(null, null); } }); + + this.node.onShow = function() { + if(!view.shownAlready) { + view.shownAlready = true; + view.selectFirstNode(); + } else if(view.currentNode) { + view.moveCarretToNode(view.getNodeElement(view.currentNode)); + } + }; + this.gridToggled = false; }, _createNode: function(wlxmlTag, wlxmlClass) { @@ -156,12 +167,7 @@ return function(sandbox) { nodeElement.addClass('rng-module-documentCanvas-currentNode'); if(options.moveCarret) { - var range = document.createRange(); - range.selectNodeContents(nodeElement[0]); - range.collapse(false); - var selection = document.getSelection(); - selection.removeAllRanges() - selection.addRange(range); + this.moveCarretToNode(nodeElement); } this.currentNode = wlxmlNode; @@ -195,7 +201,7 @@ return function(sandbox) { else { node = this.node.find('[wlxml-class|="p"]') } - this.selectNode(new wlxmlNode.Node(node)); + this.selectNode(new wlxmlNode.Node(node), {moveCarret: true}); }, toggleGrid: function(toggle) { this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle); @@ -203,6 +209,14 @@ return function(sandbox) { }, getNodeElement: function(wlxmlNode) { return this.node.find('#'+wlxmlNode.id); + }, + moveCarretToNode: function(nodeElement) { + var range = document.createRange(); + range.selectNodeContents(nodeElement[0]); + range.collapse(false); + var selection = document.getSelection(); + selection.removeAllRanges() + selection.addRange(range); } }; @@ -215,7 +229,6 @@ return function(sandbox) { setDocument: function(xml) { var transformed = transformations.fromXML.getDocumentDescription(xml); view.setBody(transformed.HTMLTree); - view.selectFirstNode(); sandbox.publish('documentSet'); }, getDocument: function() {