// Placing parts from anywhere (the bottom bar, pinned parts, the circuits // list), and the parts pinned to the left panel. Pins are kept in this // browser: built-in parts by kind, components by name, so a pinned "AND" // stays pinned in every project, whether it is the example or your own. import { BASIC_PARTS } from '~/composables/useParts' import { challengeName, defUsable, KIND_UNLOCK } from '~/lib/logic/progress' import type { ComponentDef, NodeKind } from '~/lib/logic/types' const PIN_KEY = 'logigate:pinned' const DEFAULT_PINS = ['nand', 'name:AND', 'name:OR', 'name:NOT', 'name:XOR', 'input', 'output', 'lamp', 'clock'] function loadPins(): string[] { try { const raw = JSON.parse(localStorage.getItem(PIN_KEY) ?? 'null') if (Array.isArray(raw)) return raw.filter((k): k is string => typeof k === 'string').slice(0, 40) } catch {} return [...DEFAULT_PINS] } const pins = ref(import.meta.client ? loadPins() : [...DEFAULT_PINS]) export interface PinnedPart { pin: string kind: NodeKind chip?: string name: string icon: string key?: string /** Pinned, but not here: the component is not in this project, or not usable yet. */ missing?: boolean } export function usePlace() { const ed = useEditor() function lockedHint(kind: NodeKind) { const by = KIND_UNLOCK[kind] return by ? `Solve the ${challengeName(by)} challenge to unlock this part` : '' } /** Start placing a part: click on the canvas puts it there. */ function place(kind: NodeKind, chip?: string, conn?: string) { if (chip && !ed.canPlace(chip)) { ed.play('error') return ed.notify('That component cannot go in here (it is this circuit, contains it, or is not solved yet)', 'error') } if (!ed.canPlaceKind(kind)) { ed.play('error') return ed.notify(lockedHint(kind), 'error') } ed.placing.value = { kind, chip, ...(conn ? { conn } : {}) } } /** Dragging a part onto the canvas places it where it is dropped. */ function dragStart(e: DragEvent, kind: NodeKind, chip?: string, conn?: string) { if ((chip && !ed.canPlace(chip)) || !ed.canPlaceKind(kind)) { e.preventDefault() return } e.dataTransfer?.setData('application/x-logigate', JSON.stringify({ kind, chip, conn })) if (e.dataTransfer) e.dataTransfer.effectAllowed = 'copy' ed.dragPart.value = { kind, chip, ...(conn ? { conn } : {}) } } function dragEnd() { ed.dragPart.value = null } const pinOf = (kind: NodeKind, def?: ComponentDef) => (def ? `name:${def.name}` : kind) const isPinned = (kind: NodeKind, def?: ComponentDef) => pins.value.includes(pinOf(kind, def)) function togglePin(kind: NodeKind, def?: ComponentDef) { const key = pinOf(kind, def) pins.value = pins.value.includes(key) ? pins.value.filter(k => k !== key) : [...pins.value, key] try { localStorage.setItem(PIN_KEY, JSON.stringify(pins.value)) } catch {} } function unpin(key: string) { pins.value = pins.value.filter(k => k !== key) try { localStorage.setItem(PIN_KEY, JSON.stringify(pins.value)) } catch {} } /** The pinned parts as they stand in this project. */ const pinned = computed(() => pins.value.flatMap((pin): PinnedPart[] => { if (pin.startsWith('name:')) { const name = pin.slice(5) const d = ed.project.value.components.find(c => c.name === name && c.id !== ed.def.value.id) ?? ed.project.value.components.find(c => c.name === name) const usable = !!d && d.id !== ed.def.value.id && ed.canPlace(d.id) && defUsable(ed.project.value, d) return [{ pin, kind: 'chip' as NodeKind, chip: d?.id, name, icon: 'lucide:box', missing: !usable }] } const p = BASIC_PARTS.find(b => b.kind === pin) return p ? [{ pin, kind: p.kind, name: p.name, icon: p.icon, key: p.key, missing: !ed.canPlaceKind(p.kind) }] : [] }), ) return { place, dragStart, dragEnd, lockedHint, pinned, isPinned, togglePin, unpin } }