Breaking node at beginning/end
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 16 Jul 2013 12:10:16 +0000 (14:10 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 16 Jul 2013 12:10:48 +0000 (14:10 +0200)
modules/documentCanvas/canvas/canvas.js
modules/documentCanvas/canvas/canvas.test3.js

index f80e961..88a8839 100644 (file)
@@ -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
         }
     }
 })
index 43c9083..edc81ee 100644 (file)
@@ -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() {