Files
TREK/client/src/components/Studio/StudioSwatches.tsx
T
Maurice 00018959c6 fix(studio): a full page takes no new element, the opacity slider commits a move only, a theme draft survives saves
Every add in the Studio (a sticker, a shape, an icon, a text, a frame, a line,
a travel element, a photo from the content panel or a drop), a text style and
a grid of frames appended without looking at the page's cap of 90 elements,
which paste and duplicate already kept. The save then failed with a 400 and
every later save of the book failed the same way. Each of them now leaves a
full page as it is and selects nothing, as a paste does.

The opacity slider committed on every key let go on it, so the Tab that moved
the focus there wrote the value it was last moved to, an empty undo step at
best and another colour's opacity at worst. It commits once after it moved,
on the key or the pointer let go or when the focus leaves.

The Theme panel threw away colours, faces and motifs being tried whenever the
book came back from a save by another editor or the assistant, because the
same theme read anew is a new object. The draft now follows the book's theme
only when the theme itself changed.
2026-10-24 04:19:25 +00:00

153 lines
5.5 KiB
TypeScript

import { useRef } from 'react'
import { BookOpen, History, Image } from 'lucide-react'
import { useStudioPalette } from './paint/paletteModel'
import '../../styles/studio-paint.css'
/**
* The colour picker, shared by everything in the inspector that has a colour.
*
* Its own file because both the plain elements and the travel elements need it,
* and the second copy of a swatch row is how two parts of one panel end up
* offering different palettes.
*
* The palette is print ink rather than app chrome — it does not follow the
* user's theme, because a book is printed once and read on paper, where an
* accent chosen for a dark interface means nothing.
*
* Under the fixed inks sit up to three short rows from the open book
* (paint/StudioPalette.tsx): the colours the book already uses, the colours
* of the photos on this page, and what this person picked lately. Each row
* is there only when it has something in it, so a new book shows the inks
* alone, as before.
*/
const SWATCHES = [
'#111111', '#ffffff', '#8a8578',
'#c2410c', '#b45309', '#0f766e',
'#1e3a8a', '#9f1239', '#4c1d95',
]
function Swatch({ color, on, onPick }: { color: string; on: boolean; onPick: (c: string) => void }) {
return (
<button type="button"
className={`st-swatch ${on ? 'is-on' : ''}`}
style={{ background: color }} // theme-lint-disable: a colour picker shows the colours themselves
onClick={() => onPick(color)}
aria-label={color}
aria-pressed={on}
/>
)
}
export function Swatches({ value, onPick, alpha, onAlpha, rows = true }: {
value: string
onPick: (c: string) => void
/** With `onAlpha`, an opacity slider under the swatches, 0 to 1. */
alpha?: number
/** `done` is false while the slider is dragged and true when it is let go. */
onAlpha?: (a: number, done: boolean) => void
/** The book, photo and recent rows; off where only the fixed inks make sense. */
rows?: boolean
}) {
const palette = useStudioPalette()
const current = value.toLowerCase()
// The wheel reports every colour it passes through; only the one it stops on is worth remembering.
const wheel = useRef<string | null>(null)
const pick = (c: string) => {
palette?.remember(c)
onPick(c)
}
const groups = rows && palette
? ([
['book', BookOpen, palette.book],
['photos', Image, palette.photos],
['recent', History, palette.recent],
] as const).filter(([, , list]) => list.length > 0)
: []
return (
<div className="st-swatch-stack">
<div className="st-swatches">
{SWATCHES.map(c => <Swatch key={c} color={c} on={current === c} onPick={pick} />)}
{/* Anything at all, for a book that has to match a cover or a brand. */}
<label className="st-swatch st-swatch-custom">
{/* Named by the colour it holds, the way the preset swatches above are. */}
<input
type="color"
aria-label={value}
value={value}
onChange={e => { wheel.current = e.target.value; onPick(e.target.value) }}
onBlur={() => { if (wheel.current) palette?.remember(wheel.current); wheel.current = null }}
/>
</label>
</div>
{groups.map(([key, Icon, list]) => (
<div key={key} className="st-swatch-row" role="group" aria-label={palette?.labels[key]}>
<span className="st-swatch-row-icon" title={palette?.labels[key]} aria-hidden>
<Icon size={12} strokeWidth={2} />
</span>
<div className="st-swatches">
{list.map(c => <Swatch key={c} color={c} on={current === c} onPick={pick} />)}
</div>
</div>
))}
{onAlpha && <AlphaSlider color={value} value={alpha ?? 1} onChange={onAlpha} label={palette?.labels.opacity} />}
</div>
)
}
/**
* How opaque the colour is, as a slider over the colour itself.
*
* The track runs from nothing to the full colour over a chequerboard, so the
* slider shows what it does rather than needing a label to explain it, and
* the figure beside it says the exact value for anyone matching a reference.
*/
export function AlphaSlider({ color, value, onChange, label }: {
color: string
value: number
onChange: (a: number, done: boolean) => void
/** The slider's accessible name, in the reader's language. */
label?: string
}) {
const pct = Math.round(value * 100)
// What the slider was last moved to: a controlled input may still show the old value when it is let go.
const moved = useRef(value)
// Whether it moved since the last commit: a key let go on it (the Tab that brought the focus) is no change.
const dirty = useRef(false)
const move = (raw: string) => {
moved.current = Math.max(0, Math.min(100, Number(raw))) / 100
dirty.current = true
onChange(moved.current, false)
}
const finish = () => {
if (!dirty.current) return
dirty.current = false
onChange(moved.current, true)
}
return (
<div className="st-alpha">
<div className="st-alpha-track" style={{ ['--st-alpha-colour' as string]: color }}>
<input
type="range"
min={0}
max={100}
step={1}
value={pct}
aria-label={label}
onPointerDown={() => { moved.current = value }}
onKeyDown={() => { moved.current = value }}
onChange={e => move(e.target.value)}
onPointerUp={finish}
onKeyUp={finish}
onBlur={finish}
/>
</div>
<span className="st-alpha-value">{pct}%</span>
</div>
)
}