From: Aleksander Ɓukasz Date: Fri, 28 Jun 2013 11:30:27 +0000 (+0200) Subject: Wrapping in node text with subnodes X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/1b8f7d0e5974d649dff89f50f2a26783a29af5d7?ds=sidebyside Wrapping in node text with subnodes --- diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index cafed1a..c71950d 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -61,11 +61,37 @@ Canvas.prototype.nodeInsertAfter = function(options) { element.after(options.node.dom); }; +Canvas.prototype.nodeWrap2 = function(options) { + var container = $(this.content.find('#' + options.inside.getId()).get(0)); + + var containerContent = container.contents(); + var idx1 = Math.min.apply(Math, options.textNodeIdx); + var idx2 = Math.max.apply(Math, options.textNodeIdx); + var textNode1 = $(containerContent.get(idx1)); + var textNode2 = $(containerContent.get(idx2)); + + textNode1.after(options._with.dom); + textNode1.detach(); + textNode2.detach(); + + + options._with.dom.before(textNode1.text().substr(0, options.offsetStart)); + options._with.dom.append(textNode1.text().substr(options.offsetStart)); + for(var i = idx1 + 1; i < idx2; i++) { + options._with.dom.append(containerContent[i]); + } + options._with.dom.append(textNode2.text().substr(0, options.offsetEnd)); + options._with.dom.after(textNode2.text().substr(options.offsetEnd)); +}; + Canvas.prototype.nodeWrap = function(options) { options = _.extend({textNodeIdx: 0}, options); + + if(options.textNodeIdx instanceof Array) + return this.nodeWrap2(options); var element = $(this.content.find('#' + options.inside.getId()).get(0)); - + var elementContents = element.contents(); if(elementContents.length === 0 || elementContents.length - 1 < options.textNodeIdx || diff --git a/modules/documentCanvas/canvasManager.js b/modules/documentCanvas/canvasManager.js index aa37ff2..b7e7212 100644 --- a/modules/documentCanvas/canvasManager.js +++ b/modules/documentCanvas/canvasManager.js @@ -109,13 +109,20 @@ Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) { offsetStart = offsetEnd; offsetEnd = tmp; } + + 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, offsetEnd: offsetEnd, - textNodeIdx: parent.contents().index($(selection.anchorNode)) + textNodeIdx: textNodeIdx }); this.selectNode(wrapper, {movecaret: 'end'}); } diff --git a/modules/documentCanvas/tests/canvas.test.js b/modules/documentCanvas/tests/canvas.test.js index 5e44dd9..12fb8a5 100644 --- a/modules/documentCanvas/tests/canvas.test.js +++ b/modules/documentCanvas/tests/canvas.test.js @@ -69,6 +69,14 @@ define([ assertDomEqual(c.getContent(), '
Alice has a cat
'); }); + test('wrap text with nodes inside', function() { + var c = canvas.create('
Alice has a small cat
'); + var header = c.findNodes({tag: 'header'})[0]; + var wrapper = canvasNode.create({tag: 'aside'}); + c.nodeWrap({inside: header, _with: wrapper, offsetStart: 6, offsetEnd: 4, textNodeIdx: [0,2]}) + assertDomEqual(c.getContent(), '
Alice has a small cat
'); + }); + test('split node', function() { var c = canvas.create('
Header 1
'); var header = c.findNodes({tag: 'header'})[0];