editor: Setting descriptions for transactions performed in actions
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 17 Apr 2014 10:47:52 +0000 (12:47 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 23 Apr 2014 11:05:05 +0000 (13:05 +0200)
src/editor/plugins/core/core.js
src/editor/plugins/core/lists.js
src/editor/plugins/core/switch.js
src/editor/plugins/core/templates.js

index a27ec9e..3465046 100644 (file)
@@ -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 = 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,
             }
             return {
                 allowed: allowed,
@@ -89,7 +92,8 @@ var commentAction = {
         icon: 'comment',
         execute: function(params, editor) {
             /* globals Node */
         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();
             }
             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});
                 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,
 
             return _.extend(state, {
                 allowed: true,
+                description: createParams.description,
                 execute: function(params) {
                     params.fragment.document.transaction(function() {
                         var parent = params.fragment.startNode.parent();
                 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()]
                         });
                             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'}
             ]
             fields: [
                 {label: gettext('Link'), name: 'href', type: 'input'}
             ]
-        });
+        }),
+        action = this;
     
     dialog.on('execute', function(event) {
         doc.transaction(function() {
     
     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;
             span.setAttr('href', event.formData.href);
             event.success();
             return span;
+        }, {
+            metadata: {
+                description: action.getState().description
+            }
         });
     });
     dialog.show();
         });
     });
     dialog.show();
@@ -213,12 +231,17 @@ var editLink = function(params) {
             fields: [
                 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
             ]
             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();
     
     dialog.on('execute', function(event) {
         doc.transaction(function() {
             link.setAttr('href', event.formData.href);
             event.success();
+        }, {
+            metadata: {
+                description: action.getState().description
+            }
         });
     });
     dialog.show();
         });
     });
     dialog.show();
@@ -262,8 +285,8 @@ plugin.actions = [
     undoRedoAction('undo'),
     undoRedoAction('redo'),
     commentAction,
     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);
 
     linkAction
 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
 
index 3cf1192..a8dc802 100644 (file)
@@ -34,30 +34,54 @@ var toggleListAction = function(type) {
     var execute = {
         add: function(params) {
             var boundries = getBoundriesForAList(params.fragment),
     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;
             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 */
             } 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')) {
 
             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
                 }
                     return true; // break
                 }
-            });
+            }.bind(this));
         },
         changeType: function(params) {
         },
         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
+                }
+            });
         }
     };
 
         }
     };
 
index 2403c2b..2730761 100644 (file)
@@ -16,7 +16,8 @@ var createSwitchAction = function(createParams) {
             var state = {
                     label: this.config.label
                 },
             var state = {
                     label: this.config.label
                 },
-                f = params.fragment;
+                f = params.fragment,
+                description;
 
 
             if(
 
 
             if(
@@ -37,10 +38,11 @@ var createSwitchAction = function(createParams) {
                 toSwitch = toSwitch.getParent(createParams.from);
             }
 
                 toSwitch = toSwitch.getParent(createParams.from);
             }
 
+            description = 'Switch to ' + createParams.to.name;
             return _.extend(state, {
                 allowed: !!toSwitch,
                 toggled: alreadyInTarget,
             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) {
                 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);
                         }
                         if(!_.isUndefined(createParams.to.klass)) {
                             toSwitch.setClass(createParams.to.klass);
                         }
+                    }, {
+                        metadata: {
+                            description: description
+                        }
                     });
                 }
             });
                     });
                 }
             });
index 1114258..69e30f9 100644 (file)
@@ -16,6 +16,8 @@ var insertTemplateAction = {
         icon: 'core.plus'
     },
     getState: function(params) {
         icon: 'core.plus'
     },
     getState: function(params) {
+        var description;
+
         if(!(params.template && params.template.id)) {
             return {
                 allowed: false,
         if(!(params.template && params.template.id)) {
             return {
                 allowed: false,
@@ -31,13 +33,21 @@ var insertTemplateAction = {
                     description: gettext('Wrong node selected')
             };
         }
                     description: gettext('Wrong node selected')
             };
         }
+
+        description = interpolate(gettext('Insert template %s after %s'), [params.template.name, params.fragment.node.getNearestElementNode().getTagName()]);
         return {
             allowed: true,
         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();
             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
+                    }
+                });
             }
         };
     }
             }
         };
     }