X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/e9aaf1e41c5e695136d06f008c06b287da7d3eda..9831076c8f7385dffb533e0327cc7dd7c9f1ef92:/src/editor/plugins/core/core.js diff --git a/src/editor/plugins/core/core.js b/src/editor/plugins/core/core.js index d11bf73..d0a376d 100644 --- a/src/editor/plugins/core/core.js +++ b/src/editor/plugins/core/core.js @@ -1,9 +1,15 @@ define(function(require) { 'use strict'; +/* globals gettext */ var _ = require('libs/underscore'), - plugin = {documentExtension: {textNode: {}}}; + templates = require('plugins/core/templates'), + footnote = require('plugins/core/footnote'), + switchTo = require('plugins/core/switch'), + lists = require('plugins/core/lists'), + plugin = {name: 'core', actions: [], canvas: {}, documentExtension: {textNode: {}}}, + Dialog = require('views/dialog/dialog'); plugin.documentExtension.textNode.transformations = { @@ -38,6 +44,233 @@ plugin.documentExtension.textNode.transformations = { } }; +var undoRedoAction = function(dir) { + return { + name: dir, + params: { + document: {type: 'context', name: 'document'}, + }, + stateDefaults: { + label: dir === 'undo' ? '<-' : '->', + icon: 'share-alt', + iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '', + execute: function(params) { + params.document[dir](); + }, + }, + getState: function(params) { + var allowed = params.document && !!(params.document[dir+'Stack'].length), + desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'), + descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo'); + return { + allowed: allowed, + description: allowed ? desc : descEmpty + }; + } + }; +}; + +var pad = function(number) { + if(number < 10) { + number = '0' + number; + } + return number; +}; + +var commentAction = { + name: 'comment', + params: { + fragment: {type: 'context', name: 'fragment'} + }, + stateDefaults: { + icon: 'comment', + execute: function(params, editor) { + /* globals Node */ + var node = params.fragment.node; + if(node.nodeType === Node.TEXT_NODE) { + node = node.parent(); + } + node.document.transaction(function() { + var comment = node.after({tagName: 'aside', attrs: {'class': 'comment'}}); + comment.append({text:''}); + var user = editor.getUser(), creator; + if(user) { + creator = user.name; + if(user.email) { + creator += ' (' + user.email + ')'; + } + } else { + creator = 'anonymous'; + } + + var currentDate = new Date(), + dt = pad(currentDate.getDate()) + '-' + + pad((currentDate.getMonth() + 1)) + '-' + + pad(currentDate.getFullYear()) + ' ' + + pad(currentDate.getHours()) + ':' + + pad(currentDate.getMinutes()) + ':' + + pad(currentDate.getSeconds()); + + var metadata = comment.getMetadata(); + metadata.add({key: 'creator', value: creator}); + metadata.add({key: 'date', value: dt}); + }); + }, + }, + getState: function(params) { + var state = { + allowed: params.fragment && params.fragment.isValid() && + params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot() + }; + if(state.allowed) { + state.description = gettext('Insert comment after current node'); + } + return state; + } +}; + + +var createWrapTextAction = function(createParams) { + return { + name: createParams.name, + params: { + fragment: {type: 'context', name: 'fragment'}, + }, + getState: function(params) { + var state = { + label: this.config.label + }, + parent; + + if( + !params.fragment || !params.fragment.isValid() || + !(params.fragment instanceof params.fragment.TextRangeFragment) || + !params.fragment.hasSiblingBoundries()) { + return _.extend(state, {allowed: false}); + } + + parent = params.fragment.startNode.parent(); + if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) { + return _.extend(state, {allowed: false}); + } + + return _.extend(state, { + allowed: true, + execute: function(params) { + params.fragment.document.transaction(function() { + var parent = params.fragment.startNode.parent(); + return parent.wrapText({ + _with: {tagName: 'span', attrs: {'class': createParams.klass}}, + offsetStart: params.fragment.startOffset, + offsetEnd: params.fragment.endOffset, + textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()] + }); + }); + } + }); + } + }; +}; + + +var createLinkFromSelection = function(params) { + var doc = params.fragment.document, + dialog = Dialog.create({ + title: gettext('Create link'), + executeButtonText: gettext('Apply'), + cancelButtonText: gettext('Cancel'), + fields: [ + {label: gettext('Link'), name: 'href', type: 'input'} + ] + }); + + dialog.on('execute', function(event) { + doc.transaction(function() { + var span = params.fragment.startNode.parent().wrapText({ + _with: {tagName: 'span', attrs: {'class': 'link'}}, + offsetStart: params.fragment.startOffset, + offsetEnd: params.fragment.endOffset, + textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()] + }); + span.setAttr('href', event.formData.href); + event.success(); + return span; + }); + }); + dialog.show(); +}; + +var editLink = function(params) { + var doc = params.fragment.document, + link = params.fragment.node.getParent('link'), + dialog = Dialog.create({ + title: gettext('Edit link'), + executeButtonText: gettext('Apply'), + cancelButtonText: gettext('Cancel'), + fields: [ + {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')} + ] + }); + + dialog.on('execute', function(event) { + doc.transaction(function() { + link.setAttr('href', event.formData.href); + event.success(); + }); + }); + dialog.show(); +}; + +var linkAction = { + name: 'link', + params: { + fragment: {type: 'context', name: 'fragment'} + }, + stateDefaults: { + label: gettext('link') + }, + getState: function(params) { + if(!params.fragment || !params.fragment.isValid()) { + return {allowed: false}; + } + + if(params.fragment instanceof params.fragment.TextRangeFragment) { + if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) { + return {allowed: false}; + } + return { + allowed: true, + description: gettext('Create link from selection'), + execute: createLinkFromSelection + }; + } + + if(params.fragment instanceof params.fragment.CaretFragment) { + if(params.fragment.node.isInside('link')) { + return {allowed: true, toggled: true, execute: editLink}; + } + } + return {allowed: false}; + } +}; + + +plugin.actions = [ + undoRedoAction('undo'), + undoRedoAction('redo'), + commentAction, + createWrapTextAction({name: 'emphasis', klass: 'emp'}), + createWrapTextAction({name: 'cite', klass: 'cite'}), + linkAction +].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions); + + + +plugin.config = function(config) { + // templates.actions[0].config(config.templates); + templates.actions[0].params.template.options = config.templates; +}; + return plugin; }); \ No newline at end of file