From 9370067a73c02def3b684b96f6adc69631b7f073 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 19 Nov 2013 10:56:05 +0100 Subject: [PATCH 1/1] integration wip: handle newNodeRequested in a new way --- .../modules/documentCanvas/canvas/canvas.js | 1 + .../modules/documentCanvas/canvas/utils.js | 2 +- src/editor/modules/documentCanvas/commands.js | 46 +++++++++++-------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index a4785ad..3a662e9 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -33,6 +33,7 @@ $.extend(Canvas.prototype, { this.setupEventHandling(); this.wlxmlListener.listenTo(wlxmlDocument); + this.wlxmlDocument = wlxmlDocument; }, generateCanvasDOM: function(wlxmlNode) { diff --git a/src/editor/modules/documentCanvas/canvas/utils.js b/src/editor/modules/documentCanvas/canvas/utils.js index 41a2a0a..327b0b0 100644 --- a/src/editor/modules/documentCanvas/canvas/utils.js +++ b/src/editor/modules/documentCanvas/canvas/utils.js @@ -46,7 +46,7 @@ var findCanvasElementInParent = function(wlxmlChildNode, wlxmlParentNode) { return { nearestInDocumentOrder: nearestInDocumentOrder, findCanvasElement: findCanvasElement, - findTextElementInParent: findTextElementInParent, + findCanvasElementInParent: findCanvasElementInParent, unicode: { ZWS: '\u200B' } diff --git a/src/editor/modules/documentCanvas/commands.js b/src/editor/modules/documentCanvas/commands.js index 043d6b7..69dd305 100644 --- a/src/editor/modules/documentCanvas/commands.js +++ b/src/editor/modules/documentCanvas/commands.js @@ -1,6 +1,7 @@ define([ -'modules/documentCanvas/canvas/documentElement' -], function(documentElement) { +'modules/documentCanvas/canvas/documentElement', +'./canvas/utils' +], function(documentElement, utils) { 'use strict'; @@ -79,37 +80,42 @@ commands.register('newNodeRequested', function(canvas, params) { if(cursor.isSelecting()) { if(cursor.isSelectingSiblings()) { if(cursor.isSelectingWithinElement()) { - var newElement = selectionStart.element.wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass, start: selectionStart.offset, end: selectionEnd.offset}), - caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end'; - canvas.setCurrentElement(newElement.children()[0], {caretTo: caretTo}); + var wlxmlNode = selectionStart.element.data('wlxmlNode'), + caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end', + wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}), + wrapperCanvasElement = utils.findCanvasElement(wrapper); + canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo}); } else { - var parent = selectionStart.element.parent(), + var wlxmlNode = selectionStart.element.data('wlxmlNode').parent(), caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start'; - var wrapper = canvas.wrapText({ - inside: parent, - _with: {tag: params.wlxmlTag, klass: params.wlxmlClass}, + var wrapper = wlxmlNode.wrapText({ + _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}, offsetStart: selectionStart.offset, offsetEnd: selectionEnd.offset, - textNodeIdx: [parent.childIndex(selectionStart.element), parent.childIndex(selectionEnd.element)] - }); - - canvas.setCurrentElement(wrapper.children()[caretTo === 0 ? 0 : wrapper.children().length - 1], {caretTo: caretTo}); + textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)] + }), + wrapperCanvasElement = utils.findCanvasElement(wrapper); + canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo}); } } else { - var siblingParents = canvas.getSiblingParents({element1: selectionStart.element, element2: selectionEnd.element}) + var node1 = selectionStart.element.data('wlxmlNode'), + node2 = selectionEnd.element.data('wlxmlNode'), + siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2}); + if(siblingParents) { - canvas.wrapElements({ - element1: siblingParents.element1, - element2: siblingParents.element2, - _with: {tag: params.wlxmlTag, klass: params.wlxmlClass} + canvas.wlxmlDocument.wrapNodes({ + element1: siblingParents.node1, + element2: siblingParents.node2, + _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}} }); } } } else if(canvas.getCurrentNodeElement()) { - var el = canvas.getCurrentNodeElement().wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass}); - canvas.setCurrentElement(el); + var node = findCanvasElement(canvas.getCurrentNodeElement), + wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}); + canvas.setCurrentElement(findCanvasElement(wrapper)); } -- 2.20.1