From: Aleksander Ɓukasz Date: Fri, 28 Jun 2013 10:54:05 +0000 (+0200) Subject: Fixing wrapping text when text not in first node of its parent X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/ee83d407018193482c72fdc57a8cfea17d38127a Fixing wrapping text when text not in first node of its parent --- diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index 7d75e9e..cafed1a 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -62,12 +62,16 @@ 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 !== 1 || elementContents.get(0).nodeType != 3) + if(elementContents.length === 0 || + elementContents.length - 1 < options.textNodeIdx || + elementContents.get(options.textNodeIdx).nodeType != 3) return false; - var textElement = elementContents.get(0); + var textElement = elementContents.get(options.textNodeIdx); var prefix = textElement.data.substr(0, options.offsetStart); var suffix = textElement.data.substr(options.offsetEnd); diff --git a/modules/documentCanvas/canvasManager.js b/modules/documentCanvas/canvasManager.js index 5edd4be..aa37ff2 100644 --- a/modules/documentCanvas/canvasManager.js +++ b/modules/documentCanvas/canvasManager.js @@ -110,10 +110,12 @@ Manager.prototype.insertNewNode = function(wlxmlTag, wlxmlClass) { offsetEnd = tmp; } var wrapper = canvasNode.create({tag: wlxmlTag, klass: wlxmlClass}); - this.canvas.nodeWrap({inside: canvasNode.create($(selection.anchorNode).parent()), + var parent = $(selection.anchorNode).parent(); + this.canvas.nodeWrap({inside: canvasNode.create(parent), _with: wrapper, offsetStart: offsetStart, - offsetEnd: offsetEnd + offsetEnd: offsetEnd, + textNodeIdx: parent.contents().index($(selection.anchorNode)) }); this.selectNode(wrapper, {movecaret: 'end'}); } diff --git a/modules/documentCanvas/tests/canvas.test.js b/modules/documentCanvas/tests/canvas.test.js index d336192..5e44dd9 100644 --- a/modules/documentCanvas/tests/canvas.test.js +++ b/modules/documentCanvas/tests/canvas.test.js @@ -61,6 +61,14 @@ define([ assertDomEqual(c.getContent(), '
Header 1
'); }); + test('wrap text in node - text not a first node', function() { + var c = canvas.create('
Alice has a cat
'); + var header = c.findNodes({tag: 'header'})[0]; + var wrapper = canvasNode.create({tag: 'aside'}); + c.nodeWrap({inside: header, _with: wrapper, offsetStart: 1, offsetEnd: 4, textNodeIdx: 2}); + assertDomEqual(c.getContent(), '
Alice has a cat
'); + }); + test('split node', function() { var c = canvas.create('
Header 1
'); var header = c.findNodes({tag: 'header'})[0];