mirror of
https://github.com/open-webui/computer.git
synced 2026-11-22 21:50:43 -05:00
refac
This commit is contained in:
1 parent
f634471053
commit
762af9417b
3 files changed
+124
-34
No files matched your search
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { onMount, type Snippet } from 'svelte';
|
||||
import { onMount, tick, type Snippet } from 'svelte';
|
||||
import { tooltip } from '$lib/tooltip';
|
||||
import Icon from './Icon.svelte';
|
||||
import KeyPill from './KeyPill.svelte';
|
||||
@@ -10,6 +10,7 @@
|
||||
icon?: string;
|
||||
onclick: () => void;
|
||||
active?: boolean;
|
||||
highlighted?: boolean;
|
||||
divider?: boolean;
|
||||
/** Optional image URL shown instead of icon (e.g. avatar). */
|
||||
image?: string;
|
||||
@@ -53,6 +54,9 @@
|
||||
className?: string;
|
||||
/** Horizontal alignment relative to anchor: 'start' (left) or 'end' (right). */
|
||||
align?: 'start' | 'end';
|
||||
/** Scroll the active or highlighted item into the list viewport after render. */
|
||||
scrollActiveIntoView?: boolean;
|
||||
scrollActiveBlock?: ScrollLogicalPosition;
|
||||
}
|
||||
|
||||
let {
|
||||
@@ -71,10 +75,13 @@
|
||||
empty,
|
||||
children,
|
||||
className = '',
|
||||
align = 'start'
|
||||
align = 'start',
|
||||
scrollActiveIntoView = false,
|
||||
scrollActiveBlock = 'nearest'
|
||||
}: Props = $props();
|
||||
|
||||
let menuEl: HTMLDivElement | undefined = $state();
|
||||
let listEl: HTMLDivElement | undefined = $state();
|
||||
let pos = $state<{ x: number; top?: number; bottom?: number }>({ x: -9999, top: -9999 });
|
||||
let anchorWidth = $state(0);
|
||||
let menuMaxHeight = $state<number | undefined>();
|
||||
@@ -271,6 +278,14 @@
|
||||
anchorFrame = requestAnimationFrame(trackAnchor);
|
||||
}
|
||||
|
||||
function scrollMenuItemIntoView() {
|
||||
if (!listEl) return;
|
||||
const target =
|
||||
listEl.querySelector<HTMLElement>('[data-menu-highlighted="true"]') ??
|
||||
listEl.querySelector<HTMLElement>('[data-menu-active="true"]');
|
||||
target?.scrollIntoView({ block: scrollActiveBlock });
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
let dvhProbe: HTMLDivElement | undefined;
|
||||
let dvhObserver: ResizeObserver | undefined;
|
||||
@@ -320,6 +335,14 @@
|
||||
maxHeight;
|
||||
if (menuEl) scheduleSettledUpdates();
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (!scrollActiveIntoView || !ready) return;
|
||||
items;
|
||||
void tick().then(() => {
|
||||
scrollMenuItemIntoView();
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
||||
@@ -359,7 +382,11 @@
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="flex-1 min-h-0 overflow-y-auto" style={maxHeight ? `max-height: ${maxHeight};` : ''}>
|
||||
<div
|
||||
bind:this={listEl}
|
||||
class="flex-1 min-h-0 overflow-y-auto"
|
||||
style={maxHeight ? `max-height: ${maxHeight};` : ''}
|
||||
>
|
||||
{#if children}
|
||||
{@render children()}
|
||||
{:else if items.length === 0 && empty}
|
||||
@@ -370,9 +397,12 @@
|
||||
<div class="app-divider h-px mx-1 my-0.5"></div>
|
||||
{:else}
|
||||
<div
|
||||
class="group menu-row flex items-center gap-1 w-full h-6 rounded-xl text-xs transition-colors duration-75 {item.active
|
||||
class="group menu-row flex items-center gap-1 w-full h-6 rounded-xl text-xs transition-colors duration-75 {item.active ||
|
||||
item.highlighted
|
||||
? 'app-interactive-active'
|
||||
: ''}"
|
||||
data-menu-highlighted={item.highlighted ? 'true' : undefined}
|
||||
data-menu-active={item.active ? 'true' : undefined}
|
||||
>
|
||||
<button
|
||||
class="flex items-center gap-2 min-w-0 flex-1 h-full px-2 text-inherit"
|
||||
|
||||
@@ -1116,6 +1116,11 @@
|
||||
runSlashSuggestion(slashSuggestionIds[0]);
|
||||
}
|
||||
|
||||
async function focusAfterModelSelectorClose() {
|
||||
await tick();
|
||||
focus();
|
||||
}
|
||||
|
||||
// Allow sending during streaming (message will be enqueued server-side)
|
||||
const canSend = $derived(!!(inputText.trim() && selectedModel && !sending));
|
||||
</script>
|
||||
@@ -1589,7 +1594,12 @@
|
||||
{/if}
|
||||
</div>
|
||||
<div class="self-end mr-1 flex items-center gap-2">
|
||||
<ModelSelector bind:this={modelSelector} bind:selectedModel onchange={onsettingschange} />
|
||||
<ModelSelector
|
||||
bind:this={modelSelector}
|
||||
bind:selectedModel
|
||||
onchange={onsettingschange}
|
||||
onclose={focusAfterModelSelectorClose}
|
||||
/>
|
||||
<DictateButton
|
||||
ontext={(text) => {
|
||||
inputText += text;
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
nullable?: boolean;
|
||||
nullLabel?: string;
|
||||
onchange?: (model: string | null) => void;
|
||||
onclose?: () => void;
|
||||
}
|
||||
let {
|
||||
selectedModel = $bindable(),
|
||||
@@ -18,13 +19,15 @@
|
||||
align = 'end',
|
||||
nullable = false,
|
||||
nullLabel = 'Current model',
|
||||
onchange
|
||||
onchange,
|
||||
onclose
|
||||
}: Props = $props();
|
||||
|
||||
let btnEl: HTMLButtonElement | undefined = $state();
|
||||
let searchInputEl: HTMLInputElement | undefined = $state();
|
||||
let open = $state(false);
|
||||
let search = $state('');
|
||||
let highlightedIndex = $state(0);
|
||||
let isSmallViewport = $state(false);
|
||||
|
||||
const selectorMaxHeight = $derived(isSmallViewport ? '7.5rem' : '15rem');
|
||||
@@ -35,32 +38,37 @@
|
||||
: $chatModels
|
||||
);
|
||||
|
||||
const menuItems = $derived([
|
||||
...(nullable
|
||||
? [
|
||||
{
|
||||
label: nullLabel,
|
||||
tooltip: nullLabel,
|
||||
active: selectedModel === null || selectedModel === '',
|
||||
check: true,
|
||||
onclick: () => {
|
||||
selectedModel = null;
|
||||
onchange?.(null);
|
||||
const menuItems = $derived.by(() => {
|
||||
const items = [
|
||||
...(nullable
|
||||
? [
|
||||
{
|
||||
label: nullLabel,
|
||||
tooltip: nullLabel,
|
||||
active: selectedModel === null || selectedModel === '',
|
||||
check: true,
|
||||
onclick: () => {
|
||||
selectedModel = null;
|
||||
onchange?.(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
: []),
|
||||
...filtered.map((m) => ({
|
||||
label: m.name,
|
||||
tooltip: m.name,
|
||||
active: m.id === selectedModel,
|
||||
check: true,
|
||||
onclick: () => {
|
||||
selectedModel = m.id;
|
||||
onchange?.(m.id);
|
||||
}
|
||||
}))
|
||||
]);
|
||||
]
|
||||
: []),
|
||||
...filtered.map((m) => ({
|
||||
label: m.name,
|
||||
tooltip: m.name,
|
||||
active: m.id === selectedModel,
|
||||
check: true,
|
||||
onclick: () => {
|
||||
selectedModel = m.id;
|
||||
onchange?.(m.id);
|
||||
}
|
||||
}))
|
||||
];
|
||||
|
||||
const highlighted = Math.min(highlightedIndex, Math.max(items.length - 1, 0));
|
||||
return items.map((item, index) => ({ ...item, highlighted: index === highlighted }));
|
||||
});
|
||||
|
||||
function updateViewportSize() {
|
||||
isSmallViewport = (window.visualViewport?.width ?? window.innerWidth) < 640;
|
||||
@@ -83,20 +91,44 @@
|
||||
searchInputEl?.select();
|
||||
}
|
||||
|
||||
function selectedIndex() {
|
||||
if (nullable && (selectedModel === null || selectedModel === '')) return 0;
|
||||
const index = filtered.findIndex((m) => m.id === selectedModel);
|
||||
return index >= 0 ? index + (nullable ? 1 : 0) : 0;
|
||||
}
|
||||
|
||||
function resetHighlightedIndex() {
|
||||
const total = filtered.length + (nullable ? 1 : 0);
|
||||
highlightedIndex = total > 0 ? Math.min(selectedIndex(), total - 1) : 0;
|
||||
}
|
||||
|
||||
function moveHighlightedIndex(delta: number) {
|
||||
const total = menuItems.length;
|
||||
if (total === 0) return;
|
||||
highlightedIndex = (highlightedIndex + delta + total) % total;
|
||||
}
|
||||
|
||||
export async function openSelector() {
|
||||
if ($chatModels.length === 0 && !nullable) return;
|
||||
open = true;
|
||||
search = '';
|
||||
resetHighlightedIndex();
|
||||
await focusSearchInput();
|
||||
}
|
||||
|
||||
async function toggle() {
|
||||
if (open) {
|
||||
open = false;
|
||||
onclose?.();
|
||||
return;
|
||||
}
|
||||
await openSelector();
|
||||
}
|
||||
|
||||
function closeSelector() {
|
||||
open = false;
|
||||
onclose?.();
|
||||
}
|
||||
</script>
|
||||
|
||||
<span class="relative inline-flex {open ? 'z-[1001]' : ''}">
|
||||
@@ -132,11 +164,13 @@
|
||||
<DropdownMenu
|
||||
items={menuItems}
|
||||
anchor={btnEl}
|
||||
onclose={() => (open = false)}
|
||||
onclose={closeSelector}
|
||||
{preferAbove}
|
||||
forceAbove={preferAbove}
|
||||
maxHeight={selectorMaxHeight}
|
||||
className="w-52"
|
||||
scrollActiveIntoView
|
||||
scrollActiveBlock="center"
|
||||
{align}
|
||||
>
|
||||
{#snippet header()}
|
||||
@@ -154,11 +188,27 @@
|
||||
</svg>
|
||||
<input
|
||||
bind:this={searchInputEl}
|
||||
bind:value={search}
|
||||
value={search}
|
||||
placeholder={$t('modelSelector.search')}
|
||||
class="w-full bg-transparent text-[0.6875rem] text-gray-500 dark:text-gray-400 placeholder:text-gray-300 dark:placeholder:text-gray-600 outline-none"
|
||||
oninput={(e) => {
|
||||
search = e.currentTarget.value;
|
||||
resetHighlightedIndex();
|
||||
}}
|
||||
onkeydown={(e) => {
|
||||
if (e.key === 'Escape') open = false;
|
||||
if (e.key === 'Escape') {
|
||||
closeSelector();
|
||||
} else if (e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
moveHighlightedIndex(1);
|
||||
} else if (e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
moveHighlightedIndex(-1);
|
||||
} else if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
menuItems[Math.min(highlightedIndex, menuItems.length - 1)]?.onclick();
|
||||
closeSelector();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user