From: Aleksander Ɓukasz Date: Sat, 14 Dec 2013 14:47:58 +0000 (+0100) Subject: editor: fix cursor out of text node handling Chrome hack X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/b823623776a1b3e1aee97fe071a3fc7a39ea35f3?ds=inline editor: fix cursor out of text node handling Chrome hack 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. --- diff --git a/src/editor/modules/documentCanvas/canvas/keyboard.js b/src/editor/modules/documentCanvas/canvas/keyboard.js index 0b876f0..603249e 100644 --- a/src/editor/modules/documentCanvas/canvas/keyboard.js +++ b/src/editor/modules/documentCanvas/canvas/keyboard.js @@ -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}); } });