197 lines
9.3 KiB
Vue
197 lines
9.3 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 and offers to open the circuits
|
|
// of that name. Arrows choose, Enter takes it, Shift+Enter opens a component.
|
|
import { matchScore } from '~/lib/match'
|
|
import { circuitsNamed, wayTo } from '~/lib/nav'
|
|
import type { ComponentDef, 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>()
|
|
const list = 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: 'connector', label: 'New connector type', icon: 'lucide:plug', aka: 'cable hdmi rj45 bus port', when: () => ed.canPlaceKind('portin'), run: () => ed.addConnector() },
|
|
{ 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 }
|
|
| { type: 'open'; id: string; def: string; label: string; icon: string; note?: string; here?: boolean }
|
|
|
|
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 })
|
|
// Any circuit can be opened, placeable here or not; the one you are in is only shown.
|
|
const openItem = (d: ComponentDef): Item =>
|
|
d.id === ed.def.value.id
|
|
? { type: 'open', id: `o:${d.id}`, def: d.id, label: d.name, icon: 'lucide:map-pin', note: 'you are here', here: true }
|
|
: { type: 'open', id: `o:${d.id}`, def: d.id, label: `Open ${d.name}`, icon: 'lucide:log-in' }
|
|
const offered = () => ACTIONS.filter(a => !a.when || a.when())
|
|
|
|
/** Sections to show: parts first, then circuits to open, 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))
|
|
const circuits = circuitsNamed(ed.project.value.components, q).map(openItem)
|
|
return [
|
|
{ title: 'Parts', items: parts },
|
|
{ title: 'Circuits', items: circuits },
|
|
{ title: 'Actions', items: acts },
|
|
].filter(s => s.items.length)
|
|
})
|
|
/** What the arrows walk through: everything but the circuit you are in. */
|
|
const flat = computed(() => sections.value.flatMap(s => s.items).filter(it => !(it.type === 'open' && it.here)))
|
|
const indexOf = (it: Item) => flat.value.indexOf(it)
|
|
watch(query, () => (at.value = 0))
|
|
|
|
/** Takes what was chosen; `edit` opens a component instead of placing it. */
|
|
function take(it: Item | undefined, edit = false) {
|
|
if (!it || (it.type === 'open' && it.here)) return
|
|
if (it.type === 'open') return go(it.def)
|
|
if (it.type === 'part' && it.chip && edit) return go(it.chip)
|
|
if (it.type === 'part') return emit('pick', { kind: it.kind, chip: it.chip })
|
|
emit('close')
|
|
it.act.run()
|
|
}
|
|
|
|
/** Into a circuit: back along the way when it is on it, else one step further in. */
|
|
function go(id: string) {
|
|
emit('close')
|
|
const way = wayTo(ed.nav.value, ed.mainId.value, id)
|
|
if (way === 'main') ed.goMain()
|
|
else if (way === 'open') ed.open(id)
|
|
else ed.goTo(way)
|
|
}
|
|
|
|
const pos = ref({ left: props.x, top: props.y })
|
|
onMounted(() => {
|
|
input.value?.focus()
|
|
const r = el.value!.getBoundingClientRect()
|
|
// Room below for the list at its longest, so typing never pushes the box off the window.
|
|
const tall = r.height - list.value!.offsetHeight + Math.min(420, window.innerHeight * 0.6)
|
|
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 - tall - 8)) }
|
|
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], e.shiftKey || e.ctrlKey || e.metaKey)
|
|
} 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, a circuit or an action…" />
|
|
</div>
|
|
<div ref="list" 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" class="comp-row">
|
|
<button
|
|
class="item"
|
|
:class="{ active: indexOf(it) === at }"
|
|
:disabled="it.type === 'open' && it.here"
|
|
@pointerenter="indexOf(it) >= 0 && (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 === 'part' ? 'text-(--muted)' : 'text-(--accent)'" />
|
|
<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>
|
|
<button v-if="it.type === 'part' && it.chip" class="open" title="Open it and edit inside (Shift+Enter)" @click="take(it, true)">
|
|
<Icon name="lucide:pencil" class="size-3.5" />
|
|
</button>
|
|
</li>
|
|
</ul>
|
|
</template>
|
|
<p v-if="!sections.length" class="hint px-3 pb-2">Nothing called "{{ query.trim() }}" here.</p>
|
|
</div>
|
|
<p class="quick-foot"><kbd>Enter</kbd> takes it, <kbd>Shift+Enter</kbd> opens a component to edit it.</p>
|
|
</div>
|
|
</Teleport>
|
|
</template>
|