X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/f7c97ef38dc00ec9b5358ae0bf959a5e6f99271c..84eba72af147a175fb93b7305c1d42b0a4987a8c:/modules/documentCanvas/canvasManager.js?ds=sidebyside diff --git a/modules/documentCanvas/canvasManager.js b/modules/documentCanvas/canvasManager.js index 71c9342..201394e 100644 --- a/modules/documentCanvas/canvasManager.js +++ b/modules/documentCanvas/canvasManager.js @@ -97,23 +97,39 @@ 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); + + + if(!selection.isCollapsed && $anchorNode.parent()[0] === $focusNode.parent()[0]) { + var textNodeIdx, + parent = $anchorNode.parent(), + parentContents = parent.contents(), + offsetStart = selection.anchorOffset, + offsetEnd = selection.focusOffset; + + 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($(selection.anchorNode).parent()), + this.canvas.nodeWrap({inside: canvasNode.create(parent), _with: wrapper, offsetStart: offsetStart, - offsetEnd: offsetEnd + offsetEnd: offsetEnd, + textNodeIdx: textNodeIdx }); this.selectNode(wrapper, {movecaret: 'end'}); } @@ -159,6 +175,8 @@ Manager.prototype.selectFirstNode = function() { }; Manager.prototype.movecaretToNode = function(nodeElement, where) { + if(!nodeElement.length) + return; var range = document.createRange(); range.selectNodeContents(nodeElement[0]); @@ -184,7 +202,7 @@ Manager.prototype.onEnterKey = function(e) { if(pos.isAtEnd) { newNode = canvasNode.create({tag: pos.parentNode.attr('wlxml-tag'), klass: pos.parentNode.attr('wlxml-class')}); - this.canvas.nodeInsertAfter({node: newNode, after: canvas.getNodeById(pos.parentNode.attr('id'))}); + this.canvas.nodeInsertAfter({node: newNode, after: this.canvas.getNodeById(pos.parentNode.attr('id'))}); } else { newNode = this.canvas.nodeSplit({node: contextNode, textNodeIdx: pos.textNodeIndex, offset: pos.textNodeOffset}); } @@ -215,17 +233,17 @@ Manager.prototype.command = function(command, meta) { if(command === 'createList') { var node = canvasNode.create(pos.parentNode); - if(window.getSelection().getRangeAt().collapsed && this.canvas.nodeInsideList({node: node})) { + if(window.getSelection().getRangeAt(0).collapsed && this.canvas.nodeInsideList({node: node})) { this.canvas.listRemove({pointer: node}); this.selectNode(node, {movecaret: 'end'}); this.sandbox.publish('contentChanged'); } else { - if(!this.canvas.nodeInsideList({node: node})) { - this.canvas.listCreate({start: node, end: canvasNode.create(pos.focusNode)}); + //if(!this.canvas.nodeInsideList({node: node})) { + this.canvas.listCreate({start: node, end: canvasNode.create(pos.focusNode), type: meta}); this.selectNode(node, {movecaret: 'end'}); this.sandbox.publish('contentChanged'); - } + //} } }