logigate/app/components/editor/BottomBar.vue

372 lines
17 KiB
Vue
Raw Normal View History

<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
// bar itself. Components and your connector types have their own buttons.
// Parts and components show as drawn tiles in a strip, like a city
// builder's: the mouse wheel scrolls it sideways.
import { shortCount } from '~/lib/logic/analyze'
import { BASIC_GROUPS } from '~/composables/useParts'
import { unusedComponents } from '~/lib/logic/check'
import type { ComponentDef, NodeKind } from '~/lib/logic/types'
const ed = useEditor()
const { place, dragStart, dragEnd, isPinned, togglePin, pinned, unpin } = 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)
let timer: ReturnType<typeof setTimeout> | undefined
const dock = ref<HTMLElement>()
const fly = ref<HTMLElement>()
/** Pointer position along the dock, the dock's width and the flyout's, in px. */
const at = ref({ x: 0, dock: 0, fly: 250 })
/** Where the flyout opens: centred above the pointer, kept inside the canvas. */
const anchor = computed(() => {
const half = at.value.fly / 2
return Math.min(Math.max(at.value.x, half), Math.max(half, at.value.dock - half))
})
function anchorAt(x: number) {
const r = dock.value!.getBoundingClientRect()
at.value = { ...at.value, x: x - r.left, dock: r.width }
}
function measure() {
if (fly.value) at.value = { ...at.value, fly: fly.value.offsetWidth }
const el = strip.value
edges.value = el ? { left: el.scrollLeft > 4, right: el.scrollLeft + el.clientWidth < el.scrollWidth - 4 } : { left: false, right: false }
}
// ── The strip of tiles: sideways with the wheel, arrows at the ends ──
const strip = ref<HTMLElement>()
const edges = ref({ left: false, right: false })
function onWheel(e: WheelEvent) {
const el = strip.value
if (!el || Math.abs(e.deltaX) > Math.abs(e.deltaY)) return
e.preventDefault()
el.scrollLeft += e.deltaY
}
/** Pinned parts that do not fit: the wheel scrolls them sideways too. */
function sideways(e: WheelEvent) {
const el = e.currentTarget as HTMLElement
if (el.scrollWidth <= el.clientWidth || Math.abs(e.deltaX) > Math.abs(e.deltaY)) return
e.preventDefault()
el.scrollLeft += e.deltaY
}
function page(dir: number) {
strip.value?.scrollBy({ left: dir * strip.value.clientWidth * 0.8, behavior: 'smooth' })
}
const placeable = (d: ComponentDef) => d.id !== ed.def.value.id && ed.canPlace(d.id)
/** Components placed nowhere (not the open one, Main, examples or challenges): a tab of their own, to clear out. */
const UNUSED = 'unused'
const unused = computed(() => unusedComponents(ed.project.value).filter(d => d.id !== ed.def.value.id && d.id !== ed.mainId.value))
const confirmDrop = ref<string | null>(null)
function drop(d: ComponentDef) {
if (confirmDrop.value !== d.id) return (confirmDrop.value = d.id)
confirmDrop.value = null
ed.removeComponent(d.id)
ed.notify(`Deleted "${d.name}" (Ctrl+Z brings it back)`)
}
/** The component group shown; your own components first. */
const compTab = ref('')
const compGroup = computed(
() =>
(compTab.value === UNUSED && unused.value.length ? { name: 'Unused', defs: unused.value } : undefined) ??
componentGroups.value.find(g => g.name === compTab.value) ??
// Untouched: the first group with something that can go in here.
componentGroups.value.find(g => g.defs.some(placeable)) ??
componentGroups.value[0],
)
function show(id: string, e: PointerEvent) {
clearTimeout(timer)
const go = () => {
if (open.value !== id) anchorAt(e.clientX)
open.value = id
}
// 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)
// While a part is dragged out of it, the flyout stays (it closes when the drag ends).
if (ed.dragPart.value) return
timer = setTimeout(() => (open.value = null), 220)
}
function stay() {
clearTimeout(timer)
}
function toggle(id: string, e: MouseEvent) {
if (open.value === id) open.value = null
else {
clearTimeout(timer)
anchorAt(e.clientX)
open.value = id
}
}
function pick(kind: NodeKind, chip?: string, conn?: string) {
place(kind, chip, conn)
open.value = null
}
function endDrag() {
dragEnd()
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))
/** Components that belong with a kind of part: gates (drawn as one, or in the Gates group) under Logic. */
const groupComps = computed(() =>
open.value === 'Logic' ? ed.project.value.components.filter(d => d.group?.trim() === 'Gates' || (d.symbol && d.symbol !== 'box')) : [],
)
watch([open, compTab, () => compGroup.value?.defs.length, () => groupComps.value.length], () => nextTick(measure))
const connectors = computed(() => ed.project.value.connectors ?? [])
const portsOk = computed(() => ed.canPlaceKind('portin'))
// Pinned parts sit on the bar; a pinned component this project lacks is left out.
const shown = computed(() => pinned.value.filter(p => p.kind !== 'chip' || p.chip))
const named = (p: { kind: NodeKind }) => p.kind === 'chip' || p.kind === 'nand'
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 ref="dock" class="bottom-dock" @pointerdown.stop>
<Transition name="flyout">
<div v-if="open" ref="fly" class="flyout" :class="{ 'part-flyout': open !== 'connectors' }" :style="{ left: `${anchor}px` }" @pointerenter="stay" @pointerleave="leave">
<!-- Built-in parts -->
<template v-if="group">
<p class="flyout-head">{{ group.name }}</p>
<div class="strip-wrap">
<button v-if="edges.left" class="strip-arrow left" title="Scroll left" @click="page(-1)"><Icon name="lucide:chevron-left" class="size-4" /></button>
<div ref="strip" class="part-strip" @wheel="onWheel" @scroll="measure">
<div v-for="p in group.parts" :key="p.kind" class="part-slot">
<button
class="part-tile"
: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="endDrag"
>
<span class="part-art"><EditorPartThumb :kind="p.kind" /></span>
<span class="part-name">{{ p.name }}</span>
<kbd v-if="p.key" class="part-key">{{ p.key }}</kbd>
<Icon v-if="!ed.canPlaceKind(p.kind)" name="lucide:lock" class="part-lock size-3.5" />
</button>
<button class="pin-btn" :class="{ on: isPinned(p.kind) }" :title="isPinned(p.kind) ? 'Unpin from the bar' : 'Pin to the bar, to have it at hand'" @click="togglePin(p.kind)">
<Icon :name="isPinned(p.kind) ? 'lucide:pin-off' : 'lucide:pin'" class="size-3.5" />
</button>
</div>
<div v-for="d in groupComps" :key="d.id" class="part-slot">
<button
class="part-tile"
: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="endDrag"
>
<span class="part-art"><EditorPartThumb kind="chip" :chip="d.id" /></span>
<span class="part-name">{{ d.name }}</span>
<span v-if="d.id === ed.def.value.id" class="part-meta">editing</span>
<span v-else-if="ed.transistors(d.id)" class="part-meta">{{ shortCount(ed.transistors(d.id)) }} transistors</span>
</button>
<button class="pin-btn" :class="{ on: isPinned('chip', d) }" :title="isPinned('chip', d) ? 'Unpin from the bar' : 'Pin to the bar'" @click="togglePin('chip', d)">
<Icon :name="isPinned('chip', d) ? 'lucide:pin-off' : 'lucide:pin'" class="size-3.5" />
</button>
</div>
</div>
<button v-if="edges.right" class="strip-arrow right" title="Scroll right" @click="page(1)"><Icon name="lucide:chevron-right" class="size-4" /></button>
</div>
</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 v-if="componentGroups.length > 1 || unused.length" class="part-tabs" role="tablist">
<button
v-for="g in componentGroups"
:key="g.name"
role="tab"
:aria-selected="compGroup?.name === g.name && compTab !== UNUSED"
:class="{ active: compGroup?.name === g.name && compTab !== UNUSED }"
@click="compTab = g.name"
>
{{ g.name }} <span class="count">{{ g.defs.length }}</span>
</button>
<button
v-if="unused.length"
role="tab"
class="unused-tab"
:aria-selected="compTab === UNUSED"
:class="{ active: compTab === UNUSED }"
title="Placed in no other circuit: clear out what you no longer need"
@click="compTab = UNUSED"
>
<Icon name="lucide:archive" class="size-3.5" /> Unused <span class="count">{{ unused.length }}</span>
</button>
</div>
<p v-else class="flyout-head">{{ compGroup?.name ?? 'Components' }}</p>
<div class="strip-wrap">
<button v-if="edges.left" class="strip-arrow left" title="Scroll left" @click="page(-1)"><Icon name="lucide:chevron-left" class="size-4" /></button>
<div ref="strip" class="part-strip" @wheel="onWheel" @scroll="measure">
<div v-for="d in compGroup?.defs ?? []" :key="d.id" class="part-slot">
<button
class="part-tile"
: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="endDrag"
>
<span class="part-art"><EditorPartThumb kind="chip" :chip="d.id" /></span>
<span class="part-name">{{ d.name }}</span>
<span v-if="d.id === ed.def.value.id" class="part-meta">editing</span>
<span v-else-if="ed.transistors(d.id)" class="part-meta">{{ shortCount(ed.transistors(d.id)) }} transistors</span>
</button>
<button
v-if="compTab === UNUSED"
class="pin-btn drop-btn"
:class="{ confirm: confirmDrop === d.id }"
:title="confirmDrop === d.id ? 'Click again to delete it' : 'Delete this component'"
@click="drop(d)"
@pointerleave="confirmDrop === d.id && (confirmDrop = null)"
>
<Icon name="lucide:trash-2" class="size-3.5" />
<span v-if="confirmDrop === d.id">Delete?</span>
</button>
<button v-else class="pin-btn" :class="{ on: isPinned('chip', d) }" :title="isPinned('chip', d) ? 'Unpin from the bar' : 'Pin to the bar'" @click="togglePin('chip', d)">
<Icon :name="isPinned('chip', d) ? 'lucide:pin-off' : 'lucide:pin'" class="size-3.5" />
</button>
<button v-if="d.id !== ed.def.value.id" class="pin-btn edit" title="Open it and edit inside (or double-click it on the canvas)" @click="ed.open(d.id), (open = null)">
<Icon name="lucide:pencil" class="size-3.5" />
</button>
</div>
</div>
<button v-if="edges.right" class="strip-arrow right" title="Scroll right" @click="page(1)"><Icon name="lucide:chevron-right" class="size-4" /></button>
</div>
<div class="flex gap-1">
<button class="flyout-new grow" @click="ed.createComponent(), (open = null)"><Icon name="lucide:plus" class="size-4" /> New component</button>
<button class="flyout-new" title="NOT, AND, … up to a 4-bit counter, all built from NAND" @click="ed.loadExamples(), (open = null)"><Icon name="lucide:book-open" class="size-4" /> Examples</button>
<button class="flyout-new" title="Components everyone published: find one and add it here" @click="(ed.dialog.value = 'library'), (open = null)"><Icon name="lucide:library-big" class="size-4" /> Library</button>
</div>
</template>
</div>
</Transition>
<nav class="bottom-bar" aria-label="Parts" @pointerleave="leave">
<div class="bb-group pinned" @wheel="sideways">
<button
v-for="p in shown"
:key="p.pin"
class="bar-btn pinned-part"
:class="{ blocked: p.missing, active: ed.placing.value?.kind === p.kind && ed.placing.value?.chip === p.chip }"
:title="`${p.name}${p.key ? ` (${p.key})` : ''}: click to place, drag onto the canvas. Right-click to unpin`"
:draggable="!p.missing"
@pointerenter="leave"
@click="pick(p.kind, p.chip)"
@contextmenu.prevent="unpin(p.pin)"
@dragstart="dragStart($event, p.kind, p.chip)"
@dragend="dragEnd"
>
<!-- Gates and components by name, the rest (switch, lamp, clock) by icon. -->
<Icon v-if="!named(p)" :name="p.icon" class="size-[18px]" />
<span v-else>{{ p.name }}</span>
</button>
</div>
<div class="bb-group">
<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>
</div>
<div class="bb-group end">
<button class="bar-btn" title="Find a part, a circuit to open or an action (Space or /)" @click="ed.partSearch.value++">
<Icon name="lucide:search" class="size-[18px]" />
<kbd class="bar-label">Space</kbd>
</button>
</div>
</nav>
</div>
</template>