- A menu board with pages plays whole rounds (menuMs rounds its seconds up); the editor says how long it really plays under "Seconds on screen", the card and the playlist total use it. - Seconds on screen / per page: shared NumberInput (− / +, may be emptied: an error under the field and Save blocked until filled; 1 s at least, server ge=1). - Performance (Signage -> Message & settings): widgets per slide (signage.max_widgets, 10 recommended, up to 50), checked by the server (signage.too_many_widgets) and the editor. - Text, clock, date, QR, weather, logo placed by hand: the box is exactly the content (its height x text size sets the size, the width follows; handles scale keeping the shape; text size grows the box); a slide's widget may run off the slide while 5 % stays on it. - Slider: the value as close to the slider as the frame's padding; floor and table-colour sliders use the shared Slider. Timeline cards' ring no longer clipped; larger playlist buttons. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
555 lines
34 KiB
React
555 lines
34 KiB
React
// The widgets of a text, menu or blank slide (plan step 5.6b; reworked 2026-11-21): logo or picture,
|
||
// name, date, clock, message of the day, a text of its own, QR code, weather, a menu, and stacks.
|
||
// Each in its own card: its name first, then its settings on one row (where, size, alignment, its
|
||
// own options), what needs more room on the rows under it. Where / size read "Custom" once it was
|
||
// moved or sized by hand on the preview (WidgetCanvas); picking a corner or a bar puts it back. The
|
||
// card of the widget picked on the preview is highlighted (the page doesn't jump to it: the preview
|
||
// stays in view while you drag).
|
||
// Owner review 2 (2026-11-21): the widgets' order (arrows, or drag a card by its handle; a later
|
||
// widget is drawn over an earlier one, and comes later in a bar); a text widget; weather for
|
||
// tomorrow / the day after and in three styles; a menu widget (a board's list in its box); stacks:
|
||
// parts side by side or one over the other, each its share (%), showing a widget, another stack (one
|
||
// level deep) or a whole slide from the Slides library.
|
||
// Owner review 3 (2026-11-21): a stack's parts sized by dragging the lines between them on its bar;
|
||
// "Add a widget" beside what a part shows; Delete removes the picked widget (WidgetCanvas).
|
||
// Owner review 4 (2026-11-21): a part holds several widgets, added with "Add a widget".
|
||
// Owner review 5 (2026-11-21): each widget's own look ("Text and colours": colour, a plate behind
|
||
// it, font, bold / italic / underline, text size; a QR code's colours); a widget in a stack's part
|
||
// has every setting a slide's widget has, aligned both ways in its place or moved and sized there on
|
||
// the preview (Custom); the message of the day changed from its widget. The picked widget is a path
|
||
// ("2", "2.1.0": see WidgetCanvas).
|
||
// Up to the venue's limit (10 recommended; Signage → Message & settings → Performance).
|
||
import { Fragment, useEffect, useRef, useState } from 'react'
|
||
import {
|
||
AlignCenter, AlignLeft, AlignRight, AlignVerticalJustifyCenter, AlignVerticalJustifyEnd, AlignVerticalJustifyStart, ArrowDown, ArrowUp, Bold, Italic, Palette, RotateCcw, Underline, BookImage, CalendarDays, ChevronDown, Clock, CloudSun, Columns3, GripVertical,
|
||
ImageIcon, LayoutList, MessageSquare, Plus, QrCode, Rows3, Store, Trash2, Type, UtensilsCrossed,
|
||
} from 'lucide-react'
|
||
import { cellWidgets, evenShares, setShare } from '@xenia/signage'
|
||
import { useTranslation } from '@xenia/i18n'
|
||
import { Button, ColourField, Dialog, Dropdown, Field, FieldRow, IconButton, Input, Popover, Segmented, Slider, Switch } from '@xenia/ui'
|
||
import MessageOfTheDay from './MessageOfTheDay'
|
||
import { usePerformance } from './usePerformance'
|
||
import * as api from './signageApi'
|
||
import MenuEditor from './MenuEditor'
|
||
import { MediaPick, Section } from './SignageFields'
|
||
import {
|
||
CELL_KINDS, cellWidget, fillPlace, FONT_KEYS, WIDGET_COLOURS, moved, newWidget, pickedAfterMove, presetWidget, WEATHER_CHOICES, weatherChoice, WIDGET_KINDS, WIDGET_POSITIONS,
|
||
} from './signageLogic'
|
||
|
||
const ICON = { logo: ImageIcon, name: Store, date: CalendarDays, clock: Clock, message: MessageSquare, text: Type, qr: QrCode, weather: CloudSun, menu: UtensilsCrossed, hstack: Columns3, vstack: Rows3 }
|
||
const iconOf = w => (w.kind === 'stack' ? (w.direction === 'column' ? Rows3 : Columns3) : ICON[w.kind] || LayoutList)
|
||
const kindLabel = (t, w) => t(`signage.widgets.kinds.${w.kind === 'stack' ? (w.direction === 'column' ? 'vstack' : 'hstack') : w.kind}`)
|
||
const ROW = 'grid gap-4 items-end grid-cols-[repeat(auto-fill,minmax(11rem,1fr))]'
|
||
|
||
let library = null // the Slides library (for stacks' parts), read once
|
||
|
||
function useLibrary() {
|
||
const [list, setList] = useState(library || [])
|
||
useEffect(() => {
|
||
api.listSaved().then(r => { library = r.slides; setList(r.slides) }).catch(() => {})
|
||
}, [])
|
||
return list
|
||
}
|
||
|
||
export default function WidgetsEditor({ widgets = [], onChange, media, onMedia, selected = null, onSelect, extras = {} }) {
|
||
const { t } = useTranslation('manager')
|
||
const most = usePerformance().max_widgets // the venue's limit (Performance; 10 recommended)
|
||
const [drag, setDrag] = useState(null) // { from, at }: the card dragged and where it would land
|
||
const set = (i, patch) => onChange(widgets.map((w, j) => (j === i ? { ...w, ...patch } : w)))
|
||
const add = kind => {
|
||
onChange([...widgets, newWidget(kind)])
|
||
onSelect?.(String(widgets.length))
|
||
}
|
||
const move = (from, to) => {
|
||
if (to < 0 || to >= widgets.length || from === to) return
|
||
onChange(moved(widgets, from, to))
|
||
const top = selected != null && !String(selected).includes('.') ? Number(selected) : null
|
||
onSelect?.(top == null ? null : String(pickedAfterMove(top, from, to)))
|
||
}
|
||
const over = (e, i) => {
|
||
if (!drag) return
|
||
e.preventDefault()
|
||
const r = e.currentTarget.getBoundingClientRect()
|
||
const at = e.clientY < r.top + r.height / 2 ? i : i + 1
|
||
if (at !== drag.at) setDrag(d => ({ ...d, at }))
|
||
}
|
||
const drop = () => {
|
||
if (drag && drag.at != null) move(drag.from, drag.at > drag.from ? drag.at - 1 : drag.at)
|
||
setDrag(null)
|
||
}
|
||
const line = i => drag && drag.at === i && drag.at !== drag.from && drag.at !== drag.from + 1 && (
|
||
<div className="h-0.5 -my-1.5 rounded-full bg-accent" data-testid="signage-widget-drop-line" aria-hidden="true" />
|
||
)
|
||
return (
|
||
<Section title={t('signage.widgets.title')} testid="signage-widgets">
|
||
<p className="text-caption text-ink-3 -mt-1">{t(widgets.length ? 'signage.widgets.dragHint' : 'signage.widgets.none')}</p>
|
||
{widgets.map((w, i) => {
|
||
const Icon = iconOf(w)
|
||
const custom = w.position === 'custom'
|
||
const on = selected != null && (String(selected) === String(i) || String(selected).startsWith(`${i}.`))
|
||
return (
|
||
<div key={i} className="grid gap-3">
|
||
{line(i)}
|
||
<div onPointerDown={() => onSelect?.(String(i))} onDragOver={e => over(e, i)} onDrop={drop}
|
||
className={`grid gap-4 rounded-card border p-4 transition-colors duration-fast ${on ? 'border-accent bg-accent-soft/40 ring-1 ring-accent' : 'border-line bg-surface-2'}
|
||
${drag?.from === i ? 'opacity-50' : ''}`}
|
||
data-testid="signage-widget" data-kind={w.kind} data-index={i} data-selected={on ? 'true' : undefined}>
|
||
<div className="flex items-center gap-2">
|
||
<span draggable onDragStart={e => { e.dataTransfer.effectAllowed = 'move'; setDrag({ from: i, at: null }) }} onDragEnd={() => setDrag(null)}
|
||
className="cursor-grab active:cursor-grabbing text-ink-3 hover:text-ink -ml-1 p-0.5 rounded" title={t('signage.widgets.dragToOrder')}
|
||
aria-hidden="true" data-testid="signage-widget-grip"><GripVertical size={16} /></span>
|
||
<Icon size={16} className="text-ink-2 shrink-0" aria-hidden="true" />
|
||
<span className="text-body font-semibold text-ink">{kindLabel(t, w)}</span>
|
||
<span className="flex-1" />
|
||
<IconButton size="sm" label={t('signage.widgets.earlier')} disabled={i === 0} onClick={e => { e.stopPropagation(); move(i, i - 1) }}
|
||
data-testid="signage-widget-up"><ArrowUp size={16} /></IconButton>
|
||
<IconButton size="sm" label={t('signage.widgets.later')} disabled={i === widgets.length - 1} onClick={e => { e.stopPropagation(); move(i, i + 1) }}
|
||
data-testid="signage-widget-down"><ArrowDown size={16} /></IconButton>
|
||
<IconButton size="sm" tone="danger" label={t('fees.delete')} data-testid="signage-widget-remove"
|
||
onClick={e => { e.stopPropagation(); onChange(widgets.filter((_, j) => j !== i)); onSelect?.(null) }}><Trash2 size={16} /></IconButton>
|
||
</div>
|
||
<div className={ROW}>
|
||
<Field label={t('signage.widgets.position')}>
|
||
<Dropdown value={w.position} onChange={p => p !== 'custom' && onChange(widgets.map((x, j) => (j === i ? presetWidget(x, p) : x)))}
|
||
data-testid="signage-widget-position"
|
||
options={[...WIDGET_POSITIONS.map(p => ({ value: p, label: t(`signage.widgets.positions.${p}`) })),
|
||
...(custom ? [{ value: 'custom', label: t('signage.widgets.custom') }] : [])]} />
|
||
</Field>
|
||
<Field label={t('signage.widgets.size')}>
|
||
<Dropdown value={custom ? 'custom' : (w.size || 'medium')} disabled={custom} onChange={size => set(i, { size })} data-testid="signage-widget-size"
|
||
options={[...['small', 'medium', 'large'].map(s => ({ value: s, label: t(`signage.widgets.sizes.${s}`) })),
|
||
...(custom ? [{ value: 'custom', label: t('signage.widgets.custom') }] : [])]} />
|
||
</Field>
|
||
{w.kind !== 'stack' && <AlignField w={w} set={patch => set(i, patch)} />}
|
||
<InlineFields w={w} set={patch => set(i, patch)} />
|
||
</div>
|
||
<LookFields w={w} set={patch => set(i, patch)} />
|
||
<BelowFields w={w} set={patch => set(i, patch)} media={media} onMedia={onMedia} extras={extras} path={String(i)} depth={0}
|
||
selected={selected} onSelect={onSelect} />
|
||
</div>
|
||
</div>
|
||
)
|
||
})}
|
||
{line(widgets.length)}
|
||
{widgets.length < most && (
|
||
<div className="flex flex-wrap items-center gap-1.5">
|
||
{[...WIDGET_KINDS, 'hstack', 'vstack'].map(k => {
|
||
const Icon = ICON[k]
|
||
return (
|
||
<Button key={k} size="sm" variant="secondary" icon={<Plus size={14} />} onClick={() => add(k)} data-testid={`signage-widget-add-${k}`}>
|
||
<span className="inline-flex items-center gap-1.5"><Icon size={14} aria-hidden="true" />{t(`signage.widgets.kinds.${k}`)}</span>
|
||
</Button>
|
||
)
|
||
})}
|
||
</div>
|
||
)}
|
||
{widgets.length >= most && <p className="text-caption text-ink-3" data-testid="signage-widgets-full">{t('signage.widgets.full', { count: most })}</p>}
|
||
</Section>
|
||
)
|
||
}
|
||
|
||
/** How the widget lines up in its place: auto, left, centre, right. */
|
||
function AlignField({ w, set, testid = 'signage-widget-align' }) {
|
||
const { t } = useTranslation('manager')
|
||
return (
|
||
<Field label={t('signage.widgets.align')}>
|
||
<Segmented value={w.align || 'auto'} onChange={align => set({ align })} aria-label={t('signage.widgets.align')} data-testid={testid} fill
|
||
options={[
|
||
{ value: 'auto', label: t('signage.widgets.alignAuto'), testId: `${testid}-auto` },
|
||
{ value: 'left', icon: <AlignLeft size={16} aria-hidden="true" />, ariaLabel: t('signage.aligns.left'), testId: `${testid}-left` },
|
||
{ value: 'center', icon: <AlignCenter size={16} aria-hidden="true" />, ariaLabel: t('signage.aligns.center'), testId: `${testid}-center` },
|
||
{ value: 'right', icon: <AlignRight size={16} aria-hidden="true" />, ariaLabel: t('signage.aligns.right'), testId: `${testid}-right` },
|
||
]} />
|
||
</Field>
|
||
)
|
||
}
|
||
|
||
/** How a widget in a stack's part lines up from top to bottom in its place. */
|
||
function VerticalField({ w, set }) {
|
||
const { t } = useTranslation('manager')
|
||
return (
|
||
<Field label={t('signage.widgets.vertical')}>
|
||
<Segmented value={w.vertical && w.vertical !== 'auto' ? w.vertical : 'middle'} onChange={vertical => set({ vertical })} fill data-testid="signage-cell-vertical"
|
||
options={[['top', AlignVerticalJustifyStart], ['middle', AlignVerticalJustifyCenter], ['bottom', AlignVerticalJustifyEnd]].map(([v, Icon]) => (
|
||
{ value: v, icon: <Icon size={16} aria-hidden="true" />, ariaLabel: t(`signage.verticals.${v}`), testId: `signage-cell-vertical-${v}` }))} />
|
||
</Field>
|
||
)
|
||
}
|
||
|
||
// which widgets have text (their colour, font, style and size), a QR code's own colours, a plate
|
||
const TEXT_KINDS = ['name', 'date', 'clock', 'message', 'text', 'qr', 'weather']
|
||
|
||
/**
|
||
* A widget's own look (owner review 5), folded under "Text and colours": its colour (text and
|
||
* pictures; empty = the slide's), a plate behind it, its font, bold / italic / underline and text
|
||
* size; a QR code's dots and background. A logo has only the plate; a menu has its own look.
|
||
*/
|
||
function LookFields({ w, set }) {
|
||
const { t } = useTranslation('manager')
|
||
const [open, setOpen] = useState(false)
|
||
if (w.kind === 'menu' || w.kind === 'stack') return null
|
||
const text = TEXT_KINDS.includes(w.kind)
|
||
const changed = !!(w.colour || w.background_colour || w.font || w.bold != null || w.italic || w.underline || (w.text_size && w.text_size !== 100) || w.qr_colour || w.qr_background)
|
||
const styles = [w.bold !== false && 'bold', w.italic && 'italic', w.underline && 'underline'].filter(Boolean)
|
||
return (
|
||
<div className="grid gap-2" data-testid="signage-widget-look">
|
||
<button type="button" onClick={() => setOpen(o => !o)} aria-expanded={open} data-testid="signage-widget-look-toggle"
|
||
className="justify-self-start flex items-center gap-1.5 text-label font-medium text-ink-2 hover:text-ink">
|
||
<Palette size={15} aria-hidden="true" />{t(text ? 'signage.widgets.look' : 'signage.widgets.plateOnly')}
|
||
{changed && <span className="w-1.5 h-1.5 rounded-full bg-accent" aria-hidden="true" />}
|
||
<ChevronDown size={15} className={`transition-transform duration-fast ${open ? 'rotate-180' : ''}`} aria-hidden="true" />
|
||
</button>
|
||
{open && (
|
||
<FieldRow className="-mx-3" min="10rem">
|
||
{text && <ColourField label={t('signage.widgets.colour')} value={w.colour || WIDGET_COLOURS.text} onChange={colour => set({ colour })} testId="signage-widget-colour"
|
||
auto={{ label: t('signage.widgets.asSlide'), on: !w.colour, pick: () => set({ colour: null }) }} />}
|
||
<ColourField label={t('signage.widgets.plate')} value={w.background_colour || WIDGET_COLOURS.plate} onChange={background_colour => set({ background_colour })} testId="signage-widget-plate"
|
||
auto={{ label: t('signage.widgets.noPlate'), on: !w.background_colour, pick: () => set({ background_colour: null }) }} />
|
||
{text && <>
|
||
<Field label={t('signage.font')}>
|
||
<Dropdown value={w.font || ''} onChange={v => set({ font: v || null })} data-testid="signage-widget-font"
|
||
options={[{ value: '', label: t('signage.widgets.asSlide') }, ...FONT_KEYS.map(x => ({ value: x, label: t(`signage.fonts.${x}`) }))]} />
|
||
</Field>
|
||
<Field label={t('signage.look.f.widget.style')}>
|
||
<Segmented multiple fill value={styles} data-testid="signage-widget-style"
|
||
onChange={list => set({ bold: list.includes('bold') ? null : false, italic: list.includes('italic'), underline: list.includes('underline') })}
|
||
options={[['bold', Bold], ['italic', Italic], ['underline', Underline]].map(([v, Icon]) => (
|
||
{ value: v, icon: <Icon size={16} aria-hidden="true" />, ariaLabel: t(`signage.look.${v}`), testId: `signage-widget-${v}` }))} />
|
||
</Field>
|
||
<Slider label={t('signage.widgets.textSize')} value={w.text_size || 100} min={50} max={200} step={10} suffix="%" onChange={text_size => set({ text_size })} testId="signage-widget-text-size" />
|
||
</>}
|
||
{w.kind === 'qr' && <>
|
||
<ColourField label={t('signage.widgets.qrColour')} value={w.qr_colour || WIDGET_COLOURS.qr} onChange={qr_colour => set({ qr_colour })} testId="signage-widget-qr-colour" opacity={false} />
|
||
<ColourField label={t('signage.widgets.qrBackground')} value={w.qr_background || WIDGET_COLOURS.qrBackground} onChange={qr_background => set({ qr_background })} testId="signage-widget-qr-background" />
|
||
</>}
|
||
</FieldRow>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** The message of the day's hint, and the message itself changed right here (owner review 5). */
|
||
function MessageHint() {
|
||
const { t } = useTranslation('manager')
|
||
const [open, setOpen] = useState(false)
|
||
return (
|
||
<div className="flex flex-wrap items-center gap-2 -mt-2">
|
||
<p className="text-caption text-ink-3 flex-1 min-w-[12rem]">{t('signage.widgets.ownTextHint')}</p>
|
||
<Button size="sm" variant="secondary" onClick={() => setOpen(true)} data-testid="signage-widget-message-open">{t('signage.widgets.changeMessage')}</Button>
|
||
{open && (
|
||
<Dialog open size="md" title={t('signage.settings.message')} onClose={() => setOpen(false)}
|
||
footer={<Button variant="secondary" onClick={() => setOpen(false)}>{t('overview.close')}</Button>}>
|
||
<MessageOfTheDay onSaved={() => setOpen(false)} />
|
||
</Dialog>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** The options only some widgets have, on the same row as where / size / alignment. */
|
||
function InlineFields({ w, set }) {
|
||
const { t } = useTranslation('manager')
|
||
if (w.kind === 'message') {
|
||
return (
|
||
<Field label={t('signage.widgets.ownText')} className="col-span-2">
|
||
<Input value={w.text || ''} maxLength={200} placeholder={t('signage.widgets.ownTextPlaceholder')} onChange={e => set({ text: e.target.value || null })} data-testid="signage-widget-text" />
|
||
</Field>
|
||
)
|
||
}
|
||
if (w.kind === 'text') {
|
||
return (
|
||
<Field label={t('signage.widgets.theText')} className="col-span-2">
|
||
<Input value={w.text || ''} maxLength={200} onChange={e => set({ text: e.target.value || null })} data-testid="signage-widget-own-text" />
|
||
</Field>
|
||
)
|
||
}
|
||
if (w.kind === 'date') {
|
||
return (
|
||
<Field label={t('signage.widgets.dateStyle')}>
|
||
<Dropdown value={w.date_style || 'long'} onChange={date_style => set({ date_style })}
|
||
options={[{ value: 'long', label: t('signage.widgets.dateLong') }, { value: 'short', label: t('signage.widgets.dateShort') }]} />
|
||
</Field>
|
||
)
|
||
}
|
||
if (w.kind === 'clock') {
|
||
return (
|
||
<Field label={t('signage.widgets.clockShows')}>
|
||
<Switch boxed className="h-control" checked={!!w.clock_seconds} onChange={on => set({ clock_seconds: on })} label={t('signage.widgets.seconds')} data-testid="signage-widget-seconds" />
|
||
</Field>
|
||
)
|
||
}
|
||
if (w.kind === 'qr') {
|
||
return (
|
||
<Field label={t('display.qrTarget')}>
|
||
<Dropdown value={w.qr_target || 'ordering'} onChange={qr_target => set({ qr_target })}
|
||
options={['ordering', 'menu', 'custom'].map(q => ({ value: q, label: t(`display.qrTargets.${q}`) }))} />
|
||
</Field>
|
||
)
|
||
}
|
||
if (w.kind === 'weather') {
|
||
return (<>
|
||
<Field label={t('signage.widgets.weatherDays')}>
|
||
<Dropdown value={weatherChoice(w)} data-testid="signage-widget-weather-days"
|
||
onChange={v => { const [from, days] = v.split('-').map(Number); set({ weather_from: from, weather_days: days }) }}
|
||
options={WEATHER_CHOICES.map(([from, days]) => ({ value: `${from}-${days}`, label: t(`signage.widgets.weatherChoices.${from}-${days}`) }))} />
|
||
</Field>
|
||
<Field label={t('signage.widgets.weatherStyle')}>
|
||
<Dropdown value={w.weather_style || 'both'} onChange={weather_style => set({ weather_style })} data-testid="signage-widget-weather-style"
|
||
options={['both', 'icon', 'text'].map(s => ({ value: s, label: t(`signage.widgets.weatherStyles.${s}`) }))} />
|
||
</Field>
|
||
</>)
|
||
}
|
||
return null
|
||
}
|
||
|
||
/** What needs more room, under the row: the picture, hints, the QR's own link, a menu's list, a stack's parts. */
|
||
function BelowFields({ w, set, media, onMedia, extras, path, depth, selected, onSelect }) {
|
||
const { t } = useTranslation('manager')
|
||
if (w.kind === 'logo') {
|
||
return <MediaPick kind="image" value={w.media_id} entry={media[String(w.media_id)]} testid="signage-widget-logo"
|
||
onChange={item => { onMedia(item); set({ media_id: item.id }) }} />
|
||
}
|
||
if (w.kind === 'message') return <MessageHint />
|
||
if (w.kind === 'weather') return <p className="text-caption text-ink-3 -mt-2">{t('signage.widgets.weatherNeedsInternet')}</p>
|
||
if (w.kind === 'qr') {
|
||
return (
|
||
<div className={ROW}>
|
||
{w.qr_target === 'custom' && (
|
||
<Field label={t('display.qrUrl')} className="col-span-2"><Input value={w.qr_url || ''} placeholder="https://" onChange={e => set({ qr_url: e.target.value || null })} /></Field>
|
||
)}
|
||
<Field label={t('display.qrCaption')} className="col-span-2"><Input value={w.qr_caption || ''} maxLength={80} onChange={e => set({ qr_caption: e.target.value || null })} /></Field>
|
||
</div>
|
||
)
|
||
}
|
||
if (w.kind === 'menu') return <MenuBoardFields w={w} set={set} extras={extras} path={path} />
|
||
if (w.kind === 'stack') return <StackFields w={w} set={set} media={media} onMedia={onMedia} extras={extras} path={path} depth={depth} selected={selected} onSelect={onSelect} />
|
||
return null
|
||
}
|
||
|
||
/** A menu widget's list and look: the menu board's editor, folded away until it is opened. */
|
||
function MenuBoardFields({ w, set, extras, path }) {
|
||
const { t } = useTranslation('manager')
|
||
const [open, setOpen] = useState(false)
|
||
const menu = extras?.[path]?.menu
|
||
return (
|
||
<div className="grid gap-4 rounded-control border border-line bg-surface p-3">
|
||
<button type="button" onClick={() => setOpen(o => !o)} aria-expanded={open} data-testid="signage-widget-menu-toggle"
|
||
className="flex items-center gap-2 text-left text-label font-medium text-ink">
|
||
<UtensilsCrossed size={15} className="text-ink-2" aria-hidden="true" />
|
||
<span className="flex-1">{t('signage.widgets.menuEdit', { count: menu?.count ?? 0 })}</span>
|
||
<ChevronDown size={16} className={`text-ink-3 transition-transform duration-fast ${open ? 'rotate-180' : ''}`} aria-hidden="true" />
|
||
</button>
|
||
{open && (
|
||
<MenuEditor board testid="signage-widget-menu-editor" slide={{ kind: 'menu', content: w.board || {}, menu }}
|
||
onChange={s => set({ board: s.content })} />
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* A stack's parts: side by side or one over the other, how many (2–6), their sizes (drag the lines
|
||
* between them on the bar, or focus one and use the arrow keys; "Split evenly"), and what each
|
||
* shows: widgets added with "Add a widget" (several share the part, owner review 4: one over the
|
||
* other in a side-by-side stack, side by side in the other; each with its settings, moved up or
|
||
* down, removed), another stack (one level deep), or a slide from the Slides library.
|
||
*/
|
||
function StackFields({ w, set, media, onMedia, extras, path, depth, selected, onSelect }) {
|
||
const { t } = useTranslation('manager')
|
||
const slides = useLibrary()
|
||
const cells = w.cells || []
|
||
const shares = cells.map(c => Math.round(c.share || 0))
|
||
const setCells = next => set({ cells: next })
|
||
const setCell = (j, patch) => setCells(cells.map((c, k) => {
|
||
if (k !== j) return c
|
||
const { widget: _old, ...rest } = { ...c, ...patch } // a part stored with one `widget`: now `widgets`
|
||
return rest
|
||
}))
|
||
const setShares = next => setCells(cells.map((c, k) => ({ ...c, share: next[k] })))
|
||
const count = n => {
|
||
const even = evenShares(n)
|
||
setCells(Array.from({ length: n }, (_, k) => ({ ...(cells[k] || {}), share: even[k] })))
|
||
}
|
||
const setWidgets = (j, list) => setCell(j, { widgets: list, slide_id: null })
|
||
const add = (j, v) => {
|
||
if (v === 'slide') setCell(j, { widgets: [], slide_id: slides[0]?.id ?? null })
|
||
else setWidgets(j, [...cellWidgets(cells[j]), cellWidget(v)])
|
||
}
|
||
const kinds = [...CELL_KINDS, ...(depth === 0 ? ['hstack', 'vstack'] : [])]
|
||
const label = c => {
|
||
const ws = cellWidgets(c)
|
||
if (ws.length) return ws.map(x => kindLabel(t, x)).join(' · ')
|
||
if (c.slide_id != null) return slides.find(x => x.id === c.slide_id)?.name || t('signage.widgets.stack.slide')
|
||
return t('signage.widgets.stack.nothing')
|
||
}
|
||
return (
|
||
<div className="grid gap-3 rounded-control border border-line bg-surface p-3" data-testid="signage-stack-editor" data-depth={depth}>
|
||
<div className={ROW}>
|
||
<Field label={t('signage.widgets.stack.direction')}>
|
||
<Segmented value={w.direction || 'row'} onChange={direction => set({ direction })} fill data-testid="signage-stack-direction"
|
||
options={[{ value: 'row', icon: <Columns3 size={16} aria-hidden="true" />, ariaLabel: t('signage.widgets.stack.row'), testId: 'signage-stack-row' },
|
||
{ value: 'column', icon: <Rows3 size={16} aria-hidden="true" />, ariaLabel: t('signage.widgets.stack.column'), testId: 'signage-stack-column' }]} />
|
||
</Field>
|
||
<Field label={t('signage.widgets.stack.parts')}>
|
||
<Segmented value={String(cells.length)} onChange={v => count(Number(v))} fill data-testid="signage-stack-count"
|
||
options={[2, 3, 4, 5, 6].map(n => ({ value: String(n), label: String(n), testId: `signage-stack-count-${n}` }))} />
|
||
</Field>
|
||
<div className="flex items-end">
|
||
<Button variant="secondary" onClick={() => count(cells.length)} data-testid="signage-stack-even">{t('signage.widgets.stack.even')}</Button>
|
||
</div>
|
||
</div>
|
||
<ShareBar column={w.direction === 'column'} shares={shares} labels={cells.map(label)} onChange={setShares} />
|
||
<p className="text-caption text-ink-3 -mt-1">{t(w.direction === 'column' ? 'signage.widgets.stack.barHintColumn' : 'signage.widgets.stack.barHint')} {t('signage.widgets.stack.pickHint')}</p>
|
||
{cells.map((c, j) => {
|
||
const cp = `${path}.${j}`
|
||
const ws = cellWidgets(c)
|
||
const slide = !ws.length && c.slide_id != null
|
||
const setOne = (k, patch) => setWidgets(j, ws.map((x, m) => (m === k ? { ...x, ...patch } : x)))
|
||
const moveOne = (k, to) => { if (to >= 0 && to < ws.length) setWidgets(j, moved(ws, k, to)) }
|
||
return (
|
||
<div key={j} className="grid gap-3 rounded-control border border-line bg-surface-2 p-3" data-testid="signage-stack-cell-editor" data-index={j}>
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<span className="text-label font-semibold text-ink">{t('signage.widgets.stack.partTitle', { n: j + 1 })}</span>
|
||
<span className="text-caption text-ink-3 tabular-nums" data-testid="signage-stack-share">{shares[j]}%</span>
|
||
{!ws.length && !slide && <span className="text-caption text-ink-3">· {t('signage.widgets.stack.nothing')}</span>}
|
||
<span className="flex-1" />
|
||
<AddToPart kinds={kinds} onPick={v => add(j, v)} />
|
||
</div>
|
||
{slide && (
|
||
<div className="flex items-end gap-2">
|
||
<Field label={t('signage.widgets.stack.slide')} className="flex-1 max-w-md">
|
||
<Dropdown value={String(c.slide_id)} onChange={x => setCell(j, { slide_id: x ? Number(x) : null })} data-testid="signage-stack-slide"
|
||
options={slides.length ? slides.map(s => ({ value: String(s.id), label: s.name }))
|
||
: [{ value: String(c.slide_id), label: t('signage.widgets.stack.noSlides') }]} />
|
||
</Field>
|
||
<IconButton variant="ghost" label={t('signage.widgets.stack.removeSlide')} onClick={() => setCell(j, { slide_id: null })} data-testid="signage-stack-slide-remove">
|
||
<Trash2 size={15} />
|
||
</IconButton>
|
||
</div>
|
||
)}
|
||
{ws.map((x, k) => {
|
||
const Icon = iconOf(x)
|
||
const setW = patch => setOne(k, patch)
|
||
const wp = `${cp}.${k}`
|
||
const on = selected != null && (String(selected) === wp || String(selected).startsWith(`${wp}.`))
|
||
const custom = x.position === 'custom' && x.box
|
||
return (
|
||
<div key={k} onPointerDown={e => { e.stopPropagation(); onSelect?.(wp) }}
|
||
className={`grid gap-3 rounded-control border p-3 transition-colors duration-fast ${on ? 'border-accent ring-1 ring-accent bg-surface' : 'border-line bg-surface'}`}
|
||
data-testid="signage-stack-part-widget" data-kind={x.kind} data-path={wp} data-selected={on ? 'true' : undefined}>
|
||
<div className="flex items-center gap-2">
|
||
<Icon size={15} className="text-ink-2 shrink-0" aria-hidden="true" />
|
||
<span className="text-label font-semibold text-ink flex-1 min-w-0 truncate">{kindLabel(t, x)}</span>
|
||
{ws.length > 1 && <>
|
||
<IconButton size="sm" variant="ghost" label={t('signage.widgets.stack.before')} disabled={k === 0} onClick={() => moveOne(k, k - 1)}><ArrowUp size={14} /></IconButton>
|
||
<IconButton size="sm" variant="ghost" label={t('signage.widgets.stack.after')} disabled={k === ws.length - 1} onClick={() => moveOne(k, k + 1)}><ArrowDown size={14} /></IconButton>
|
||
</>}
|
||
<IconButton size="sm" variant="ghost" label={t('signage.widgets.stack.removeWidget')} onClick={() => setWidgets(j, ws.filter((_, m) => m !== k))}
|
||
data-testid="signage-stack-part-remove"><Trash2 size={14} /></IconButton>
|
||
</div>
|
||
{x.kind !== 'stack' && (
|
||
<div className={ROW}>
|
||
<Field label={t('signage.widgets.inPart')}>
|
||
{custom
|
||
? <Button variant="secondary" icon={<RotateCcw size={14} />} onClick={() => setW(fillPlace(x))} data-testid="signage-cell-fill">{t('signage.widgets.backInPlace')}</Button>
|
||
: <span className="h-control flex items-center text-label text-ink-2" data-testid="signage-cell-place">{t('signage.widgets.fillsPlace')}</span>}
|
||
</Field>
|
||
<AlignField w={x} set={setW} testid="signage-cell-align" />
|
||
<VerticalField w={x} set={setW} />
|
||
<InlineFields w={x} set={setW} />
|
||
</div>
|
||
)}
|
||
<LookFields w={x} set={setW} />
|
||
<BelowFields w={x} set={setW} media={media} onMedia={onMedia} extras={extras} path={wp} depth={depth + 1} selected={selected} onSelect={onSelect} />
|
||
</div>
|
||
)
|
||
})}
|
||
</div>
|
||
)
|
||
})}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* The stack's parts drawn to scale; the lines between them are dragged to change their sizes (the
|
||
* two parts beside a line share their room, each keeps at least 5 %), or focused and moved with the
|
||
* arrow keys (Shift: 5 %).
|
||
*/
|
||
function ShareBar({ column, shares, labels, onChange }) {
|
||
const { t } = useTranslation('manager')
|
||
const bar = useRef(null)
|
||
const drag = (e, j) => {
|
||
e.preventDefault()
|
||
const el = bar.current
|
||
if (!el) return
|
||
const r = el.getBoundingClientRect()
|
||
const len = column ? r.height : r.width
|
||
const p0 = column ? e.clientY : e.clientX
|
||
const start = [...shares]
|
||
const move = ev => {
|
||
const d = (((column ? ev.clientY : ev.clientX) - p0) / len) * 100
|
||
onChange(setShare(start, j, start[j] + d))
|
||
}
|
||
const up = () => { window.removeEventListener('pointermove', move); window.removeEventListener('pointerup', up) }
|
||
window.addEventListener('pointermove', move)
|
||
window.addEventListener('pointerup', up)
|
||
}
|
||
const key = (e, j) => {
|
||
const step = e.shiftKey ? 5 : 1
|
||
const d = { ArrowLeft: -step, ArrowUp: -step, ArrowRight: step, ArrowDown: step }[e.key]
|
||
if (d == null) return
|
||
e.preventDefault()
|
||
e.stopPropagation()
|
||
onChange(setShare(shares, j, shares[j] + d))
|
||
}
|
||
return (
|
||
<div ref={bar} className={`flex ${column ? 'flex-col h-40 w-full max-w-md' : 'h-14 w-full'} rounded-control overflow-hidden border border-line select-none`}
|
||
data-testid="signage-stack-bar">
|
||
{shares.map((sh, j) => (
|
||
<Fragment key={j}>
|
||
<span style={{ flex: `${sh} 1 0%` }} className="min-w-0 min-h-0 grid place-items-center content-center gap-0.5 bg-accent-soft text-accent-ink px-1 text-center">
|
||
<span className="text-label font-semibold tabular-nums">{sh}%</span>
|
||
<span className="text-caption truncate max-w-full">{labels[j]}</span>
|
||
</span>
|
||
{j < shares.length - 1 && (
|
||
<span role="separator" tabIndex={0} aria-orientation={column ? 'horizontal' : 'vertical'} aria-valuenow={sh} aria-valuemin={5} aria-valuemax={95}
|
||
aria-label={t('signage.widgets.stack.line', { a: j + 1, b: j + 2 })} onPointerDown={e => drag(e, j)} onKeyDown={e => key(e, j)}
|
||
className={`relative shrink-0 bg-accent outline-none focus-visible:ring-2 focus-visible:ring-accent ring-offset-1 ${column ? 'h-1.5 cursor-row-resize' : 'w-1.5 cursor-col-resize'}
|
||
after:absolute ${column ? 'after:-inset-y-2 after:inset-x-0' : 'after:-inset-x-2 after:inset-y-0'}`}
|
||
data-testid="signage-stack-line" data-index={j} />
|
||
)}
|
||
</Fragment>
|
||
))}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** "Add a widget" to a part: every widget (and a slide from the Slides library) in a small panel
|
||
* over the page; widgets add up, a slide takes the part's place. */
|
||
function AddToPart({ kinds, onPick }) {
|
||
const { t } = useTranslation('manager')
|
||
const [open, setOpen] = useState(false)
|
||
const btn = useRef(null)
|
||
const choose = k => { onPick(k); setOpen(false) }
|
||
return (
|
||
<>
|
||
<Button ref={btn} size="sm" variant="secondary" icon={<Plus size={14} />} onClick={() => setOpen(o => !o)} aria-expanded={open} aria-haspopup="menu" data-testid="signage-stack-add">
|
||
{t('signage.widgets.stack.add')}
|
||
</Button>
|
||
<Popover open={open} anchor={btn} onClose={() => setOpen(false)} role="menu" className="w-[22rem] grid grid-cols-2 gap-1.5 p-2" data-testid="signage-stack-add-menu">
|
||
{[...kinds, 'slide'].map(k => {
|
||
const Icon = k === 'slide' ? BookImage : ICON[k]
|
||
return (
|
||
<button key={k} type="button" role="menuitem" onClick={() => choose(k)} data-testid={`signage-stack-add-${k}`}
|
||
className={`flex items-center gap-2 px-2.5 py-2 rounded-control text-left text-label text-ink hover:bg-surface-2 ${k === 'slide' ? 'col-span-2' : ''}`}>
|
||
<Icon size={15} className="text-ink-2 shrink-0" aria-hidden="true" />
|
||
{k === 'slide' ? t('signage.widgets.stack.aSlideInstead') : t(`signage.widgets.kinds.${k}`)}
|
||
</button>
|
||
)
|
||
})}
|
||
</Popover>
|
||
</>
|
||
)
|
||
}
|