From 981e87b8a8d4dfca0c816d8bcedcd7c4dbdab9f4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 13 May 2014 12:31:09 +0200 Subject: [PATCH 1/1] editor: plugins/core - support for undo/redo selection restore in most actions --- src/editor/modules/documentCanvas/canvas/canvas.js | 13 +++++++++++++ src/editor/modules/documentCanvas/documentCanvas.js | 13 ++----------- src/editor/plugins/core/core.js | 12 ++++++++---- src/editor/plugins/core/links/linkElement.js | 11 ++++++++--- src/editor/plugins/core/lists.js | 9 ++++++--- src/editor/plugins/core/switch.js | 3 ++- 6 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index 7599a71..a057e05 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -309,6 +309,19 @@ $.extend(Canvas.prototype, Backbone.Events, { return new Selection(this); }, + select: function(fragment) { + if(fragment instanceof this.wlxmlDocument.RangeFragment) { + this.setCurrentElement(fragment.endNode, {caretTo: fragment.endOffset}); + } else if(fragment instanceof this.wlxmlDocument.NodeFragment) { + var params = { + caretTo: fragment instanceof this.wlxmlDocument.CaretFragment ? fragment.offset : 'start' + }; + this.setCurrentElement(fragment.node, params); + } else { + logger.debug('Fragment not supported'); + } + }, + setCurrentElement: function(element, params) { if(!element) { logger.debug('Invalid element passed to setCurrentElement: ' + element); diff --git a/src/editor/modules/documentCanvas/documentCanvas.js b/src/editor/modules/documentCanvas/documentCanvas.js index 98c09d2..8742aef 100644 --- a/src/editor/modules/documentCanvas/documentCanvas.js +++ b/src/editor/modules/documentCanvas/documentCanvas.js @@ -84,18 +84,9 @@ return function(sandbox) { onAfterActionExecuted: function(action, ret) { if(ret && ret.isValid()) { logger.debug('The action returned a valid fragment'); - if(ret instanceof canvas.wlxmlDocument.RangeFragment) { - canvas.setCurrentElement(ret.endNode, {caretTo: ret.endOffset}); - } else if(ret instanceof canvas.wlxmlDocument.NodeFragment) { - var params = { - caretTo: ret instanceof canvas.wlxmlDocument.CaretFragment ? ret.offset : 'start' - }; - canvas.setCurrentElement(ret.node, params); - } else { - logger.debug('Fragment not supported'); - } - return; + return canvas.select(ret); } + logger.debug('No valid fragment returned from the action'); (actionHandlers[action.getPluginName()] || []).forEach(function(handler) { diff --git a/src/editor/plugins/core/core.js b/src/editor/plugins/core/core.js index ba6229f..bbc27ac 100644 --- a/src/editor/plugins/core/core.js +++ b/src/editor/plugins/core/core.js @@ -220,7 +220,8 @@ var createWrapTextAction = function(createParams) { return newFragment; }, { metadata: { - description: createParams.unwrapDescription + description: createParams.unwrapDescription, + fragment: params.fragment }, success: callback }); @@ -256,7 +257,8 @@ var createWrapTextAction = function(createParams) { } }, { metadata: { - description: createParams.wrapDescription + description: createParams.wrapDescription, + fragment: params.fragment }, success: callback }); @@ -298,7 +300,8 @@ var createLinkFromSelection = function(callback, params) { return doc.createFragment(doc.CaretFragment, {node: span.contents()[0], offset:0}); }, { metadata: { - description: action.getState().description + description: action.getState().description, + fragment: params.fragment }, success: callback }); @@ -326,7 +329,8 @@ var editLink = function(callback, params) { return params.fragment; }, { metadata: { - description: action.getState().description + description: action.getState().description, + fragment: params.fragment }, success: callback }); diff --git a/src/editor/plugins/core/links/linkElement.js b/src/editor/plugins/core/links/linkElement.js index 9e4a935..503e958 100644 --- a/src/editor/plugins/core/links/linkElement.js +++ b/src/editor/plugins/core/links/linkElement.js @@ -39,6 +39,7 @@ _.extend(linkElement, { changeLink: function(e) { var el = this, + doc = this.wlxmlNode.document, dialog = Dialog.create({ title: gettext('Edit link'), executeButtonText: gettext('Apply'), @@ -60,7 +61,8 @@ _.extend(linkElement, { event.success(); }, { metadata: { - description: gettext('Edit link') + description: gettext('Edit link'), + fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0}) } }); }); @@ -68,12 +70,15 @@ _.extend(linkElement, { }, deleteLink: function() { - var el = this; + var el = this, + doc = this.wlxmlNode.document; + el.wlxmlNode.document.transaction(function() { el.wlxmlNode.unwrapContent(); }, { metadata: { - description: gettext('Remove link') + description: gettext('Remove link'), + fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:0}) } }); }, diff --git a/src/editor/plugins/core/lists.js b/src/editor/plugins/core/lists.js index 5a1b875..662dc53 100644 --- a/src/editor/plugins/core/lists.js +++ b/src/editor/plugins/core/lists.js @@ -49,7 +49,8 @@ var toggleListAction = function(type) { return doc.createFragment(doc.CaretFragment, {node: text, offset:0}); }, { metadata: { - description: action.getState().description + description: action.getState().description, + fragment: params.fragment }, success: callback }); @@ -77,7 +78,8 @@ var toggleListAction = function(type) { return toret; }, { metadata: { - description: action.getState().description + description: action.getState().description, + fragment: params.fragment }, success: callback }); @@ -99,7 +101,8 @@ var toggleListAction = function(type) { } }, { metadata: { - description: action.getState().description + description: action.getState().description, + fragment: params.fragment }, success: callback }); diff --git a/src/editor/plugins/core/switch.js b/src/editor/plugins/core/switch.js index edb8e95..645f7ad 100644 --- a/src/editor/plugins/core/switch.js +++ b/src/editor/plugins/core/switch.js @@ -55,7 +55,8 @@ var createSwitchAction = function(createParams) { return f.document.createFragment(f.CaretFragment, {node: f.document.getNodeByPath(textNodePath), offset: f.offset}); }, { metadata: { - description: description + description: description, + fragment: params.fragment }, success: callback }); -- 2.20.1