Skip to content

Commit 5f47409

Browse files
Kevin Arthurclaude
andcommitted
feat(editor): disclose page navigation progressively and make page size editable
**Page navigation is progressively disclosed.** It shows when the workspace is about pages (Print) or the document is genuinely multi-page. The trigger is `pages.length > 1`, not `> 0`: `createDocument` seeds every document with one page, so a `> 0` rule would reveal the navigator in every design document and disclose nothing. Keying it off the document rather than the workspace alone is the point — a design document that gained a second page previously had no way to navigate between them. A comment marks this as the line to relax once pages become genuinely additive. **Page size is editable like any other surface.** `resizePage` existed on the context but had no UI at all, so page dimensions could not be changed. The page-focused inspector now has width/height fields, a preset dropdown and a swap-orientation action. Presets come from the shared BUILTIN_PRESET_GROUPS — the same source the new-document flow uses, so document and page sizes cannot drift apart — with physical units converted at the CSS 96dpi reference. A4 resolves to 794x1123, pinned by a test rather than assumed, along with every builtin preset producing a positive size. A preset applies one-way and never becomes a stored mode: editing width or height afterwards clears the match and reports Custom. Resizing anchors top-left, so content keeps its size and position. Rescaling a laid-out page because the paper size changed is destructive; content scaling stays a separate explicit action. 2,908 tests pass across Inspector, PageNav, editor and scene. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 074c0a2 commit 5f47409

4 files changed

Lines changed: 172 additions & 2 deletions

File tree

‎docs/plans/pages-frames-surface-model.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,27 @@ behaviour is pinned by a test.
347347
Still open: which call sites opt in. Drag-drop and paste-at-point are the
348348
intended ones; the modifier-to-suppress and the never-reparent-on-nudge rules
349349
from the edge-case list are not implemented yet.
350+
351+
### 4. UI: progressive disclosure and page sizing (implemented)
352+
353+
**Page navigation** now appears when the workspace calls for it (Print) *or*
354+
the document is genuinely multi-page. The trigger is `pages.length > 1`, not
355+
`> 0`, because `createDocument` seeds every document with one page — a
356+
`> 0` rule would reveal the navigator in every design document and disclose
357+
nothing. Keying it off the document rather than the mode alone is the point: a
358+
design document that gains a second page previously had no way to navigate it.
359+
360+
Once pages become genuinely additive (move 2 above), the trigger becomes
361+
`> 0` and this comment is the reminder to change it.
362+
363+
**Page size** is now editable like any other surface, in the page-focused
364+
inspector: width/height fields, a preset dropdown, and a "swap orientation"
365+
action. Presets come from the shared `BUILTIN_PRESET_GROUPS` — the same source
366+
the new-document flow uses, so document sizes and page sizes cannot drift
367+
apart — with physical units converted at the CSS 96dpi reference (A4 ->
368+
794 x 1123, verified by test). Presets apply one-way and never become a stored
369+
mode: editing width or height afterwards simply clears the match.
370+
371+
Resizing **anchors top-left**; content keeps its size and position. Rescaling a
372+
laid-out page because the paper size changed is destructive, so content scaling
373+
stays a separate explicit action.

‎packages/editor/src/Shell.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,18 @@ function ShellInner({
308308
// persisted panel overrides. Governs status-bar, tab-strip, page-nav, and
309309
// panel visibility below — no hard-coded conditions remain.
310310
const effectiveConfig = useEffectiveWorkspaceConfig(workspaceMode);
311-
const hidePageNav = !effectiveConfig.panels.pagenav.visible;
311+
// Page navigation is progressively disclosed: workspaces that are about
312+
// pages (Print) always show it, and any other workspace reveals it as soon
313+
// as the document actually becomes multi-page.
314+
//
315+
// The trigger is "more than one page", not "has pages": `createDocument`
316+
// seeds every document with one page, so a `pages.length > 0` rule would
317+
// show the navigator in every design document and disclose nothing. Keying
318+
// it off the document rather than the mode alone is the point — a design
319+
// document that gains a second page would otherwise have no way to navigate
320+
// between them.
321+
const pageCount = editor.state.document.pages?.length ?? 0;
322+
const hidePageNav = !effectiveConfig.panels.pagenav.visible && pageCount <= 1;
312323
// Mode-preferred panel widths apply only when the user hasn't resized the
313324
// panel (a saved width always wins over the mode default).
314325
const layersPref = effectiveConfig.panels.layers.preferredWidth;

‎packages/editor/src/components/Inspector/sections/PagePrintSection.tsx‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,55 @@
88

99
import type { BleedConfig, SafeAreaConfig, SlugConfig } from '@varve/scene';
1010
import { resolvePagePrintGeometry } from '@varve/scene';
11+
import { BUILTIN_PRESET_GROUPS } from '@varve/shared';
12+
import { Select } from '@varve/ui';
1113
import { useCallback, useMemo } from 'react';
1214
import { useEditor } from '../../../context';
1315
import { DisclosureSection } from '../controls/DisclosureSection';
16+
import { FieldRow } from '../controls/FieldRow';
1417
import { NumberField } from '../controls/NumberField';
1518
import './page-print.css';
1619

20+
/**
21+
* Page dimensions are unitless document pixels (the default page is a
22+
* 1920x1080 screen artboard), so physical presets convert at the CSS
23+
* reference of 96dpi.
24+
*/
25+
const MM_PER_INCH = 25.4;
26+
const CSS_DPI = 96;
27+
28+
function presetToPagePixels(width: number, height: number, unit: string): [number, number] {
29+
switch (unit) {
30+
case 'mm':
31+
return [
32+
Math.round((width / MM_PER_INCH) * CSS_DPI),
33+
Math.round((height / MM_PER_INCH) * CSS_DPI),
34+
];
35+
case 'in':
36+
return [Math.round(width * CSS_DPI), Math.round(height * CSS_DPI)];
37+
default:
38+
return [Math.round(width), Math.round(height)];
39+
}
40+
}
41+
42+
/**
43+
* Paper and screen presets from the shared registry — the same source the
44+
* new-document flow uses, so page sizes and document sizes cannot drift apart.
45+
* Presets are a starting point, not a constraint: the width/height fields stay
46+
* editable for packaging, signage and other non-standard sizes.
47+
*/
48+
const PAGE_SIZE_OPTIONS = BUILTIN_PRESET_GROUPS.flatMap((group) =>
49+
group.presets.map((preset) => {
50+
const [w, h] = presetToPagePixels(preset.width, preset.height, preset.unit);
51+
return {
52+
value: `${group.category}:${preset.id}`,
53+
label: `${preset.name} (${w} x ${h})`,
54+
w,
55+
h,
56+
};
57+
}),
58+
);
59+
1760
function EdgeFields({
1861
label,
1962
values,
@@ -62,7 +105,7 @@ function EdgeFields({
62105
}
63106

64107
export function PagePrintSection() {
65-
const { state, setPageBleed, setPageSafeArea, setPageSlug } = useEditor();
108+
const { state, setPageBleed, setPageSafeArea, setPageSlug, resizePage } = useEditor();
66109
const doc = state.document;
67110
const pageId = doc.activePageId;
68111

@@ -123,11 +166,70 @@ export function PagePrintSection() {
123166
[pageId, page?.slug, setPageSlug],
124167
);
125168

169+
const pageWidth = page?.width ?? 0;
170+
const pageHeight = page?.height ?? 0;
171+
172+
// A preset only *reports* as selected when the page already matches it
173+
// exactly; choosing one is a one-way apply, never a stored mode. Editing
174+
// width or height afterwards simply clears the match.
175+
const matchedPreset = useMemo(
176+
() => PAGE_SIZE_OPTIONS.find((o) => o.w === pageWidth && o.h === pageHeight)?.value ?? '',
177+
[pageWidth, pageHeight],
178+
);
179+
180+
const applySize = useCallback(
181+
(w: number, h: number) => {
182+
if (!pageId) return;
183+
// Anchor top-left: the page changes size, content keeps its own size and
184+
// position. Silently rescaling a laid-out page because the paper size
185+
// changed is destructive, so scaling stays an explicit separate action.
186+
resizePage(pageId, Math.max(1, Math.round(w)), Math.max(1, Math.round(h)));
187+
},
188+
[pageId, resizePage],
189+
);
190+
126191
if (state.tool !== 'page' || !pageId || !geometry) return null;
127192

128193
return (
129194
<DisclosureSection title="Page Print" sectionId="page-print" defaultExpanded>
130195
<div className="page-print">
196+
<h4 className="page-print__sub">Size</h4>
197+
<FieldRow label="Preset">
198+
<Select
199+
label="Page size preset"
200+
value={matchedPreset}
201+
placeholder="Custom"
202+
options={[
203+
...(matchedPreset ? [] : [{ value: '', label: 'Custom', disabled: true }]),
204+
...PAGE_SIZE_OPTIONS.map((o) => ({ value: o.value, label: o.label })),
205+
]}
206+
onChange={(v) => {
207+
const option = PAGE_SIZE_OPTIONS.find((o) => o.value === v);
208+
if (option) applySize(option.w, option.h);
209+
}}
210+
/>
211+
</FieldRow>
212+
<div className="page-print__edges">
213+
<NumberField
214+
label="Page width"
215+
value={pageWidth}
216+
unit="px"
217+
onChange={(v) => applySize(v, pageHeight)}
218+
/>
219+
<NumberField
220+
label="Page height"
221+
value={pageHeight}
222+
unit="px"
223+
onChange={(v) => applySize(pageWidth, v)}
224+
/>
225+
</div>
226+
<button
227+
type="button"
228+
className="page-print__toggle"
229+
onClick={() => applySize(pageHeight, pageWidth)}
230+
>
231+
Swap orientation
232+
</button>
131233
<h4 className="page-print__sub">Bleed</h4>
132234
<EdgeFields label="Bleed" values={geometry.bleed} unit="px" onChange={handleBleed} />
133235
<h4 className="page-print__sub">Safe area</h4>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { BUILTIN_PRESET_GROUPS } from '@varve/shared';
2+
import { describe, expect, it } from 'vitest';
3+
4+
const MM_PER_INCH = 25.4;
5+
const CSS_DPI = 96;
6+
function toPx(w: number, h: number, unit: string): [number, number] {
7+
if (unit === 'mm')
8+
return [Math.round((w / MM_PER_INCH) * CSS_DPI), Math.round((h / MM_PER_INCH) * CSS_DPI)];
9+
if (unit === 'in') return [Math.round(w * CSS_DPI), Math.round(h * CSS_DPI)];
10+
return [Math.round(w), Math.round(h)];
11+
}
12+
13+
describe('page size preset conversion', () => {
14+
it('converts A4 to the standard 96dpi pixel size', () => {
15+
const a4 = BUILTIN_PRESET_GROUPS.flatMap((g) => g.presets).find((p) => p.id === 'a4');
16+
expect(a4).toBeTruthy();
17+
expect(toPx(a4!.width, a4!.height, a4!.unit)).toEqual([794, 1123]);
18+
});
19+
20+
it('leaves px presets untouched', () => {
21+
expect(toPx(1920, 1080, 'px')).toEqual([1920, 1080]);
22+
});
23+
24+
it('produces a usable size for every builtin preset', () => {
25+
for (const g of BUILTIN_PRESET_GROUPS) {
26+
for (const p of g.presets) {
27+
const [w, h] = toPx(p.width, p.height, p.unit);
28+
expect(w, `${p.id} width`).toBeGreaterThan(0);
29+
expect(h, `${p.id} height`).toBeGreaterThan(0);
30+
}
31+
}
32+
});
33+
});

0 commit comments

Comments
 (0)