177 lines
8.1 KiB
Vue
177 lines
8.1 KiB
Vue
<script setup lang="ts">
|
|
// The search box at the mouse (Space, /, Shift+A or Ctrl+K): parts to place
|
|
// right there, and things to do. Empty, it shows the parts used lately and
|
|
// the common actions; typing narrows both. Arrows choose, Enter takes it.
|
|
import { matchScore } from '~/lib/match'
|
|
import type { NodeKind } from '~/lib/logic/types'
|
|
|
|
const props = defineProps<{ x: number; y: number }>()
|
|
const emit = defineEmits<{ pick: [p: { kind: NodeKind; chip?: string }]; fit: []; close: [] }>()
|
|
|
|
const ed = useEditor()
|
|
const { search, recentParts } = useParts()
|
|
const query = ref('')
|
|
const at = ref(0)
|
|
const input = ref<HTMLInputElement>()
|
|
const el = ref<HTMLElement>()
|
|
|
|
interface Action {
|
|
id: string
|
|
label: string
|
|
icon: string
|
|
keys?: string
|
|
/** Other words it is found by. */
|
|
aka?: string
|
|
/** Only offered when this holds. */
|
|
when?: () => boolean
|
|
run: () => void
|
|
}
|
|
|
|
const ACTIONS: Action[] = [
|
|
{ id: 'new-comp', label: 'Create a new component', icon: 'lucide:square-plus', aka: 'new part chip block', run: () => ed.createComponent() },
|
|
{
|
|
id: 'package',
|
|
label: 'Make a component from the selection',
|
|
icon: 'lucide:package',
|
|
aka: 'group package extract',
|
|
when: () => ed.selection.value.length > 1,
|
|
run: () => ed.packageSelected('Part'),
|
|
},
|
|
{ id: 'find', label: 'Find anything in the project', icon: 'lucide:search', keys: 'Ctrl+F', aka: 'search where used', run: () => (ed.dialog.value = 'search') },
|
|
{ id: 'table', label: 'Truth table', icon: 'lucide:table', aka: 'test outputs', run: () => (ed.dialog.value = 'table') },
|
|
{ id: 'fit', label: 'Fit the circuit to the screen', icon: 'lucide:scan', keys: 'F', aka: 'zoom view', run: () => emit('fit') },
|
|
{ id: 'tidy', label: 'Tidy this circuit', icon: 'lucide:wand-sparkles', aka: 'wires clean route layout', when: () => ed.def.value.wires.length > 0, run: () => ed.tidyAll() },
|
|
{ id: 'tidy-all', label: 'Tidy every circuit', icon: 'lucide:sparkles', aka: 'wires clean route layout all components', run: () => ed.tidyEverything() },
|
|
{ id: 'examples', label: 'Add example parts', icon: 'lucide:book-open', aka: 'library not and or adder counter', run: () => ed.loadExamples() },
|
|
{ id: 'library', label: 'Library: components everyone published', icon: 'lucide:library-big', aka: 'public shared community download import browse', run: () => (ed.dialog.value = 'library') },
|
|
{
|
|
id: 'lib-publish',
|
|
label: 'Publish to the library',
|
|
icon: 'lucide:book-up',
|
|
aka: 'share upload public community',
|
|
when: () => !ed.libraryProblem.value && !ed.ownEntry(ed.def.value),
|
|
run: () => (ed.dialog.value = 'library-publish'),
|
|
},
|
|
{
|
|
id: 'lib-update',
|
|
label: 'Update in the library',
|
|
icon: 'lucide:book-up',
|
|
aka: 'publish share upload new version public',
|
|
when: () => !ed.libraryProblem.value && !!ed.ownEntry(ed.def.value),
|
|
run: () => (ed.dialog.value = 'library-publish'),
|
|
},
|
|
{ id: 'connector', label: 'New connector type', icon: 'lucide:plug', aka: 'cable hdmi rj45 bus port', when: () => ed.canPlaceKind('portin'), run: () => ed.addConnector() },
|
|
{ id: 'power', label: 'Power off and on', icon: 'lucide:power', aka: 'reset state memory bits clear zero restart', when: () => ed.circuitStats.value.sequential, run: () => ed.resetState() },
|
|
{ id: 'undo', label: 'Undo', icon: 'lucide:undo-2', keys: 'Ctrl+Z', when: () => ed.canUndo.value, run: () => ed.undo() },
|
|
{ id: 'print', label: 'Print or save as PDF', icon: 'lucide:printer', keys: 'Ctrl+P', aka: 'sheet export', run: () => ed.exportSheet('print') },
|
|
{ id: 'settings', label: 'Settings', icon: 'lucide:settings', aka: 'symbols sound wires options', run: () => (ed.dialog.value = 'settings') },
|
|
{ id: 'help', label: 'How to play', icon: 'lucide:circle-help', keys: '?', aka: 'help shortcuts keys', run: () => (ed.showHelp.value = true) },
|
|
{ id: 'menu', label: 'Menu: save, load, main menu', icon: 'lucide:menu', keys: 'Esc', aka: 'pause quit', run: () => ed.pause() },
|
|
]
|
|
|
|
type Item = { type: 'part'; id: string; kind: NodeKind; chip?: string; label: string; icon: string; note?: string } | { type: 'action'; id: string; act: Action; label: string; icon: string; note?: string }
|
|
|
|
const partItem = (p: { kind: NodeKind; chip?: string; name: string; icon: string }, note?: string): Item => ({
|
|
type: 'part',
|
|
id: `p:${p.chip ?? p.kind}`,
|
|
kind: p.kind,
|
|
chip: p.chip,
|
|
label: p.name,
|
|
icon: p.icon,
|
|
note,
|
|
})
|
|
const actItem = (a: Action): Item => ({ type: 'action', id: `a:${a.id}`, act: a, label: a.label, icon: a.icon, note: a.keys })
|
|
const offered = () => ACTIONS.filter(a => !a.when || a.when())
|
|
|
|
/** Sections to show: parts first, then actions. */
|
|
const sections = computed(() => {
|
|
const q = query.value.trim()
|
|
if (!q) {
|
|
const recent = recentParts.value.map(r => partItem(r, 'recent'))
|
|
const parts = recent.length ? recent : search('', 6).map(h => partItem(h, h.group))
|
|
return [
|
|
{ title: recent.length ? 'Recent parts' : 'Parts', items: parts },
|
|
{ title: 'Actions', items: offered().slice(0, 7).map(actItem) },
|
|
]
|
|
}
|
|
const parts = search(q, 8).map(h => partItem(h, h.group))
|
|
const acts = offered()
|
|
.map(a => ({ a, s: Math.max(matchScore(a.label, q.toLowerCase()), matchScore(a.aka ?? '', q.toLowerCase()) * 0.8) }))
|
|
.filter(x => x.s > 0)
|
|
.sort((x, y) => y.s - x.s)
|
|
.slice(0, 5)
|
|
.map(x => actItem(x.a))
|
|
return [
|
|
{ title: 'Parts', items: parts },
|
|
{ title: 'Actions', items: acts },
|
|
].filter(s => s.items.length)
|
|
})
|
|
const flat = computed(() => sections.value.flatMap(s => s.items))
|
|
const indexOf = (it: Item) => flat.value.indexOf(it)
|
|
watch(query, () => (at.value = 0))
|
|
|
|
function take(it: Item | undefined) {
|
|
if (!it) return
|
|
if (it.type === 'part') return emit('pick', { kind: it.kind, chip: it.chip })
|
|
emit('close')
|
|
it.act.run()
|
|
}
|
|
|
|
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 - 440)) }
|
|
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 = flat.value.length
|
|
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
|
e.preventDefault()
|
|
if (n) at.value = (at.value + (e.key === 'ArrowDown' ? 1 : -1) + n) % n
|
|
nextTick(() => el.value?.querySelector('.item.active')?.scrollIntoView({ block: 'nearest' }))
|
|
} else if (e.key === 'Enter') {
|
|
e.preventDefault()
|
|
take(flat.value[at.value])
|
|
} 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:search" class="size-3.5 text-(--muted)" />
|
|
<input ref="input" v-model="query" placeholder="Find a part or an action…" />
|
|
</div>
|
|
<div class="quick-list">
|
|
<template v-for="s in sections" :key="s.title">
|
|
<p class="quick-head">{{ s.title }}</p>
|
|
<ul class="items">
|
|
<li v-for="it in s.items" :key="it.id">
|
|
<button class="item" :class="{ active: indexOf(it) === at }" @pointerenter="at = indexOf(it)" @click="take(it)">
|
|
<span v-if="it.type === 'part' && it.kind === 'nand'" class="nand-badge">&</span>
|
|
<Icon v-else :name="it.icon" class="size-4 shrink-0" :class="it.type === 'action' ? 'text-(--accent)' : 'text-(--muted)'" />
|
|
<span class="grow truncate">{{ it.label }}</span>
|
|
<kbd v-if="it.type === 'action' && it.note">{{ it.note }}</kbd>
|
|
<span v-else-if="it.note" class="group-tag">{{ it.note }}</span>
|
|
</button>
|
|
</li>
|
|
</ul>
|
|
</template>
|
|
<p v-if="!flat.length" class="hint px-3 pb-2">Nothing called "{{ query.trim() }}" here.</p>
|
|
</div>
|
|
</div>
|
|
</Teleport>
|
|
</template>
|