open-webui/src/lib/components/chat/MessageInput/Commands/Prompts.svelte

72 lines
1.9 KiB
Svelte
Raw Normal View History

2024-01-02 08:55:28 +00:00
<script lang="ts">
2025-09-12 16:31:57 +00:00
import Tooltip from '$lib/components/common/Tooltip.svelte';
2025-07-01 11:57:01 +00:00
import { tick, getContext, onMount, onDestroy } from 'svelte';
2024-03-01 09:18:07 +00:00
import { toast } from 'svelte-sonner';
2024-01-02 08:55:28 +00:00
const i18n = getContext('i18n');
2025-09-12 16:31:57 +00:00
export let query = '';
export let prompts = [];
export let onSelect = (e) => {};
2024-08-23 12:31:39 +00:00
let selectedPromptIdx = 0;
2025-09-12 16:31:57 +00:00
export let filteredItems = [];
2024-01-02 08:55:28 +00:00
2025-09-12 16:31:57 +00:00
$: filteredItems = prompts
.filter((p) => p.command.toLowerCase().includes(query.toLowerCase()))
2024-01-02 09:12:49 +00:00
.sort((a, b) => a.title.localeCompare(b.title));
2024-01-02 08:55:28 +00:00
2025-09-12 16:31:57 +00:00
$: if (query) {
2024-08-23 12:31:39 +00:00
selectedPromptIdx = 0;
2024-01-02 08:55:28 +00:00
}
export const selectUp = () => {
2024-08-23 12:31:39 +00:00
selectedPromptIdx = Math.max(0, selectedPromptIdx - 1);
2024-01-02 08:55:28 +00:00
};
export const selectDown = () => {
2025-09-12 16:31:57 +00:00
selectedPromptIdx = Math.min(selectedPromptIdx + 1, filteredItems.length - 1);
2024-01-02 08:55:28 +00:00
};
2025-09-12 16:31:57 +00:00
export const select = async () => {
const command = filteredItems[selectedPromptIdx];
if (command) {
onSelect({ type: 'prompt', data: command });
2025-07-01 11:57:01 +00:00
}
};
2024-01-02 08:55:28 +00:00
</script>
2025-09-12 16:31:57 +00:00
<div class="px-2 text-xs text-gray-500 py-1">
{$i18n.t('Prompts')}
</div>
{#if filteredItems.length > 0}
<div class=" space-y-0.5 scrollbar-hidden">
{#each filteredItems as promptItem, promptIdx}
<Tooltip content={promptItem.title} placement="top-start">
<button
class=" px-3 py-1 rounded-xl w-full text-left {promptIdx === selectedPromptIdx
? ' bg-gray-50 dark:bg-gray-800 selected-command-option-button'
: ''} truncate"
type="button"
on:click={() => {
onSelect({ type: 'prompt', data: promptItem });
}}
on:mousemove={() => {
selectedPromptIdx = promptIdx;
}}
on:focus={() => {}}
data-selected={promptIdx === selectedPromptIdx}
2024-01-03 05:35:47 +00:00
>
2025-09-12 16:31:57 +00:00
<span class=" font-medium text-black dark:text-gray-100">
{promptItem.command}
</span>
<span class=" text-xs text-gray-600 dark:text-gray-100">
{promptItem.title}
</span>
</button>
</Tooltip>
{/each}
2024-01-02 08:55:28 +00:00
</div>
{/if}