logigate/app/components/editor/PinsPane.vue

167 lines
6.7 KiB
Vue

<script setup lang="ts">
// Every input and output of the open circuit in one list, to rename them
// quickly (type a name, Enter goes to the next) and to try them out.
import { duplicateNames, entryLabel, ioList, numberedNames, type IoEntry } from '~/lib/logic/bulk'
import type { CircuitNode } from '~/lib/logic/types'
const ed = useEditor()
const io = computed(() => ioList(ed.def.value))
const groups = computed(() => [
{ id: 'in' as const, title: 'Inputs', entries: io.value.inputs, dup: duplicateNames(io.value.inputs) },
{ id: 'out' as const, title: 'Outputs', entries: io.value.outputs, dup: duplicateNames(io.value.outputs) },
])
const selected = computed(() => new Set(ed.selection.value))
type Block = { panel: CircuitNode; items: { e: IoEntry; i: number }[] } | { e: IoEntry; i: number }
/** Rows of one panel are framed together under the panel's name. */
function blocks(entries: IoEntry[]): Block[] {
const out: Block[] = []
entries.forEach((e, i) => {
const last = out[out.length - 1]
if (e.row && last && 'panel' in last && last.panel.id === e.node.id) last.items.push({ e, i })
else if (e.row) out.push({ panel: e.node, items: [{ e, i }] })
else out.push({ e, i })
})
return out
}
const keyOf = (e: IoEntry) => (e.row ? `${e.node.id}/${e.row.id}` : e.node.id)
function pinNumber(e: IoEntry, entries: IoEntry[]) {
return entries.filter(x => x.pin).indexOf(e) + 1
}
function kindText(e: IoEntry, entries: IoEntry[]): string {
if (e.row && e.pin) return `pin ${pinNumber(e, entries)} · ${e.row.type}`
if (e.row) return e.row.type === 'clock' && e.row.hz ? `clock ${e.row.hz} Hz` : e.row.type
if (e.pin) return `pin ${pinNumber(e, entries)}`
if (e.node.kind === 'clock') return e.node.hz ? `clock ${e.node.hz} Hz` : 'clock'
return { button: 'button', lamp: 'lamp', hex: 'display' }[e.node.kind as string] ?? e.node.kind
}
function placeholder(e: IoEntry, entries: IoEntry[]): string {
if (e.row) return e.row.name?.trim() || 'no name'
if (e.pin) return `${e.node.kind === 'input' ? 'in' : 'out'}${pinNumber(e, entries)}`
return 'no name'
}
const list = ref<HTMLElement>()
function next(ev: KeyboardEvent, group: string, i: number) {
;(ev.target as HTMLInputElement).blur()
const el = list.value?.querySelector<HTMLInputElement>(`input[data-group="${group}"][data-i="${i + 1}"]`)
el?.focus()
el?.select()
}
// Numbering a whole side.
const numbering = ref<null | 'in' | 'out'>(null)
const pattern = ref('D#')
const start = ref(0)
const pinsOnly = ref(true)
const targets = computed(() => {
const g = groups.value.find(x => x.id === numbering.value)
return g ? g.entries.filter(e => !pinsOnly.value || e.pin) : []
})
const preview = computed(() => numberedNames(pattern.value, targets.value.length, start.value))
function openNumbering(id: 'in' | 'out') {
numbering.value = numbering.value === id ? null : id
pattern.value = id === 'in' ? 'D#' : 'Q#'
pinsOnly.value = groups.value.find(x => x.id === id)!.entries.some(e => e.pin)
}
function applyNumbering() {
targets.value.forEach((e, i) => {
const name = preview.value[i]!
if (e.row) e.row.label = name
else {
e.node.label = name
delete e.node.autoLabel
}
})
ed.commit()
numbering.value = null
}
</script>
<template>
<div ref="list">
<section v-for="g in groups" :key="g.id" class="io-section">
<div class="io-head">
<h3>
{{ g.title }} <span class="count-pill">{{ g.entries.length }}</span>
</h3>
<button v-if="g.entries.length > 1" class="btn-sm" title="Name them all with a numbered pattern" @click="openNumbering(g.id)">
<Icon name="lucide:list-ordered" class="size-3.5" /> Number all
</button>
</div>
<form v-if="numbering === g.id" class="numbering" @submit.prevent="applyNumbering">
<div class="grid grid-cols-[1fr_5rem] gap-2">
<label class="field mb-0">
<span>Pattern (# is the number)</span>
<input v-model="pattern" autofocus />
</label>
<label class="field mb-0">
<span>From</span>
<input v-model.number="start" type="number" min="0" />
</label>
</div>
<label v-if="g.entries.some(e => !e.pin) && g.entries.some(e => e.pin)" class="check mt-2">
<input v-model="pinsOnly" type="checkbox" /> Only the pins (top to bottom)
</label>
<p class="hint mt-1">{{ preview.slice(0, 6).join(', ') }}<template v-if="preview.length > 6">, …</template></p>
<div class="flex gap-2">
<button class="btn btn-primary grow" type="submit" :disabled="!targets.length">Rename {{ targets.length }}</button>
<button class="btn" type="button" @click="numbering = null">Cancel</button>
</div>
</form>
<p v-if="!g.entries.length" class="hint">
{{ g.id === 'in' ? 'No inputs yet. Place an input switch (I) to give this circuit an input pin.' : 'No outputs yet. Place an output pin (O) or a lamp (L).' }}
</p>
<div v-else class="io-list">
<template v-for="b in blocks(g.entries)" :key="'panel' in b ? `p-${b.panel.id}` : keyOf(b.e)">
<div v-if="'panel' in b" class="io-group" :class="{ selected: selected.has(b.panel.id) }">
<div class="io-group-head">
<Icon name="lucide:sliders-vertical" class="size-3.5 shrink-0" />
<span class="grow truncate">{{ b.panel.label?.trim() || 'Input panel' }}</span>
<button class="icon-btn io-locate" title="Show on the canvas" @click="ed.focusNode(b.panel.id)"><Icon name="lucide:locate-fixed" class="size-3.5" /></button>
</div>
<EditorPinRow
v-for="it in b.items"
:key="keyOf(it.e)"
:e="it.e"
:group="g.id"
:index="it.i"
:kind="kindText(it.e, g.entries)"
:placeholder="placeholder(it.e, g.entries)"
:dup="g.dup.has(entryLabel(it.e).toLowerCase())"
:selected="false"
@next="next($event, g.id, it.i)"
/>
</div>
<EditorPinRow
v-else
:e="b.e"
:group="g.id"
:index="b.i"
:kind="kindText(b.e, g.entries)"
:placeholder="placeholder(b.e, g.entries)"
:dup="g.dup.has(entryLabel(b.e).toLowerCase())"
:selected="selected.has(b.e.node.id)"
@next="next($event, g.id, b.i)"
/>
</template>
</div>
</section>
<section>
<p class="hint">
Pins are what the circuit shows when you use it as a component, top to bottom. Click a value to flip a switch (hold a button).
Type a name and press Enter for the next one.
</p>
</section>
</div>
</template>