From 5beaa5d89291a326425c921890462bb754c7fbb5 Mon Sep 17 00:00:00 2001 From: Aryan Kothari <87589047+thearyadev@users.noreply.github.com> Date: Tue, 23 Jul 2024 11:05:11 -0400 Subject: [PATCH 1/5] feat: select model with arrow keys (+enter) --- .../chat/ModelSelector/Selector.svelte | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/lib/components/chat/ModelSelector/Selector.svelte b/src/lib/components/chat/ModelSelector/Selector.svelte index e7ac15cee..c374b4598 100644 --- a/src/lib/components/chat/ModelSelector/Selector.svelte +++ b/src/lib/components/chat/ModelSelector/Selector.svelte @@ -17,6 +17,7 @@ import { getModels } from '$lib/apis'; import Tooltip from '$lib/components/common/Tooltip.svelte'; + import WebParams from '$lib/components/documents/Settings/WebParams.svelte'; const i18n = getContext('i18n'); const dispatch = createEventDispatcher(); @@ -43,6 +44,9 @@ let searchValue = ''; let ollamaVersion = null; + let pseudoSelectedIndex = 0; + let autoScrollTimeout; + $: filteredItems = items.filter( (item) => (searchValue @@ -239,10 +243,21 @@ placeholder={searchPlaceholder} autocomplete="off" on:keydown={(e) => { - if (e.code === 'Enter' && filteredItems.length > 0) { - value = filteredItems[0].value; + if (e.code === 'Enter') { + value = filteredItems[pseudoSelectedIndex].value; show = false; + return; // dont need to scroll on selection + } else if (e.code === 'ArrowDown') { + pseudoSelectedIndex = Math.min(pseudoSelectedIndex + 1, filteredItems.length - 1); + } else if (e.code === 'ArrowUp') { + pseudoSelectedIndex = Math.max(pseudoSelectedIndex - 1, 0); + } else { + // if the user types something, reset to the top selection. + pseudoSelectedIndex = 0; } + + const item = document.querySelector(`[data-pseudo-selected="true"]`); + item?.scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'instant' }); }} /> @@ -255,11 +270,13 @@