X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/c9ec6d3adaf0d2119ed2ef7eecc2e5b525f2f86d..4c64934185c2bd340c01de48882943af66c563f0:/modules/visualEditor.js diff --git a/modules/visualEditor.js b/modules/visualEditor.js index 39859a8..dfa8335 100644 --- a/modules/visualEditor.js +++ b/modules/visualEditor.js @@ -34,13 +34,7 @@ rng.modules.visualEditor = function(sandbox) { this.node.on('keydown', '#rng-visualEditor-contentWrapper', function(e) { if(e.which === 13) { e.preventDefault(); - var anchor = $(window.getSelection().anchorNode); - if(anchor[0].nodeType === Node.TEXT_NODE) - anchor = anchor.parent(); - var newNode = anchor.clone().empty(); - newNode.attr('id', ''); - anchor.after(newNode); - view.selectNode(newNode); + view.insertNewNode(null, null); } }); @@ -77,9 +71,6 @@ rng.modules.visualEditor = function(sandbox) { var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { - if(mutation.addedNodes.length > 0) { - console.log(mutation.addedNodes); - } _.each(mutation.addedNodes, function(node) { node = $(node); node.parent().find('[wlxml-tag]').each(function() { @@ -95,6 +86,54 @@ rng.modules.visualEditor = function(sandbox) { this.gridToggled = false; }, + _createNode: function(wlxmlTag, wlxmlClass) { + var toBlock = ['div', 'document', 'section', 'header']; + var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span'; + var toret = $('<' + htmlTag + '>'); + toret.attr('wlxml-tag', wlxmlTag); + if(wlxmlClass) + toret.attr('wlxml-class', wlxmlClass); + return toret; + }, + insertNewNode: function(wlxmlTag, wlxmlClass) { + //TODO: Insert inline + var anchor = $(window.getSelection().anchorNode); + if(anchor[0].nodeType === Node.TEXT_NODE) + anchor = anchor.parent(); + if(anchor.text() === '') { + var todel = anchor; + anchor = anchor.parent(); + todel.remove(); + } + var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class')); + anchor.after(newNode); + mediator.nodeCreated(newNode); + isDirty = true; + }, + wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) { + + 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 = this._createNode(wlxmlTag, wlxmlClass); + newNode.text(core); + $(node).replaceWith(newNode); + newNode.before(prefix); + newNode.after(suffix); + mediator.nodeCreated(newNode); + isDirty = true; + } + }, getMetaData: function() { var toret = {}; this.metaTable.find('tr').each(function() { @@ -121,8 +160,12 @@ rng.modules.visualEditor = function(sandbox) { return this.node.find('#rng-visualEditor-content').html(); }, _markSelected: function(node) { + this.dimNode(node); + this.node.find('.rng-current').removeClass('rng-current'); + node.addClass('rng-current'); + this.currentNode = node; mediator.nodeSelected(node); }, @@ -142,12 +185,20 @@ rng.modules.visualEditor = function(sandbox) { this.selectNode(node); }, highlightNode: function(node) { - if(!this.gridToggled) + if(!this.gridToggled) { node.addClass('rng-hover'); + var label = node.attr('wlxml-tag'); + if(node.attr('wlxml-class')) + label += ' / ' + node.attr('wlxml-class'); + var tag = $('
').addClass('rng-visualEditor-nodeHoverTag').text(label); + node.append(tag); + } }, dimNode: function(node) { - if(!this.gridToggled) + if(!this.gridToggled) { node.removeClass('rng-hover'); + node.find('.rng-visualEditor-nodeHoverTag').remove(); + } }, highlightNodeById: function(id) { var node = this.node.find('#'+id); @@ -179,6 +230,9 @@ rng.modules.visualEditor = function(sandbox) { toggleGrid: function(toggle) { this.node.find('[wlxml-tag]').toggleClass('rng-hover', toggle); this.gridToggled = toggle; + }, + toggleTags: function(toggle) { + } }; @@ -202,31 +256,7 @@ rng.modules.visualEditor = function(sandbox) { 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; - } - }); - + view.node.on('click', '.rng-visualEditor-editPaneSurrouding a', function(e) { var target = $(e.target); mediator.nodeDimmedById(target.attr('data-id')); @@ -258,7 +288,7 @@ rng.modules.visualEditor = function(sandbox) { id: node.parent().attr('id') } var children = []; - node.children().each(function() { + node.children('[wlxml-tag]').each(function() { var child = $(this); children.push({repr: child.attr('wlxml-tag') + ' / ' + (child.attr('wlxml-class') || '[[no class]]'), id: child.attr('id')}); }); @@ -286,7 +316,13 @@ rng.modules.visualEditor = function(sandbox) { btn.toggleClass('active') mediator.toolbarButtonToggled(btn.attr('data-btn'), btn.hasClass('active')); } + if(btn.attr('data-btn-type') === 'cmd') { + mediator.toolbarButtonCmd(btn.attr('data-btn')); + } }); + }, + getOption: function(option) { + return this.node.find('.rng-visualEditor-toolbarOption[data-option=' + option +']').val(); } } @@ -300,7 +336,6 @@ rng.modules.visualEditor = function(sandbox) { }, nodeCreated: function(node) { view.selectNode(node); - }, nodeSelected: function(node) { sideBarView.updateEditPane(node); @@ -317,6 +352,19 @@ rng.modules.visualEditor = function(sandbox) { toolbarButtonToggled: function(btn, toggle) { if(btn === 'grid') view.toggleGrid(toggle); + if(btn === 'tags') + view.toggleTags(toggle); + }, + toolbarButtonCmd: function(btn) { + if(btn === 'new-node') { + if(window.getSelection().isCollapsed) + view.insertNewNode(toolbarView.getOption('newTag-tag'), toolbarView.getOption('newTag-class')); + else { + this.wrapWithNodeRequest(toolbarView.getOption('newTag-tag'), toolbarView.getOption('newTag-class')); + } + + + } }, nodeHovered: function(node) { view.highlightNode(node); @@ -325,6 +373,9 @@ rng.modules.visualEditor = function(sandbox) { nodeBlured: function(node) { view.dimNode(node); sideBarView.dimNode(node.attr('id')); + }, + wrapWithNodeRequest: function(wlxmlTag, wlxmlClass) { + view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass); } }