X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/542ac5b2a68670e2c48bb1695a7eb52333ce5d20..01143abd8ad6f5ad5f5a3485c0a8a20580ce2171:/modules/documentCanvas/documentCanvas.js diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js index 1b919ee..bb8ad4a 100644 --- a/modules/documentCanvas/documentCanvas.js +++ b/modules/documentCanvas/documentCanvas.js @@ -6,13 +6,15 @@ define([ './wlxmlNode', 'libs/text!./template.html'], function(_, transformations, wlxmlNode, template) { - +'use strict'; return function(sandbox) { var view = { node: $(_.template(template)()), currentNode: null, + shownAlready: false, + scrollbarPosition: 0, setup: function() { var view = this; @@ -50,23 +52,20 @@ return function(sandbox) { view.insertNewNode(null, null); } }); - - - var observer = new MutationObserver(function(mutations) { - mutations.forEach(function(mutation) { - _.each(mutation.addedNodes, function(node) { - node = $(node); - node.parent().find('[wlxml-tag]').each(function() { - tag = $(this); - if(!tag.attr('id')) - tag.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);})); - }); - }); - }); - }); - var config = { attributes: true, childList: true, characterData: true, subtree: true }; - observer.observe(this.node.find('#rng-module-documentCanvas-contentWrapper')[0], config); - + + 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) { @@ -76,6 +75,7 @@ return function(sandbox) { toret.attr('wlxml-tag', wlxmlTag); if(wlxmlClass) toret.attr('wlxml-class', wlxmlClass); + toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);})); return toret; }, insertNewNode: function(wlxmlTag, wlxmlClass) { @@ -100,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'); }, @@ -124,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'); } @@ -151,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'); } @@ -162,42 +162,38 @@ return function(sandbox) { getBody: function() { return this.node.find('#rng-module-documentCanvas-content').html(); }, - selectNode: function(node, options) { + selectNode: function(wlxmlNode, options) { options = options || {}; - this.dimNode(node); + var nodeElement = this.getNodeElement(wlxmlNode) - this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode'); - this.getNodeElement(node).addClass('rng-module-documentCanvas-currentNode'); - this.currentNode = node; + this.dimNode(wlxmlNode); - if(options.doFocus) { - var range = document.createRange(); - range.selectNodeContents(this.getNodeElement(node)[0]); - range.collapse(false); - - var selection = document.getSelection(); - selection.removeAllRanges() - selection.addRange(range); + this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode'); + nodeElement.addClass('rng-module-documentCanvas-currentNode'); + + if(options.movecaret) { + this.movecaretToNode(nodeElement); } - sandbox.publish('nodeSelected', node); + this.currentNode = wlxmlNode; + sandbox.publish('nodeSelected', wlxmlNode); }, - highlightNode: function(node) { - node = this.getNodeElement(node); + highlightNode: function(wlxmlNode) { + var nodeElement = this.getNodeElement(wlxmlNode); if(!this.gridToggled) { - node.addClass('rng-common-hoveredNode'); - var label = node.attr('wlxml-tag'); - if(node.attr('wlxml-class')) - label += ' / ' + node.attr('wlxml-class'); + nodeElement.addClass('rng-common-hoveredNode'); + var label = nodeElement.attr('wlxml-tag'); + if(nodeElement.attr('wlxml-class')) + label += ' / ' + nodeElement.attr('wlxml-class'); var tag = $('