From: Aleksander Ɓukasz Date: Fri, 28 Jun 2013 12:32:07 +0000 (+0200) Subject: Fixing wrapping nodes with subnodes X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/f3acead9b013a34d21bd75867c3351862b5df07f Fixing wrapping nodes with subnodes canvas.nodeWrap was given wrong arguments when text selected on the left of first subnode was longer than the text selected on the right of last subnode --- diff --git a/modules/documentCanvas/canvasManager.js b/modules/documentCanvas/canvasManager.js index b7e7212..aff3b46 100644 --- a/modules/documentCanvas/canvasManager.js +++ b/modules/documentCanvas/canvasManager.js @@ -102,22 +102,27 @@ Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) { if(selection.getRangeAt(0).collapsed) { } else { + var textNodeIdx; + var parent = $(selection.anchorNode).parent(); var offsetStart = selection.anchorOffset; var offsetEnd = selection.focusOffset; - if(offsetStart > offsetEnd) { - var tmp = offsetStart; - offsetStart = offsetEnd; - offsetEnd = tmp; - } + if(selection.anchorNode === selection.focusNode) { + if(offsetStart > offsetEnd) { + var tmp = offsetStart; + offsetStart = offsetEnd; + offsetEnd = tmp; + } + textNodeIdx = parent.contents().index($(selection.anchorNode)); + } else { + if(parent.contents().index($(selection.anchorNode)) > parent.contents().index($(selection.focusNode))) { + offsetStart = selection.focusOffset; + offsetEnd = selection.anchorOffset; + } + textNodeIdx = [parent.contents().index($(selection.anchorNode)), parent.contents().index($(selection.focusNode))]; + } var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass}); - var parent = $(selection.anchorNode).parent(); - - var textNodeIdx = parent.contents().index($(selection.anchorNode)); - if(selection.anchorNode != selection.focusNode) { - textNodeIdx = [textNodeIdx, parent.contents().index($(selection.focusNode))]; - } this.canvas.nodeWrap({inside: canvasNode.create(parent), _with: wrapper, offsetStart: offsetStart,