X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/153ea475b5d188797738c961411ca8d019101b9a..3733d5e0527a89111f9e33bb8380fed20ea768fc:/modules/visualEditor.js diff --git a/modules/visualEditor.js b/modules/visualEditor.js index d152acd..bb4ad31 100644 --- a/modules/visualEditor.js +++ b/modules/visualEditor.js @@ -3,6 +3,7 @@ rng.modules.visualEditor = function(sandbox) { var view = { node: $(sandbox.getTemplate('main')()), + currentNode: null, setup: function() { var view = this; @@ -89,6 +90,15 @@ rng.modules.visualEditor = function(sandbox) { _markSelected: function(node) { this.node.find('.rng-current').removeClass('rng-current'); node.addClass('rng-current'); + this.currentNode = node; + mediator.nodeSelected(node); + }, + markFirstSelected: function() { + var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() { + return $(this).clone().children().remove().end().text().trim() !== ''; + }).first(); + if(firstNodeWithText.length) + $(firstNodeWithText[0]).click().focus(); }, _addMetaRow: function(key, value) { var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''})); @@ -110,6 +120,37 @@ rng.modules.visualEditor = function(sandbox) { view.selectTab(target.attr('data-content-id')); }); view.selectTab('rng-visualEditor-edit'); + + view.node.on('change', '.rng-visualEditor-editPaneNodeForm select', function(e) { + var target = $(e.target); + var attr = target.attr('id').split('-')[2].split('editPane')[1].substr(0,3) === 'Tag' ? 'tag' : 'class'; + mediator.getCurrentNode().attr('wlxml-'+attr, target.val()); + isDirty = true; + }); + + view.node.on('change', '.rng-visualEditor-editPaneSelectionForm select', function(e) { + var target = $(e.target); + var selection = window.getSelection(); + if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) { + var startOffset = selection.anchorOffset; + var endOffset = selection.focusOffset; + if(startOffset > endOffset) { + var tmp = startOffset; + startOffset = endOffset; + endOffset = tmp; + } + var node = selection.anchorNode; + var prefix = node.data.substr(0, startOffset); + var suffix = node.data.substr(endOffset); + var core = node.data.substr(startOffset, endOffset - startOffset); + var newNode = $('' + core + ''); + $(node).replaceWith(newNode); + newNode.before(prefix); + newNode.after(suffix); + mediator.nodeCreated(newNode); + isDirty = true; + } + }); }, selectTab: function(id) { this.node.find('.rng-visualEditor-sidebarContentItem').hide(); @@ -117,14 +158,39 @@ rng.modules.visualEditor = function(sandbox) { this.node.find('#rng-visualEditor-sidebarButtons li').removeClass('active'); this.node.find('#rng-visualEditor-sidebarButtons li a[data-content-id=' + id + ']').parent().addClass('active'); + }, + updateEditPane: function(node) { + var pane = this.node.find('#rng-visualEditor-edit'); + pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')}))); } - } view.setup(); sideBarView.setup(); + var mediator = { + getCurrentNode: function() { + return view.currentNode; + }, + nodeCreated: 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); + + }, + nodeSelected: function(node) { + sideBarView.updateEditPane(node); + } + } + var isDirty = false; + var wasShownAlready = false; return { @@ -148,6 +214,12 @@ rng.modules.visualEditor = function(sandbox) { }, setDirty: function(dirty) { isDirty = dirty; + }, + onShowed: function() { + if(!wasShownAlready) { + wasShownAlready = true; + view.markFirstSelected(); + } } }