X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/ec10eee3168f37beedfb5dea757a4bdec2f2f36e..04b969b68c922970339a783e865bb1f9c593946c:/src/editor/plugins/core/core.js diff --git a/src/editor/plugins/core/core.js b/src/editor/plugins/core/core.js index 425b161..c2c5ffa 100644 --- a/src/editor/plugins/core/core.js +++ b/src/editor/plugins/core/core.js @@ -8,12 +8,34 @@ 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: {}, elementNode: {}}}, + plugin = {name: 'core', actions: [], canvas: {}, documentExtension: {textNode: {}, documentNode: {}}}, Dialog = require('views/dialog/dialog'), canvasElements = require('plugins/core/canvasElements'), - metadataEditor = require('plugins/core/metadataEditor/metadataEditor'); - - + metadataEditor = require('plugins/core/metadataEditor/metadataEditor'), + edumed = require('plugins/core/edumed/edumed'), + add_attachments = require('views/attachments/add_attachments'); + + +var exerciseFix = function(newNodes) { + var list, exercise, max, addedItem, answerValues; + if(newNodes.created.is('item')) { + list = newNodes.created.parent(); + exercise = list.parent(); + if(exercise && exercise.is('exercise')) { + if(exercise.is('exercise.order')) { + answerValues = exercise.object.getItems() + .map(function(item) { + if(!addedItem && item.node.sameNode(newNodes.created)) { + addedItem = item; + } + return item.getAnswer(); + }); + max = Math.max.apply(Math.max, answerValues); + addedItem.setAnswer(max + 1); + } + } + } +}; plugin.documentExtension.textNode.transformations = { breakContent: { @@ -38,6 +60,21 @@ plugin.documentExtension.textNode.transformations = { return true; // break } }); + + /* + /* + This makes sure that adding a new item to the list in some of the edumed exercises + sets an answer attribute that makes sense (and not just copies it which would create + a duplicate value). + + This won't be neccessary when/if we introduce canvas element own key event handlers. + + Alternatively, WLXML elements could implement their own item split methods that we + would delegate to. + */ + exerciseFix(newNodes); + /* */ + parentDescribingNodes.forEach(function(node) { newNodes.first.append(node); }); @@ -83,6 +120,18 @@ plugin.documentExtension.textNode.transformations = { parent.contents().some(function(n) { return move(n, newNode); }); + if(newNodes.second.contents()[0].getText().length === 0) { + var textNode = newNodes.second.contents()[0]; + newNodes.second.detach(); + newNodes.second = parent; + emptyText = newNodes.second.append(textNode); + } + } + + var newNodeText = newNodes.second.contents()[0].getText(); + if(newNodes.second.is({tagName: 'header'}) && newNodeText === '') { + newNodes.second = newNodes.second.setTag('div'); + newNodes.second.setClass('p'); } return _.extend(newNodes, {emptyText: emptyText}); @@ -124,7 +173,7 @@ plugin.documentExtension.textNode.transformations = { } }; -plugin.documentExtension.elementNode.transformations = { +plugin.documentExtension.documentNode.transformations = { moveUp: function() { var toMerge = this, prev = toMerge.prev(); @@ -138,11 +187,16 @@ plugin.documentExtension.elementNode.transformations = { } ret = to.append(node); - if(idx === 0) { + if(idx === 0 && ret.nodeType === Node.TEXT_NODE) { toret = { node: ret, offset: ret.getText().length - len }; + } else if(!toret) { + toret = { + node: ret.getFirstTextNode(), + offset: 0 + }; } }); from.detach(); @@ -152,24 +206,97 @@ plugin.documentExtension.elementNode.transformations = { var strategies = [ { applies: function() { - return toMerge.is('p'); + return toMerge.nodeType === Node.TEXT_NODE && prev.is({tagName: 'span'}); }, run: function() { - if(prev && prev.is('p') || prev.is({tagName: 'header'})) { + var textNode = prev.getLastTextNode(), + txt, prevText, prevTextLen; + if(textNode) { + txt = textNode.getText(); + if(txt.length > 1) { + textNode.setText(txt.substr(0, txt.length-1)); + return {node: toMerge, offset: 0}; + } else { + if((prevText = prev.prev()) && prevText.nodeType === Node.TEXT_NODE) { + prevTextLen = prevText.getText().length; + } + prev.detach(); + return { + node: prevText ? prevText : toMerge, + offset : prevText ? prevTextLen : 0 + }; + } + } + } + }, + { + applies: function() { + return toMerge.is({tagName: 'div', 'klass': 'p'}) || (toMerge.is({tagName: 'div'}) && toMerge.getClass() === ''); + }, + run: function() { + if (!prev) + return; + if(prev.is('p') || prev.is({tagName: 'header'})) { return merge(toMerge, prev); } - if(prev && prev.is('list')) { + if(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: 'span'}); + }, + run: function() { + /* globals Node */ + var toret = {node: toMerge.contents()[0] , offset: 0}, + txt, txtNode, parent; + if(!prev) { + toMerge.parents().some(function(p) { + if(p.is({tagName: 'span'})) { + parent = prev = p; + } else { + if(!parent) { + parent = p; + } + prev = prev && prev.prev(); + return true; + } + }); + } + if(!prev) { + return parent.moveUp(); + } + else if(prev.nodeType === Node.TEXT_NODE && (txt = prev.getText())) { + prev.setText(txt.substr(0, txt.length-1)); + return toret; + } else if(prev.is({tagName: 'span'})) { + if((txtNode = prev.getLastTextNode())) { + txt = txtNode.getText(); + if(txt.length > 1) { + txtNode.setText(txt.substr(0, txt.length-1)); + } else { + if(txtNode.parent().contents().length === 1) { + txtNode.parent().detach(); + } else { + txtNode.detach(); + } + + } + return toret; + } + } + + } + }, { applies: function() { return toMerge.is({tagName: 'header'}); }, run: function() { - if(prev && prev.is('p') || prev.is({tagName: 'header'})) { + if(prev && (prev.is('p') || prev.is({tagName: 'header'}))) { return merge(toMerge, prev); } } @@ -202,6 +329,13 @@ plugin.documentExtension.elementNode.transformations = { } }); return toret; + }, + insertNewNode: function () { + var node = this; + var newElement = this.document.createDocumentNode({tagName: 'div', attrs: {class: 'p'}}); + node.after(newElement); + newElement.append({text: ''}); + return newElement; } }; @@ -366,7 +500,7 @@ var createWrapTextAction = function(createParams) { }); } - if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) { + if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundaries()) { parent = params.fragment.startNode.parent(); if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) { return _.extend(state, {allowed: false}); @@ -409,13 +543,21 @@ var createWrapTextAction = function(createParams) { var createLinkFromSelection = function(callback, params) { - var doc = params.fragment.document, - dialog = Dialog.create({ + var fragment = params.fragment, + doc = fragment.document, + text = fragment.startNode.nativeNode.data.substring(fragment.startOffset, fragment.endOffset), + url; + if (text.indexOf('//') >= 0 && text.indexOf(' ') < 0) { + url = text; + } else if (text.substr(0, 4) === 'www.' && text.indexOF(' ') < 0) { + url = 'http://' + text; + } + var dialog = Dialog.create({ title: gettext('Create link'), executeButtonText: gettext('Apply'), cancelButtonText: gettext('Cancel'), fields: [ - {label: gettext('Link'), name: 'href', type: 'input', + {label: gettext('Link'), name: 'href', type: 'input', initialValue: url || '', prePasteHandler: function(text) { return params.fragment.document.getLinkForUrl(text); }.bind(this) @@ -444,6 +586,7 @@ var createLinkFromSelection = function(callback, params) { }); }); dialog.show(); + add_attachments(dialog); }; var editLink = function(callback, params) { @@ -489,7 +632,7 @@ var linkAction = { } if(params.fragment instanceof params.fragment.TextRangeFragment) { - if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) { + if(!params.fragment.hasSiblingBoundaries() || params.fragment.startNode.parent().is('link')) { return {allowed: false}; } return { @@ -523,8 +666,7 @@ plugin.actions = [ createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}), linkAction, metadataEditor.action(metadataParams) -].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions); - +].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions, edumed.actions); plugin.config = function(config) { @@ -541,7 +683,7 @@ plugin.config = function(config) { }); }; -plugin.canvasElements = canvasElements; +plugin.canvasElements = canvasElements.concat(edumed.canvasElements); return plugin;