logigate/app/components/editor/SearchDialog.vue

121 lines
4.4 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
// Find anything in the project (Ctrl+F): parts in every circuit, and
// components with every place they are used. Enter or a click goes there.
import { searchProject, type PartHit } from '~/lib/logic/search'
const ed = useEditor()
const query = ref('')
const at = ref(0)
const input = ref<HTMLInputElement>()
onMounted(() => input.value?.focus())
const found = computed(() => searchProject(ed.project.value, query.value))
/** Every result in list order, for the arrow keys. */
const flat = computed(() => [
...found.value.components.map(c => ({ go: () => ed.open(c.def.id) })),
...found.value.parts.map(p => ({ go: () => goPart(p) })),
])
watch(query, () => (at.value = 0))
function close() {
ed.dialog.value = null
}
/** Opens the circuit it is in (unless it is open) and centres on it. */
function goTo(defId: string, nodeId: string) {
close()
if (ed.def.value.id !== defId) ed.open(defId)
nextTick(() => ed.focusNode(nodeId))
}
function goPart(p: PartHit) {
goTo(p.def, p.node)
}
function onKey(e: KeyboardEvent) {
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
e.preventDefault()
const n = flat.value.length
if (n) at.value = (at.value + (e.key === 'ArrowDown' ? 1 : -1) + n) % n
} else if (e.key === 'Enter') {
e.preventDefault()
const hit = flat.value[at.value]
if (hit) {
close()
hit.go()
}
} else if (e.key === 'Escape') close()
}
const ICONS: Record<string, string> = {
chip: 'lucide:cpu',
nand: 'lucide:square-function',
input: 'lucide:toggle-left',
output: 'lucide:log-out',
lamp: 'lucide:lightbulb',
button: 'lucide:circle-dot',
clock: 'lucide:audio-waveform',
panel: 'lucide:sliders-vertical',
hex: 'lucide:binary',
note: 'lucide:sticky-note',
bundle: 'lucide:merge',
split: 'lucide:split',
tag: 'lucide:flag',
ref: 'lucide:tag',
pixel: 'lucide:square',
}
</script>
<template>
<div class="modal-backdrop search-backdrop" @click.self="close">
<div class="modal search-modal" role="dialog" aria-label="Search the project" @keydown="onKey">
<div class="search-field">
<Icon name="lucide:search" class="size-4 shrink-0" />
<input ref="input" v-model="query" placeholder="Find a part, pin, signal or component…" />
<kbd>Esc</kbd>
</div>
<p v-if="!query.trim()" class="hint search-empty">Names, kinds (lamp, NAND), signals, panel rows and note text, in every circuit of this project.</p>
<p v-else-if="!flat.length" class="hint search-empty">Nothing called "{{ query.trim() }}".</p>
<div v-else class="search-results">
<template v-if="found.components.length">
<h3>Components</h3>
<div
v-for="(c, i) in found.components"
:key="c.def.id"
class="search-hit"
:class="{ active: at === i }"
@pointerenter="at = i"
>
<button class="search-main" @click="close(), ed.open(c.def.id)">
<Icon name="lucide:package" class="size-4 shrink-0" />
<span class="grow truncate">{{ c.def.name }}</span>
<span class="search-where">{{ c.usedIn.length ? `used in ${c.usedIn.length} circuit${c.usedIn.length === 1 ? '' : 's'}` : 'not used in another circuit' }}</span>
</button>
<div v-if="c.usedIn.length" class="search-uses">
<button v-for="u in c.usedIn" :key="u.def.id" class="btn-sm" :title="`Open ${u.def.name} at the first one`" @click="goTo(u.def.id, u.nodes[0]!)">
{{ u.def.name }}<span v-if="u.nodes.length > 1" class="muted"> ×{{ u.nodes.length }}</span>
</button>
</div>
</div>
</template>
<template v-if="found.parts.length">
<h3>Parts</h3>
<button
v-for="(p, i) in found.parts"
:key="`${p.def}/${p.node}`"
class="search-hit search-main"
:class="{ active: at === found.components.length + i }"
@pointerenter="at = found.components.length + i"
@click="goPart(p)"
>
<Icon :name="ICONS[p.kind] ?? 'lucide:box'" class="size-4 shrink-0" />
<span class="grow truncate">{{ p.text }}<span v-if="p.detail" class="muted"> · {{ p.detail }}</span></span>
<span class="search-where">{{ p.def === ed.def.value.id ? 'here' : p.defName }}</span>
</button>
</template>
</div>
</div>
</div>
</template>