X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/3a363b061bed9e1ca68989889bae9c701ddce772..9088c2ab96934ee0318e55dadf3573f3325201ff:/modules/visualEditor.js?ds=sidebyside diff --git a/modules/visualEditor.js b/modules/visualEditor.js index 4c8af20..65de452 100644 --- a/modules/visualEditor.js +++ b/modules/visualEditor.js @@ -1,5 +1,6 @@ -rng.modules.visualEditor = function(sandbox) { - var transformations = rng.modules.visualEditor.transformations; +define(['./visualEditor.transformations'], function(transformations) { + +return function(sandbox) { var view = { node: $(sandbox.getTemplate('main')()), @@ -15,8 +16,8 @@ rng.modules.visualEditor = function(sandbox) { isDirty = true; }); - this.node.on('mouseover', '[wlxml-tag]', function(e) { view.highlightNode($(e.target));}); - this.node.on('mouseout', '[wlxml-tag]', function(e) { view.dimNode($(e.target));}); + this.node.on('mouseover', '[wlxml-tag]', function(e) { mediator.nodeHovered($(e.target));}); + this.node.on('mouseout', '[wlxml-tag]', function(e) { mediator.nodeBlured($(e.target));}); this.node.on('click', '[wlxml-tag]', function(e) { console.log('clicked node type: '+e.target.nodeType); view._markSelected($(e.target)); @@ -34,13 +35,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 +72,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() { @@ -91,7 +83,91 @@ rng.modules.visualEditor = function(sandbox) { }); }); var config = { attributes: true, childList: true, characterData: true, subtree: true }; - observer.observe(this.node.find('#rng-visualEditor-contentWrapper')[0], config); + observer.observe(this.node.find('#rng-visualEditor-contentWrapper')[0], config); + + 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); + var anchorOffset = window.getSelection().anchorOffset; + if(anchor[0].nodeType === Node.TEXT_NODE) + anchor = anchor.parent(); + if(anchor.text() === '') { + var todel = anchor; + anchor = anchor.parent(); + todel.remove(); + } + if(anchorOffset > 0 && anchorOffset < anchor.text().length) { + if(wlxmlTag === null && wlxmlClass === null) { + return this.splitWithNewNode(anchor); + } + return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass); + } + var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class')); + if(anchorOffset === 0) + anchor.before(newNode) + else + 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 || 'test'); + $(node).replaceWith(newNode); + newNode.before(prefix); + newNode.after(suffix); + mediator.nodeCreated(newNode); + isDirty = true; + } + }, + splitWithNewNode: function(node) { + 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 anchor = selection.anchorNode; + var prefix = anchor.data.substr(0, startOffset); + var suffix = anchor.data.substr(endOffset); + var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class')); + var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class')); + var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class')); + prefixNode.text(prefix); + suffixNode.text(suffix); + node.replaceWith(newNode); + newNode.before(prefixNode); + newNode.after(suffixNode); + mediator.nodeCreated(newNode); + isDirty = true; + } }, getMetaData: function() { var toret = {}; @@ -119,8 +195,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); }, @@ -140,12 +220,20 @@ rng.modules.visualEditor = function(sandbox) { this.selectNode(node); }, highlightNode: function(node) { - node.addClass('rng-hover'); - mediator.nodeHighlighted(node); + 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) { - node.removeClass('rng-hover'); - mediator.nodeDimmed(node); + if(!this.gridToggled) { + node.removeClass('rng-hover'); + node.find('.rng-visualEditor-nodeHoverTag').remove(); + } }, highlightNodeById: function(id) { var node = this.node.find('#'+id); @@ -173,6 +261,13 @@ rng.modules.visualEditor = function(sandbox) { var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''})); newRow.appendTo(this.metaTable); return newRow; + }, + toggleGrid: function(toggle) { + this.node.find('[wlxml-tag]').toggleClass('rng-hover', toggle); + this.gridToggled = toggle; + }, + toggleTags: function(toggle) { + } }; @@ -183,6 +278,7 @@ rng.modules.visualEditor = function(sandbox) { var view = this; this.node.find('#rng-visualEditor-sidebarButtons a').click(function(e) { e.preventDefault(); + e.stopPropagation(); var target = $(e.currentTarget); if(!target.attr('data-content-id')) return; @@ -196,31 +292,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')); @@ -245,16 +317,18 @@ rng.modules.visualEditor = function(sandbox) { }, updateEditPane: function(node) { var pane = this.node.find('#rng-visualEditor-edit'); + var parentClass = node.parent().attr('wlxml-class'); pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')}))); - var parent = { - repr: node.parent().attr('wlxml-tag') + ' / ' + (node.parent().attr('wlxml-class') || '[[no class]]'), + var parent = node.parent('[wlxml-tag]').length ? { + repr: node.parent().attr('wlxml-tag') + (parentClass ? ' / ' + parentClass : ''), id: node.parent().attr('id') - } + } : undefined; 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')}); + var childClass = child.attr('wlxml-class'); + children.push({repr: child.attr('wlxml-tag') + (childClass ? ' / ' + childClass : ''), id: child.attr('id')}); }); var naviTemplate = sandbox.getTemplate('editPaneNavigation')({parent: parent, children: children}); pane.find('.rng-visualEditor-editPaneSurrouding > div').html($(naviTemplate)); @@ -269,8 +343,63 @@ rng.modules.visualEditor = function(sandbox) { } } + var toolbarView = { + node: view.node.find('#rng-visualEditor-toolbar'), + setup: function() { + var view = this; + + view.node.find('button').click(function(e) { + var btn = $(e.currentTarget); + if(btn.attr('data-btn-type') === 'toggle') { + 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'), btn.attr('data-meta')); + } + }); + }, + getOption: function(option) { + return this.node.find('.rng-visualEditor-toolbarOption[data-option=' + option +']').val(); + } + } + + var statusBarView = { + node: view.node.find('#rng-visualEditor-statusbar'), + setup: function() { + var view = this; + view.node.on('mouseenter', 'a', function(e) { + var target = $(e.target); + mediator.nodeHighlightedById(target.attr('data-id')); + }); + view.node.on('mouseleave', 'a', function(e) { + var target = $(e.target); + mediator.nodeDimmedById(target.attr('data-id')); + }); + view.node.on('click', 'a', function(e) { + e.preventDefault(); + mediator.nodeSelectedById($(e.target).attr('data-id')); + }); + }, + + showNode: function(node) { + this.node.empty(); + this.node.html(sandbox.getTemplate('statusBarNodeDisplay')({node: node, parents: node.parents('[wlxml-tag]')})); + //node.parents('[wlxml-tag]') + }, + + highlightNode: function(id) { + this.node.find('a[data-id="'+id+'"]').addClass('rng-hover'); + }, + dimNode: function(id) { + this.node.find('a[data-id="' +id+'"]').removeClass('rng-hover'); + } + } + view.setup(); sideBarView.setup(); + toolbarView.setup(); + statusBarView.setup(); var mediator = { getCurrentNode: function() { @@ -278,10 +407,10 @@ rng.modules.visualEditor = function(sandbox) { }, nodeCreated: function(node) { view.selectNode(node); - }, nodeSelected: function(node) { sideBarView.updateEditPane(node); + statusBarView.showNode(node); }, nodeSelectedById: function(id) { view.selectNodeById(id); @@ -292,12 +421,44 @@ rng.modules.visualEditor = function(sandbox) { nodeDimmedById: function(id) { view.dimNodeById(id); }, - nodeHighlighted: function(node) { + toolbarButtonToggled: function(btn, toggle) { + if(btn === 'grid') + view.toggleGrid(toggle); + if(btn === 'tags') + view.toggleTags(toggle); + }, + toolbarButtonCmd: function(btn, meta) { + if(btn === 'new-node') { + var wlxmlTag = toolbarView.getOption('newTag-tag'); + var wlxmlClass = toolbarView.getOption('newTag-class'); + if(meta) { + var split = meta.split('/'); + wlxmlTag = split[0]; + wlxmlClass = split[1]; + } + if(window.getSelection().isCollapsed) + view.insertNewNode(wlxmlTag, wlxmlClass); + else { + this.wrapWithNodeRequest(wlxmlTag, wlxmlClass); + } + + + } + }, + nodeHovered: function(node) { + view.highlightNode(node); sideBarView.highlightNode(node.attr('id')); + statusBarView.highlightNode(node.attr('id')); }, - nodeDimmed: function(node) { + nodeBlured: function(node) { + view.dimNode(node); sideBarView.dimNode(node.attr('id')); + statusBarView.dimNode(node.attr('id')); + }, + wrapWithNodeRequest: function(wlxmlTag, wlxmlClass) { + view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass); } + } var isDirty = false; @@ -334,4 +495,6 @@ rng.modules.visualEditor = function(sandbox) { } } -}; \ No newline at end of file +}; + +}); \ No newline at end of file