logigate/app/components/editor/PinRow.vue

66 lines
2.4 KiB
Vue
Raw Permalink Normal View History

<script setup lang="ts">
// One line of the Pins list: live value (click to flip a switch, hold for a
// button), the name to edit, what it is, and a button to find it.
import { entryLabel, type IoEntry } from '~/lib/logic/bulk'
const props = defineProps<{ e: IoEntry; group: 'in' | 'out'; index: number; kind: string; placeholder: string; dup: boolean; selected: boolean }>()
const emit = defineEmits<{ next: [e: KeyboardEvent] }>()
const ed = useEditor()
const n = computed(() => props.e.node)
const type = computed(() => (props.e.row ? (n.value.kind === 'outpanel' ? 'lamp' : props.e.row.type) : n.value.kind))
const switchable = computed(() => type.value === 'input' || type.value === 'switch')
const holdable = computed(() => type.value === 'button')
const value = computed(() => {
const id = n.value.id
if (props.e.row) return String(ed.val(id, props.e.row.id))
if (n.value.kind === 'hex') return [0, 1, 2, 3].reduce((v, i) => v + (ed.val(id, `b${i}`) << i), 0).toString(16).toUpperCase()
return String(ed.val(id, 'q'))
})
function flip() {
if (!switchable.value) return
if (props.e.row) ed.toggleRow(n.value, props.e.row)
else ed.toggleInput(n.value)
}
function hold(down: boolean) {
if (!holdable.value) return
if (props.e.row) ed.pressRow(n.value, props.e.row, down)
else ed.press(n.value, down)
}
</script>
<template>
<div class="io-row" :class="{ selected, dup }">
<button
class="io-value"
:class="{ on: value !== '0', live: switchable || holdable }"
:title="switchable ? 'Click to flip' : holdable ? 'Hold to press' : `Now ${value}`"
:tabindex="switchable || holdable ? 0 : -1"
@click="flip"
@pointerdown="hold(true)"
@pointerup="hold(false)"
@pointerleave="hold(false)"
>
{{ value }}
</button>
<input
class="io-name"
:value="entryLabel(e)"
:placeholder="placeholder"
:data-group="group"
:data-i="index"
spellcheck="false"
@focus="ed.selectOnly([n.id])"
@change="ed.setLabel(n, ($event.target as HTMLInputElement).value, e.row)"
@keydown.enter.prevent="emit('next', $event)"
/>
<span class="io-kind" :title="dup ? 'Another one has the same name' : ''">
<Icon v-if="dup" name="lucide:triangle-alert" class="size-3" />{{ kind }}
</span>
<button class="icon-btn io-locate" title="Show on the canvas" @click="ed.focusNode(n.id)"><Icon name="lucide:locate-fixed" class="size-3.5" /></button>
</div>
</template>