X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/1b8f7d0e5974d649dff89f50f2a26783a29af5d7..b20691e774cac0cbf2ee174472a1b1dbc94fa61b:/modules/documentCanvas/canvasManager.js diff --git a/modules/documentCanvas/canvasManager.js b/modules/documentCanvas/canvasManager.js index b7e7212..201394e 100644 --- a/modules/documentCanvas/canvasManager.js +++ b/modules/documentCanvas/canvasManager.js @@ -97,27 +97,34 @@ Manager.prototype.selectNode = function(cnode, options) { }; Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) { - var selection = window.getSelection(); - - if(selection.getRangeAt(0).collapsed) { - - } else { - var offsetStart = selection.anchorOffset; - var offsetEnd = selection.focusOffset; - if(offsetStart > offsetEnd) { - var tmp = offsetStart; - offsetStart = offsetEnd; - offsetEnd = tmp; - } + var selection = window.getSelection(), + $anchorNode = $(selection.anchorNode), + $focusNode = $(selection.focusNode); - var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass}); - var parent = $(selection.anchorNode).parent(); + if(!selection.isCollapsed && $anchorNode.parent()[0] === $focusNode.parent()[0]) { + var textNodeIdx, + parent = $anchorNode.parent(), + parentContents = parent.contents(), + offsetStart = selection.anchorOffset, + offsetEnd = selection.focusOffset; - var textNodeIdx = parent.contents().index($(selection.anchorNode)); - if(selection.anchorNode != selection.focusNode) { - textNodeIdx = [textNodeIdx, parent.contents().index($(selection.focusNode))]; + if(selection.anchorNode === selection.focusNode) { + if(offsetStart > offsetEnd) { + var tmp = offsetStart; + offsetStart = offsetEnd; + offsetEnd = tmp; + } + textNodeIdx = parentContents.index($anchorNode); + } else { + if(parentContents.index($anchorNode) > parentContents.index($focusNode)) { + offsetStart = selection.focusOffset; + offsetEnd = selection.anchorOffset; + } + textNodeIdx = [parentContents.index($anchorNode), parentContents.index($focusNode)]; } + + var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass}); this.canvas.nodeWrap({inside: canvasNode.create(parent), _with: wrapper, offsetStart: offsetStart,