206 lines
8.5 KiB
Vue
206 lines
8.5 KiB
Vue
|
|
<script setup lang="ts">
|
||
|
|
// The floating bar at the bottom of the canvas, like Figma's toolbar: one
|
||
|
|
// button per kind of part. Pointing at one opens its parts above it; click
|
||
|
|
// a part to place it (or drag it onto the canvas), pin it to keep it on the
|
||
|
|
// left. Components and your connector types have their own buttons.
|
||
|
|
import { shortCount } from '~/lib/logic/analyze'
|
||
|
|
import { BASIC_GROUPS } from '~/composables/useParts'
|
||
|
|
import type { ComponentDef, NodeKind } from '~/lib/logic/types'
|
||
|
|
|
||
|
|
const ed = useEditor()
|
||
|
|
const { place, dragStart, dragEnd, isPinned, togglePin } = usePlace()
|
||
|
|
const { componentGroups } = useParts()
|
||
|
|
|
||
|
|
const ICONS: Record<string, string> = {
|
||
|
|
Logic: 'lucide:square-function',
|
||
|
|
Inputs: 'lucide:toggle-left',
|
||
|
|
Outputs: 'lucide:lightbulb',
|
||
|
|
Wiring: 'lucide:spline',
|
||
|
|
Notes: 'lucide:sticky-note',
|
||
|
|
}
|
||
|
|
|
||
|
|
type Cat = { id: string; label: string; icon: string }
|
||
|
|
const cats = computed<Cat[]>(() => [
|
||
|
|
...BASIC_GROUPS.map(g => ({ id: g.name, label: g.name, icon: ICONS[g.name] ?? 'lucide:shapes' })),
|
||
|
|
{ id: 'connectors', label: 'Connectors', icon: 'lucide:plug' },
|
||
|
|
{ id: 'components', label: 'Components', icon: 'lucide:boxes' },
|
||
|
|
])
|
||
|
|
|
||
|
|
// ── Flyout: opens on pointing, stays while the pointer is over it ──
|
||
|
|
const open = ref<string | null>(null)
|
||
|
|
const anchor = ref(0)
|
||
|
|
let timer: ReturnType<typeof setTimeout> | undefined
|
||
|
|
|
||
|
|
function show(id: string, e: Event) {
|
||
|
|
clearTimeout(timer)
|
||
|
|
const el = e.currentTarget as HTMLElement
|
||
|
|
const go = () => {
|
||
|
|
open.value = id
|
||
|
|
anchor.value = el.offsetLeft + el.offsetWidth / 2
|
||
|
|
}
|
||
|
|
// Moving along the bar switches at once; the first one waits a moment.
|
||
|
|
if (open.value) go()
|
||
|
|
else timer = setTimeout(go, 110)
|
||
|
|
}
|
||
|
|
|
||
|
|
function leave() {
|
||
|
|
clearTimeout(timer)
|
||
|
|
timer = setTimeout(() => (open.value = null), 220)
|
||
|
|
}
|
||
|
|
|
||
|
|
function stay() {
|
||
|
|
clearTimeout(timer)
|
||
|
|
}
|
||
|
|
|
||
|
|
function toggle(id: string, e: Event) {
|
||
|
|
if (open.value === id) open.value = null
|
||
|
|
else {
|
||
|
|
clearTimeout(timer)
|
||
|
|
const el = e.currentTarget as HTMLElement
|
||
|
|
open.value = id
|
||
|
|
anchor.value = el.offsetLeft + el.offsetWidth / 2
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function pick(kind: NodeKind, chip?: string, conn?: string) {
|
||
|
|
place(kind, chip, conn)
|
||
|
|
open.value = null
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Which button the part being placed belongs to, to show it as active. */
|
||
|
|
const activeCat = computed(() => {
|
||
|
|
const p = ed.placing.value
|
||
|
|
if (!p) return null
|
||
|
|
if (p.kind === 'chip') return 'components'
|
||
|
|
if (p.kind === 'portin' || p.kind === 'portout') return 'connectors'
|
||
|
|
return BASIC_GROUPS.find(g => g.parts.some(x => x.kind === p.kind))?.name ?? null
|
||
|
|
})
|
||
|
|
|
||
|
|
const group = computed(() => BASIC_GROUPS.find(g => g.name === open.value))
|
||
|
|
const connectors = computed(() => ed.project.value.connectors ?? [])
|
||
|
|
const portsOk = computed(() => ed.canPlaceKind('portin'))
|
||
|
|
const placeable = (d: ComponentDef) => d.id !== ed.def.value.id && ed.canPlace(d.id)
|
||
|
|
|
||
|
|
function onKey(e: KeyboardEvent) {
|
||
|
|
if (e.key === 'Escape' && open.value) {
|
||
|
|
open.value = null
|
||
|
|
e.stopPropagation()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
onMounted(() => window.addEventListener('keydown', onKey, true))
|
||
|
|
onBeforeUnmount(() => window.removeEventListener('keydown', onKey, true))
|
||
|
|
</script>
|
||
|
|
|
||
|
|
<template>
|
||
|
|
<div class="bottom-dock" @pointerdown.stop>
|
||
|
|
<Transition name="flyout">
|
||
|
|
<div v-if="open" class="flyout" :style="{ left: `${anchor}px` }" @pointerenter="stay" @pointerleave="leave">
|
||
|
|
<!-- Built-in parts -->
|
||
|
|
<template v-if="group">
|
||
|
|
<p class="flyout-head">{{ group.name }}</p>
|
||
|
|
<ul class="flyout-list">
|
||
|
|
<li v-for="p in group.parts" :key="p.kind">
|
||
|
|
<button
|
||
|
|
class="flyout-item"
|
||
|
|
:class="{ blocked: !ed.canPlaceKind(p.kind), active: ed.placing.value?.kind === p.kind && !ed.placing.value?.chip }"
|
||
|
|
:title="p.hint"
|
||
|
|
draggable="true"
|
||
|
|
@click="pick(p.kind)"
|
||
|
|
@dragstart="dragStart($event, p.kind)"
|
||
|
|
@dragend="dragEnd"
|
||
|
|
>
|
||
|
|
<span v-if="p.kind === 'nand'" class="nand-badge">&</span>
|
||
|
|
<Icon v-else :name="p.icon" class="size-4 shrink-0" />
|
||
|
|
<span class="grow truncate">{{ p.name }}</span>
|
||
|
|
<kbd v-if="p.key">{{ p.key }}</kbd>
|
||
|
|
</button>
|
||
|
|
<button class="pin-btn" :class="{ on: isPinned(p.kind) }" :title="isPinned(p.kind) ? 'Unpin from the left' : 'Pin to the left, to have it at hand'" @click="togglePin(p.kind)">
|
||
|
|
<Icon :name="isPinned(p.kind) ? 'lucide:pin-off' : 'lucide:pin'" class="size-3.5" />
|
||
|
|
</button>
|
||
|
|
</li>
|
||
|
|
</ul>
|
||
|
|
</template>
|
||
|
|
|
||
|
|
<!-- Connector types -->
|
||
|
|
<template v-else-if="open === 'connectors'">
|
||
|
|
<p class="flyout-head">Connectors</p>
|
||
|
|
<p v-if="!connectors.length" class="flyout-empty">Make a cable type of your own: HDMI, RJ45, a bus. It gets an Out and an In port.</p>
|
||
|
|
<ul class="flyout-list">
|
||
|
|
<li v-for="c in connectors" :key="c.id">
|
||
|
|
<span class="flyout-item static">
|
||
|
|
<span class="conn-swatch" :class="c.color ? `c-${c.color}` : ''" />
|
||
|
|
<span class="grow truncate">{{ c.name }}</span>
|
||
|
|
<span class="flyout-note">{{ c.lines.length }} lines</span>
|
||
|
|
</span>
|
||
|
|
<button
|
||
|
|
v-for="side in ['portout', 'portin'] as const"
|
||
|
|
:key="side"
|
||
|
|
class="btn-sm"
|
||
|
|
:disabled="!portsOk"
|
||
|
|
draggable="true"
|
||
|
|
:title="side === 'portout' ? `Where a ${c.name} cable leaves this circuit` : `Where a ${c.name} cable comes in`"
|
||
|
|
@click="pick(side, undefined, c.id)"
|
||
|
|
@dragstart="dragStart($event, side, undefined, c.id)"
|
||
|
|
@dragend="dragEnd"
|
||
|
|
>
|
||
|
|
{{ side === 'portout' ? 'Out' : 'In' }}
|
||
|
|
</button>
|
||
|
|
<button class="pin-btn" title="Edit its name, colour and lines" @click="ed.editConnector(c.id), (open = null)"><Icon name="lucide:pencil" class="size-3.5" /></button>
|
||
|
|
</li>
|
||
|
|
</ul>
|
||
|
|
<button class="flyout-new" :disabled="!portsOk" @click="ed.addConnector(), (open = null)"><Icon name="lucide:plus" class="size-4" /> New connector type</button>
|
||
|
|
</template>
|
||
|
|
|
||
|
|
<!-- Components, by group -->
|
||
|
|
<template v-else-if="open === 'components'">
|
||
|
|
<div class="flyout-scroll">
|
||
|
|
<template v-for="g in componentGroups" :key="g.name">
|
||
|
|
<p class="flyout-head">{{ g.name }}</p>
|
||
|
|
<ul class="flyout-list">
|
||
|
|
<li v-for="d in g.defs" :key="d.id">
|
||
|
|
<button
|
||
|
|
class="flyout-item"
|
||
|
|
:class="{ blocked: !placeable(d), active: ed.placing.value?.chip === d.id }"
|
||
|
|
:title="d.id === ed.def.value.id ? 'You are editing this one' : d.description || ''"
|
||
|
|
:draggable="placeable(d)"
|
||
|
|
@click="pick('chip', d.id)"
|
||
|
|
@dragstart="dragStart($event, 'chip', d.id)"
|
||
|
|
@dragend="dragEnd"
|
||
|
|
>
|
||
|
|
<Icon name="lucide:box" class="size-4 shrink-0" />
|
||
|
|
<span class="grow truncate">{{ d.name }}</span>
|
||
|
|
<span v-if="ed.transistors(d.id)" class="flyout-note">{{ shortCount(ed.transistors(d.id)) }}T</span>
|
||
|
|
</button>
|
||
|
|
<button class="pin-btn" :class="{ on: isPinned('chip', d) }" :title="isPinned('chip', d) ? 'Unpin from the left' : 'Pin to the left'" @click="togglePin('chip', d)">
|
||
|
|
<Icon :name="isPinned('chip', d) ? 'lucide:pin-off' : 'lucide:pin'" class="size-3.5" />
|
||
|
|
</button>
|
||
|
|
</li>
|
||
|
|
</ul>
|
||
|
|
</template>
|
||
|
|
</div>
|
||
|
|
<button class="flyout-new" @click="ed.createComponent(), (open = null)"><Icon name="lucide:plus" class="size-4" /> New component</button>
|
||
|
|
</template>
|
||
|
|
</div>
|
||
|
|
</Transition>
|
||
|
|
|
||
|
|
<nav class="bottom-bar" aria-label="Parts" @pointerleave="leave">
|
||
|
|
<button
|
||
|
|
v-for="c in cats"
|
||
|
|
:key="c.id"
|
||
|
|
class="bar-btn"
|
||
|
|
:class="{ open: open === c.id, active: activeCat === c.id }"
|
||
|
|
:title="c.label"
|
||
|
|
@pointerenter="show(c.id, $event)"
|
||
|
|
@click="toggle(c.id, $event)"
|
||
|
|
>
|
||
|
|
<Icon :name="c.icon" class="size-[18px]" />
|
||
|
|
<span class="bar-label">{{ c.label }}</span>
|
||
|
|
</button>
|
||
|
|
<span class="bar-sep" />
|
||
|
|
<button class="bar-btn" title="Find a part (/)" @click="ed.toggleSidebar('left', true), ed.partSearch.value++">
|
||
|
|
<Icon name="lucide:search" class="size-[18px]" />
|
||
|
|
</button>
|
||
|
|
</nav>
|
||
|
|
</div>
|
||
|
|
</template>
|