R panel rows editor: drag to reorder, add above or below, gaps you can see, no folded settings

Each row is one line: a grip to drag it, its colour (click for the next),
its name, switch / button / clock as three icons, its key (or Hz for a
clock), remove. Pointing between two rows offers to add a row there or
more or less space; empty rows show as that much dashed space. Alt + up
or down in a name moves the row. Rows keep their wires when moved.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-23 12:59:16 +02:00
parent 8300712a21
commit 55bba95098
5 changed files with 504 additions and 122 deletions

View File

@ -2121,63 +2121,264 @@ kbd {
} }
/* ── Panel rows in the inspector ────────────────────────────────── */ /* ── Panel rows in the inspector ────────────────────────────────── */
.panel-rows { /* Rows of a panel: one line each, a grip to drag, space between rows to add to. */
display: grid; .rows-head {
gap: 4px;
margin-bottom: 6px;
}
.panel-rows li {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 4px; justify-content: space-between;
margin-bottom: 4px;
} }
.panel-rows input, .rows-all {
.panel-rows select {
min-width: 0;
flex: 1;
}
.panel-rows input.hz {
flex: 0 0 56px;
}
.panel-rows li {
flex-wrap: wrap;
}
.panel-rows li.open {
padding-bottom: 4px;
border-bottom: 1px solid var(--border);
}
.panel-rows .icon-btn.active {
color: var(--text);
}
.panel-rows .row-more {
display: flex; display: flex;
flex: 1 0 100%;
flex-wrap: wrap;
align-items: center; align-items: center;
gap: 6px 8px; gap: 2px;
padding-left: 26px;
font-size: 11px; font-size: 11px;
color: var(--muted); color: var(--muted);
} }
.panel-rows .row-more label { .kind-btn {
display: flex; display: grid;
align-items: center; place-items: center;
gap: 4px; width: 22px;
height: 22px;
border-radius: 5px;
color: var(--muted);
} }
.panel-rows .row-more input { .kind-btn:hover {
flex: 0 0 40px; background: var(--panel-2);
width: 40px; color: var(--text);
padding: 2px 4px; }
.panel-rows {
display: flex;
flex-direction: column;
margin-bottom: 6px;
user-select: none;
}
.panel-rows li.row {
position: relative;
display: flex;
align-items: center;
gap: 3px;
min-height: 30px;
border-radius: 6px;
}
.panel-rows li.row.lifted {
opacity: 0.45;
}
.panel-rows li.row.drop-above::before,
.panel-rows li.row.drop-below::after {
content: '';
position: absolute;
left: 0;
right: 0;
height: 2px;
border-radius: 1px;
background: var(--accent);
}
.panel-rows li.row.drop-above::before {
top: -3px;
}
.panel-rows li.row.drop-below::after {
bottom: -3px;
}
.row-grip {
display: grid;
place-items: center;
width: 12px;
height: 26px;
flex-shrink: 0;
color: var(--muted);
cursor: grab;
touch-action: none;
}
.panel-rows.dragging,
.panel-rows.dragging .row-grip {
cursor: grabbing;
}
.row-grip:hover {
color: var(--text);
}
.row-color {
width: 14px;
height: 14px;
flex-shrink: 0;
border-radius: 50%;
outline: 1px solid var(--border);
}
.row-color.none {
outline: 0;
}
.row-color.lamp-swatch:hover {
outline: 2px solid var(--text);
}
.panel-rows input {
min-width: 0;
padding: 4px 6px;
}
.panel-rows .row-name {
flex: 1;
}
.kind-toggle {
display: flex;
flex-shrink: 0;
padding: 1px;
border: 1px solid var(--border);
border-radius: 6px;
background: var(--panel-2);
}
.kind-toggle button {
display: grid;
place-items: center;
width: 20px;
height: 22px;
border-radius: 4px;
color: var(--muted);
}
.kind-toggle button:hover {
color: var(--text);
}
.kind-toggle button.on {
background: rgb(76 194 255 / 0.18);
color: var(--accent);
}
.panel-rows .row-side {
flex: 0 0 32px;
width: 32px;
padding: 4px 2px;
text-align: center; text-align: center;
font-size: 11px;
}
.panel-rows .key-input {
cursor: pointer;
text-transform: uppercase;
}
.panel-rows .key-input::placeholder {
text-transform: none;
}
/* Between two rows: thin until pointed at; empty rows show as that much space. */
.row-between {
position: relative;
height: 8px;
display: flex;
align-items: center;
justify-content: flex-end;
}
.row-between.spaced {
height: calc(var(--gap) * 9px + 8px);
margin: 2px 0;
border: 1px dashed var(--border);
border-radius: 6px;
}
.gap-label {
padding-right: 8px;
font-size: 10px;
color: var(--muted);
}
.between-tools {
position: absolute;
z-index: 2;
left: 50%;
top: 50%;
display: flex;
gap: 2px;
padding: 2px;
transform: translate(-50%, -50%);
background: var(--panel);
border: 1px solid var(--border);
border-radius: 7px;
box-shadow: 0 4px 14px rgb(0 0 0 / 0.4);
opacity: 0;
pointer-events: none;
transition: opacity 0.1s;
}
.row-between:not(.end)::before {
content: '';
position: absolute;
left: 18px;
right: 0;
top: 50%;
height: 1px;
background: var(--accent);
opacity: 0;
}
.panel-rows:not(.dragging) .row-between:hover .between-tools,
.row-between:focus-within .between-tools {
opacity: 1;
pointer-events: auto;
}
.panel-rows:not(.dragging) .row-between:not(.spaced):hover::before {
opacity: 0.6;
}
.between-tools button {
display: flex;
align-items: center;
gap: 3px;
padding: 2px 7px;
border-radius: 5px;
font-size: 11px;
font-weight: 600;
color: var(--muted);
white-space: nowrap;
}
.between-tools button:hover {
background: var(--panel-2);
color: var(--text);
}
.row-between.end {
height: auto;
justify-content: flex-start;
margin-top: 4px;
}
.add-row {
display: flex;
align-items: center;
gap: 5px;
width: 100%;
padding: 5px 8px;
border: 1px dashed var(--border);
border-radius: 6px;
font-size: 12px;
font-weight: 600;
color: var(--muted);
}
.add-row:hover {
border-color: var(--accent);
color: var(--text);
} }
.swatches.mini { .swatches.mini {
@ -5169,3 +5370,14 @@ button.tile:hover {
bottom: calc(40% + 14px); bottom: calc(40% + 14px);
} }
} }
.panel-rows .row-side.hz {
appearance: textfield;
-moz-appearance: textfield;
}
.panel-rows .row-side.hz::-webkit-inner-spin-button,
.panel-rows .row-side.hz::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}

View File

@ -13,10 +13,10 @@ import { LAMP_COLORS, WIRE_COLORS, type CircuitNode, type NodeKind, type PanelCo
const props = defineProps<{ node: CircuitNode }>() const props = defineProps<{ node: CircuitNode }>()
const ed = useEditor() const ed = useEditor()
const CONTROLS: { value: PanelControl; label: string }[] = [ const CONTROLS: { value: PanelControl; label: string; icon: string }[] = [
{ value: 'switch', label: 'Switch' }, { value: 'switch', label: 'Switch', icon: 'lucide:toggle-left' },
{ value: 'button', label: 'Button' }, { value: 'button', label: 'Button', icon: 'lucide:circle-dot' },
{ value: 'clock', label: 'Clock' }, { value: 'clock', label: 'Clock', icon: 'lucide:audio-waveform' },
] ]
const HINTS: Record<string, string> = { const HINTS: Record<string, string> = {
@ -34,7 +34,7 @@ const HINTS: Record<string, string> = {
tag: 'Wire a signal into it and give it a name. Every "Use signal" with that name in this circuit carries the same signal, without a wire. Names stay inside this circuit.', tag: 'Wire a signal into it and give it a name. Every "Use signal" with that name in this circuit carries the same signal, without a wire. Names stay inside this circuit.',
ref: 'Carries the signal of the "Send signal" with the same name in this circuit. Select it to see all places that signal goes.', ref: 'Carries the signal of the "Send signal" with the same name in this circuit. Select it to see all places that signal goes.',
outpanel: 'A lamp per row, named after what it shows. Inside a component its rows are output pins, top to bottom with the other outputs. Each row passes its signal on to the right.', outpanel: 'A lamp per row, named after what it shows. Inside a component its rows are output pins, top to bottom with the other outputs. Each row passes its signal on to the right.',
panel: 'Click a row on the canvas to flip its switch or hold its button, or press its key. Rows without a name take the name of the pin they are wired to. Open a row (the arrow) for its key, colour, spacing and order.', panel: 'Click a row on the canvas to flip its switch or hold its button, or press its key. Rows without a name take the name of the pin they are wired to. Drag a row by its grip to move it; point between two rows to add one there or change the space.',
} }
const n = computed(() => props.node) const n = computed(() => props.node)
@ -219,17 +219,70 @@ function tabOn(row: PanelRow, i: number, e: KeyboardEvent) {
if (e.shiftKey || n.value.attach || i !== (n.value.rows?.length ?? 0) - 1 || !input.value.trim()) return if (e.shiftKey || n.value.attach || i !== (n.value.rows?.length ?? 0) - 1 || !input.value.trim()) return
e.preventDefault() e.preventDefault()
setRowLabel(row, e) setRowLabel(row, e)
ed.addRow(n.value) addAt(n.value.rows?.length ?? 0)
nextTick(() => document.querySelector<HTMLInputElement>('.panel-rows > li:last-child > input')?.focus())
} }
/** Rows showing their extra settings (key, colour, spacing, order). */ const rowList = ref<HTMLElement>()
const openRows = ref(new Set<string>()) const focusRow = (id: string) => nextTick(() => rowList.value?.querySelector<HTMLInputElement>(`[data-row-id="${id}"] .row-name`)?.focus())
function toggleRow(id: string) {
const s = new Set(openRows.value) /** A new row at a place in the order, its name ready to type. */
if (!s.delete(id)) s.add(id) function addAt(index: number) {
openRows.value = s const row = ed.insertRow(n.value, index)
if (row) focusRow(row.id)
} }
/** The row's colour: a click takes the next one, Shift + click the one before. */
function nextColor(row: PanelRow, e: MouseEvent) {
const order = [undefined, ...LAMP_COLORS]
const k = order.indexOf(row.color)
ed.setRowColor(row, order[(k + (e.shiftKey ? -1 : 1) + order.length) % order.length])
}
/** Alt + ↑/↓ in a row's name moves the row. */
function moveByKey(i: number, e: KeyboardEvent) {
if (!e.altKey || n.value.attach) return
const to = i + (e.key === 'ArrowUp' ? -1 : 1)
const row = n.value.rows?.[i]
if (!row || to < 0 || to >= (n.value.rows?.length ?? 0)) return
e.preventDefault()
ed.moveRowTo(n.value, i, to)
focusRow(row.id)
}
// Dragging a row by its grip: a line shows where it goes; dropping moves it there.
const rowDrag = ref<{ from: number; before: number } | null>(null)
function startRowDrag(from: number, e: PointerEvent) {
if (n.value.attach) return
e.preventDefault()
rowDrag.value = { from, before: from }
const move = (ev: PointerEvent) => {
const items = [...(rowList.value?.querySelectorAll<HTMLElement>('li.row') ?? [])]
let before = items.length
for (let k = 0; k < items.length; k++) {
const r = items[k]!.getBoundingClientRect()
if (ev.clientY < r.top + r.height / 2) {
before = k
break
}
}
rowDrag.value = { from, before }
}
const up = () => {
window.removeEventListener('pointermove', move)
window.removeEventListener('pointerup', up)
const d = rowDrag.value
rowDrag.value = null
if (d) ed.moveRowTo(n.value, d.from, d.before > d.from ? d.before - 1 : d.before)
}
window.addEventListener('pointermove', move)
window.addEventListener('pointerup', up)
}
/** Where the drop line shows: above row k, or (k = rows) below the last. */
const dropAt = computed(() => {
const d = rowDrag.value
return d && d.before !== d.from && d.before !== d.from + 1 ? d.before : null
})
const rowGaps = computed(() => { const rowGaps = computed(() => {
const at = panelOffsets(n.value.rows ?? []) const at = panelOffsets(n.value.rows ?? [])
return at.map((y, i) => (i ? y - at[i - 1]! - 1 : 0)) return at.map((y, i) => (i ? y - at[i - 1]! - 1 : 0))
@ -441,73 +494,98 @@ function setRowHz(row: PanelRow, e: Event) {
<button class="btn-sm" title="Keep the panel and its wires, but stop following the component" @click="ed.undock(n)">Detach</button> <button class="btn-sm" title="Keep the panel and its wires, but stop following the component" @click="ed.undock(n)">Detach</button>
</p> </p>
<div class="field"> <div class="field">
<div class="rows-head">
<span>Rows</span> <span>Rows</span>
<ul class="panel-rows"> <span v-if="!outPanel && (n.rows?.length ?? 0) > 1" class="rows-all">
<li v-for="(r, i) in n.rows ?? []" :key="r.id" :class="{ open: openRows.has(r.id) }"> all
<button <button v-for="c in CONTROLS" :key="c.value" class="kind-btn" :title="`Every row a ${c.label.toLowerCase()}`" @click="ed.setRowsType(n, c.value)">
class="icon-btn" <Icon :name="c.icon" class="size-3.5" />
:class="{ active: openRows.has(r.id) }"
:title="openRows.has(r.id) ? 'Fewer settings' : 'Key, colour, spacing and order'"
@click="toggleRow(r.id)"
>
<Icon :name="openRows.has(r.id) ? 'lucide:chevron-down' : 'lucide:chevron-right'" class="size-3.5" />
</button> </button>
<input :value="r.label ?? ''" :placeholder="rowLabel({ ...r, label: undefined }, i)" title="Name" @change="setRowLabel(r, $event)" @keydown.tab="tabOn(r, i, $event)" /> </span>
<select v-if="!outPanel" :value="r.type" title="Kind" @change="ed.setRowType(r, ($event.target as HTMLSelectElement).value as PanelControl)"> </div>
<option v-for="c in CONTROLS" :key="c.value" :value="c.value">{{ c.label }}</option> <ul ref="rowList" class="panel-rows" :class="{ dragging: !!rowDrag }">
</select> <template v-for="(r, i) in n.rows ?? []" :key="r.id">
<!-- Between two rows: add one here, or more or less empty space -->
<li
v-if="!n.attach"
class="row-between"
:class="{ spaced: i > 0 && rowGaps[i]! > 0 }"
:style="i > 0 && rowGaps[i] ? { '--gap': Math.min(rowGaps[i]!, 5) } : undefined"
>
<span v-if="i > 0 && rowGaps[i]" class="gap-label">{{ rowGaps[i] }} empty</span>
<span class="between-tools">
<button :title="i === 0 ? 'Add a row above the first' : 'Add a row here'" @click="addAt(i)"><Icon name="lucide:plus" class="size-3" /> Row</button>
<template v-if="i > 0">
<button title="One empty row more here" @click="ed.setRowGap(n, i, rowGaps[i]! + 1)"><Icon name="lucide:plus" class="size-3" /> Space</button>
<button v-if="rowGaps[i]" title="One empty row less here" @click="ed.setRowGap(n, i, rowGaps[i]! - 1)"><Icon name="lucide:minus" class="size-3" /> Space</button>
</template>
</span>
</li>
<li class="row" :data-row-id="r.id" :class="{ lifted: rowDrag?.from === i, 'drop-above': dropAt === i, 'drop-below': dropAt === (n.rows?.length ?? 0) && i === (n.rows?.length ?? 0) - 1 }">
<span v-if="!n.attach" class="row-grip" title="Drag to move (or Alt + ↑ ↓ in the name)" @pointerdown="startRowDrag(i, $event)">
<Icon name="lucide:grip-vertical" class="size-3.5" />
</span>
<button
v-if="r.type !== 'clock' || outPanel"
class="row-color lamp-swatch"
:class="r.color ? `l-${r.color}` : [outPanel ? 'default' : 'default-on', { button: r.type === 'button' && !outPanel }]"
:title="`Colour while on: ${r.color ?? 'default'}. Click for the next, Shift + click for the one before`"
@click="nextColor(r, $event)"
/>
<span v-else class="row-color none" />
<input <input
v-if="r.type === 'clock'" class="row-name"
:value="r.label ?? ''"
:placeholder="rowLabel({ ...r, label: undefined }, i)"
title="Name"
@change="setRowLabel(r, $event)"
@keydown.tab="tabOn(r, i, $event)"
@keydown.up="moveByKey(i, $event)"
@keydown.down="moveByKey(i, $event)"
/>
<span v-if="!outPanel" class="kind-toggle" role="radiogroup" aria-label="Kind">
<button
v-for="c in CONTROLS"
:key="c.value"
role="radio"
:aria-checked="r.type === c.value"
:class="{ on: r.type === c.value }"
:title="c.label"
@click="ed.setRowType(r, c.value)"
>
<Icon :name="c.icon" class="size-3.5" />
</button>
</span>
<input
v-if="r.type === 'clock' && !outPanel"
type="number" type="number"
min="0.1" min="0.1"
max="1000" max="1000"
step="any" step="any"
class="hz" class="row-side hz"
:value="r.hz ?? ''" :value="r.hz ?? ''"
placeholder="Hz" placeholder="Hz"
title="Frequency in Hz; empty follows the top bar" title="Frequency in Hz; empty follows the top bar"
@change="setRowHz(r, $event)" @change="setRowHz(r, $event)"
/> />
<input
v-else-if="!outPanel"
class="row-side key-input"
:value="r.key?.toUpperCase() ?? ''"
readonly
placeholder="key"
title="Keyboard key: click, then press it (Backspace clears)"
@keydown="setRowKey(r, $event)"
/>
<button class="icon-btn" title="Remove this row" @click="ed.dropRow(n, r.id)"><Icon name="lucide:x" class="size-3.5" /></button> <button class="icon-btn" title="Remove this row" @click="ed.dropRow(n, r.id)"><Icon name="lucide:x" class="size-3.5" /></button>
<div v-if="openRows.has(r.id)" class="row-more"> </li>
<label v-if="r.type !== 'clock' && !outPanel" title="Keyboard key that flips the switch or holds the button"> </template>
<span>Key</span> <li v-if="!n.attach" class="row-between end">
<input :value="r.key?.toUpperCase() ?? ''" readonly placeholder="–" class="key-input" @keydown="setRowKey(r, $event)" /> <button class="add-row" title="Add a row below the last (or Tab in the last name)" @click="addAt(n.rows?.length ?? 0)">
</label> <Icon name="lucide:plus" class="size-3.5" /> Add row
<label v-if="!n.attach && i > 0" title="Empty rows above this one"> </button>
<span>Space</span>
<input type="number" min="0" max="20" :value="rowGaps[i]" @change="ed.setRowGap(n, i, Number(($event.target as HTMLInputElement).value))" />
</label>
<span v-if="!n.attach" class="flex gap-0.5">
<button class="icon-btn" title="Move up" :disabled="i === 0" @click="ed.moveRow(n, i, -1)"><Icon name="lucide:arrow-up" class="size-3.5" /></button>
<button class="icon-btn" title="Move down" :disabled="i === (n.rows?.length ?? 0) - 1" @click="ed.moveRow(n, i, 1)"><Icon name="lucide:arrow-down" class="size-3.5" /></button>
</span>
<div v-if="r.type !== 'clock' || outPanel" class="swatches mini" title="Colour while on">
<button
class="swatch lamp-swatch"
:class="[outPanel ? 'default' : 'default-on', { active: !r.color, button: r.type === 'button' && !outPanel }]"
title="Default"
@click="ed.setRowColor(r, undefined)"
/>
<button
v-for="c in LAMP_COLORS"
:key="c"
class="swatch lamp-swatch"
:class="[`l-${c}`, { active: r.color === c }]"
:title="c"
@click="ed.setRowColor(r, c)"
/>
</div>
</div>
</li> </li>
</ul> </ul>
<div class="flex flex-wrap items-center gap-1">
<button v-if="!n.attach" class="btn-sm" @click="ed.addRow(n)"><Icon name="lucide:plus" class="size-3.5" /> Add row</button>
<template v-if="!outPanel && (n.rows?.length ?? 0) > 1">
<span class="hint ml-auto">All rows:</span>
<button v-for="c in CONTROLS" :key="c.value" class="btn-sm" :title="`Make every row a ${c.label.toLowerCase()}`" @click="ed.setRowsType(n, c.value)">{{ c.label }}</button>
</template>
</div>
</div> </div>
<EditorFold id="panel-options" title="Panel options"> <EditorFold id="panel-options" title="Panel options">
<button class="btn w-full" :disabled="!n.rows?.length" @click="ed.startLinking([n.id], outPanel ? 'in' : 'out')"> <button class="btn w-full" :disabled="!n.rows?.length" @click="ed.startLinking([n.id], outPanel ? 'in' : 'out')">

View File

@ -26,7 +26,7 @@ import { CHALLENGES, challengeSteps, type Challenge } from '~/lib/logic/challeng
import { compile, INVERTIBLE, invertedPins, rowSource } from '~/lib/logic/compile' import { compile, INVERTIBLE, invertedPins, rowSource } from '~/lib/logic/compile'
import { chipPins, MAX_BITS, panelOffsets, pinKindOf, shapeOf, type Pt } from '~/lib/logic/geometry' import { chipPins, MAX_BITS, panelOffsets, pinKindOf, shapeOf, type Pt } from '~/lib/logic/geometry'
import { adoptName, continueSeries, newPinLabel, uniqueLabel, usedLabels } from '~/lib/logic/labels' import { adoptName, continueSeries, newPinLabel, uniqueLabel, usedLabels } from '~/lib/logic/labels'
import { attachPanel, combineIntoPanel, detachPanel, dockedPanels, mergePanels, newPanelRows, removeRow, syncPanels } from '~/lib/logic/panels' import { attachPanel, combineIntoPanel, detachPanel, dockedPanels, insertPanelRow, mergePanels, movePanelRow, newPanelRows, removeRow, syncPanels } from '~/lib/logic/panels'
import { import {
addExamples, addExamples,
containsDef, containsDef,
@ -1132,6 +1132,20 @@ function create() {
commit() commit()
} }
/** A new row at a place in a free panel's order (above the row there); returns it. */
function insertRow(panel: CircuitNode, index: number): PanelRow | undefined {
if (panel.attach) return
const row = insertPanelRow(panel, index)
commit()
return row
}
/** A row of a free panel dragged to another place in the order. */
function moveRowTo(panel: CircuitNode, from: number, to: number) {
if (panel.attach || !movePanelRow(panel, from, to)) return
commit()
}
function dropRow(panel: CircuitNode, rowId: string) { function dropRow(panel: CircuitNode, rowId: string) {
removeRow(def.value, panel, rowId) removeRow(def.value, panel, rowId)
commit() commit()
@ -2271,6 +2285,8 @@ function create() {
setRowType, setRowType,
addRow, addRow,
dropRow, dropRow,
insertRow,
moveRowTo,
withDocked, withDocked,
withFollowers, withFollowers,
issues, issues,

View File

@ -4,7 +4,7 @@
// input, an output panel on its right a row per output. A docked panel moves // input, an output panel on its right a row per output. A docked panel moves
// with its component and follows it when the component gains or loses pins. // with its component and follows it when the component gains or loses pins.
import { chipPins, rowPin, shapeOf, type DefLookup } from './geometry' import { chipPins, panelOffsets, rowPin, shapeOf, type DefLookup } from './geometry'
import { pinName } from './labels' import { pinName } from './labels'
import { lookup, uid } from './ops' import { lookup, uid } from './ops'
import type { CircuitNode, ComponentDef, PanelRow, Project } from './types' import type { CircuitNode, ComponentDef, PanelRow, Project } from './types'
@ -169,6 +169,41 @@ export function removeRow(def: ComponentDef, panel: CircuitNode, rowId: string)
if (panel.attach) panel.skip = [...new Set([...(panel.skip ?? []), rowId])] if (panel.attach) panel.skip = [...new Set([...(panel.skip ?? []), rowId])]
} }
/**
* A new row at place `index` in a free panel's order (0: above the first,
* rows.length: below the last), the same kind as its neighbour. It takes the
* place of the row it goes above; that row and the ones below move down one,
* keeping the gaps between them.
*/
export function insertPanelRow(panel: CircuitNode, index: number): PanelRow {
const rows = panel.rows ?? []
const at = panelOffsets(rows)
const i = Math.max(0, Math.min(Math.round(index), rows.length))
const row = newPanelRows(1)[0]!
row.type = rows[i]?.type ?? rows[i - 1]?.type ?? 'switch'
const place = i < rows.length ? at[i]! : at.length ? at[at.length - 1]! + 1 : 0
rows.forEach((r, k) => (r.dy = k < i ? at[k]! : at[k]! + 1))
row.dy = place
rows.splice(i, 0, row)
panel.rows = rows
return row
}
/**
* Moves a row of a free panel from one place in the order to another. The
* places stay where they are (gaps too); the rows trade them, and each row
* keeps its wires. False when nothing moved.
*/
export function movePanelRow(panel: CircuitNode, from: number, to: number): boolean {
const rows = panel.rows ?? []
if (from === to || from < 0 || to < 0 || from >= rows.length || to >= rows.length) return false
const at = panelOffsets(rows)
const [r] = rows.splice(from, 1)
rows.splice(to, 0, r!)
rows.forEach((x, k) => (x.dy = at[k]!))
return true
}
/** A docked panel's pins drive the component; undocking keeps rows, wires and position. */ /** A docked panel's pins drive the component; undocking keeps rows, wires and position. */
export function detachPanel(panel: CircuitNode) { export function detachPanel(panel: CircuitNode) {
delete panel.attach delete panel.attach

View File

@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
import { compile } from '../app/lib/logic/compile' import { compile } from '../app/lib/logic/compile'
import { chipPins, panelOffsets } from '../app/lib/logic/geometry' import { chipPins, panelOffsets } from '../app/lib/logic/geometry'
import { createProject, lookup } from '../app/lib/logic/ops' import { createProject, lookup } from '../app/lib/logic/ops'
import { attachPanel, combineIntoPanel, mergePanels } from '../app/lib/logic/panels' import { attachPanel, combineIntoPanel, insertPanelRow, mergePanels, movePanelRow } from '../app/lib/logic/panels'
import { Simulator } from '../app/lib/logic/sim' import { Simulator } from '../app/lib/logic/sim'
import type { ComponentDef } from '../app/lib/logic/types' import type { ComponentDef } from '../app/lib/logic/types'
@ -190,3 +190,44 @@ describe('combining parts into a panel', () => {
expect(combineIntoPanel(def, ['s'], lookup(p))).toBeNull() expect(combineIntoPanel(def, ['s'], lookup(p))).toBeNull()
}) })
}) })
describe('editing the rows of a free panel', () => {
const panel = () => ({
id: 'p',
kind: 'panel' as const,
x: 0,
y: 0,
rows: [
{ id: 'a', type: 'switch' as const },
{ id: 'b', type: 'button' as const, dy: 3 },
{ id: 'c', type: 'button' as const },
],
})
it('a row added above another takes its place; the rest move down, gaps kept', () => {
const p = panel()
expect(panelOffsets(p.rows)).toEqual([0, 3, 4])
const r = insertPanelRow(p, 1)
expect(p.rows.map(x => x.id)).toEqual(['a', r.id, 'b', 'c'])
expect(panelOffsets(p.rows)).toEqual([0, 3, 4, 5])
expect(r.type).toBe('button')
})
it('rows added above the first and below the last', () => {
const p = panel()
const top = insertPanelRow(p, 0)
expect(panelOffsets(p.rows)).toEqual([0, 1, 4, 5])
expect(top.type).toBe('switch')
const end = insertPanelRow(p, 99)
expect(p.rows.at(-1)!.id).toBe(end.id)
expect(panelOffsets(p.rows)).toEqual([0, 1, 4, 5, 6])
})
it('a dragged row trades places: the places and gaps stay', () => {
const p = panel()
expect(movePanelRow(p, 2, 0)).toBe(true)
expect(p.rows.map(x => x.id)).toEqual(['c', 'a', 'b'])
expect(panelOffsets(p.rows)).toEqual([0, 3, 4])
expect(movePanelRow(p, 1, 1)).toBe(false)
})
})