From 76e3bcaa4206423c4be4770736e6b8f84940b248 Mon Sep 17 00:00:00 2001 From: Griffin T Date: Mon, 9 Nov 2020 15:34:56 +0800 Subject: [PATCH] Keep cursor at the end of line when going through history --- resources/scripts/components/server/Console.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/resources/scripts/components/server/Console.tsx b/resources/scripts/components/server/Console.tsx index abf423d34..c14354a75 100644 --- a/resources/scripts/components/server/Console.tsx +++ b/resources/scripts/components/server/Console.tsx @@ -80,12 +80,16 @@ export default () => { TERMINAL_PRELUDE + 'Server marked as ' + state + '...\u001b[0m', ); - const handleCommandKeydown = (e: React.KeyboardEvent) => { + const handleCommandKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'ArrowUp') { const newIndex = Math.min(historyIndex + 1, history!.length - 1); setHistoryIndex(newIndex); e.currentTarget.value = history![newIndex] || ''; + + // By default up arrow will also bring the cursor to the start of the line, + // so we'll preventDefault to keep it at the end. + e.preventDefault(); } if (e.key === 'ArrowDown') { @@ -185,7 +189,7 @@ export default () => { type={'text'} disabled={!instance || !connected} css={tw`bg-transparent text-neutral-100 p-2 pl-0 w-full`} - onKeyDown={e => handleCommandKeydown(e)} + onKeyDown={handleCommandKeyDown} />