From 3c9f6963b81141ac8560c25288697aa060480379 Mon Sep 17 00:00:00 2001 From: Gnome Ann <> Date: Mon, 23 Aug 2021 18:00:35 -0400 Subject: [PATCH] Allow using left and right arrow keys to move between chunks --- static/application.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/static/application.js b/static/application.js index 0b89b4c4..4a8453b6 100644 --- a/static/application.js +++ b/static/application.js @@ -635,11 +635,45 @@ function chunkOnKeyDown(event) { if(event.keyCode == 27 || (!event.shiftKey && event.keyCode == 13)) { setTimeout(function () { event.target.blur(); - }, 5); + }, 25); event.preventDefault(); return; } + // Allow left and right arrow keys to move between chunks + switch(event.keyCode) { + case 37: // left + case 39: // right + old_selection_offset = getSelection().focusOffset; + setTimeout(function () { + // Wait a few milliseconds and check if the caret has moved + new_selection = getSelection(); + if(old_selection_offset != new_selection.focusOffset) { + return; + } + // If it hasn't moved, we're at the beginning or end of a chunk + // and the caret must be moved to a different chunk + chunk = document.activeElement; + switch(event.keyCode) { + case 37: // left + if((chunk = chunk.previousSibling) && chunk.tagName == "CHUNK") { + range = document.createRange(); + range.selectNodeContents(chunk); + range.collapse(false); + new_selection.removeAllRanges(); + new_selection.addRange(range); + } + break; + + case 39: // right + if((chunk = chunk.nextSibling) && chunk.tagName == "CHUNK") { + chunk.focus(); + } + } + }, 2); + return; + } + // Don't allow any edits if not connected to server if(!connected) { event.preventDefault(); @@ -674,7 +708,7 @@ function submitEditedChunk(event) { if(document.activeElement.tagName == "CHUNK") { document.activeElement.blur(); } - }, 5); + }, 25); current_chunk_changed = false; // Enter edit mode if we aren't already in edit mode