X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/0c57fd826a58a217f499b5084c837fb8ef3f6d4f..ec10eee3168f37beedfb5dea757a4bdec2f2f36e:/src/editor/plugins/core/core.js diff --git a/src/editor/plugins/core/core.js b/src/editor/plugins/core/core.js index c12405b..425b161 100644 --- a/src/editor/plugins/core/core.js +++ b/src/editor/plugins/core/core.js @@ -8,9 +8,10 @@ var _ = require('libs/underscore'), footnote = require('plugins/core/footnote'), switchTo = require('plugins/core/switch'), lists = require('plugins/core/lists'), - plugin = {name: 'core', actions: [], canvas: {}, documentExtension: {textNode: {}}}, + plugin = {name: 'core', actions: [], canvas: {}, documentExtension: {textNode: {}, elementNode: {}}}, Dialog = require('views/dialog/dialog'), - canvasElements = require('plugins/core/canvasElements'); + canvasElements = require('plugins/core/canvasElements'), + metadataEditor = require('plugins/core/metadataEditor/metadataEditor'); @@ -18,26 +19,73 @@ plugin.documentExtension.textNode.transformations = { breakContent: { impl: function(args) { var node = this, + isSpan = node.parent().getTagName() === 'span', + parentDescribingNodes = [], newNodes, emptyText; newNodes = node.split({offset: args.offset}); - [newNodes.first, newNodes.second].some(function(newNode) { - if(!(newNode.contents().length)) { - emptyText = newNode.append({text: ''}); - return true; // break - } - }); newNodes.second.contents() .filter(function(child) { return child.object.describesParent; }) .forEach(function(child) { //child.detach(); - newNodes.first.append(child); + parentDescribingNodes.push(child); + child.detach(); + }); + [newNodes.first, newNodes.second].some(function(newNode) { + if(!(newNode.contents().length)) { + emptyText = newNode.append({text: ''}); + return true; // break + } + }); + parentDescribingNodes.forEach(function(node) { + newNodes.first.append(node); + }); + + var parent, newNode; + + var copyNode = function(n) { + var attrs = {}; + n.getAttrs().forEach(function(attr) { + attrs[attr.name] = attr.value; }); + + return node.document.createDocumentNode({ + tagName: n.getTagName(), + attrs: attrs + }); + }; + + var move = function(node, to) { + var copy; + if(!node.containsNode(newNodes.second)) { + to.append(node); + return false; + } else { + if(!node.sameNode(newNodes.second)) { + copy = to.append(copyNode(node)); + node.contents().some(function(n) { + return move(n, copy); + }); + } + return true; + } + }; + + if(isSpan) { + newNodes.first.parents().some(function(p) { + if(p.getTagName() !== 'span') { + parent = p; + return true; + } + }); + newNode = parent.before({tagName: parent.getTagName(), attrs: {'class': parent.getClass()}}); + parent.contents().some(function(n) { + return move(n, newNode); + }); + } + return _.extend(newNodes, {emptyText: emptyText}); - }, - getChangeRoot: function() { - return this.context.parent().parent(); } }, mergeContentUp: function() { @@ -76,6 +124,87 @@ plugin.documentExtension.textNode.transformations = { } }; +plugin.documentExtension.elementNode.transformations = { + moveUp: function() { + var toMerge = this, + prev = toMerge.prev(); + + var merge = function(from, to) { + var toret; + from.contents().forEach(function(node, idx) { + var len, ret; + if(idx === 0 && node.nodeType === Node.TEXT_NODE) { + len = node.getText().length; + } + ret = to.append(node); + + if(idx === 0) { + toret = { + node: ret, + offset: ret.getText().length - len + }; + } + }); + from.detach(); + return toret; + }; + + var strategies = [ + { + applies: function() { + return toMerge.is('p'); + }, + run: function() { + if(prev && prev.is('p') || prev.is({tagName: 'header'})) { + return merge(toMerge, prev); + } + if(prev && prev.is('list')) { + var items = prev.contents().filter(function(n) { return n.is('item');}); + return merge(toMerge, items[items.length-1]); + } + } + }, + { + applies: function() { + return toMerge.is({tagName: 'header'}); + }, + run: function() { + if(prev && prev.is('p') || prev.is({tagName: 'header'})) { + return merge(toMerge, prev); + } + } + }, + { + applies: function() { + return toMerge.is('item'); + }, + run: function() { + var list; + if(prev && prev.is('item')) { + return merge(toMerge, prev); + } else if(!prev && (list = toMerge.parent()) && list.is('list')) { + list.before(toMerge); + toMerge.setClass('p'); + if(!list.contents().length) { + list.detach(); + } + return {node: toMerge.contents()[0], offset:0}; + } + } + } + ]; + + var toret; + strategies.some(function(strategy) { + if(strategy.applies()) { + toret = strategy.run(); + return true; + } + }); + return toret; + } +}; + var undoRedoAction = function(dir) { return { name: dir, @@ -384,6 +513,7 @@ var linkAction = { } }; +var metadataParams = {}; plugin.actions = [ undoRedoAction('undo'), @@ -391,7 +521,8 @@ plugin.actions = [ commentAction, createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}), createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}), - linkAction + linkAction, + metadataEditor.action(metadataParams) ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions); @@ -399,6 +530,15 @@ plugin.actions = [ plugin.config = function(config) { // templates.actions[0].config(config.templates); templates.actions[0].params.template.options = config.templates; + metadataParams.config = (config.metadata || []).sort(function(configRow1, configRow2) { + if(configRow1.key < configRow2.key) { + return -1; + } + if(configRow1.key > configRow2.key) { + return 1; + } + return 0; + }); }; plugin.canvasElements = canvasElements;