editor: return fragments from actions, set this fragment on canvas if possible
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 22 Apr 2014 10:47:12 +0000 (12:47 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 23 Apr 2014 11:05:05 +0000 (13:05 +0200)
src/editor/modules/documentCanvas/documentCanvas.js
src/editor/plugins/core/core.js
src/editor/plugins/core/lists.js
src/editor/plugins/core/switch.js

index 3e6a209..9a88b75 100644 (file)
@@ -3,11 +3,15 @@
 define([
 'libs/jquery',
 'libs/underscore',
 define([
 'libs/jquery',
 'libs/underscore',
+'fnpjs/logging/logging',
 './canvas/canvas',
 './canvas/canvas',
-'libs/text!./template.html'], function($, _, canvas3, template) {
+'libs/text!./template.html'], function($, _, logging, canvas3, template) {
 
 'use strict';
 
 
 'use strict';
 
+
+var logger = logging.getLogger('documentCanvas');
+
 return function(sandbox) {
 
     var canvas = canvas3.fromXMLDocument(null);
 return function(sandbox) {
 
     var canvas = canvas3.fromXMLDocument(null);
@@ -72,6 +76,13 @@ return function(sandbox) {
             canvas.setCurrentElement(node);
         },
         onAfterActionExecuted: function(action, ret) {
             canvas.setCurrentElement(node);
         },
         onAfterActionExecuted: function(action, ret) {
+            if(ret && ret instanceof canvas.wlxmlDocument.CaretFragment && ret.isValid()) {
+                logger.debug('The action returned a valid fragment');
+                canvas.setCurrentElement(ret.node, {caretTo: ret.offset});
+                return;
+            }
+            logger.debug('No valid fragment returned from the action');
+
             (actionHandlers[action.getPluginName()] || []).forEach(function(handler) {
                 handler(canvas, action, ret);
             });
             (actionHandlers[action.getPluginName()] || []).forEach(function(handler) {
                 handler(canvas, action, ret);
             });
index 4c2f4c0..5d3a57f 100644 (file)
@@ -206,15 +206,16 @@ var createLinkFromSelection = function(callback, params) {
     
     dialog.on('execute', function(event) {
         doc.transaction(function() {
     
     dialog.on('execute', function(event) {
         doc.transaction(function() {
-            var span =  params.fragment.startNode.parent().wrapText({
+            var span =  action.params.fragment.startNode.parent().wrapText({
                 _with: {tagName: 'span', attrs: {'class': 'link'}},
                 offsetStart: params.fragment.startOffset,
                 offsetEnd: params.fragment.endOffset,
                 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
                 _with: {tagName: 'span', attrs: {'class': 'link'}},
                 offsetStart: params.fragment.startOffset,
                 offsetEnd: params.fragment.endOffset,
                 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
-            });
+            }),
+                doc = params.fragment.document;
             span.setAttr('href', event.formData.href);
             event.success();
             span.setAttr('href', event.formData.href);
             event.success();
-            return span;
+            return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0});
         }, {
             metadata: {
                 description: action.getState().description
         }, {
             metadata: {
                 description: action.getState().description
@@ -242,6 +243,7 @@ var editLink = function(callback, params) {
         doc.transaction(function() {
             link.setAttr('href', event.formData.href);
             event.success();
         doc.transaction(function() {
             link.setAttr('href', event.formData.href);
             event.success();
+            return params.fragment;
         }, {
             metadata: {
                 description: action.getState().description
         }, {
             metadata: {
                 description: action.getState().description
index 911a1f2..427673e 100644 (file)
@@ -41,7 +41,12 @@ var toggleListAction = function(type) {
                 listParams.node1 = boundries.node1;
                 listParams.node2 = boundries.node2;
                 boundries.node1.document.transaction(function() {
                 listParams.node1 = boundries.node1;
                 listParams.node2 = boundries.node2;
                 boundries.node1.document.transaction(function() {
-                    boundries.node1.document.createList(listParams);
+                    var list = boundries.node1.document.createList(listParams),
+                        item1 = list.object.getItem(0),
+                        text = item1 ? item1.contents()[0] : undefined, //
+                        doc = boundries.node1.document;
+
+                   return doc.createFragment(doc.CaretFragment, {node: text, offset:0});
                 }, {
                     metadata: {
                         description: action.getState().description
                 }, {
                     metadata: {
                         description: action.getState().description
index 57b28b9..9b34721 100644 (file)
@@ -32,7 +32,8 @@ var createSwitchAction = function(createParams) {
 
             var node = f instanceof f.CaretFragment ? f.node.parent() : f.getCommonParent(),
                 alreadyInTarget = node.isInside(createParams.to),
 
             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);
 
             if(!toSwitch.is(createParams.from)) {
                 toSwitch = toSwitch.getParent(createParams.from);
@@ -51,6 +52,7 @@ var createSwitchAction = function(createParams) {
                         if(!_.isUndefined(createParams.to.klass)) {
                             toSwitch.setClass(createParams.to.klass);
                         }
                         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
                     }, {
                         metadata: {
                             description: description
@@ -63,12 +65,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 {
 
 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
 };
 
 });
\ No newline at end of file