152 lines
6.1 KiB
Vue
152 lines
6.1 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 jumps to the next one.
|
||
|
|
import { duplicateNames, entryLabel, ioList, numberedNames, type IoEntry } from '~/lib/logic/bulk'
|
||
|
|
import { KIND_NAMES } from '~/lib/logic/progress'
|
||
|
|
|
||
|
|
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))
|
||
|
|
|
||
|
|
const keyOf = (e: IoEntry) => (e.row ? `${e.node.id}/${e.row.id}` : e.node.id)
|
||
|
|
|
||
|
|
function kindText(e: IoEntry, entries: IoEntry[]): string {
|
||
|
|
if (e.row) return `${e.node.label?.trim() || 'Panel'} · ${e.row.type}`
|
||
|
|
const kind = KIND_NAMES[e.node.kind] ?? e.node.kind
|
||
|
|
if (!e.pin) return kind
|
||
|
|
return `${kind} · pin ${entries.filter(x => x.pin).indexOf(e) + 1}`
|
||
|
|
}
|
||
|
|
|
||
|
|
function placeholder(e: IoEntry, entries: IoEntry[]): string {
|
||
|
|
if (e.row) return e.row.name?.trim() || 'no name'
|
||
|
|
if (e.pin) {
|
||
|
|
const i = entries.filter(x => x.pin).indexOf(e) + 1
|
||
|
|
return e.node.kind === 'input' ? `in${i}` : `out${i}`
|
||
|
|
}
|
||
|
|
return 'no name'
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Live value: 0/1, or the digit a display shows. */
|
||
|
|
function value(e: IoEntry): string {
|
||
|
|
const id = e.node.id
|
||
|
|
if (e.row) return String(ed.val(id, e.row.id))
|
||
|
|
if (e.node.kind === 'hex') return [0, 1, 2, 3].reduce((n, i) => n + (ed.val(id, `b${i}`) << i), 0).toString(16).toUpperCase()
|
||
|
|
return String(ed.val(id, ['output', 'lamp'].includes(e.node.kind) ? 'a' : 'q'))
|
||
|
|
}
|
||
|
|
|
||
|
|
const list = ref<HTMLElement>()
|
||
|
|
function next(e: KeyboardEvent, group: string, i: number) {
|
||
|
|
;(e.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 group.
|
||
|
|
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">
|
||
|
|
<div class="flex items-center gap-2">
|
||
|
|
<h3 class="grow">
|
||
|
|
{{ 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>
|
||
|
|
<ul v-else class="io-list">
|
||
|
|
<li
|
||
|
|
v-for="(e, i) in g.entries"
|
||
|
|
:key="keyOf(e)"
|
||
|
|
:class="{ selected: selected.has(e.node.id), dup: g.dup.has(entryLabel(e).toLowerCase()), pin: e.pin }"
|
||
|
|
>
|
||
|
|
<span class="io-value" :class="{ on: value(e) !== '0' }" :title="`Now ${value(e)}`">{{ value(e) }}</span>
|
||
|
|
<div class="io-main">
|
||
|
|
<input
|
||
|
|
class="io-name"
|
||
|
|
:value="entryLabel(e)"
|
||
|
|
:placeholder="placeholder(e, g.entries)"
|
||
|
|
:data-group="g.id"
|
||
|
|
:data-i="i"
|
||
|
|
spellcheck="false"
|
||
|
|
@focus="ed.selectOnly([e.node.id])"
|
||
|
|
@change="ed.setLabel(e.node, ($event.target as HTMLInputElement).value, e.row)"
|
||
|
|
@keydown.enter.prevent="next($event, g.id, i)"
|
||
|
|
/>
|
||
|
|
<span class="io-kind">
|
||
|
|
<Icon v-if="g.dup.has(entryLabel(e).toLowerCase())" name="lucide:triangle-alert" class="size-3" title="Another one has the same name" />
|
||
|
|
{{ kindText(e, g.entries) }}
|
||
|
|
</span>
|
||
|
|
</div>
|
||
|
|
<button class="icon-btn" title="Show on the canvas" @click="ed.focusNode(e.node.id)"><Icon name="lucide:locate-fixed" class="size-3.5" /></button>
|
||
|
|
</li>
|
||
|
|
</ul>
|
||
|
|
</section>
|
||
|
|
<section>
|
||
|
|
<p class="hint">
|
||
|
|
Pins are what the circuit shows when you use it as a component, ordered top to bottom. Type a name and press Enter to go to the
|
||
|
|
next one. Clicking a name selects the part; the target icon centres the canvas on it.
|
||
|
|
</p>
|
||
|
|
</section>
|
||
|
|
</div>
|
||
|
|
</template>
|