Files
xenia-pos-local/packages/floorplan/src/Shapes.jsx
T
bonaminandClaude Opus 5.5 244e93f827 feat(manager): the floor plan editor reworked; Management -> Tables on the page rule; area colours
- Floor editor: one toolbar over the map and the panels (grid on / off, grid resolution Ultra high
  ... Very low, edit the shape, Fit width / Whole plan, undo, save), panels top-aligned and sticky.
- The floor's shape: a '+' on the edge under the pointer, press to add a corner (and drag it);
  resting on a corner shows a bubble to remove it (never below 3).
- Areas: resize handles on the sides and corners, a colour (pastel, see-through; floor_items.color,
  migration 0041), the name repeated in the corner when tables cover it (live maps too).
- Labels can be clicked again after they're placed (transparent fill, not none).
- Management -> Tables: <Page> with the tabs above (?tab=), the list as a container, toolbar
  buttons one height.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 15:17:45 +02:00

122 lines
6.5 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The pieces of a floor plan as SVG (plan step 6.8): the floor (outline, grid, background
// picture), the walls / bar / labels / areas, and a table with its chairs and what it shows.
// Colours are @xenia/ui tokens (CSS variables), so the same drawing works in every app.
import { chairs, durationText, itemFontSize, outlineOf, peopleText, pointsAttr, TONES, toneOf } from './geometry.js'
/** The floor: background picture, the floor's shape, and the grid (when asked). */
export function Floor({ plan, showGrid = false, mediaBase = '' }) {
const pts = pointsAttr(outlineOf(plan))
const g = plan.grid_size
const clip = `floor-clip-${plan.id ?? 'new'}`
return (
<g>
<defs>
<clipPath id={clip}><polygon points={pts} /></clipPath>
{showGrid && g > 0 && (
<pattern id={`${clip}-grid`} width={g} height={g} patternUnits="userSpaceOnUse">
<path d={`M ${g} 0 L 0 0 0 ${g}`} fill="none" stroke="var(--x-line)" strokeWidth="1" />
</pattern>
)}
</defs>
<polygon points={pts} fill="var(--x-surface)" stroke="var(--x-line-strong)" strokeWidth="3" />
{plan.background?.full && (
<image href={`${mediaBase}${plan.background.full}`} x="0" y="0" width={plan.width} height={plan.height}
preserveAspectRatio="xMidYMid slice" opacity={plan.background_opacity ?? 0.6} clipPath={`url(#${clip})`} />
)}
{showGrid && g > 0 && <rect x="0" y="0" width={plan.width} height={plan.height} fill={`url(#${clip}-grid)`} clipPath={`url(#${clip})`} pointerEvents="none" />}
</g>
)
}
const ITEM_LOOK = {
wall: { fill: 'var(--x-ink-2)', stroke: 'none' },
bar: { fill: 'var(--x-surface-3)', stroke: 'var(--x-ink-3)' },
label: { fill: 'transparent', stroke: 'none' }, // transparent, not none: a label can be clicked
area: { fill: 'var(--x-surface-2)', stroke: 'var(--x-line-strong)', dash: '8 6' },
}
/**
* A wall, the bar, a label or an area. An area may have a colour (drawn see-through, 2026-11-20);
* `corner`: its name is covered by tables, so it shows in the top-left corner as well.
*/
export function Item({ item, selected = false, label, corner = false, ...rest }) {
const look = ITEM_LOOK[item.kind] ?? ITEM_LOOK.area
const w = item.width
const h = item.height
const text = item.text || (item.kind === 'bar' ? label : '')
const colour = item.kind === 'area' && item.color ? item.color : null
const fs = text ? itemFontSize(item, text) : 0
return (
<g transform={`translate(${item.x} ${item.y}) rotate(${item.rotation || 0})`} {...rest}>
<rect x={-w / 2} y={-h / 2} width={w} height={h} rx={item.kind === 'wall' ? 2 : 8}
fill={colour ?? look.fill} fillOpacity={colour ? 0.3 : undefined}
stroke={selected ? 'var(--x-accent)' : colour ?? look.stroke} strokeOpacity={colour && !selected ? 0.9 : undefined}
strokeWidth={selected ? 4 : 2} strokeDasharray={selected ? undefined : look.dash} />
{item.kind === 'label' && selected && <rect x={-w / 2} y={-h / 2} width={w} height={h} fill="none" stroke="var(--x-accent)" strokeWidth="2" strokeDasharray="6 4" />}
{text && (
<text x="0" y="0" textAnchor="middle" dominantBaseline="central" fill="var(--x-ink-2)" pointerEvents="none"
fontSize={fs} fontWeight="600" transform={`rotate(${-(item.rotation || 0)})`}>{text}</text>
)}
{text && corner && (
<text x={-w / 2 + 10} y={-h / 2 + 10 + Math.min(16, fs) / 2} dominantBaseline="central" fill="var(--x-ink-2)" pointerEvents="none"
fontSize={Math.min(16, fs)} fontWeight="700" data-testid="floor-area-corner">{text}</text>
)}
</g>
)
}
/** A small person (for seats / guests): drawn, so it looks the same everywhere. */
function Person({ x, y, size, color }) {
const r = size / 2
return (
<g transform={`translate(${x} ${y})`} fill={color}>
<circle cx={0} cy={-r * 0.35} r={r * 0.32} />
<path d={`M ${-r * 0.6} ${r * 0.55} Q 0 ${-r * 0.25} ${r * 0.6} ${r * 0.55} Z`} />
</g>
)
}
/**
* A table: its shape, chairs for its seats, and (live) its name, guests / seats, time open,
* waiters and a "ready" count. `now` (ms) moves the clock.
*/
export function TableShape({ table, live = true, selected = false, now = Date.now(), ...rest }) {
const tone = TONES[live ? toneOf(table) : 'free']
const w = table.width || 80
const h = table.height || 80
const rot = table.rotation || 0
const o = live ? table.order : null
const fs = Math.max(11, Math.min(22, Math.min(w, h) * 0.24))
const lines = [table.name]
const second = o ? `${o.guests ?? '–'}/${table.seats ?? '–'}` : table.seats ? `${table.seats}` : ''
const third = o ? [durationText(o.opened_at ? Math.max(0, Math.floor((now - new Date(o.opened_at).getTime()) / 60000)) : null), peopleText(o.people)].filter(Boolean).join(' · ') : ''
return (
<g transform={`translate(${table.x} ${table.y})`} data-table={table.id} data-state={live ? toneOf(table) : undefined} {...rest}>
<g transform={`rotate(${rot})`}>
{chairs(table.shape, w, h, table.seats).map(([cx, cy], i) => (
<circle key={i} cx={cx} cy={cy} r={Math.max(6, Math.min(10, fs * 0.45))} fill="var(--x-surface-2)" stroke={tone.stroke} strokeWidth="1.5" />
))}
{table.shape === 'round'
? <ellipse cx="0" cy="0" rx={w / 2} ry={h / 2} fill={tone.fill} stroke={selected ? 'var(--x-accent)' : tone.stroke} strokeWidth={selected ? 5 : 3} />
: <rect x={-w / 2} y={-h / 2} width={w} height={h} rx="10" fill={tone.fill} stroke={selected ? 'var(--x-accent)' : tone.stroke} strokeWidth={selected ? 5 : 3} />}
</g>
<g fill={tone.ink} pointerEvents="none">
<text x="0" y={second || third ? -fs * (third ? 0.9 : 0.45) : 0} textAnchor="middle" dominantBaseline="central" fontSize={fs} fontWeight="700">{lines[0]}</text>
{second && (
<g>
<Person x={-fs * 0.95} y={third ? fs * 0.15 : fs * 0.6} size={fs * 0.85} color={tone.ink} />
<text x={fs * 0.1} y={third ? fs * 0.15 : fs * 0.6} textAnchor="middle" dominantBaseline="central" fontSize={fs * 0.75} fontWeight="600">{second}</text>
</g>
)}
{third && <text x="0" y={fs * 1.1} textAnchor="middle" dominantBaseline="central" fontSize={fs * 0.62}>{third}</text>}
</g>
{o?.ready > 0 && (
<g transform={`translate(${w / 2 - 4} ${-h / 2 + 4})`} pointerEvents="none">
<circle r={fs * 0.55} fill="var(--x-success)" />
<text textAnchor="middle" dominantBaseline="central" fontSize={fs * 0.6} fontWeight="700" fill="var(--x-success-fg)">{o.ready}</text>
</g>
)}
</g>
)
}