From: Aleksander Ɓukasz Date: Tue, 16 Jul 2013 12:10:16 +0000 (+0200) Subject: Breaking node at beginning/end X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/5c2e36d7dbb5d7437b70183f87fb68ccb6c5d866 Breaking node at beginning/end --- diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index f80e961..88a8839 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -74,8 +74,19 @@ $.extend(Canvas.prototype, { e.preventDefault(); var cursor = canvas.getCursor(); if(!cursor.isSelecting()) { - var position = cursor.getPosition(); - position.element.split({offset: position.offset}); + var position = cursor.getPosition(), + elements = position.element.split({offset: position.offset}), + newEmpty, + goto; + + if(position.offsetAtBeginning) + newEmpty = elements.first; + else if(position.offsetAtEnd) + newEmpty = elements.second; + if(newEmpty) { + goto = newEmpty.append(documentElement.DocumentTextElement.create({text: '\u200B'}, this)); + canvas.setCurrentElement(goto); + } } } }); @@ -425,7 +436,9 @@ $.extend(Cursor.prototype, { if(which === 'anchor') { return { element: anchorElement, - offset: selection.anchorOffset + offset: selection.anchorOffset, + offsetAtBeginning: selection.anchorOffset === 0, + offsetAtEnd: anchorElement && anchorElement.getText().length === selection.anchorOffset }; } @@ -467,7 +480,9 @@ $.extend(Cursor.prototype, { return { element: element, - offset: offset + offset: offset, + offsetAtBeginning: offset === 0, + offsetAtEnd: element.getText().length === offset } } }) diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 43c9083..edc81ee 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -980,6 +980,17 @@ describe('Canvas', function() { expect(cursor.isSelecting()).to.equal(false, 'cursor is not selecting anything'); expect(position.element.getText()).to.equal('Alice has a cat'); expect(position.offset).to.equal(5); + expect(position.offsetAtEnd).to.equal(false, 'offset is not at end'); + + getSelection.returns({ + anchorNode: text, + focusNode: text, + anchorOffset: 15, + focusOffset: 15, + isCollapsed: true + }); + + expect(cursor.getPosition().offsetAtEnd).to.equal(true, 'offset at end'); }); it('returns boundries of selection when browser selection not collapsed', function() {