X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/258a2d6877a76d51253f5f70d4a7df6bfe81b90f..4e0553edaf87974f2e9565fcb0514b928cb48d8e:/modules/visualEditor.js diff --git a/modules/visualEditor.js b/modules/visualEditor.js index 012f7a9..c30c282 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')()), @@ -106,6 +107,12 @@ rng.modules.visualEditor = function(sandbox) { 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) @@ -115,7 +122,6 @@ rng.modules.visualEditor = function(sandbox) { 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; @@ -130,7 +136,7 @@ rng.modules.visualEditor = function(sandbox) { var suffix = node.data.substr(endOffset); var core = node.data.substr(startOffset, endOffset - startOffset); var newNode = this._createNode(wlxmlTag, wlxmlClass); - newNode.text(core); + newNode.text(core || 'test'); $(node).replaceWith(newNode); newNode.before(prefix); newNode.after(suffix); @@ -138,6 +144,31 @@ rng.modules.visualEditor = function(sandbox) { 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').each(function() { @@ -285,16 +316,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 = node.parent('[wlxml-tag]').length ? { - repr: node.parent().attr('wlxml-tag') + ' / ' + (node.parent().attr('wlxml-class') || '[[no class]]'), + repr: node.parent().attr('wlxml-tag') + (parentClass ? ' / ' + parentClass : ''), id: node.parent().attr('id') } : undefined; var children = []; 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)); @@ -321,7 +354,7 @@ rng.modules.visualEditor = function(sandbox) { mediator.toolbarButtonToggled(btn.attr('data-btn'), btn.hasClass('active')); } if(btn.attr('data-btn-type') === 'cmd') { - mediator.toolbarButtonCmd(btn.attr('data-btn')); + mediator.toolbarButtonCmd(btn.attr('data-btn'), btn.attr('data-meta')); } }); }, @@ -393,12 +426,19 @@ rng.modules.visualEditor = function(sandbox) { if(btn === 'tags') view.toggleTags(toggle); }, - toolbarButtonCmd: function(btn) { + 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(toolbarView.getOption('newTag-tag'), toolbarView.getOption('newTag-class')); + view.insertNewNode(wlxmlTag, wlxmlClass); else { - this.wrapWithNodeRequest(toolbarView.getOption('newTag-tag'), toolbarView.getOption('newTag-class')); + this.wrapWithNodeRequest(wlxmlTag, wlxmlClass); } @@ -454,4 +494,6 @@ rng.modules.visualEditor = function(sandbox) { } } -}; \ No newline at end of file +}; + +}); \ No newline at end of file