diff --git a/packages/floorplan/src/FloorView.jsx b/packages/floorplan/src/FloorView.jsx index e3ae4f3..606b957 100644 --- a/packages/floorplan/src/FloorView.jsx +++ b/packages/floorplan/src/FloorView.jsx @@ -3,12 +3,31 @@ // on its live events. A tab per floor plan (of the zone, when one is chosen), zoom buttons, and // the tables not on any plan as buttons under it. A tap on a table calls onPick(table). // Styles are inline with @xenia/ui tokens, so it fits the apps with and without Tailwind. -import { useEffect, useMemo, useState } from 'react' +import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react' import { useTranslation } from '@xenia/i18n' import { plansFor, splitTables, toneOf, TONES } from './geometry.js' import { Floor, Item, TableShape } from './Shapes.jsx' -const ZOOMS = [0.5, 0.75, 1, 1.25, 1.5, 2, 3] +const ZOOMS = [0.25, 0.35, 0.5, 0.75, 1, 1.25, 1.5, 2, 3] + +/** The view the person left (zoom + scroll, per plan), kept on this device (2026-11-20). */ +const store = { + read(key) { try { return JSON.parse(localStorage.getItem(key)) ?? null } catch { return null } }, + write(key, v) { try { localStorage.setItem(key, JSON.stringify(v)) } catch { /* private mode */ } }, +} + +/** The size of an element, kept up to date. */ +function useSize(ref) { + const [size, setSize] = useState({ width: 0, height: 0 }) + useLayoutEffect(() => { + const el = ref.current + if (!el) return undefined + const ro = new ResizeObserver(([e]) => setSize({ width: e.contentRect.width, height: e.contentRect.height })) + ro.observe(el) + return () => ro.disconnect() + }) + return size +} function useMinuteClock() { const [now, setNow] = useState(() => Date.now()) @@ -26,15 +45,40 @@ const btn = active => ({ }) const round = { width: 36, height: 36, borderRadius: 999, border: '1px solid var(--x-line)', background: 'var(--x-surface)', color: 'var(--x-ink)', fontSize: 18, fontWeight: 700, cursor: 'pointer' } -export function FloorView({ data, zone = null, onPick, mediaBase = '', height = '100%' }) { +export function FloorView({ data, zone = null, onPick, mediaBase = '', height = '100%', storageKey = 'xenia.floorView' }) { const { t } = useTranslation('common') const now = useMinuteClock() const plans = useMemo(() => plansFor(data?.plans ?? [], zone), [data, zone]) - const [chosen, setChosen] = useState(null) - const [zoom, setZoom] = useState(null) // null = fit the width + const [chosen, setChosen] = useState(() => store.read(storageKey)?.plan ?? null) const plan = plans.find(p => p.id === chosen) ?? plans[0] ?? null + const viewKey = plan ? `${storageKey}:${plan.id}` : null + // 'width' = fit the width (scrolls down), 'all' = the whole plan on screen, a number = a zoom + const [zoom, setZoomState] = useState(() => (viewKey && store.read(viewKey)?.zoom) || 'width') + const box = useRef(null) + const size = useSize(box) const { placed, unplaced } = useMemo(() => splitTables(data?.tables ?? [], plan?.id ?? -1, zone), [data, plan, zone]) + // another plan: its own saved view + const shownKey = useRef(viewKey) + useEffect(() => { + if (shownKey.current === viewKey) return + shownKey.current = viewKey + setZoomState((viewKey && store.read(viewKey)?.zoom) || 'width') + }, [viewKey]) + // back where the person left it (once the plan is drawn) + const restored = useRef(null) + useLayoutEffect(() => { + const el = box.current + if (!el || !viewKey || restored.current === viewKey || !size.width) return + restored.current = viewKey + const saved = store.read(viewKey) + if (saved) { el.scrollLeft = saved.left ?? 0; el.scrollTop = saved.top ?? 0 } + }) + const save = patch => { if (viewKey) store.write(viewKey, { ...(store.read(viewKey) ?? {}), ...patch }) } + const setZoom = z => { setZoomState(z); save({ zoom: z }) } + const choose = id => { setChosen(id); store.write(storageKey, { plan: id }) } + const onScroll = e => save({ left: Math.round(e.currentTarget.scrollLeft), top: Math.round(e.currentTarget.scrollTop) }) + if (!data) return null if (!plan) { return ( @@ -44,25 +88,30 @@ export function FloorView({ data, zone = null, onPick, mediaBase = '', height = ) } - const step = d => setZoom(z => { - const cur = z ?? 1 - const i = ZOOMS.findIndex(v => v >= cur - 0.001) - return ZOOMS[Math.min(ZOOMS.length - 1, Math.max(0, (i < 0 ? ZOOMS.length - 1 : i) + d))] - }) + // the scale on screen now: a fit is worked out from the space there is + const fitWidth = size.width ? size.width / plan.width : 1 + const fitAll = size.width && size.height ? Math.min(size.width / plan.width, size.height / plan.height) : fitWidth + const scale = zoom === 'width' ? fitWidth : zoom === 'all' ? fitAll : zoom + const step = d => { + const next = d > 0 ? ZOOMS.find(v => v > scale + 0.01) : [...ZOOMS].reverse().find(v => v < scale - 0.01) + setZoom(next ?? (d > 0 ? ZOOMS[ZOOMS.length - 1] : ZOOMS[0])) + } return (