Files
TREK/client/src/components/Studio/sidebar/StudioPagesPanel.tsx
T
Maurice bdf5767a5f feat(studio): rebuild both side panels around one head, one row and one order
The rail lists its sections in the order a book is made: Pages, Content,
Layouts, Elements and Travel, then Theme and Layers set apart below a rule.
Stickers are a tab of Elements beside the icons instead of a rail section of
their own, and Elements has six tabs (text, shapes, lines, frames with the
grids, icons, stickers). The quotation mark moved to the text styles, the
outline rectangle to the basic shapes and the plain rule to the lines. The rail
and every tab row are tab lists the arrow keys walk through, and a long name
wraps instead of being cut. The icon and sticker searches say in the field how
many match, which the sticker library's own head used to.

Every panel on both sides opens on the same head, and every labelled control
uses one row that moves the control under its label when the two do not fit,
so no label sits in a fixed column any more. Number fields, chips, sliders,
tab labels, theme names and travel cards wrap the same way, a country beside
a stop wraps under itself, and an upload cell holds its words to two lines
with the whole label in its tooltip. The heads, shelves and groups are real
headings, so a screen reader can walk the panels by them.

The inspector shows every kind in one order: what the element is and says,
its position and size, its look, its effects, then Arrange. Align and the
stacking buttons are one Arrange group with a button that opens the page in
Layers; its rows sit under their labels, each in two sets that move to a
second line whole. Rarely used groups start folded and open on an element
that already uses them (a turned element, a map held at a zoom, a summary set
in another face), and every group mounts afresh for the next selection. A
sticker's duplicate angle and opacity are gone in favour of the shared
position group, which now has the turn buttons for every kind. With several
elements selected it keeps their angle, opacity and turn, and the mirror when
all of them are stickers, and leaves the frame's numbers to one element. A
photo's fill page and fill spread sit under its crop. With nothing selected
the inspector shows the page background, the book's theme with the way to the
Theme panel, and the page numbers.

Seventeen translation keys nothing has used since before the rebuild (the old
inspector title, the empty spread note, the summary and country list names,
the roads buttons and others) are gone from every language. The broad key
template the old panels used had counted them as reached; the format names,
the empty photo grid messages and the map source hints are now named where the
key check can read them.

Also fixed on the way: the travel stops list shared its class with the
gradient stops and was laid out as sixteen pixel handles; the alignment
buttons were named in raw English; the map's print resolution note joined its
two halves with a dash.
2026-10-24 03:08:27 +00:00

228 lines
8.7 KiB
TypeScript

import type { BookPageSetup } from '@trek/shared';
import { ChevronDown, ChevronUp, Copy, FileDown, Plus, Trash2 } from 'lucide-react';
import { useRef, useState, type ChangeEvent } from 'react';
import { useElementSize } from '../../../hooks/useElementSize';
import { useStudioStore } from '../../../store/studioStore';
import { folioLabel, foliosOf } from '../bookSheets';
import { MAX_SPREAD_FILE_BYTES, importSpread } from '../spreadFile';
import { SpreadThumb } from '../SpreadThumb';
import { PanelHead } from '../StudioPanelHead';
/**
* The Pages section of the rail: the book's pages in order, the actions on
* each, and the two ways to a new one.
*
* Page thumbnails are drawn at a `scale()` rather than laid out by CSS (the
* sheet inside them is sized in millimetres), so they need an actual pixel
* width, and it is **measured** rather than derived. Deriving it was tried
* twice and was wrong twice: the scrollbar appears only once the list is long
* enough and takes a width that depends on the platform, and the arithmetic
* that ignored it produced a card overhanging its own selection outline.
* Measuring the row the cards sit in gets every contribution at once.
*/
/** The narrowest a page preview is drawn. */
const MIN_PREVIEW_W = 120;
/** The button's border and padding, which the row's width still includes. */
const THUMB_CHROME = (2 + 3) * 2;
export function StudioPagesPanel({
page,
pxPerMm,
bookView,
t,
}: {
page: BookPageSetup;
pxPerMm: number;
bookView: boolean;
t: (k: string) => string;
}) {
const doc = useStudioStore((s) => s.doc);
const active = useStudioStore((s) => s.activeSpread);
// The page a chat card points at, outlined here (journey-studio.md 4.2).
const hinted = useStudioStore((s) => s.hintSpread);
const setActive = useStudioStore((s) => s.setActiveSpread);
const addSpread = useStudioStore((s) => s.addSpread);
const insertSpread = useStudioStore((s) => s.insertSpread);
const duplicateSpread = useStudioStore((s) => s.duplicateSpread);
const removeSpread = useStudioStore((s) => s.removeSpread);
const moveSpread = useStudioStore((s) => s.moveSpread);
const canEditSpread = useStudioStore((s) => s.canEditSpread);
const spreads = doc?.spreads ?? [];
const list = useElementSize<HTMLDivElement>();
const thumbWidth = Math.max(MIN_PREVIEW_W, list.width - THUMB_CHROME);
/** Where a new spread would land: after the last inner one. */
const lastInner = spreads.reduce((last, sp, i) => (sp.role === 'inner' ? i : last), -1);
const fileInput = useRef<HTMLInputElement>(null);
const [importError, setImportError] = useState<string | null>(null);
const importFile = async (e: ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
// Cleared straight away so choosing the same file twice in a row still fires a change event the second time.
e.target.value = '';
if (!file) return;
setImportError(null);
// Checked before reading rather than after: the point of a limit on a file someone else wrote is not to parse it.
if (file.size > MAX_SPREAD_FILE_BYTES) {
setImportError(t('journey.studio.importSpreadFailed'));
return;
}
try {
const spread = importSpread(JSON.parse(await file.text()), page);
if (!spread) {
setImportError(t('journey.studio.importSpreadFailed'));
return;
}
insertSpread(lastInner, spread);
} catch {
setImportError(t('journey.studio.importSpreadFailed'));
}
};
return (
<>
<PanelHead label={t('journey.studio.pages')} count={spreads.length} />
<div className="st-panel-scroll">
<div className="st-thumbs" ref={list.ref}>
{spreads.map((sp, i) => {
const editable = canEditSpread(i);
const folios = foliosOf(spreads, i, page.pageNumbers.startAt);
return (
<div className="st-thumb-row" key={sp.id}>
<button
type="button"
className={`st-thumb ${i === active ? 'is-active' : ''} ${sp.id === hinted ? 'is-hinted' : ''}`}
aria-current={i === active ? 'page' : undefined}
onClick={() => setActive(i)}
>
<SpreadThumb
spread={sp}
page={page}
width={thumbWidth}
pxPerMm={pxPerMm}
folios={folios}
fold={bookView}
className="st-thumb-sheet"
/>
<span className="st-thumb-label">
{sp.role === 'cover'
? t('journey.studio.cover')
: sp.role === 'back'
? t('journey.studio.backCover')
: folioLabel(folios)}
</span>
</button>
{/*
The actions sit on the thumbnail rather than in a menu bar of
their own: what you are acting on is the page you are pointing
at. The cover and the back cover have none: a book has exactly
one of each, and moving or deleting them is not a thing anyone
means to do.
*/}
{editable && (
<div className="st-thumb-actions">
<button
type="button"
onClick={() => moveSpread(i, -1)}
disabled={!canEditSpread(i - 1)}
title={t('journey.studio.movePageUp')}
aria-label={t('journey.studio.movePageUp')}
>
<ChevronUp size={13} />
</button>
<button
type="button"
onClick={() => moveSpread(i, 1)}
disabled={!canEditSpread(i + 1)}
title={t('journey.studio.movePageDown')}
aria-label={t('journey.studio.movePageDown')}
>
<ChevronDown size={13} />
</button>
<button
type="button"
onClick={() => duplicateSpread(i)}
title={t('journey.studio.duplicatePage')}
aria-label={t('journey.studio.duplicatePage')}
>
<Copy size={12} />
</button>
<button
type="button"
className="is-danger"
onClick={() => removeSpread(i)}
title={t('journey.studio.deletePage')}
aria-label={t('journey.studio.deletePage')}
>
<Trash2 size={12} />
</button>
</div>
)}
{/* Insert between two spreads, where the new one will go: a single
"add" at the end cannot express "another page here". */}
{editable && (
<button
type="button"
className="st-thumb-insert"
onClick={() => addSpread(i)}
title={t('journey.studio.addPageAfter')}
aria-label={t('journey.studio.addPageAfter')}
>
<Plus size={12} />
</button>
)}
</div>
);
})}
</div>
{/*
Two ways to get a new page: an empty one, or somebody else's design.
Side by side because they answer the same question; the import is not
a setting buried in a menu, it is the other half of "add".
*/}
<div className="st-add-row">
<button
type="button"
className="st-add-page"
onClick={() => addSpread(lastInner)}
title={t('journey.studio.addPage')}
aria-label={t('journey.studio.addPage')}
>
<Plus size={14} aria-hidden />
{t('journey.studio.addPageShort')}
</button>
<button
type="button"
className="st-add-page is-import"
onClick={() => fileInput.current?.click()}
title={t('journey.studio.importSpreadHint')}
>
<FileDown size={14} aria-hidden />
{t('journey.studio.importSpread')}
</button>
</div>
<input
ref={fileInput}
type="file"
accept=".json,application/json"
hidden
onChange={(e) => {
void importFile(e);
}}
/>
{importError && (
<p className="st-add-error" role="alert">
{importError}
</p>
)}
</div>
</>
);
}