smartxml: Getting rid of nodeMoved event in favor of `move` flag on nodeDetached...
[fnpeditor.git] / src / editor / plugins / core / switch.js
index 2730761..645f7ad 100644 (file)
@@ -14,7 +14,7 @@ var createSwitchAction = function(createParams) {
         },
         getState: function(params) {
             var state = {
-                    label: this.config.label
+                    label: createParams.to.name
                 },
                 f = params.fragment,
                 description;
@@ -32,18 +32,19 @@ var createSwitchAction = function(createParams) {
 
             var node = f instanceof f.CaretFragment ? f.node.parent() : f.getCommonParent(),
                 alreadyInTarget = node.isInside(createParams.to),
-                toSwitch = node;
+                toSwitch = node,
+                textNodePath = (f.node || f.startNode).getPath();
 
             if(!toSwitch.is(createParams.from)) {
                 toSwitch = toSwitch.getParent(createParams.from);
             }
 
-            description = 'Switch to ' + createParams.to.name;
+            description = gettext('Switch to') + ' ' + createParams.to.name;
             return _.extend(state, {
-                allowed: !!toSwitch,
+                allowed: !!toSwitch || alreadyInTarget,
                 toggled: alreadyInTarget,
                 description: description,
-                execute: alreadyInTarget ? function() {} : function() {
+                execute: alreadyInTarget ? function() {} : function(callback) {
                     f.document.transaction(function() {
                         if(createParams.to.tagName) {
                             toSwitch = toSwitch.setTag(createParams.to.tagName);
@@ -51,10 +52,13 @@ var createSwitchAction = function(createParams) {
                         if(!_.isUndefined(createParams.to.klass)) {
                             toSwitch.setClass(createParams.to.klass);
                         }
+                        return f.document.createFragment(f.CaretFragment, {node: f.document.getNodeByPath(textNodePath), offset: f.offset});
                     }, {
                         metadata: {
-                            description: description
-                        }
+                            description: description,
+                            fragment: params.fragment
+                        },
+                        success: callback
                     });
                 }
             });
@@ -62,12 +66,25 @@ var createSwitchAction = function(createParams) {
     };
 };
 
+var headerAction = createSwitchAction({name: 'switchToHeader', from: {tagName: 'div', klass: 'p'}, to: {tagName: 'header', klass: '', name: gettext('header')}}),
+    paragraphAction = createSwitchAction({name: 'switchToParagraph', from: {tagName: 'header'}, to: {tagName: 'div', klass: 'p', name: gettext('paragraf')}});
 
 return {
-    actions: [
-        createSwitchAction({name: 'switchToHeader', from: {tagName: 'div', klass: 'p'}, to: {tagName: 'header', klass: '', name: gettext('header')}}),
-        createSwitchAction({name: 'switchToParagraph', from: {tagName: 'header'}, to: {tagName: 'div', klass: 'p', name: gettext('paragraf')}})
-    ]
+    actions: [headerAction, paragraphAction],
+    canvasActionHandler: {
+        handles: [headerAction, paragraphAction],
+        // handle: function(canvas, action, ret) {
+        //     var params = {},
+        //         f;
+        //     if(ret && ret.node2) {
+        //         f = ret.oldFragment;
+        //         if(f && f instanceof f.CaretFragment) {
+        //             params.caretTo = f.offset;
+        //         }
+        //         canvas.setCurrentElement(ret.node2, params);
+        //     }
+        // }
+    }
 };
 
 });
\ No newline at end of file