From: Aleksander Ɓukasz Date: Thu, 17 Apr 2014 10:47:52 +0000 (+0200) Subject: editor: Setting descriptions for transactions performed in actions X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/e6993e4bfe0e5fd38ccf554da489b5af3625c2fb?ds=sidebyside editor: Setting descriptions for transactions performed in actions --- diff --git a/src/editor/plugins/core/core.js b/src/editor/plugins/core/core.js index a27ec9e..3465046 100644 --- a/src/editor/plugins/core/core.js +++ b/src/editor/plugins/core/core.js @@ -63,7 +63,10 @@ var undoRedoAction = function(dir) { desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'), descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo'); if(allowed) { - desc += ': ' + (_.last(params.document[dir+'Stack']).metadata || gettext('unknown operation')); + var metadata = _.last(params.document[dir+'Stack']).metadata; + if(metadata) { + desc += ': ' + (metadata.description || gettext('unknown operation')); + } } return { allowed: allowed, @@ -89,7 +92,8 @@ var commentAction = { icon: 'comment', execute: function(params, editor) { /* globals Node */ - var node = params.fragment.node; + var node = params.fragment.node, + action = this; if(node.nodeType === Node.TEXT_NODE) { node = node.parent(); } @@ -117,6 +121,10 @@ var commentAction = { var metadata = comment.getMetadata(); metadata.add({key: 'creator', value: creator}); metadata.add({key: 'date', value: dt}); + }, { + metadata: { + description: action.getState().description + } }); }, }, @@ -159,6 +167,7 @@ var createWrapTextAction = function(createParams) { return _.extend(state, { allowed: true, + description: createParams.description, execute: function(params) { params.fragment.document.transaction(function() { var parent = params.fragment.startNode.parent(); @@ -168,6 +177,10 @@ var createWrapTextAction = function(createParams) { offsetEnd: params.fragment.endOffset, textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()] }); + }, { + metadata: { + description: createParams.description + } }); } }); @@ -185,7 +198,8 @@ var createLinkFromSelection = function(params) { fields: [ {label: gettext('Link'), name: 'href', type: 'input'} ] - }); + }), + action = this; dialog.on('execute', function(event) { doc.transaction(function() { @@ -198,6 +212,10 @@ var createLinkFromSelection = function(params) { span.setAttr('href', event.formData.href); event.success(); return span; + }, { + metadata: { + description: action.getState().description + } }); }); dialog.show(); @@ -213,12 +231,17 @@ var editLink = function(params) { fields: [ {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')} ] - }); + }), + action = this; dialog.on('execute', function(event) { doc.transaction(function() { link.setAttr('href', event.formData.href); event.success(); + }, { + metadata: { + description: action.getState().description + } }); }); dialog.show(); @@ -262,8 +285,8 @@ plugin.actions = [ undoRedoAction('undo'), undoRedoAction('redo'), commentAction, - createWrapTextAction({name: 'emphasis', klass: 'emp'}), - createWrapTextAction({name: 'cite', klass: 'cite'}), + createWrapTextAction({name: 'emphasis', klass: 'emp', description: gettext('Mark as emphasized')}), + createWrapTextAction({name: 'cite', klass: 'cite', description: gettext('Mark as citation')}), linkAction ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions); diff --git a/src/editor/plugins/core/lists.js b/src/editor/plugins/core/lists.js index 3cf1192..a8dc802 100644 --- a/src/editor/plugins/core/lists.js +++ b/src/editor/plugins/core/lists.js @@ -34,30 +34,54 @@ var toggleListAction = function(type) { var execute = { add: function(params) { var boundries = getBoundriesForAList(params.fragment), - listParams = {klass: type === 'Bullet' ? 'list' : 'list.enum'}; + listParams = {klass: type === 'Bullet' ? 'list' : 'list.enum'}, + action = this; + if(boundries && boundries.node1) { listParams.node1 = boundries.node1; listParams.node2 = boundries.node2; - boundries.node1.document.createList(listParams); + boundries.node1.document.transaction(function() { + boundries.node1.document.createList(listParams); + }, { + metadata: { + description: action.getState().description + } + }); } else { throw new Error('Invalid boundries'); } }, remove: function(params) { /* globals Node */ - var current = params.fragment.node; + var current = params.fragment.node, + action = this; var toSearch = current.nodeType === Node.ELEMENT_NODE ? [current] : []; toSearch = toSearch.concat(current.parents()); toSearch.some(function(node) { if(node.is('list')) { - node.object.extractListItems(); + node.document.transaction(function() { + node.object.extractListItems(); + }, { + metadata: { + description: action.getState().description + } + }); + return true; // break } - }); + }.bind(this)); }, changeType: function(params) { - params.fragment.node.getParent('list').setClass(type === 'Bullet' ? 'list' : 'list.enum'); + var node = params.fragment.node, + action = this; + node.document.transaction(function() { + node.getParent('list').setClass(type === 'Bullet' ? 'list' : 'list.enum'); + }, { + metadata: { + description: action.getState().description + } + }); } }; diff --git a/src/editor/plugins/core/switch.js b/src/editor/plugins/core/switch.js index 2403c2b..2730761 100644 --- a/src/editor/plugins/core/switch.js +++ b/src/editor/plugins/core/switch.js @@ -16,7 +16,8 @@ var createSwitchAction = function(createParams) { var state = { label: this.config.label }, - f = params.fragment; + f = params.fragment, + description; if( @@ -37,10 +38,11 @@ var createSwitchAction = function(createParams) { toSwitch = toSwitch.getParent(createParams.from); } + description = 'Switch to ' + createParams.to.name; return _.extend(state, { allowed: !!toSwitch, toggled: alreadyInTarget, - description: 'Switch to ' + createParams.to.name, + description: description, execute: alreadyInTarget ? function() {} : function() { f.document.transaction(function() { if(createParams.to.tagName) { @@ -49,6 +51,10 @@ var createSwitchAction = function(createParams) { if(!_.isUndefined(createParams.to.klass)) { toSwitch.setClass(createParams.to.klass); } + }, { + metadata: { + description: description + } }); } }); diff --git a/src/editor/plugins/core/templates.js b/src/editor/plugins/core/templates.js index 1114258..69e30f9 100644 --- a/src/editor/plugins/core/templates.js +++ b/src/editor/plugins/core/templates.js @@ -16,6 +16,8 @@ var insertTemplateAction = { icon: 'core.plus' }, getState: function(params) { + var description; + if(!(params.template && params.template.id)) { return { allowed: false, @@ -31,13 +33,21 @@ var insertTemplateAction = { description: gettext('Wrong node selected') }; } + + description = interpolate(gettext('Insert template %s after %s'), [params.template.name, params.fragment.node.getNearestElementNode().getTagName()]); return { allowed: true, - description: interpolate(gettext('Insert template %s after %s'), [params.template.name, params.fragment.node.getNearestElementNode().getTagName()]), + description: description, execute: function(params) { var node = params.fragment.node.getNearestElementNode(); - var toAdd = node.document.createDocumentNode(params.template.content); - node.after(toAdd); + node.document.transaction(function() { + var toAdd = node.document.createDocumentNode(params.template.content); + node.after(toAdd); + }, { + metadata: { + description: description + } + }); } }; }