X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/ee83d407018193482c72fdc57a8cfea17d38127a..84eba72af147a175fb93b7305c1d42b0a4987a8c:/modules/documentCanvas/canvas.js?ds=sidebyside diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index cafed1a..23cdf0a 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -63,24 +63,38 @@ Canvas.prototype.nodeInsertAfter = function(options) { Canvas.prototype.nodeWrap = function(options) { options = _.extend({textNodeIdx: 0}, options); - - var element = $(this.content.find('#' + options.inside.getId()).get(0)); - - var elementContents = element.contents(); - if(elementContents.length === 0 || - elementContents.length - 1 < options.textNodeIdx || - elementContents.get(options.textNodeIdx).nodeType != 3) - return false; - var textElement = elementContents.get(options.textNodeIdx); - - 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); + 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) {