|
8 | 8 |
|
9 | 9 | import type { BleedConfig, SafeAreaConfig, SlugConfig } from '@varve/scene'; |
10 | 10 | import { resolvePagePrintGeometry } from '@varve/scene'; |
| 11 | +import { BUILTIN_PRESET_GROUPS } from '@varve/shared'; |
| 12 | +import { Select } from '@varve/ui'; |
11 | 13 | import { useCallback, useMemo } from 'react'; |
12 | 14 | import { useEditor } from '../../../context'; |
13 | 15 | import { DisclosureSection } from '../controls/DisclosureSection'; |
| 16 | +import { FieldRow } from '../controls/FieldRow'; |
14 | 17 | import { NumberField } from '../controls/NumberField'; |
15 | 18 | import './page-print.css'; |
16 | 19 |
|
| 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 | + |
17 | 60 | function EdgeFields({ |
18 | 61 | label, |
19 | 62 | values, |
@@ -62,7 +105,7 @@ function EdgeFields({ |
62 | 105 | } |
63 | 106 |
|
64 | 107 | export function PagePrintSection() { |
65 | | - const { state, setPageBleed, setPageSafeArea, setPageSlug } = useEditor(); |
| 108 | + const { state, setPageBleed, setPageSafeArea, setPageSlug, resizePage } = useEditor(); |
66 | 109 | const doc = state.document; |
67 | 110 | const pageId = doc.activePageId; |
68 | 111 |
|
@@ -123,11 +166,70 @@ export function PagePrintSection() { |
123 | 166 | [pageId, page?.slug, setPageSlug], |
124 | 167 | ); |
125 | 168 |
|
| 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 | + |
126 | 191 | if (state.tool !== 'page' || !pageId || !geometry) return null; |
127 | 192 |
|
128 | 193 | return ( |
129 | 194 | <DisclosureSection title="Page Print" sectionId="page-print" defaultExpanded> |
130 | 195 | <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> |
131 | 233 | <h4 className="page-print__sub">Bleed</h4> |
132 | 234 | <EdgeFields label="Bleed" values={geometry.bleed} unit="px" onChange={handleBleed} /> |
133 | 235 | <h4 className="page-print__sub">Safe area</h4> |
|
0 commit comments