1 define(function(require) {
6 var _ = require('libs/underscore');
9 var createSwitchAction = function(createParams) {
11 name: createParams.name,
13 fragment: {type: 'context', name: 'fragment'},
15 getState: function(params) {
17 label: this.config.label
24 !(f && f.isValid()) ||
25 !((f instanceof f.CaretFragment) || (f instanceof f.TextRangeFragment && f.getCommonParent()))
27 return _.extend(state, {
29 description: 'wrong or no selection'
33 var node = f instanceof f.CaretFragment ? f.node.parent() : f.getCommonParent(),
34 alreadyInTarget = node.isInside(createParams.to),
36 textNodePath = (f.node || f.startNode).getPath();
38 if(!toSwitch.is(createParams.from)) {
39 toSwitch = toSwitch.getParent(createParams.from);
42 description = 'Switch to ' + createParams.to.name;
43 return _.extend(state, {
45 toggled: alreadyInTarget,
46 description: description,
47 execute: alreadyInTarget ? function() {} : function(callback) {
48 f.document.transaction(function() {
49 if(createParams.to.tagName) {
50 toSwitch = toSwitch.setTag(createParams.to.tagName);
52 if(!_.isUndefined(createParams.to.klass)) {
53 toSwitch.setClass(createParams.to.klass);
55 return f.document.createFragment(f.CaretFragment, {node: f.document.getNodeByPath(textNodePath), offset: f.offset});
58 description: description
68 var headerAction = createSwitchAction({name: 'switchToHeader', from: {tagName: 'div', klass: 'p'}, to: {tagName: 'header', klass: '', name: gettext('header')}}),
69 paragraphAction = createSwitchAction({name: 'switchToParagraph', from: {tagName: 'header'}, to: {tagName: 'div', klass: 'p', name: gettext('paragraf')}});
72 actions: [headerAction, paragraphAction],
73 canvasActionHandler: {
74 handles: [headerAction, paragraphAction],
75 // handle: function(canvas, action, ret) {
78 // if(ret && ret.node2) {
79 // f = ret.oldFragment;
80 // if(f && f instanceof f.CaretFragment) {
81 // params.caretTo = f.offset;
83 // canvas.setCurrentElement(ret.node2, params);