From 471df249233a0064cdd3c4efe890536d8b304037 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 22 Apr 2014 12:47:12 +0200 Subject: [PATCH] editor: return fragments from actions, set this fragment on canvas if possible --- .../modules/documentCanvas/documentCanvas.js | 13 +++++++++- src/editor/plugins/core/core.js | 8 +++--- src/editor/plugins/core/lists.js | 7 +++++- src/editor/plugins/core/switch.js | 25 +++++++++++++++---- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/editor/modules/documentCanvas/documentCanvas.js b/src/editor/modules/documentCanvas/documentCanvas.js index 3e6a209..9a88b75 100644 --- a/src/editor/modules/documentCanvas/documentCanvas.js +++ b/src/editor/modules/documentCanvas/documentCanvas.js @@ -3,11 +3,15 @@ define([ 'libs/jquery', 'libs/underscore', +'fnpjs/logging/logging', './canvas/canvas', -'libs/text!./template.html'], function($, _, canvas3, template) { +'libs/text!./template.html'], function($, _, logging, canvas3, template) { 'use strict'; + +var logger = logging.getLogger('documentCanvas'); + return function(sandbox) { var canvas = canvas3.fromXMLDocument(null); @@ -72,6 +76,13 @@ return function(sandbox) { 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); }); diff --git a/src/editor/plugins/core/core.js b/src/editor/plugins/core/core.js index 4c2f4c0..5d3a57f 100644 --- a/src/editor/plugins/core/core.js +++ b/src/editor/plugins/core/core.js @@ -206,15 +206,16 @@ var createLinkFromSelection = function(callback, params) { 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()] - }); + }), + doc = params.fragment.document; 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 @@ -242,6 +243,7 @@ var editLink = function(callback, params) { doc.transaction(function() { link.setAttr('href', event.formData.href); event.success(); + return params.fragment; }, { metadata: { description: action.getState().description diff --git a/src/editor/plugins/core/lists.js b/src/editor/plugins/core/lists.js index 911a1f2..427673e 100644 --- a/src/editor/plugins/core/lists.js +++ b/src/editor/plugins/core/lists.js @@ -41,7 +41,12 @@ var toggleListAction = function(type) { 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 diff --git a/src/editor/plugins/core/switch.js b/src/editor/plugins/core/switch.js index 57b28b9..9b34721 100644 --- a/src/editor/plugins/core/switch.js +++ b/src/editor/plugins/core/switch.js @@ -32,7 +32,8 @@ 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); @@ -51,6 +52,7 @@ 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 @@ -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 { - 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 -- 2.20.1