editor: plugins/core - support for undo/redo selection restore in most actions
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 13 May 2014 10:31:09 +0000 (12:31 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 13 May 2014 10:31:09 +0000 (12:31 +0200)
src/editor/modules/documentCanvas/canvas/canvas.js
src/editor/modules/documentCanvas/documentCanvas.js
src/editor/plugins/core/core.js
src/editor/plugins/core/links/linkElement.js
src/editor/plugins/core/lists.js
src/editor/plugins/core/switch.js

index 7599a71..a057e05 100644 (file)
@@ -309,6 +309,19 @@ $.extend(Canvas.prototype, Backbone.Events, {
         return new Selection(this);
     },
 
         return new Selection(this);
     },
 
+    select: function(fragment) {
+        if(fragment instanceof this.wlxmlDocument.RangeFragment) {
+            this.setCurrentElement(fragment.endNode, {caretTo: fragment.endOffset});
+        } else if(fragment instanceof this.wlxmlDocument.NodeFragment) {
+            var params = {
+                caretTo: fragment instanceof this.wlxmlDocument.CaretFragment ? fragment.offset : 'start'
+            };
+            this.setCurrentElement(fragment.node, params);
+        } else {
+            logger.debug('Fragment not supported');
+        }
+    },
+
     setCurrentElement: function(element, params) {
         if(!element) {
             logger.debug('Invalid element passed to setCurrentElement: ' + element);
     setCurrentElement: function(element, params) {
         if(!element) {
             logger.debug('Invalid element passed to setCurrentElement: ' + element);
index 98c09d2..8742aef 100644 (file)
@@ -84,18 +84,9 @@ return function(sandbox) {
         onAfterActionExecuted: function(action, ret) {
             if(ret && ret.isValid()) {
                 logger.debug('The action returned a valid fragment');
         onAfterActionExecuted: function(action, ret) {
             if(ret && ret.isValid()) {
                 logger.debug('The action returned a valid fragment');
-                if(ret instanceof canvas.wlxmlDocument.RangeFragment) {
-                    canvas.setCurrentElement(ret.endNode, {caretTo: ret.endOffset});
-                } else if(ret instanceof canvas.wlxmlDocument.NodeFragment) {
-                    var params = {
-                        caretTo: ret instanceof canvas.wlxmlDocument.CaretFragment ? ret.offset : 'start'
-                    };
-                    canvas.setCurrentElement(ret.node, params);
-                } else {
-                    logger.debug('Fragment not supported');
-                }
-                return;
+                return canvas.select(ret);
             }
             }
+
             logger.debug('No valid fragment returned from the action');
 
             (actionHandlers[action.getPluginName()] || []).forEach(function(handler) {
             logger.debug('No valid fragment returned from the action');
 
             (actionHandlers[action.getPluginName()] || []).forEach(function(handler) {
index ba6229f..bbc27ac 100644 (file)
@@ -220,7 +220,8 @@ var createWrapTextAction = function(createParams) {
                             return newFragment;
                         }, {
                             metadata: {
                             return newFragment;
                         }, {
                             metadata: {
-                                description: createParams.unwrapDescription
+                                description: createParams.unwrapDescription,
+                                fragment: params.fragment
                             },
                             success: callback
                         });
                             },
                             success: callback
                         });
@@ -256,7 +257,8 @@ var createWrapTextAction = function(createParams) {
                             }
                         }, {
                             metadata: {
                             }
                         }, {
                             metadata: {
-                                description: createParams.wrapDescription
+                                description: createParams.wrapDescription,
+                                fragment: params.fragment
                             },
                             success: callback
                         });
                             },
                             success: callback
                         });
@@ -298,7 +300,8 @@ var createLinkFromSelection = function(callback, params) {
             return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
         }, {
             metadata: {
             return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
         }, {
             metadata: {
-                description: action.getState().description
+                description: action.getState().description,
+                fragment: params.fragment
             },
             success: callback
         });
             },
             success: callback
         });
@@ -326,7 +329,8 @@ var editLink = function(callback, params) {
             return params.fragment;
         }, {
             metadata: {
             return params.fragment;
         }, {
             metadata: {
-                description: action.getState().description
+                description: action.getState().description,
+                fragment: params.fragment
             },
             success: callback
         });
             },
             success: callback
         });
index 9e4a935..503e958 100644 (file)
@@ -39,6 +39,7 @@ _.extend(linkElement, {
 
     changeLink: function(e) {
         var el = this,
 
     changeLink: function(e) {
         var el = this,
+            doc = this.wlxmlNode.document,
             dialog = Dialog.create({
             title: gettext('Edit link'),
             executeButtonText: gettext('Apply'),
             dialog = Dialog.create({
             title: gettext('Edit link'),
             executeButtonText: gettext('Apply'),
@@ -60,7 +61,8 @@ _.extend(linkElement, {
                 event.success();
             }, {
                 metadata: {
                 event.success();
             }, {
                 metadata: {
-                    description: gettext('Edit link')
+                    description: gettext('Edit link'),
+                    fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
                 }
             });
         });
                 }
             });
         });
@@ -68,12 +70,15 @@ _.extend(linkElement, {
     },
 
     deleteLink: function() {
     },
 
     deleteLink: function() {
-        var el = this;
+        var el = this,
+            doc = this.wlxmlNode.document;
+
         el.wlxmlNode.document.transaction(function() {
             el.wlxmlNode.unwrapContent();
         }, {
             metadata: {
         el.wlxmlNode.document.transaction(function() {
             el.wlxmlNode.unwrapContent();
         }, {
             metadata: {
-                description: gettext('Remove link')
+                description: gettext('Remove link'),
+                fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0})
             }
         });
     },
             }
         });
     },
index 5a1b875..662dc53 100644 (file)
@@ -49,7 +49,8 @@ var toggleListAction = function(type) {
                    return doc.createFragment(doc.CaretFragment, {node: text, offset:0});
                 }, {
                     metadata: {
                    return doc.createFragment(doc.CaretFragment, {node: text, offset:0});
                 }, {
                     metadata: {
-                        description: action.getState().description
+                        description: action.getState().description,
+                        fragment: params.fragment
                     },
                     success: callback
                 });
                     },
                     success: callback
                 });
@@ -77,7 +78,8 @@ var toggleListAction = function(type) {
                         return toret;
                     }, {
                         metadata: {
                         return toret;
                     }, {
                         metadata: {
-                            description: action.getState().description
+                            description: action.getState().description,
+                            fragment: params.fragment
                         },
                         success: callback
                     });
                         },
                         success: callback
                     });
@@ -99,7 +101,8 @@ var toggleListAction = function(type) {
                 }
             }, {
                 metadata: {
                 }
             }, {
                 metadata: {
-                    description: action.getState().description
+                    description: action.getState().description,
+                    fragment: params.fragment
                 },
                 success: callback
             });
                 },
                 success: callback
             });
index edb8e95..645f7ad 100644 (file)
@@ -55,7 +55,8 @@ var createSwitchAction = function(createParams) {
                         return f.document.createFragment(f.CaretFragment, {node: f.document.getNodeByPath(textNodePath), offset: f.offset});
                     }, {
                         metadata: {
                         return f.document.createFragment(f.CaretFragment, {node: f.document.getNodeByPath(textNodePath), offset: f.offset});
                     }, {
                         metadata: {
-                            description: description
+                            description: description,
+                            fragment: params.fragment
                         },
                         success: callback
                     });
                         },
                         success: callback
                     });