Files
scalesApp/frontend/src/components/ReportEditor/a4/A4PageSetup.jsx
T
gitea 122480cd46 Add the A4 report designer and fix band layout
Brings in the A4 (millimetre) report designer alongside the existing
matrix editor, migrates the frontend from react-scripts to Vite/Vitest,
and fixes four defects found while using the A4 band tools.

Layout — layout.js now implements the standard banded-report model:
everything prints where it was drawn, and a band growing per data row is
the only thing that moves anything. Previously `paginate` re-flowed every
band from the top margin, so a band placed halfway down the page
previewed at the top, and `header` was treated as a page header and
hoisted above the statics drawn before it. A header is a COLUMN header:
it prints in place and repeats only on a continuation page. Statics now
take part too — one drawn below a band keeps its drawn gap as that band
grows, and follows onto the next page rather than vanishing when pushed
past the bottom margin. The displacement is keyed on the band's design
bottom, not on an instance's laid-out y: every row of a band shares one
design position, so keying on the instance measured the gap from row 1
and let the static overlap rows 2..n.

Band children — a child's x is relative to its band, as its y already
was. layout() emitted it raw, so a field printed measured from the paper
edge instead of the band's left margin. Instances now carry bandX.

Band placement — a band's x comes from the page margins, so creation
takes only the click's Y (the factory's x was being overwritten by the
spread order) and dragging is locked to the vertical axis. Bands render
as a strip, with top/bottom rules only, matching the matrix editor.
fromApiReport reflows on load so reports saved with a stray x straighten
themselves. Matrix bands are constrained to vertical too.

Drag lifecycle — useElementDrag re-subscribed its document listeners on
every render during a drag, because the effect depended on an unmemoized
onDrag. Each re-subscribe removed the mouseup listener before adding the
next, measured as add/remove/add/remove/add across two mousemoves, and a
release landing in that gap was lost: the element kept following the
cursor until the next click. Snapping made it routine, since a held
guide stops x/y changing and leaves the guide overlay's render sitting
between the move and the release. onDrag and dragData now go through
refs so the effect depends on [isDragging] alone.

Note: the matrix editor has no test coverage, so its band constraint is
verified by reading only. The A4 suite is 201 passing tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-20 12:52:20 +03:00

122 lines
4.3 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import React from 'react';
import { MmInput } from './A4Inspector';
import { FONT_FAMILIES } from './fonts';
import { PAGE } from './units';
import '../ObjectInspector.css';
import './A4Inspector.css';
import './A4PageSetup.css';
/**
* Page setup panel: orientation, the four margins in mm, and the defaults new
* elements inherit.
*
* Margins are NOT cosmetic. A band's x/width are derived from the content area
* (A4Canvas `contentWidthOf`), so changing a margin has to reflow every existing
* band — otherwise a band keeps the width it was created with and silently
* overhangs the new margin. `A4ReportEditor.handlePageSetupChange` owns that
* reflow; this panel only reports the new setup.
*
* Margins use MmInput for the same reason every other mm field does: a
* `type="number"` input discards the intermediate "1." and the decimal point
* appears not to type. Font size is pt with a 0.5 step, matching the inspector.
*/
const MARGIN_FIELDS = [
{ key: 'top', label: 'Top' },
{ key: 'right', label: 'Right' },
{ key: 'bottom', label: 'Bottom' },
{ key: 'left', label: 'Left' }
];
const FONT_SIZES = [6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 32, 36, 48];
function A4PageSetup({ pageSetup, onChange, onClose }) {
const setup = pageSetup || {};
const margins = setup.margins || {};
const orientation = setup.orientation === 'landscape' ? 'landscape' : 'portrait';
const size = PAGE.A4[orientation];
const handleMarginChange = (key, value) => {
onChange({ ...setup, margins: { ...margins, [key]: value } });
};
return (
<div className="a4-page-setup-overlay" onClick={onClose}>
<div className="a4-page-setup" onClick={e => e.stopPropagation()}>
<div className="a4-page-setup-header">
<h2>Page Setup</h2>
<button className="a4-page-setup-close" onClick={onClose} title="Close">×</button>
</div>
<div className="a4-page-setup-body a4-inspector">
<div className="inspector-section">
<div className="section-title">Page</div>
<div className="property-row">
<label>Orientation</label>
<select
value={orientation}
onChange={e => onChange({ ...setup, orientation: e.target.value })}
>
<option value="portrait">Portrait</option>
<option value="landscape">Landscape</option>
</select>
</div>
<div className="a4-page-setup-dims">
A4 {orientation} — {size.w} × {size.h} mm
</div>
</div>
<div className="inspector-section">
<div className="section-title">Margins (mm)</div>
{MARGIN_FIELDS.map(({ key, label }) => (
<MmInput
key={key}
label={label}
value={margins[key] ?? 0}
min={0}
onCommit={(v) => handleMarginChange(key, v)}
/>
))}
<div className="a4-page-setup-hint">
Bands span the content width, so changing a side margin resizes
every band in the report.
</div>
</div>
<div className="inspector-section">
<div className="section-title">Defaults for new elements</div>
<div className="property-row">
<label>Font</label>
<select
value={setup.defaultFont || 'Arial'}
onChange={e => onChange({ ...setup, defaultFont: e.target.value })}
>
{FONT_FAMILIES.map(f => (
<option key={f} value={f}>{f}</option>
))}
</select>
</div>
<div className="property-row">
<label>Size (pt)</label>
<select
value={setup.defaultFontSize || 10}
onChange={e => onChange({ ...setup, defaultFontSize: parseFloat(e.target.value) })}
>
{FONT_SIZES.map(s => (
<option key={s} value={s}>{s}</option>
))}
</select>
</div>
<div className="a4-page-setup-hint">
Applies to elements created from now on. Existing elements keep
the font they were given.
</div>
</div>
</div>
</div>
</div>
);
}
export default A4PageSetup;