68 lines
2.6 KiB
Vue
68 lines
2.6 KiB
Vue
|
|
<script setup lang="ts">
|
||
|
|
// Adding a part from the keyboard (Shift+A or Ctrl+K): a search box at the
|
||
|
|
// mouse; type a few letters, arrows to choose, Enter puts it right there.
|
||
|
|
import type { NodeKind } from '~/lib/logic/types'
|
||
|
|
|
||
|
|
const props = defineProps<{ x: number; y: number }>()
|
||
|
|
const emit = defineEmits<{ pick: [p: { kind: NodeKind; chip?: string }]; close: [] }>()
|
||
|
|
|
||
|
|
const { search } = useParts()
|
||
|
|
const query = ref('')
|
||
|
|
const at = ref(0)
|
||
|
|
const input = ref<HTMLInputElement>()
|
||
|
|
const el = ref<HTMLElement>()
|
||
|
|
const hits = computed(() => search(query.value, 10))
|
||
|
|
watch(query, () => (at.value = 0))
|
||
|
|
|
||
|
|
const pos = ref({ left: props.x, top: props.y })
|
||
|
|
onMounted(() => {
|
||
|
|
input.value?.focus()
|
||
|
|
const r = el.value!.getBoundingClientRect()
|
||
|
|
pos.value = { left: Math.max(8, Math.min(props.x, window.innerWidth - r.width - 8)), top: Math.max(8, Math.min(props.y, window.innerHeight - 380)) }
|
||
|
|
window.addEventListener('pointerdown', outside, true)
|
||
|
|
})
|
||
|
|
onBeforeUnmount(() => window.removeEventListener('pointerdown', outside, true))
|
||
|
|
|
||
|
|
function outside(e: Event) {
|
||
|
|
if (!el.value?.contains(e.target as Node)) emit('close')
|
||
|
|
}
|
||
|
|
|
||
|
|
function onKey(e: KeyboardEvent) {
|
||
|
|
e.stopPropagation()
|
||
|
|
const n = hits.value.length
|
||
|
|
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
||
|
|
e.preventDefault()
|
||
|
|
if (n) at.value = (at.value + (e.key === 'ArrowDown' ? 1 : -1) + n) % n
|
||
|
|
} else if (e.key === 'Enter') {
|
||
|
|
e.preventDefault()
|
||
|
|
const h = hits.value[at.value]
|
||
|
|
if (h) emit('pick', { kind: h.kind, chip: h.chip })
|
||
|
|
} else if (e.key === 'Escape') {
|
||
|
|
e.preventDefault()
|
||
|
|
emit('close')
|
||
|
|
}
|
||
|
|
}
|
||
|
|
</script>
|
||
|
|
|
||
|
|
<template>
|
||
|
|
<Teleport to="body">
|
||
|
|
<div ref="el" class="quick-add" :style="{ left: `${pos.left}px`, top: `${pos.top}px` }" @keydown="onKey">
|
||
|
|
<div class="search part-search">
|
||
|
|
<Icon name="lucide:plus" class="size-3.5 text-(--muted)" />
|
||
|
|
<input ref="input" v-model="query" placeholder="Add a part here…" />
|
||
|
|
</div>
|
||
|
|
<ul class="items">
|
||
|
|
<li v-for="(h, i) in hits" :key="h.chip ?? h.kind">
|
||
|
|
<button class="item" :class="{ active: i === at }" @pointerenter="at = i" @click="emit('pick', { kind: h.kind, chip: h.chip })">
|
||
|
|
<span v-if="h.kind === 'nand'" class="nand-badge">&</span>
|
||
|
|
<Icon v-else :name="h.icon" class="size-4 shrink-0 text-(--muted)" />
|
||
|
|
<span class="grow truncate">{{ h.name }}</span>
|
||
|
|
<span class="group-tag">{{ h.group }}</span>
|
||
|
|
</button>
|
||
|
|
</li>
|
||
|
|
</ul>
|
||
|
|
<p v-if="!hits.length" class="hint px-3 pb-2">Nothing called "{{ query.trim() }}" fits here.</p>
|
||
|
|
</div>
|
||
|
|
</Teleport>
|
||
|
|
</template>
|