even more tutorial in editor (status bar)
[fnpeditor.git] / src / editor / plugins / core / switch.js
index 2403c2b..4587318 100644 (file)
@@ -10,13 +10,14 @@ var createSwitchAction = function(createParams) {
     return  {
         name: createParams.name,
         params: {
-            fragment: {type: 'context', name: 'fragment'},
+            fragment: {type: 'context', name: 'fragment'}
         },
         getState: function(params) {
             var state = {
-                    label: this.config.label
+                    label: createParams.to.name
                 },
-                f = params.fragment;
+                f = params.fragment,
+                description;
 
 
             if(
@@ -25,23 +26,25 @@ var createSwitchAction = function(createParams) {
             ) {
                 return _.extend(state, {
                     allowed: false,
-                    description: 'wrong or no selection'
+                    description: gettext('wrong or no selection')
                 });
             }
 
             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 = gettext('Switch to') + ' ' + createParams.to.name;
             return _.extend(state, {
-                allowed: !!toSwitch,
+                allowed: !!toSwitch || alreadyInTarget,
                 toggled: alreadyInTarget,
-                description: 'Switch to ' + createParams.to.name,
-                execute: alreadyInTarget ? function() {} : function() {
+                description: description,
+                execute: alreadyInTarget ? function() {} : function(callback) {
                     f.document.transaction(function() {
                         if(createParams.to.tagName) {
                             toSwitch = toSwitch.setTag(createParams.to.tagName);
@@ -49,6 +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,
+                            fragment: params.fragment
+                        },
+                        success: callback
                     });
                 }
             });
@@ -56,12 +66,27 @@ 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('paragraph')}}),
+    imageAction = createSwitchAction({name: 'switchToImage', from: {}, to: {tagName: 'div', klass: 'img', name: gettext('image')}}),
+    videoAction = createSwitchAction({name: 'switchToVideo', from: {}, to: {tagName: 'div', klass: 'video', name: gettext('video')}});
 
 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, imageAction, videoAction],
+    canvasActionHandler: {
+        handles: [headerAction, paragraphAction, imageAction, videoAction]
+        // 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