X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/c80dddd346b2eec6135657ae4e047cff042fbfe5..dcbcc948a5d3044fee2839dbe508ab1eb4ba54dc:/modules/documentCanvas/documentCanvas.js?ds=sidebyside diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js index bd959fe..bb8ad4a 100644 --- a/modules/documentCanvas/documentCanvas.js +++ b/modules/documentCanvas/documentCanvas.js @@ -13,6 +13,8 @@ return function(sandbox) { var view = { node: $(_.template(template)()), currentNode: null, + shownAlready: false, + scrollbarPosition: 0, setup: function() { var view = this; @@ -50,6 +52,20 @@ return function(sandbox) { view.insertNewNode(null, null); } }); + + this.node.onShow = function() { + if(!view.shownAlready) { + view.shownAlready = true; + view.selectFirstNode(); + } else if(view.currentNode) { + view.movecaretToNode(view.getNodeElement(view.currentNode)); + view.node.find('#rng-module-documentCanvas-contentWrapper').scrollTop(view.scrollbarPosition); + } + }; + this.node.onHide = function() { + view.scrollbarPosition = view.node.find('#rng-module-documentCanvas-contentWrapper').scrollTop(); + } + this.gridToggled = false; }, _createNode: function(wlxmlTag, wlxmlClass) { @@ -84,7 +100,7 @@ return function(sandbox) { anchor.before(newNode) else anchor.after(newNode); - this.selectNode(newNode); + this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true}); //isDirty = true; sandbox.publish('contentChanged'); }, @@ -108,7 +124,7 @@ return function(sandbox) { newNode.before(prefix); newNode.after(suffix); - this.selectNode(newNode); + this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true}); //isDirty = true; sandbox.publish('contentChanged'); } @@ -135,7 +151,7 @@ return function(sandbox) { newNode.before(prefixNode); newNode.after(suffixNode); - this.selectNode(newNode); + this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true}); //isDirty = true; sandbox.publish('contentChanged'); } @@ -155,13 +171,8 @@ return function(sandbox) { this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode'); 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); + if(options.movecaret) { + this.movecaretToNode(nodeElement); } this.currentNode = wlxmlNode; @@ -195,7 +206,7 @@ return function(sandbox) { else { node = this.node.find('[wlxml-class|="p"]') } - this.selectNode(new wlxmlNode.Node(node)); + this.selectNode(new wlxmlNode.Node(node), {movecaret: true}); }, toggleGrid: function(toggle) { this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle); @@ -203,6 +214,14 @@ return function(sandbox) { }, getNodeElement: function(wlxmlNode) { return this.node.find('#'+wlxmlNode.id); + }, + movecaretToNode: function(nodeElement) { + var range = document.createRange(); + range.selectNodeContents(nodeElement[0]); + range.collapse(false); + var selection = document.getSelection(); + selection.removeAllRanges() + selection.addRange(range); } }; @@ -211,11 +230,12 @@ return function(sandbox) { /* public api */ return { start: function() { sandbox.publish('ready'); }, - getView: function() { return view.node; }, + getView: function() { + return view.node; + }, setDocument: function(xml) { var transformed = transformations.fromXML.getDocumentDescription(xml); view.setBody(transformed.HTMLTree); - view.selectFirstNode(); sandbox.publish('documentSet'); }, getDocument: function() { @@ -235,7 +255,7 @@ return function(sandbox) { }, selectNode: function(wlxmlNode) { if(!wlxmlNode.is(view.currentNode)) - view.selectNode(wlxmlNode, {moveCarret: true}); + view.selectNode(wlxmlNode, {movecaret: true}); }, toggleGrid: function(toggle) { view.toggleGrid(toggle);