editor: fix cursor out of text node handling Chrome hack
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sat, 14 Dec 2013 14:47:58 +0000 (15:47 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 15 Dec 2013 21:32:51 +0000 (22:32 +0100)
The case where there were no more text nodes above/below was
not handled correctly. Now, if that happens, we revert in the
opposite direction, ideally to place where we started.

This might be better handled at the keydown phase.

src/editor/modules/documentCanvas/canvas/keyboard.js

index 0b876f0..603249e 100644 (file)
@@ -139,18 +139,24 @@ handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARRO
             caretTo = false;
         if(!element) {
             // Chrome hack
-            var direction;
-            if(event.which === KEYS.ARROW_LEFT  || event.which === KEYS.ARROW_UP) {
-                direction = 'above';
-                caretTo = 'end';
-            } else {
-                direction = 'below';
-                caretTo = 'start';
+
+            var moves = [{direction: 'above', caretTo: 'end'}, {direction: 'below', caretTo: 'start'}];
+            if(event.which === KEYS.ARROW_RIGHT  || event.which === KEYS.ARROW_DOWN) {
+                moves.reverse();
             }
-            /* globals window */
-            element = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]:visible', direction, window.getSelection().focusNode));
+            moves.some(function(move) {
+                /* globals window */
+                var targetNode = utils.nearestInDocumentOrder('[document-text-element]:visible', move.direction, window.getSelection().focusNode);
+                if(targetNode) {
+                    element = canvas.getDocumentElement(targetNode);
+                    caretTo = move.caretTo;
+                    return true; // break
+                }
+            });
+        }
+        if(element) {
+            canvas.setCurrentElement(element, {caretTo: caretTo});
         }
-        canvas.setCurrentElement(element, {caretTo: caretTo});
     }
 });