148 lines
6.1 KiB
Vue
148 lines
6.1 KiB
Vue
<script setup lang="ts">
|
|
// Edit a connector type of your own, like HDMI or RJ45: its name, colour and
|
|
// lines. A line runs out → in (from an "out" port to an "in" port), or back.
|
|
// Every port and cable of this type follows the changes.
|
|
import { WIRE_COLORS, type ConnectorType, type WireColor } from '~/lib/logic/types'
|
|
|
|
const ed = useEditor()
|
|
const close = () => (ed.dialog.value = null)
|
|
|
|
const type = computed<ConnectorType | undefined>(() => ed.project.value.connectors?.find(c => c.id === ed.connectorFor.value))
|
|
const uses = computed(() => (type.value ? ed.connectorUses(type.value.id) : 0))
|
|
const confirmDelete = ref(false)
|
|
|
|
/** Every change is one step to undo, and reaches every port of this type. */
|
|
const save = () => ed.commit()
|
|
|
|
function setName(e: Event) {
|
|
if (!type.value) return
|
|
type.value.name = (e.target as HTMLInputElement).value.trim().slice(0, 40) || 'Connector'
|
|
save()
|
|
}
|
|
|
|
function setColor(c: WireColor | undefined) {
|
|
if (!type.value) return
|
|
if (c) type.value.color = c
|
|
else delete type.value.color
|
|
save()
|
|
}
|
|
|
|
function setLine(i: number, e: Event) {
|
|
const line = type.value?.lines[i]
|
|
if (!line) return
|
|
line.name = (e.target as HTMLInputElement).value.trim().slice(0, 24) || `L${i}`
|
|
save()
|
|
}
|
|
|
|
function flip(i: number) {
|
|
const line = type.value?.lines[i]
|
|
if (!line) return
|
|
if (line.back) delete line.back
|
|
else line.back = true
|
|
save()
|
|
}
|
|
|
|
function move(i: number, by: number) {
|
|
const lines = type.value?.lines
|
|
if (!lines || i + by < 0 || i + by >= lines.length) return
|
|
const [l] = lines.splice(i, 1)
|
|
lines.splice(i + by, 0, l!)
|
|
save()
|
|
}
|
|
|
|
function remove(i: number) {
|
|
type.value?.lines.splice(i, 1)
|
|
save()
|
|
}
|
|
|
|
/** Tab in the last line's name, once it is filled in, adds a line and goes on to it. */
|
|
function lineTab(i: number, e: KeyboardEvent) {
|
|
const lines = type.value?.lines
|
|
if (e.shiftKey || !lines || i !== lines.length - 1 || !(e.target as HTMLInputElement).value.trim()) return
|
|
e.preventDefault()
|
|
setLine(i, e)
|
|
addLine()
|
|
}
|
|
|
|
function addLine() {
|
|
const lines = type.value?.lines
|
|
if (!lines || lines.length >= 64) return
|
|
// Continue a series: D0, D1 → D2.
|
|
const last = lines.at(-1)?.name ?? 'D-1'
|
|
const m = /^(.*?)(\d+)$/.exec(last)
|
|
lines.push({ name: m ? `${m[1]}${Number(m[2]) + 1}` : `L${lines.length}` })
|
|
save()
|
|
nextTick(() => document.querySelector<HTMLInputElement>('.conn-lines li:last-child input')?.select())
|
|
}
|
|
|
|
function remove_type() {
|
|
if (!type.value) return
|
|
ed.removeConnector(type.value.id)
|
|
}
|
|
|
|
const forward = computed(() => type.value?.lines.filter(l => !l.back).length ?? 0)
|
|
</script>
|
|
|
|
<template>
|
|
<div class="modal-backdrop" @click.self="close">
|
|
<div v-if="type" class="modal conn-modal" role="dialog" aria-labelledby="conn-title">
|
|
<div class="flex items-start justify-between gap-4">
|
|
<div>
|
|
<h2 id="conn-title">Connector type</h2>
|
|
<p class="hint mt-1">
|
|
A cable of this type joins an <strong>out</strong> port to an <strong>in</strong> port and carries all its lines.
|
|
{{ uses ? `Used by ${uses} port${uses === 1 ? '' : 's'}; they follow every change.` : 'Not used yet: place its ports from the Connectors list.' }}
|
|
</p>
|
|
</div>
|
|
<button class="tb-btn" title="Close" @click="close"><Icon name="lucide:x" class="size-4" /></button>
|
|
</div>
|
|
|
|
<div class="sel-row mt-4">
|
|
<label class="field grow">
|
|
<span>Name</span>
|
|
<input :value="type.name" maxlength="40" placeholder="e.g. HDMI, RJ45, Bus" @change="setName" @keydown.enter="($event.target as HTMLInputElement).blur()" />
|
|
</label>
|
|
</div>
|
|
<div class="field">
|
|
<span>Colour of its cables</span>
|
|
<div class="swatches">
|
|
<button class="swatch none" :class="{ active: !type.color }" title="Default: grey" @click="setColor(undefined)" />
|
|
<button v-for="c in WIRE_COLORS" :key="c" class="swatch" :class="[`c-${c}`, { active: type.color === c }]" :title="c" @click="setColor(c)" />
|
|
</div>
|
|
</div>
|
|
|
|
<div class="field">
|
|
<span>Lines ({{ forward }} out → in, {{ type.lines.length - forward }} back)</span>
|
|
<ul class="conn-lines">
|
|
<li v-for="(l, i) in type.lines" :key="i">
|
|
<span class="conn-index">{{ i }}</span>
|
|
<input :value="l.name" maxlength="24" @change="setLine(i, $event)" @keydown.tab="lineTab(i, $event)" @keydown.enter="($event.target as HTMLInputElement).blur()" />
|
|
<button
|
|
class="btn-sm conn-dir"
|
|
:class="{ back: l.back }"
|
|
:title="l.back ? 'Runs back: from the in port to the out port (like an acknowledge or hot-plug line). Click to turn it around.' : 'Runs from the out port to the in port. Click to make it a line back.'"
|
|
@click="flip(i)"
|
|
>
|
|
{{ l.back ? '← back' : 'out → in' }}
|
|
</button>
|
|
<button class="icon-btn" title="Move up" :disabled="i === 0" @click="move(i, -1)"><Icon name="lucide:arrow-up" class="size-3.5" /></button>
|
|
<button class="icon-btn" title="Move down" :disabled="i === type.lines.length - 1" @click="move(i, 1)"><Icon name="lucide:arrow-down" class="size-3.5" /></button>
|
|
<button class="icon-btn danger" title="Remove this line" @click="remove(i)"><Icon name="lucide:x" class="size-3.5" /></button>
|
|
</li>
|
|
</ul>
|
|
<button class="btn-sm self-start" :disabled="type.lines.length >= 64" @click="addLine"><Icon name="lucide:plus" class="size-3.5" /> Add line</button>
|
|
<p v-if="uses" class="hint mt-2">Ports and cables match lines by their place in this list: moving a line moves what it carries.</p>
|
|
</div>
|
|
|
|
<div class="mt-4 flex items-center gap-2">
|
|
<button class="btn btn-primary" @click="close">Done</button>
|
|
<span class="grow" />
|
|
<button v-if="!confirmDelete" class="btn btn-ghost" :disabled="uses > 0" :title="uses ? 'Still used by ports' : ''" @click="confirmDelete = true">
|
|
<Icon name="lucide:trash-2" class="size-4" /> Delete type
|
|
</button>
|
|
<button v-else class="btn btn-danger" @click="remove_type">Really delete?</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|