fix: persist manual node sizes and add width/height inputs

Nested vm/lxc/docker leaf nodes lost their size on reload: the
deserialize restore branch excluded those types entirely, so a
resized child snapped back to content-fit. Gate the restore on
container_mode instead of node type.

Also prefer explicit width/height over the DOM-measured value when
serializing, so a manual resize persists its exact target rather than
drifting to the fractional content-fit size.

Add a Size section to the detail panel with W/H number inputs
(setNodeSize store action, clamped to the resizer minimums). Inputs
resync live when the node is resized by corner drag, without
clobbering active keystrokes.

ha-relevant: yes
This commit is contained in:
Pouzor
2026-06-17 11:55:55 +02:00
parent 96bb048228
commit 2a4d109ee6
6 changed files with 237 additions and 10 deletions
+81 -1
View File
@@ -21,7 +21,7 @@ type PropForm = { key: string; value: string; icon: string | null; visible: bool
const EMPTY_PROP: PropForm = { key: '', value: '', icon: null, visible: true }
export function DetailPanel({ onEdit }: DetailPanelProps) {
const { nodes, selectedNodeId, selectedNodeIds, setSelectedNode, deleteNode, updateNode, snapshotHistory, createGroup, ungroup, removeFromGroup } = useCanvasStore()
const { nodes, selectedNodeId, selectedNodeIds, setSelectedNode, deleteNode, updateNode, snapshotHistory, createGroup, ungroup, removeFromGroup, setNodeSize } = useCanvasStore()
const serviceStatuses = useCanvasStore((s) => s.serviceStatuses)
const [addingForNode, setAddingForNode] = useState<string | null>(null)
@@ -255,6 +255,13 @@ export function DetailPanel({ onEdit }: DetailPanelProps) {
{data.last_seen && <DetailRow label="Last Seen" value={new Date(/[Zz]|[+-]\d{2}:?\d{2}$/.test(data.last_seen) ? data.last_seen : data.last_seen + 'Z').toLocaleString()} />}
</div>
{/* Size section — manual width/height entry for pixel-exact sizing */}
<SizeFields
key={node.id}
node={node}
onCommit={(size) => { snapshotHistory(); setNodeSize(node.id, size) }}
/>
{/* Properties section */}
<div className="px-4 py-3 border-t border-border">
<div className="flex items-center justify-between mb-2">
@@ -345,6 +352,79 @@ export function DetailPanel({ onEdit }: DetailPanelProps) {
)
}
// --- Size fields (manual width/height entry) ---
const round = (v: number | undefined): string => (v != null ? String(Math.round(v)) : '')
function SizeFields({ node, onCommit }: { node: Node<NodeData>; onCommit: (size: { width?: number; height?: number }) => void }) {
// Live size from the explicit dimension, falling back to the DOM-measured
// size so the field shows the node's current footprint even before resize.
const liveWidth = round(node.width ?? node.measured?.width)
const liveHeight = round(node.height ?? node.measured?.height)
const [width, setWidth] = useState(liveWidth)
const [height, setHeight] = useState(liveHeight)
// Resync the fields when the node is resized by dragging its corner handle
// (which mutates node.width/height in the store). Adjusting state during
// render — the React-blessed alternative to an effect — keeps it in step
// without cascading renders. The field the user is actively editing is left
// alone so we don't clobber their keystrokes mid-type.
const [editing, setEditing] = useState<'width' | 'height' | null>(null)
const [prev, setPrev] = useState({ w: liveWidth, h: liveHeight })
if (prev.w !== liveWidth || prev.h !== liveHeight) {
setPrev({ w: liveWidth, h: liveHeight })
if (editing !== 'width') setWidth(liveWidth)
if (editing !== 'height') setHeight(liveHeight)
}
const commit = (raw: string, axis: 'width' | 'height') => {
const n = Number(raw)
if (!raw.trim() || Number.isNaN(n) || n <= 0) {
// Reset the field to the live value on invalid input — no mutation.
if (axis === 'width') setWidth(round(node.width ?? node.measured?.width))
else setHeight(round(node.height ?? node.measured?.height))
return
}
onCommit({ [axis]: n })
}
return (
<div className="px-4 py-3 border-t border-border">
<span className="text-xs text-muted-foreground">Size</span>
<div className="mt-2 flex items-center gap-2">
<label className="flex flex-1 items-center gap-1.5">
<span className="text-[10px] text-muted-foreground/70 w-3">W</span>
<Input
type="number"
min={140}
aria-label="Width"
value={width}
onFocus={() => setEditing('width')}
onChange={(e) => setWidth(e.target.value)}
onBlur={() => { setEditing(null); commit(width, 'width') }}
onKeyDown={(e) => { if (e.key === 'Enter') e.currentTarget.blur() }}
className="h-7 text-xs font-mono"
/>
</label>
<label className="flex flex-1 items-center gap-1.5">
<span className="text-[10px] text-muted-foreground/70 w-3">H</span>
<Input
type="number"
min={50}
aria-label="Height"
value={height}
onFocus={() => setEditing('height')}
onChange={(e) => setHeight(e.target.value)}
onBlur={() => { setEditing(null); commit(height, 'height') }}
onKeyDown={(e) => { if (e.key === 'Enter') e.currentTarget.blur() }}
className="h-7 text-xs font-mono"
/>
</label>
</div>
</div>
)
}
// --- Multi-select panel ---
interface MultiSelectPanelProps {
@@ -36,6 +36,7 @@ function setupStore(nodeData: Partial<NodeData> = {}, serviceStatuses: Record<st
snapshotHistory: vi.fn(),
createGroup: vi.fn(),
ungroup: vi.fn(),
setNodeSize: vi.fn(),
serviceStatuses,
}
// Support both the bare destructure call and the selector-based call.
@@ -571,4 +572,69 @@ describe('DetailPanel', () => {
expect(row?.textContent).not.toMatch(/Invalid Date/)
})
})
describe('Size section', () => {
function setupSized(node: Partial<Node<NodeData>>, setNodeSize = vi.fn()) {
const state = {
nodes: [{ ...makeNode({}), ...node }],
selectedNodeId: 'n1',
selectedNodeIds: [],
setSelectedNode: vi.fn(),
deleteNode: vi.fn(),
updateNode: vi.fn(),
snapshotHistory: vi.fn(),
createGroup: vi.fn(),
ungroup: vi.fn(),
setNodeSize,
serviceStatuses: {},
}
vi.mocked(canvasStore.useCanvasStore).mockImplementation(
((sel?: (s: typeof state) => unknown) => (sel ? sel(state) : state)) as unknown as typeof canvasStore.useCanvasStore,
)
return setNodeSize
}
it('shows the current width/height, preferring explicit over measured', () => {
setupSized({ width: 220, height: 130, measured: { width: 999, height: 999 } })
render(<DetailPanel onEdit={vi.fn()} />)
expect((screen.getByLabelText('Width') as HTMLInputElement).value).toBe('220')
expect((screen.getByLabelText('Height') as HTMLInputElement).value).toBe('130')
})
it('falls back to the measured size before a resize', () => {
setupSized({ measured: { width: 187, height: 73 } })
render(<DetailPanel onEdit={vi.fn()} />)
expect((screen.getByLabelText('Width') as HTMLInputElement).value).toBe('187')
expect((screen.getByLabelText('Height') as HTMLInputElement).value).toBe('73')
})
it('commits a manual width on blur', () => {
const setNodeSize = setupSized({ width: 200, height: 100 })
render(<DetailPanel onEdit={vi.fn()} />)
const w = screen.getByLabelText('Width')
fireEvent.change(w, { target: { value: '260' } })
fireEvent.blur(w)
expect(setNodeSize).toHaveBeenCalledWith('n1', { width: 260 })
})
it('reverts invalid input without committing', () => {
const setNodeSize = setupSized({ width: 200, height: 100 })
render(<DetailPanel onEdit={vi.fn()} />)
const w = screen.getByLabelText('Width') as HTMLInputElement
fireEvent.change(w, { target: { value: 'abc' } })
fireEvent.blur(w)
expect(setNodeSize).not.toHaveBeenCalled()
expect(w.value).toBe('200')
})
it('resyncs the field when the node is resized by corner drag', () => {
setupSized({ width: 200, height: 100 })
const { rerender } = render(<DetailPanel onEdit={vi.fn()} />)
expect((screen.getByLabelText('Width') as HTMLInputElement).value).toBe('200')
// Simulate a corner-drag resize updating the store dimension.
setupSized({ width: 340, height: 100 })
rerender(<DetailPanel onEdit={vi.fn()} />)
expect((screen.getByLabelText('Width') as HTMLInputElement).value).toBe('340')
})
})
})