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.
caretTo = false;
if(!element) {
// Chrome hack
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});