X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/f7c97ef38dc00ec9b5358ae0bf959a5e6f99271c..84eba72af147a175fb93b7305c1d42b0a4987a8c:/modules/documentCanvas/canvas.js diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index 9ae3a53..23cdf0a 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -62,21 +62,39 @@ Canvas.prototype.nodeInsertAfter = function(options) { }; Canvas.prototype.nodeWrap = function(options) { - var element = $(this.content.find('#' + options.inside.getId()).get(0)); - - var elementContents = element.contents(); - if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3) - return false; - var textElement = elementContents.get(0); - - var prefix = textElement.data.substr(0, options.offsetStart); - var suffix = textElement.data.substr(options.offsetEnd); - var core = textElement.data.substr(options.offsetStart, options.offsetEnd - options.offsetStart); - options._with.setContent(core); - - $(textElement).replaceWith(options._with.dom); - options._with.dom.before(prefix); - options._with.dom.after(suffix); + options = _.extend({textNodeIdx: 0}, options); + if(typeof options.textNodeIdx === 'number') + options.textNodeIdx = [options.textNodeIdx]; + + var container = $(this.content.find('#' + options.inside.getId()).get(0)), + containerContent = container.contents(), + idx1 = Math.min.apply(Math, options.textNodeIdx), + idx2 = Math.max.apply(Math, options.textNodeIdx), + textNode1 = $(containerContent.get(idx1)), + textNode2 = $(containerContent.get(idx2)), + sameNode = textNode1.get(0) === textNode2.get(0), + prefixOutside = textNode1.text().substr(0, options.offsetStart), + prefixInside = textNode1.text().substr(options.offsetStart), + suffixInside = textNode2.text().substr(0, options.offsetEnd), + suffixOutside = textNode2.text().substr(options.offsetEnd) + ; + + textNode1.after(options._with.dom); + textNode1.detach(); + + options._with.dom.before(prefixOutside); + if(sameNode) { + var core = textNode1.text().substr(options.offsetStart, options.offsetEnd - options.offsetStart); + options._with.setContent(core); + } else { + textNode2.detach(); + options._with.dom.append(prefixInside); + for(var i = idx1 + 1; i < idx2; i++) { + options._with.dom.append(containerContent[i]); + } + options._with.dom.append(suffixInside); + } + options._with.dom.after(suffixOutside); }; Canvas.prototype.nodeSplit = function(options) { @@ -98,7 +116,7 @@ Canvas.prototype.nodeSplit = function(options) { var node = this; if(passed) succeedingNodes.push(node); - if(node.isSameNode(textNode.get(0))) + if(node === textNode.get(0)) passed = true; }); @@ -125,7 +143,7 @@ Canvas.prototype.nodeRemove = function(options) { Canvas.prototype.listCreate = function(options) { var element1 = $(this.content.find('#' + options.start.getId()).get(0)); var element2 = $(this.content.find('#' + options.end.getId()).get(0)); - if(!element1.parent().get(0).isSameNode(element2.parent().get(0))) + if(!element1.parent().get(0) === element2.parent().get(0)) return false; var parent = element1.parent(); @@ -142,7 +160,7 @@ Canvas.prototype.listCreate = function(options) { var canvas = this; parent.contents().each(function() { var node = this; - if(node.isSameNode(element1.get(0))) + if(node === element1.get(0)) place = 'inside'; if(place === 'inside') { var $node; @@ -156,12 +174,24 @@ Canvas.prototype.listCreate = function(options) { $node.attr('wlxml-class', 'item'); nodesToWrap.push($node); } - if(node.isSameNode(element2.get(0))) + if(node === element2.get(0)) return false; }); - var list = canvasNode.create({tag: 'div', klass: 'list-items'}).dom; //this._createNode('div', 'list-items'); - element1.before(list); + var list = canvasNode.create({tag: 'div', klass: 'list-items' + (options.type === 'enum' ? '-enum' : '')}).dom; //this._createNode('div', 'list-items'); + + var parentNode = options.start.parent(); + + var toret; + if(parentNode && parentNode.isOfClass('list-items')) { + list.wrap('
'); + toret = list.parent(); + } else { + toret = list; + } + + + element1.before(toret); nodesToWrap.forEach(function(node) { node.remove(); @@ -172,11 +202,21 @@ Canvas.prototype.listCreate = function(options) { Canvas.prototype.listRemove = function(options) { var pointerElement = $(this.content.find('#' + options.pointer.getId())); var listElement = options.pointer.getClass() === 'list-items' ? pointerElement : - pointerElement.parent('[wlxml-class="list-items"][wlxml-tag]'); + pointerElement.parents('[wlxml-class|="list-items"][wlxml-tag]'); - listElement.find('[wlxml-class=item]').each(function() { - $(this).removeAttr('wlxml-class'); - }); + var nested = false; + if(listElement.length > 1) { + listElement = $(listElement[0]); + nested = true; + } + + if(nested) { + listElement.unwrap(); + } else { + listElement.find('[wlxml-class=item]').each(function() { + $(this).removeAttr('wlxml-class'); + }); + } listElement.children().unwrap(); }; @@ -190,10 +230,10 @@ Canvas.prototype.getPrecedingNode = function(options) { Canvas.prototype.nodeInsideList = function(options) { if(options.node) { - if(options.node.getClass() === 'list-items' || options.node.getClass() === 'item') + if(options.node.isOfClass('list-items') || options.node.isOfClass('item')) return true; var pointerElement = $(this.content.find('#' + options.node.getId())); - return pointerElement.parents('list-items, item').length > 0; + return pointerElement.parents('[wlxml-class=list-items], [wlxml-class=item]').length > 0; } return false; };