X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/542ac5b2a68670e2c48bb1695a7eb52333ce5d20..4c81ad4075b0e8e67f984cce3494f9853179a9d6:/modules/documentCanvas/documentCanvas.js?ds=sidebyside diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js index 1b919ee..1577be0 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; @@ -49,24 +51,30 @@ return function(sandbox) { e.preventDefault(); view.insertNewNode(null, null); } + if(e.which === 8) { + var anchor = window.getSelection().anchorNode; + var len = anchor.length; + console.log(len); + if(len === 1) { + e.preventDefault(); + $(anchor).parent().text(''); + } + } }); - - - 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,20 +84,24 @@ 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) { //TODO: Insert inline var anchor = $(window.getSelection().anchorNode); var anchorOffset = window.getSelection().anchorOffset; - if(anchor[0].nodeType === Node.TEXT_NODE) + var textLen; + if(anchor[0].nodeType === Node.TEXT_NODE) { + textLen = anchor.text().length; anchor = anchor.parent(); + } if(anchor.text() === '') { var todel = anchor; anchor = anchor.parent(); todel.remove(); } - if(anchorOffset > 0 && anchorOffset < anchor.text().length) { + if(anchorOffset > 0 && anchorOffset < textLen) { if(wlxmlTag === null && wlxmlClass === null) { return this.splitWithNewNode(anchor); } @@ -100,7 +112,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 +136,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 +163,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 +174,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 = $('
').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label); - node.append(tag); + nodeElement.append(tag); } }, - dimNode: function(node) { - node = this.getNodeElement(node); + dimNode: function(wlxmlNode) { + var nodeElement = this.getNodeElement(wlxmlNode); if(!this.gridToggled) { - node.removeClass('rng-common-hoveredNode'); - node.find('.rng-module-documentCanvas-hoveredNodeTag').remove(); + nodeElement.removeClass('rng-common-hoveredNode'); + nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove(); } }, selectFirstNode: function() { @@ -210,7 +218,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); @@ -218,6 +226,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); } }; @@ -226,16 +242,22 @@ 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(); - //isDirty = false; + sandbox.publish('documentSet'); + }, + getDocument: function() { + return transformations.toXML.getXML(view.getBody()); }, modifyCurrentNode: function(attr, value) { - if(view.currentNode) - view.currentNode.attr('wlxml-'+attr, value); + if(view.currentNode) { + view.getNodeElement(view.currentNode).attr('wlxml-'+attr, value); + sandbox.publish('contentChanged'); + } }, highlightNode: function(wlxmlNode) { view.highlightNode(wlxmlNode); @@ -244,7 +266,8 @@ return function(sandbox) { view.dimNode(wlxmlNode); }, selectNode: function(wlxmlNode) { - view.selectNode(wlxmlNode, {doFocus: true}); + if(!wlxmlNode.is(view.currentNode)) + view.selectNode(wlxmlNode, {movecaret: true}); }, toggleGrid: function(toggle) { view.toggleGrid(toggle);