diff --git a/zenno/src/lib/CharaPick.svelte b/zenno/src/lib/CharaPick.svelte index bcc330d..7956be0 100644 --- a/zenno/src/lib/CharaPick.svelte +++ b/zenno/src/lib/CharaPick.svelte @@ -2,7 +2,7 @@ import type { Character } from '$lib/data/character'; import type { Snippet } from 'svelte'; import type { ClassValue } from 'svelte/elements'; - import { stringsearch } from './stringsearch'; + import Pick from './Pick.svelte'; interface Props { id: string; @@ -15,139 +15,9 @@ let { id, characters, value = $bindable(), class: className, option, required = false }: Props = $props(); - let popover: HTMLElement | undefined = $state(); - let optionsContainer: HTMLElement | undefined = $state(); - const expanded = $derived(!popover?.hidden); - - let search = $state(''); - const charas = $derived(characters ?? []); - const searchedCharas = $derived(stringsearch(search, charas, ({ name }) => name) || charas); - $effect(() => { - if (required && value == null && charas.length > 0) { - value = charas[0]; - } - }); - - function setByElem(o: HTMLElement) { - const j = parseInt(o.dataset.charaId ?? ''); - value = charas.find((c) => c.chara_id === j) ?? value; - o.focus(); - } - - function onkeydown(evt: KeyboardEvent) { - switch (evt.key) { - case 'ArrowDown': { - const opts = [...optionsContainer!.children] as HTMLElement[]; - const i = opts.findIndex((n) => parseInt(n.dataset.charaId ?? '') === value?.chara_id); - const o = i != null && i >= 0 ? opts[Math.min(i + 1, opts.length - 1)] : opts[0]; - setByElem(o); - break; - } - case 'ArrowUp': { - const opts = [...optionsContainer!.children] as HTMLElement[]; - const i = opts.findIndex((n) => parseInt(n.dataset.charaId ?? '') === value?.chara_id); - const o = i != null && i > 0 ? opts[i - 1] : opts[0]; - setByElem(o); - break; - } - case 'Home': { - search = ''; - setByElem(optionsContainer!.children[0] as HTMLElement); - break; - } - case 'End': { - search = ''; - setByElem(optionsContainer!.children[optionsContainer!.childElementCount - 1] as HTMLElement); - break; - } - case ' ': - popover!.showPopover(); - break; - case 'Escape': - // TODO(zeph): restore chara that was selected when the popup opened? - // for now just hide - popover!.hidePopover(); - break; - case 'Enter': - popover!.togglePopover(); - break; - default: - return; - } - evt.preventDefault(); - } + const key = (v: Character) => ({id: v.chara_id, name: v.name}); -