X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/ebaba7ac4948ae4280dea0a623045ffb6d5a722f..bb4f4e65b84c6df29088e2b3fdff09105e0154a9:/modules/visualEditor.js diff --git a/modules/visualEditor.js b/modules/visualEditor.js index 5833b71..65de452 100644 --- a/modules/visualEditor.js +++ b/modules/visualEditor.js @@ -1,8 +1,10 @@ -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')()), + currentNode: null, setup: function() { var view = this; @@ -14,14 +16,14 @@ rng.modules.visualEditor = function(sandbox) { isDirty = true; }); - 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('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)); }); - this.node.on('keyup', function(e) { + this.node.on('keyup', '#rng-visualEditor-contentWrapper', function(e) { var anchor = $(window.getSelection().anchorNode); if(anchor[0].nodeType === Node.TEXT_NODE) anchor = anchor.parent(); @@ -30,11 +32,19 @@ rng.modules.visualEditor = function(sandbox) { view._markSelected(anchor); }); + this.node.on('keydown', '#rng-visualEditor-contentWrapper', function(e) { + if(e.which === 13) { + e.preventDefault(); + view.insertNewNode(null, null); + } + }); + - this.metaTable = this.node.find('#rng-visualEditor-meta table'); + var metaTable = this.metaTable = this.node.find('#rng-visualEditor-meta table'); - this.metaTable.find('.rng-visualEditor-metaAddBtn').click(function() { - view._addMetaRow(); + this.node.find('.rng-visualEditor-metaAddBtn').click(function() { + var newRow = view._addMetaRow('', ''); + $(newRow.find('td div')[0]).focus(); isDirty = true; }); @@ -42,15 +52,130 @@ rng.modules.visualEditor = function(sandbox) { $(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(); + } + + }); + + + 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-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 = {}; - this.metaTable.find('tr').not('.rng-visualEditor-addMetaRow').each(function() { + this.metaTable.find('tr').each(function() { var tr = $(this); - var inputs = $(this).find('td input'); - var key = $(inputs[0]).val() - var value = $(inputs[1]).val() + var inputs = $(this).find('td [contenteditable]'); + var key = $(inputs[0]).text(); + var value = $(inputs[1]).text(); toret[key] = value; }); console.log(toret); @@ -58,7 +183,7 @@ rng.modules.visualEditor = function(sandbox) { }, setMetaData: function(metadata) { var view = this; - this.metaTable.find('tr').not('.rng-visualEditor-addMetaRow').remove(); + this.metaTable.find('tr').remove(); _.each(_.keys(metadata), function(key) { view._addMetaRow(key, metadata[key]); }); @@ -70,17 +195,274 @@ 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); + }, + selectNode: 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); + }, + selectNodeById: function(id) { + var node = this.node.find('#'+id); + if(node) + this.selectNode(node); + }, + highlightNode: function(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 = $('