X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/3185249361cacf28b88b00093f8b262795c8fa3c..7e96084b977d9ecea7b0569fad7357035c017325:/modules/visualEditor.js diff --git a/modules/visualEditor.js b/modules/visualEditor.js index ce62c6a..9721d29 100644 --- a/modules/visualEditor.js +++ b/modules/visualEditor.js @@ -4,38 +4,80 @@ rng.modules.visualEditor = function(sandbox) { var view = { node: $(sandbox.getTemplate('main')()), setup: function() { - var node = this.node; - node.find('#rng-visualEditor-content').on('keyup', function() { + var view = this; + + this.node.find('#rng-visualEditor-content').on('keyup', function() { isDirty = true; }); - node.find('#rng-visualEditor-meta').on('keyup', function() { + this.node.find('#rng-visualEditor-meta').on('keyup', function() { isDirty = true; }); - this.node.on('mouseover', '.rng', function(e) { $(e.target).addClass('rng-hover')}); - this.node.on('mouseout', '.rng', function(e) { $(e.target).removeClass('rng-hover')}); - this.node.on('click', '.rng', function(e) { - node.find('.rng').removeClass('rng-current'); - $(e.target).addClass('rng-current'); - }); + this.node.on('mouseover', '[wlxml-tag]', function(e) { $(e.target).addClass('rng-hover')}); + this.node.on('mouseout', '[wlxml-tag]', function(e) { $(e.target).removeClass('rng-hover')}); + this.node.on('click', '[wlxml-tag]', function(e) { + console.log('clicked node type: '+e.target.nodeType); + view._markSelected($(e.target)); + }); + + this.node.on('keyup', '#rng-visualEditor-contentWrapper', function(e) { + var anchor = $(window.getSelection().anchorNode); + if(anchor[0].nodeType === Node.TEXT_NODE) + anchor = anchor.parent(); + if(!anchor.is('[wlxml-tag]')) + return; + view._markSelected(anchor); + }); + + + var metaTable = this.metaTable = this.node.find('#rng-visualEditor-meta table'); + + this.node.find('.rng-visualEditor-metaAddBtn').click(function() { + var newRow = view._addMetaRow('', ''); + $(newRow.find('td div')[0]).focus(); + isDirty = true; + }); + + this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) { + $(e.target).closest('tr').remove(); + isDirty = true; + }); + + this.metaTable.on('keydown', '[contenteditable]', function(e) { + console.log(e.which); + if(e.which === 13) { + if($(document.activeElement).hasClass('rng-visualEditor-metaItemKey')) { + metaTable.find('.rng-visualEditor-metaItemValue').focus(); + } else { + var input = $(''); + input.appendTo('body').focus() + view.node.find('.rng-visualEditor-metaAddBtn').focus(); + input.remove(); + } + e.preventDefault(); + } + + }); + }, getMetaData: function() { var toret = {}; - this.node.find('#rng-visualEditor-meta table tr').each(function() { + this.metaTable.find('tr').each(function() { var tr = $(this); - var key = $(tr.find('td')[0]).html(); - var value = $(tr.find('td input')[0]).val(); + var inputs = $(this).find('td [contenteditable]'); + var key = $(inputs[0]).text(); + var value = $(inputs[1]).text(); toret[key] = value; }); console.log(toret); return toret; }, setMetaData: function(metadata) { - var table = this.node.find('#rng-visualEditor-meta table'); - table.empty(); + var view = this; + this.metaTable.find('tr').remove(); _.each(_.keys(metadata), function(key) { - $(sandbox.getTemplate('metaItem')({key: key, value: metadata[key]})).appendTo(table); + view._addMetaRow(key, metadata[key]); }); }, setBody: function(HTMLTree) { @@ -43,7 +85,16 @@ rng.modules.visualEditor = function(sandbox) { }, getBody: function() { return this.node.find('#rng-visualEditor-content').html(); - } + }, + _markSelected: function(node) { + this.node.find('.rng-current').removeClass('rng-current'); + node.addClass('rng-current'); + }, + _addMetaRow: function(key, value) { + var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''})); + newRow.appendTo(this.metaTable); + return newRow; + } }; view.setup();