@cosmicdrift/kumiko-document-designer 0.417.0 → 0.419.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/web/apply-layout-dialog.d.ts +7 -0
  2. package/dist/web/apply-layout-dialog.js +95 -0
  3. package/dist/web/company-data-rail.d.ts +5 -0
  4. package/dist/web/company-data-rail.js +51 -0
  5. package/dist/web/frame-position.d.ts +18 -0
  6. package/dist/web/frame-position.js +28 -0
  7. package/dist/web/frame-settings-section.d.ts +13 -0
  8. package/dist/web/frame-settings-section.js +57 -0
  9. package/dist/web/i18n.js +250 -3
  10. package/dist/web/layout-appearance-section.d.ts +8 -0
  11. package/dist/web/layout-appearance-section.js +55 -0
  12. package/dist/web/layout-editor-body.js +5 -2
  13. package/dist/web/layout-editor-logic.d.ts +3 -1
  14. package/dist/web/layout-editor-logic.js +15 -7
  15. package/dist/web/layout-editor-paper.d.ts +1 -1
  16. package/dist/web/layout-editor-paper.js +36 -8
  17. package/dist/web/layout-html.js +2 -2
  18. package/dist/web/layout-meta.d.ts +37 -0
  19. package/dist/web/layout-meta.js +83 -1
  20. package/dist/web/layout-rails.d.ts +7 -1
  21. package/dist/web/layout-rails.js +9 -4
  22. package/dist/web/layout-types.d.ts +2 -2
  23. package/dist/web/layout-usage-section.d.ts +4 -0
  24. package/dist/web/layout-usage-section.js +71 -0
  25. package/dist/web/page-canvas.d.ts +6 -3
  26. package/dist/web/page-canvas.js +23 -32
  27. package/dist/web/page-ruler.d.ts +6 -0
  28. package/dist/web/page-ruler.js +33 -0
  29. package/dist/web/paper-geometry.d.ts +50 -0
  30. package/dist/web/paper-geometry.js +106 -0
  31. package/dist/web/paper-sheet.d.ts +11 -0
  32. package/dist/web/paper-sheet.js +24 -0
  33. package/dist/web/pdf-editor-panes.js +5 -4
  34. package/dist/web/pdf-editor-view.d.ts +4 -0
  35. package/dist/web/pdf-editor-view.js +23 -3
  36. package/dist/web/pdf-left-rail.d.ts +8 -0
  37. package/dist/web/pdf-left-rail.js +23 -3
  38. package/dist/web/settings-panel.d.ts +10 -2
  39. package/dist/web/settings-panel.js +32 -17
  40. package/dist/web/use-apply-layout.d.ts +28 -0
  41. package/dist/web/use-apply-layout.js +13 -0
  42. package/dist/web/use-layout-editor-state.d.ts +5 -0
  43. package/dist/web/use-layout-editor-state.js +38 -15
  44. package/dist/web/use-pdf-editor-state.d.ts +5 -0
  45. package/dist/web/use-pdf-editor-state.js +42 -10
  46. package/package.json +6 -6
  47. package/src/changes.json +25 -0
@@ -1,47 +1,38 @@
1
- import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { DocumentContentEditor, PDF_DEFAULT_MARGIN_MM, PDF_DOCUMENT_BLOCKS, PDF_FOOTER_MARGIN_MM, } from "@cosmicdrift/kumiko-bundled-features/template-resolver/web";
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { DocumentContentEditor, PDF_DOCUMENT_BLOCKS, } from "@cosmicdrift/kumiko-bundled-features/template-resolver/web";
3
3
  import { useTranslation } from "@cosmicdrift/kumiko-renderer";
4
- import { cn } from "@cosmicdrift/kumiko-renderer/web";
5
- import { useCallback } from "react";
6
4
  import { LayoutHtml } from "./layout-html.js";
7
- import { MARGIN_SIDES } from "./pdf-meta.js";
8
- const MM_TO_PX = 96 / 25.4;
9
- // Page width in CSS px at 96 dpi as a cap; the page shrinks with a narrow column instead of scrolling sideways.
10
- const PAGE_WIDTH_CLASSES = {
11
- "A4-portrait": "max-w-[794px]",
12
- "A4-landscape": "max-w-[1123px]",
13
- "Letter-portrait": "max-w-[816px]",
14
- "Letter-landscape": "max-w-[1056px]",
15
- };
5
+ import { layoutValuesOf, pdfLayoutLookOf } from "./layout-meta.js";
6
+ import { layoutRegionFlags, paperLookOf, paperSizeMm, resolveMarginsMm } from "./paper-geometry.js";
7
+ import { PaperSheet } from "./paper-sheet.js";
16
8
  function RegionBlock({ html, systemLabels, resourceSrc, caption, }) {
17
9
  if (html.trim() === "")
18
10
  return null;
19
- return (_jsxs("div", { className: "relative border border-dashed border-border bg-muted/40 p-2 pt-4 text-xs text-muted-foreground", children: [_jsx("span", { className: "absolute left-2 top-0.5 text-[10px] uppercase tracking-wide", children: caption }), _jsx(LayoutHtml, { html: html, systemLabels: systemLabels, ...(resourceSrc !== undefined && { resourceSrc }) })] }));
11
+ return (_jsxs("div", { className: "relative border border-dashed border-border bg-muted/40 p-2 pt-4 text-muted-foreground", children: [_jsx("span", { className: "absolute left-2 top-0.5 text-[10px] uppercase tracking-wide", children: caption }), _jsx(LayoutHtml, { html: html, systemLabels: systemLabels, ...(resourceSrc !== undefined && { resourceSrc }) })] }));
20
12
  }
21
13
  // The letterhead is a band across the whole paper, outside the page margins, like on printed letter paper.
22
14
  function HeaderBand({ html, systemLabels, resourceSrc, caption, }) {
23
- return (_jsxs("div", { className: "relative border-b border-border bg-muted px-[8%] pb-4 pt-6 text-xs text-foreground [&_td:last-child]:whitespace-nowrap", "data-testid": "document-designer-letterhead", children: [_jsx("span", { className: "absolute right-[8%] top-2 text-[10px] uppercase tracking-wide text-muted-foreground", children: caption }), _jsx(LayoutHtml, { html: html, systemLabels: systemLabels, ...(resourceSrc !== undefined && { resourceSrc }) })] }));
15
+ return (_jsxs("div", { className: "absolute inset-x-0 top-0 min-h-(--paper-margin-top) border-b border-border bg-muted pt-[calc(4*var(--paper-mm))] pr-(--paper-margin-right) pb-[calc(2*var(--paper-mm))] pl-(--paper-margin-left) text-(length:--paper-font-size) text-foreground [&_td]:border-0! [&_th]:border-0! [&_p]:text-[length:inherit] [&_p]:leading-[inherit] [&_td]:align-middle [&_td:last-child]:whitespace-nowrap", "data-testid": "document-designer-letterhead", children: [_jsx("span", { className: "absolute right-(--paper-margin-right) top-0.5 text-[9px] leading-none uppercase tracking-wide text-muted-foreground", children: caption }), _jsx(LayoutHtml, { html: html, systemLabels: systemLabels, ...(resourceSrc !== undefined && { resourceSrc }) })] }));
16
+ }
17
+ // Anchored to the bottom of page 1 (not the paper end: the sheet grows with long content), like the PDF footer region.
18
+ function FooterBand({ html, systemLabels, resourceSrc, caption, }) {
19
+ return (_jsxs("div", { className: "absolute inset-x-0 top-(--paper-height) flex min-h-(--paper-margin-bottom) -translate-y-full flex-col border-t border-border bg-muted pt-[calc(1.5*var(--paper-mm))] pr-(--paper-margin-right) pb-[calc(2*var(--paper-mm))] pl-(--paper-margin-left) text-(length:--paper-font-size) text-foreground [&_td]:border-0! [&_th]:border-0! [&_p]:text-[length:inherit] [&_p]:leading-[inherit] [&_td]:align-middle [&_td:last-child]:whitespace-nowrap", "data-testid": "document-designer-footer-band", children: [_jsx("span", { className: "mb-[calc(1*var(--paper-mm))] block text-[10px] leading-none uppercase tracking-wide text-muted-foreground", children: caption }), _jsx("div", { className: "mt-auto", children: _jsx(LayoutHtml, { html: html, systemLabels: systemLabels, ...(resourceSrc !== undefined && { resourceSrc }) }) })] }));
24
20
  }
25
21
  export function PageCanvas(props) {
26
22
  const t = useTranslation();
27
23
  const { meta } = props;
28
- const showRegions = meta.layoutHeaderFooter && props.regions !== null;
29
- // The letterhead band sits in the top margin, so the content starts right below it.
30
- const showLetterhead = showRegions && props.regions !== null && props.regions.header.trim() !== "";
31
- // Same fallback as the PDF renderer: a footer region reserves a taller bottom margin (the header band replaces the top margin on the canvas).
32
- const hasFooter = showRegions && props.regions !== null && props.regions.footer.trim() !== "";
24
+ const { showRegions, hasHeader, hasFooter } = layoutRegionFlags(meta.layoutHeaderFooter, props.regions);
25
+ // The letterhead band overlays the top margin; it never pushes the content down.
26
+ const showLetterhead = hasHeader;
27
+ const size = paperSizeMm(meta.pageFormat, meta.orientation);
28
+ const margins = resolveMarginsMm({
29
+ template: meta.margins,
30
+ layout: props.layoutMeta.margins ?? {},
31
+ hasHeader,
32
+ hasFooter,
33
+ });
33
34
  const fromLayout = t("documentDesigner.canvas.fromLayout", { layout: props.layoutName });
34
35
  const regionCaption = (region) => `${t(`documentDesigner.editor.region.${region}`)} · ${fromLayout}`;
35
- // CSS variables instead of a style prop: margins are user data, classes are static.
36
- const applyMargins = useCallback((element) => {
37
- if (element === null)
38
- return;
39
- for (const side of MARGIN_SIDES) {
40
- const fallbackMm = side === "bottom" && hasFooter ? PDF_FOOTER_MARGIN_MM : PDF_DEFAULT_MARGIN_MM;
41
- const mm = meta.margins[side] === "" ? fallbackMm : meta.margins[side];
42
- element.style.setProperty(`--page-margin-${side}`, `${Math.round(mm * MM_TO_PX)}px`);
43
- }
44
- }, [meta.margins, hasFooter]);
45
- const renderPaper = (editable) => (_jsxs("div", { className: "flex w-full flex-col items-center gap-2", children: [_jsxs("div", { "data-testid": "document-designer-page", className: cn("flex min-h-[1000px] w-full flex-col bg-card shadow-md", PAGE_WIDTH_CLASSES[`${meta.pageFormat}-${meta.orientation}`]), children: [showLetterhead && props.regions !== null && (_jsx(HeaderBand, { html: props.regions.header, systemLabels: props.systemLabels, resourceSrc: props.resourceSrc, caption: fromLayout })), _jsxs("div", { ref: applyMargins, className: cn("flex min-h-0 flex-1 flex-col pr-(--page-margin-right) pb-(--page-margin-bottom) pl-(--page-margin-left)", !showLetterhead && "pt-(--page-margin-top)"), children: [_jsxs("div", { className: "relative my-2 min-h-0 flex-1 border border-dashed border-primary/40 p-2 pt-5", "data-testid": "document-designer-content-area", children: [_jsx("span", { className: "absolute left-2 top-0.5 text-[10px] uppercase tracking-wide text-primary", children: t("documentDesigner.editor.region.content") }), editable] }), showRegions && props.regions !== null && (_jsxs(_Fragment, { children: [_jsx(RegionBlock, { html: props.regions.signature, systemLabels: props.systemLabels, resourceSrc: props.resourceSrc, caption: regionCaption("signature") }), _jsx(RegionBlock, { html: props.regions.footer, systemLabels: props.systemLabels, resourceSrc: props.resourceSrc, caption: regionCaption("footer") })] }))] })] }), _jsx("p", { className: "text-xs text-muted-foreground", children: t("documentDesigner.canvas.caption", { format: meta.pageFormat }) })] }));
46
- return (_jsx("div", { className: "flex min-h-0 min-w-0 flex-1 flex-col", "data-testid": "pdf-editor-center", children: _jsx(DocumentContentEditor, { id: "document-designer-content", value: props.content, onChange: props.onContentChange, variables: props.variableNames, imageVariables: props.imageVariables, readOnly: false, blocks: PDF_DOCUMENT_BLOCKS, variableLabels: props.variableLabels, systemVariables: props.systemVariables, resourcePreviews: props.resourcePreviews, chrome: "canvas", renderPaper: renderPaper, onEditorReady: props.onEditorReady }) }));
36
+ const renderPaper = (editable) => (_jsxs("div", { className: "flex w-full flex-col items-center gap-2", children: [_jsxs(PaperSheet, { size: size, margins: margins, look: paperLookOf(pdfLayoutLookOf(props.layoutMeta)), testId: "document-designer-page", children: [showLetterhead && props.regions !== null && (_jsx(HeaderBand, { html: props.regions.header, systemLabels: props.systemLabels, resourceSrc: props.resourceSrc, caption: fromLayout })), _jsxs("div", { className: "flex min-h-0 flex-1 flex-col pt-(--paper-margin-top) pr-(--paper-margin-right) pb-(--paper-margin-bottom) pl-(--paper-margin-left)", children: [_jsxs("div", { className: "min-h-0 flex-1 outline-1 outline-dashed outline-primary/40 outline-offset-4", "data-testid": "document-designer-content-area", children: [_jsx("span", { className: "absolute top-[calc(var(--paper-margin-top)-0.75rem)] left-[calc(var(--paper-margin-left)+0.5rem)] z-10 bg-card px-1.5 text-[10px] leading-none uppercase tracking-wide text-primary", children: t("documentDesigner.editor.region.content") }), editable] }), showRegions && props.regions !== null && (_jsx(RegionBlock, { html: props.regions.signature, systemLabels: props.systemLabels, resourceSrc: props.resourceSrc, caption: regionCaption("signature") }))] }), hasFooter && props.regions !== null && (_jsx(FooterBand, { html: props.regions.footer, systemLabels: props.systemLabels, resourceSrc: props.resourceSrc, caption: fromLayout }))] }), _jsx("p", { className: "text-xs text-muted-foreground", children: t("documentDesigner.canvas.caption", { format: meta.pageFormat }) })] }));
37
+ return (_jsx("div", { className: "flex min-h-0 min-w-0 flex-1 flex-col", "data-testid": "pdf-editor-center", children: _jsx(DocumentContentEditor, { id: "document-designer-content", value: props.content, onChange: props.onContentChange, variables: props.variableNames, imageVariables: props.imageVariables, readOnly: false, blocks: PDF_DOCUMENT_BLOCKS, variableLabels: props.variableLabels, systemVariables: props.systemVariables, resourcePreviews: props.resourcePreviews, chrome: "canvas", renderPaper: renderPaper, layoutValues: layoutValuesOf(pdfLayoutLookOf(props.layoutMeta)), onEditorReady: props.onEditorReady, onFrameControlChange: props.onFrameControlChange }) }));
47
38
  }
@@ -0,0 +1,6 @@
1
+ import type { ReactNode } from "react";
2
+ import { type MarginsMm, type PaperSize } from "./paper-geometry.ts";
3
+ export declare function PageRuler({ size, margins, }: {
4
+ readonly size: PaperSize;
5
+ readonly margins: MarginsMm;
6
+ }): ReactNode;
@@ -0,0 +1,33 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { applyCssVariables, percentOfExtent, rulerHalfMarks, rulerMarks, } from "./paper-geometry.js";
3
+ const ZONE_EDGE_CLASS = {
4
+ left: "inset-y-0 left-0 w-(--ruler-extent)",
5
+ right: "inset-y-0 right-0 w-(--ruler-extent)",
6
+ top: "inset-x-0 top-0 h-(--ruler-extent)",
7
+ bottom: "inset-x-0 bottom-0 h-(--ruler-extent)",
8
+ };
9
+ function MarginZone({ side, percent, }) {
10
+ return (_jsx("div", { "data-testid": `page-ruler-margin-${side}`, className: `absolute bg-status-warn/20 ${ZONE_EDGE_CLASS[side]}`, ref: (element) => applyCssVariables(element, { "--ruler-extent": `${percent}%` }) }));
11
+ }
12
+ function RulerTick({ cm, percent, axis, }) {
13
+ const horizontal = axis === "horizontal";
14
+ return (_jsxs("span", { "data-ruler-cm": cm, className: horizontal
15
+ ? "absolute inset-y-0 left-(--ruler-at) w-0"
16
+ : "absolute inset-x-0 top-(--ruler-at) h-0", ref: (element) => applyCssVariables(element, { "--ruler-at": `${percent}%` }), children: [_jsx("span", { className: horizontal
17
+ ? "absolute top-0.5 left-0 -translate-x-1/2 leading-none"
18
+ : "absolute top-0 left-0.5 -translate-y-1/2 leading-none", children: cm }), _jsx("span", { className: horizontal
19
+ ? "absolute bottom-0 left-0 h-1.5 border-l border-muted-foreground/70"
20
+ : "absolute top-0 right-0 w-1.5 border-t border-muted-foreground/70" })] }));
21
+ }
22
+ function RulerHalfTick({ mm, percent, axis, }) {
23
+ return (_jsx("span", { "data-ruler-half": mm, className: axis === "horizontal"
24
+ ? "absolute bottom-0 left-(--ruler-at) h-1 border-l border-muted-foreground/40"
25
+ : "absolute top-(--ruler-at) right-0 w-1 border-t border-muted-foreground/40", ref: (element) => applyCssVariables(element, { "--ruler-at": `${percent}%` }) }));
26
+ }
27
+ function RulerMarks({ extentMm, axis, }) {
28
+ return (_jsxs(_Fragment, { children: [rulerHalfMarks(extentMm).map((mark) => (_jsx(RulerHalfTick, { mm: mark.mm, percent: mark.percent, axis: axis }, mark.mm))), rulerMarks(extentMm).map((mark) => (_jsx(RulerTick, { cm: mark.cm, percent: mark.percent, axis: axis }, mark.cm)))] }));
29
+ }
30
+ // A child of the paper wrapper: its percentages are the paper's, so it scales with the page.
31
+ export function PageRuler({ size, margins, }) {
32
+ return (_jsxs(_Fragment, { children: [_jsxs("div", { "data-testid": "page-ruler-top", "aria-hidden": "true", className: "pointer-events-none absolute -top-4 right-0 left-0 h-4 select-none overflow-hidden border-b bg-muted text-[9px] text-muted-foreground", children: [_jsx(MarginZone, { side: "left", percent: percentOfExtent(margins.left, size.widthMm) }), _jsx(MarginZone, { side: "right", percent: percentOfExtent(margins.right, size.widthMm) }), _jsx(RulerMarks, { extentMm: size.widthMm, axis: "horizontal" })] }), _jsxs("div", { "data-testid": "page-ruler-left", "aria-hidden": "true", className: "pointer-events-none absolute top-0 -left-4 h-(--paper-height) w-4 select-none overflow-hidden border-r bg-muted text-[9px] text-muted-foreground", children: [_jsx(MarginZone, { side: "top", percent: percentOfExtent(margins.top, size.heightMm) }), _jsx(MarginZone, { side: "bottom", percent: percentOfExtent(margins.bottom, size.heightMm) }), _jsx(RulerMarks, { extentMm: size.heightMm, axis: "vertical" })] })] }));
33
+ }
@@ -0,0 +1,50 @@
1
+ import { type PdfTableStyle } from "@cosmicdrift/kumiko-bundled-features/template-resolver/web";
2
+ import { type PdfLayoutLook } from "./layout-meta.ts";
3
+ import { type MarginSide, type Orientation, type PageFormat } from "./pdf-meta.ts";
4
+ import type { LayoutRegionsHtml } from "./types.ts";
5
+ export type PaperSize = {
6
+ readonly widthMm: number;
7
+ readonly heightMm: number;
8
+ };
9
+ export type MarginsMm = Readonly<Record<MarginSide, number>>;
10
+ export type PartialMarginsMm = Readonly<Partial<Record<MarginSide, number>>>;
11
+ export type MarginInput = Readonly<Record<MarginSide, number | "">>;
12
+ export declare function paperSizeMm(format: PageFormat, orientation: Orientation): PaperSize;
13
+ export declare function paperWidthPt(size: PaperSize): number;
14
+ export declare const EMPTY_MARGIN_INPUT: MarginInput;
15
+ export declare function resolveMarginsMm(input: {
16
+ readonly template: MarginInput;
17
+ readonly layout: PartialMarginsMm;
18
+ readonly hasHeader: boolean;
19
+ readonly hasFooter: boolean;
20
+ }): MarginsMm;
21
+ export declare function layoutRegionFlags(layoutHeaderFooter: boolean, regions: LayoutRegionsHtml | null): {
22
+ readonly showRegions: boolean;
23
+ readonly hasHeader: boolean;
24
+ readonly hasFooter: boolean;
25
+ };
26
+ export declare function percentOfExtent(mm: number, extentMm: number): number;
27
+ export type RulerMark = {
28
+ readonly cm: number;
29
+ readonly percent: number;
30
+ };
31
+ export declare function rulerMarks(extentMm: number): readonly RulerMark[];
32
+ export type RulerHalfMark = {
33
+ readonly mm: number;
34
+ readonly percent: number;
35
+ };
36
+ export declare function rulerHalfMarks(extentMm: number): readonly RulerHalfMark[];
37
+ export type PaperLook = {
38
+ readonly tableStyle: PdfTableStyle;
39
+ readonly fontStack: string | null;
40
+ readonly fontSizePt: number;
41
+ readonly textColor: string | null;
42
+ readonly tableHeaderBackground: string | null;
43
+ };
44
+ export declare function paperVariables(input: {
45
+ readonly size: PaperSize;
46
+ readonly margins: MarginsMm;
47
+ readonly look: PaperLook;
48
+ }): Readonly<Record<string, string>>;
49
+ export declare function applyCssVariables(element: HTMLElement | null, variables: Readonly<Record<string, string>>): void;
50
+ export declare function paperLookOf(look: PdfLayoutLook): PaperLook;
@@ -0,0 +1,106 @@
1
+ import { DOCUMENT_FONT_PRINT_STACKS, PDF_DEFAULT_MARGIN_MM, PDF_FOOTER_MARGIN_MM, PDF_HEADER_MARGIN_MM, } from "@cosmicdrift/kumiko-bundled-features/template-resolver/web";
2
+ import { DEFAULT_PDF_FONT_SIZE_PT, isHexColor, PDF_LAYOUT_FONT_SIZE_RANGE, } from "./layout-meta.js";
3
+ import { MARGIN_SIDES } from "./pdf-meta.js";
4
+ const MM_PER_INCH = 25.4;
5
+ const PT_PER_INCH = 72;
6
+ const MM_PER_CM = 10;
7
+ const PORTRAIT_SIZES_MM = {
8
+ A4: { widthMm: 210, heightMm: 297 },
9
+ Letter: { widthMm: 215.9, heightMm: 279.4 },
10
+ };
11
+ export function paperSizeMm(format, orientation) {
12
+ const portrait = PORTRAIT_SIZES_MM[format];
13
+ return orientation === "portrait"
14
+ ? portrait
15
+ : { widthMm: portrait.heightMm, heightMm: portrait.widthMm };
16
+ }
17
+ export function paperWidthPt(size) {
18
+ return Number(((size.widthMm * PT_PER_INCH) / MM_PER_INCH).toFixed(2));
19
+ }
20
+ export const EMPTY_MARGIN_INPUT = { top: "", right: "", bottom: "", left: "" };
21
+ // Mirrors the renderer: the template's own value wins over the layout's, which wins over the default.
22
+ export function resolveMarginsMm(input) {
23
+ const { template, layout } = input;
24
+ const pick = (side, fallback) => {
25
+ const own = template[side];
26
+ return own === "" ? (layout[side] ?? fallback) : own;
27
+ };
28
+ return {
29
+ top: pick("top", input.hasHeader ? PDF_HEADER_MARGIN_MM : PDF_DEFAULT_MARGIN_MM),
30
+ right: pick("right", PDF_DEFAULT_MARGIN_MM),
31
+ bottom: pick("bottom", input.hasFooter ? PDF_FOOTER_MARGIN_MM : PDF_DEFAULT_MARGIN_MM),
32
+ left: pick("left", PDF_DEFAULT_MARGIN_MM),
33
+ };
34
+ }
35
+ export function layoutRegionFlags(layoutHeaderFooter, regions) {
36
+ const showRegions = layoutHeaderFooter && regions !== null;
37
+ return {
38
+ showRegions,
39
+ hasHeader: showRegions && regions.header.trim() !== "",
40
+ hasFooter: showRegions && regions.footer.trim() !== "",
41
+ };
42
+ }
43
+ export function percentOfExtent(mm, extentMm) {
44
+ return Number(((mm / extentMm) * 100).toFixed(3));
45
+ }
46
+ export function rulerMarks(extentMm) {
47
+ // A mark on the paper edge would be cut off by the ruler, so the last full centimetre stays inside.
48
+ const count = Math.ceil(extentMm / MM_PER_CM) - 1;
49
+ return Array.from({ length: count }, (_, index) => {
50
+ const cm = index + 1;
51
+ return { cm, percent: percentOfExtent(cm * MM_PER_CM, extentMm) };
52
+ });
53
+ }
54
+ const MM_PER_HALF_CM = 5;
55
+ // The 5 mm ticks between the centimetre ticks: 5, 15, 25 ... while they still fit on the paper.
56
+ export function rulerHalfMarks(extentMm) {
57
+ const count = Math.floor((extentMm - MM_PER_HALF_CM) / MM_PER_CM) + 1;
58
+ return Array.from({ length: Math.max(count, 0) }, (_, index) => {
59
+ const mm = MM_PER_HALF_CM + index * MM_PER_CM;
60
+ return { mm, percent: percentOfExtent(mm, extentMm) };
61
+ });
62
+ }
63
+ // 100cqw only resolves for descendants of the sized container, so these are set on a child that fills the paper.
64
+ export function paperVariables(input) {
65
+ const { size, margins, look } = input;
66
+ const variables = {
67
+ "--paper-mm": `calc(100cqw / ${size.widthMm})`,
68
+ "--paper-pt": `calc(100cqw / ${paperWidthPt(size)})`,
69
+ "--paper-height": `calc(${size.heightMm} * var(--paper-mm))`,
70
+ "--paper-font-size": `calc(${look.fontSizePt} * var(--paper-pt))`,
71
+ "--page-margin-left": "0px",
72
+ "--first-page-top": "0px",
73
+ "--page-margin-top-mm": String(margins.top),
74
+ };
75
+ for (const side of MARGIN_SIDES) {
76
+ variables[`--paper-margin-${side}`] = `calc(${margins[side]} * var(--paper-mm))`;
77
+ }
78
+ if (look.fontStack !== null)
79
+ variables["--paper-font-family"] = look.fontStack;
80
+ if (look.textColor !== null)
81
+ variables["--paper-text-color"] = look.textColor;
82
+ if (look.tableHeaderBackground !== null) {
83
+ variables["--paper-table-header"] = look.tableHeaderBackground;
84
+ }
85
+ return variables;
86
+ }
87
+ export function applyCssVariables(element, variables) {
88
+ if (element === null)
89
+ return;
90
+ for (const [name, value] of Object.entries(variables))
91
+ element.style.setProperty(name, value);
92
+ }
93
+ // A half-typed value must not blow up the paper, so anything invalid falls back to the default.
94
+ export function paperLookOf(look) {
95
+ const size = look.fontSizePt;
96
+ const sizeUsable = size !== "" && size >= PDF_LAYOUT_FONT_SIZE_RANGE.min && size <= PDF_LAYOUT_FONT_SIZE_RANGE.max;
97
+ return {
98
+ tableStyle: look.tableStyle,
99
+ fontStack: DOCUMENT_FONT_PRINT_STACKS[look.fontFamily],
100
+ fontSizePt: sizeUsable ? size : DEFAULT_PDF_FONT_SIZE_PT,
101
+ textColor: isHexColor(look.textColor) ? look.textColor : null,
102
+ tableHeaderBackground: isHexColor(look.tableHeaderBackground)
103
+ ? look.tableHeaderBackground
104
+ : null,
105
+ };
106
+ }
@@ -0,0 +1,11 @@
1
+ import type { ReactNode } from "react";
2
+ import { type MarginsMm, type PaperLook, type PaperSize } from "./paper-geometry.ts";
3
+ /** The measured container is the outer element; the paper child fills it so cq lengths resolve for it and everything inside. */
4
+ export declare function PaperSheet({ size, margins, look, testId, className, children, }: {
5
+ readonly size: PaperSize;
6
+ readonly margins: MarginsMm;
7
+ readonly look: PaperLook;
8
+ readonly testId: string;
9
+ readonly className?: string;
10
+ readonly children: ReactNode;
11
+ }): ReactNode;
@@ -0,0 +1,24 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { cn } from "@cosmicdrift/kumiko-renderer/web";
3
+ import { PageRuler } from "./page-ruler.js";
4
+ import { applyCssVariables, paperVariables, } from "./paper-geometry.js";
5
+ const RULER_GUTTER_REM = 1;
6
+ // Text in the outer columns sits on the text margin; a header background is a visible box whose
7
+ // edge is flush instead, so its th keep the padding.
8
+ const FLUSH_CELLS_CLASS = "[&_tr>:first-child]:pl-0 [&_tr>:last-child]:pr-0";
9
+ const FLUSH_BODY_CELLS_CLASS = "[&_tr>td:first-child]:pl-0 [&_tr>td:last-child]:pr-0";
10
+ // Same grey as the PDF table rule (TABLE_LINE in document-pdf-style.ts).
11
+ const PRINT_RULE_COLOR_CLASS = "[&_td]:border-[#bbb] [&_th]:border-[#bbb]";
12
+ const TABLE_STYLE_CLASS = {
13
+ plain: "[&_td]:border-0 [&_th]:border-0",
14
+ lines: `[&_td]:border-0 [&_th]:border-0 [&_td]:border-b [&_th]:border-b ${PRINT_RULE_COLOR_CLASS}`,
15
+ header: `[&_td]:border-0 [&_th]:border-0 [&_th]:border-b ${PRINT_RULE_COLOR_CLASS}`,
16
+ grid: `[&_td]:border [&_th]:border ${PRINT_RULE_COLOR_CLASS}`,
17
+ };
18
+ /** The measured container is the outer element; the paper child fills it so cq lengths resolve for it and everything inside. */
19
+ export function PaperSheet({ size, margins, look, testId, className, children, }) {
20
+ return (_jsx("div", { className: "@container w-full max-w-(--paper-outer-max) pt-4 pl-4", ref: (element) => applyCssVariables(element, {
21
+ "--paper-outer-max": `calc(${size.widthMm}mm + ${RULER_GUTTER_REM}rem)`,
22
+ }), children: _jsxs("div", { "data-testid": testId, "data-document-paper": "", "data-paper-width-mm": size.widthMm, "data-paper-margin-left-mm": margins.left, "data-paper-margin-right-mm": margins.right, "data-paper-margin-top-mm": margins.top, className: cn("relative flex min-h-(--paper-height) w-full flex-col bg-card text-(length:--paper-font-size) text-(color:--paper-text-color) font-(family-name:--paper-font-family) shadow-md [&_th]:bg-(--paper-table-header)", TABLE_STYLE_CLASS[look.tableStyle], look.tableStyle !== "grid" &&
23
+ (look.tableHeaderBackground === null ? FLUSH_CELLS_CLASS : FLUSH_BODY_CELLS_CLASS), className), ref: (element) => applyCssVariables(element, paperVariables({ size, margins, look })), children: [_jsx(PageRuler, { size: size, margins: margins }), children] }) }));
24
+ }
@@ -1,8 +1,9 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { PreviewPane } from "@cosmicdrift/kumiko-renderer";
3
+ import { LoadingState } from "@cosmicdrift/kumiko-renderer/web";
3
4
  import { PlaceholderSection, SystemPlaceholderSection } from "./mail-left-rail.js";
4
5
  import { PageCanvas } from "./page-canvas.js";
5
- import { PdfResourceSection } from "./pdf-left-rail.js";
6
+ import { PdfCompanyDataSection, PdfResourceSection } from "./pdf-left-rail.js";
6
7
  import { serializePdfMeta } from "./pdf-meta.js";
7
8
  import { PdfPreviewControllerContext } from "./pdf-preview-renderer.js";
8
9
  import { SettingsPanel } from "./settings-panel.js";
@@ -10,13 +11,13 @@ import { DOCUMENT_PDF_KIND } from "./types.js";
10
11
  export function renderPdfLeftRail(c) {
11
12
  const { s, view, detail, actions } = c;
12
13
  const { insert, filesAvailable, scope } = s.host;
13
- return (_jsxs(_Fragment, { children: [_jsx(PlaceholderSection, { validation: view.placeholderSource, onInsert: insert.insertPlaceholder, idPrefix: "pdf" }), _jsx(SystemPlaceholderSection, { validation: view.placeholderSource, onInsert: insert.insertPlaceholder, idPrefix: "pdf", isLayoutOwned: view.layoutFills }), _jsx(PdfResourceSection, { resourceKeys: view.resourceKeys, canUpload: detail.scope === scope, filesAvailable: filesAvailable, layoutName: view.layoutName, onInsert: insert.insertResource, onUpload: actions.uploadResource })] }));
14
+ return (_jsxs(_Fragment, { children: [_jsx(PlaceholderSection, { validation: view.placeholderSource, onInsert: insert.insertPlaceholder, idPrefix: "pdf" }), _jsx(PdfCompanyDataSection, { onInsertPlaceholder: insert.insertPlaceholder, used: view.companyDataUsed }), _jsx(SystemPlaceholderSection, { validation: view.placeholderSource, onInsert: insert.insertPlaceholder, idPrefix: "pdf", isLayoutOwned: view.layoutFills }), _jsx(PdfResourceSection, { resourceKeys: view.resourceKeys, canUpload: detail.scope === scope, filesAvailable: filesAvailable, layoutName: view.layoutName, onInsert: insert.insertResource, onUpload: actions.uploadResource })] }));
14
15
  }
15
16
  export function renderPdfEditCanvas(c) {
16
17
  const { s, view, current, actions } = c;
17
18
  const { insert } = s.host;
18
19
  const { vocabulary } = s;
19
- return (_jsx("div", { className: "flex min-h-0 min-w-0 flex-1 flex-col", onFocusCapture: insert.markFocused("body"), children: _jsx(PageCanvas, { meta: current.draft.meta, content: current.draft.content, onContentChange: (content) => actions.updateDraft({ ...current.draft, content }), regions: s.server.layout?.regions ?? null, layoutName: view.layoutName, resourceSrc: view.layoutResourceSrc, variableLabels: vocabulary.variableLabels, systemVariables: vocabulary.systemVariables, systemLabels: vocabulary.systemLabels, variableNames: vocabulary.variableNames, imageVariables: vocabulary.imageVariables, resourcePreviews: vocabulary.resourcePreviews, onEditorReady: insert.register("body") }) }));
20
+ return (_jsx("div", { className: "flex min-h-0 min-w-0 flex-1 flex-col", onFocusCapture: insert.markFocused("body"), children: s.server.paperReady ? (_jsx(PageCanvas, { meta: current.draft.meta, layoutMeta: s.server.layoutMeta, content: current.draft.content, onContentChange: (content) => actions.updateDraft({ ...current.draft, content }), regions: s.server.layout?.regions ?? null, layoutName: view.layoutName, resourceSrc: view.layoutResourceSrc, variableLabels: vocabulary.variableLabels, systemVariables: vocabulary.systemVariables, systemLabels: vocabulary.systemLabels, variableNames: vocabulary.variableNames, imageVariables: vocabulary.imageVariables, resourcePreviews: vocabulary.resourcePreviews, onEditorReady: insert.register("body"), onFrameControlChange: s.ui.setFrameControl })) : (_jsx("div", { className: "m-6 rounded-md bg-card p-6 shadow-sm", children: _jsx(LoadingState, { rows: 8 }) })) }));
20
21
  }
21
22
  export function renderPdfPreviewPane(c) {
22
23
  const { s, view, actions, current, slug } = c;
@@ -39,5 +40,5 @@ export function renderPdfPreviewPane(c) {
39
40
  }
40
41
  export function renderPdfSettings(c) {
41
42
  const { s, view, current, detail, actions } = c;
42
- return (_jsx(SettingsPanel, { meta: current.draft.meta, onMetaChange: (meta) => actions.updateDraft({ ...current.draft, meta }), layouts: s.server.layouts.rows, locale: s.doc.locale, version: { id: detail.id, version: detail.version, status: detail.status }, vocabulary: view.vocabulary, insert: s.host.insert, draftKey: `${current.key}:${s.ui.draftEpoch}` }));
43
+ return (_jsx(SettingsPanel, { meta: current.draft.meta, onMetaChange: (meta) => actions.updateDraft({ ...current.draft, meta }), layouts: s.server.layouts.rows, locale: s.doc.locale, version: { id: detail.id, version: detail.version, status: detail.status }, vocabulary: view.vocabulary, insert: s.host.insert, frameControl: s.ui.frameControl, paper: view.paper, marginsMm: view.marginsMm, inheritedMarginsMm: s.server.paperReady ? view.inheritedMarginsMm : null, draftKey: `${current.key}:${s.ui.draftEpoch}` }));
43
44
  }
@@ -9,6 +9,9 @@ export declare function buildPdfView(input: {
9
9
  readonly current: EditorState;
10
10
  readonly detail: TemplateDetail;
11
11
  }): {
12
+ paper: import("./paper-geometry.ts").PaperSize;
13
+ marginsMm: Readonly<Record<import("./pdf-meta.ts").MarginSide, number>>;
14
+ inheritedMarginsMm: Readonly<Record<import("./pdf-meta.ts").MarginSide, number>>;
12
15
  dirty: boolean;
13
16
  generatedFresh: boolean;
14
17
  canPublish: boolean;
@@ -29,6 +32,7 @@ export declare function buildPdfView(input: {
29
32
  variables: Readonly<Record<string, import("./mail-types.ts").MailVariable>>;
30
33
  systemPlaceholders: readonly string[];
31
34
  } | null;
35
+ companyDataUsed: ReadonlySet<string>;
32
36
  layoutFills: (name: string) => boolean;
33
37
  resourceKeys: string[];
34
38
  title: string;
@@ -1,7 +1,8 @@
1
1
  import { fileUrl } from "./file-name.js";
2
+ import { EMPTY_MARGIN_INPUT, layoutRegionFlags, paperSizeMm, resolveMarginsMm, } from "./paper-geometry.js";
2
3
  import { snapshotOf } from "./pdf-editor-draft.js";
3
4
  import { pdfFooterMessage } from "./pdf-footer.js";
4
- import { visibleSystemPlaceholders } from "./pdf-left-rail.js";
5
+ import { companyNamesUsedIn, railSystemPlaceholders, withoutCompanyNames, } from "./pdf-left-rail.js";
5
6
  import { draftBesideActiveSentence } from "./status-label.js";
6
7
  import { parseJsonObject } from "./template-rows.js";
7
8
  import { DEFAULT_LAYOUT_SLUG } from "./types.js";
@@ -46,8 +47,8 @@ function placeholderSourceOf(s) {
46
47
  ? null
47
48
  : {
48
49
  ...validation,
49
- variables: s.vocabulary.variables,
50
- systemPlaceholders: visibleSystemPlaceholders(validation.systemPlaceholders),
50
+ variables: withoutCompanyNames(s.vocabulary.variables),
51
+ systemPlaceholders: railSystemPlaceholders(validation.systemPlaceholders),
51
52
  };
52
53
  }
53
54
  export function buildPdfView(input) {
@@ -61,7 +62,25 @@ export function buildPdfView(input) {
61
62
  const overviewRow = s.host.overview.rows.find((candidate) => candidate.slug === slug);
62
63
  const systemVersion = overviewRow?.systemVersion ?? null;
63
64
  const showOverrideLinks = s.server.showOrigin && detail.scope === "system";
65
+ const { meta } = current.draft;
66
+ const { hasHeader, hasFooter } = layoutRegionFlags(meta.layoutHeaderFooter, layout?.regions ?? null);
67
+ const layoutMargins = s.server.layoutMeta.margins ?? {};
68
+ const marginsMm = resolveMarginsMm({
69
+ template: meta.margins,
70
+ layout: layoutMargins,
71
+ hasHeader,
72
+ hasFooter,
73
+ });
74
+ const inheritedMarginsMm = resolveMarginsMm({
75
+ template: EMPTY_MARGIN_INPUT,
76
+ layout: layoutMargins,
77
+ hasHeader,
78
+ hasFooter,
79
+ });
64
80
  return {
81
+ paper: paperSizeMm(meta.pageFormat, meta.orientation),
82
+ marginsMm,
83
+ inheritedMarginsMm,
65
84
  dirty: snapshotOf(current.draft) !== current.snapshot,
66
85
  generatedFresh: generated !== null && generated.forSnapshot === snapshotOf(current.draft),
67
86
  canPublish: validation?.canPublish === true,
@@ -71,6 +90,7 @@ export function buildPdfView(input) {
71
90
  layoutName,
72
91
  layoutResourceSrc: (key) => key === "logo" && brandingLogoFile !== "" ? fileUrl(brandingLogoFile) : undefined,
73
92
  placeholderSource: placeholderSourceOf(s),
93
+ companyDataUsed: companyNamesUsedIn(current.draft.content),
74
94
  layoutFills: (name) => layout !== null &&
75
95
  (layout.html.includes(`data-variable="${name}"`) || layout.html.includes(`{{${name}}}`)),
76
96
  resourceKeys: Object.keys(parseJsonObject(detail.linkedResources)),
@@ -1,5 +1,9 @@
1
1
  import type { ReactNode } from "react";
2
2
  export declare function visibleSystemPlaceholders(names: readonly string[]): readonly string[];
3
+ export declare function railSystemPlaceholders(names: readonly string[]): readonly string[];
4
+ export declare function placeholdersUsedIn(content: string, names: readonly string[]): ReadonlySet<string>;
5
+ export declare function companyNamesUsedIn(content: string): ReadonlySet<string>;
6
+ export declare function withoutCompanyNames<T>(variables: Readonly<Record<string, T>>): Readonly<Record<string, T>>;
3
7
  export declare function PdfResourceSection({ resourceKeys, canUpload, filesAvailable, layoutName, onInsert, onUpload, }: {
4
8
  readonly resourceKeys: readonly string[];
5
9
  readonly canUpload: boolean;
@@ -8,3 +12,7 @@ export declare function PdfResourceSection({ resourceKeys, canUpload, filesAvail
8
12
  readonly onInsert: (key: string) => void;
9
13
  readonly onUpload: (file: File) => Promise<void>;
10
14
  }): ReactNode;
15
+ export declare function PdfCompanyDataSection({ onInsertPlaceholder, used, }: {
16
+ readonly onInsertPlaceholder: (name: string) => void;
17
+ readonly used: ReadonlySet<string>;
18
+ }): ReactNode;
@@ -1,11 +1,27 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { COMPANY_PLACEHOLDERS } from "@cosmicdrift/kumiko-bundled-features/template-resolver/web";
2
3
  import { usePrimitives, useTranslation } from "@cosmicdrift/kumiko-renderer";
3
4
  import { UploadZone } from "@cosmicdrift/kumiko-renderer/web";
5
+ import { CompanyDataRail } from "./company-data-rail.js";
4
6
  import { RailField, RailHint, RailRow, RailSection } from "./rail.js";
5
- // unsubscribeUrl belongs to mail; the PDF designer never offers it.
6
- const HIDDEN_SYSTEM_PLACEHOLDERS = new Set(["unsubscribeUrl"]);
7
+ const COMPANY_DATA_NAMES = ["tenantName", ...COMPANY_PLACEHOLDERS];
8
+ // unsubscribeUrl belongs to mail.
7
9
  export function visibleSystemPlaceholders(names) {
8
- return names.filter((name) => !HIDDEN_SYSTEM_PLACEHOLDERS.has(name));
10
+ return names.filter((name) => name !== "unsubscribeUrl");
11
+ }
12
+ // The company names have their own rail in the PDF designer, so the placeholder lists skip them.
13
+ export function railSystemPlaceholders(names) {
14
+ return visibleSystemPlaceholders(names).filter((name) => !COMPANY_DATA_NAMES.includes(name));
15
+ }
16
+ // The validation never lists system placeholders as used, so the rail reads the draft itself.
17
+ export function placeholdersUsedIn(content, names) {
18
+ return new Set(names.filter((name) => content.includes(`data-variable="${name}"`) || content.includes(`{{${name}}}`)));
19
+ }
20
+ export function companyNamesUsedIn(content) {
21
+ return placeholdersUsedIn(content, COMPANY_DATA_NAMES);
22
+ }
23
+ export function withoutCompanyNames(variables) {
24
+ return Object.fromEntries(Object.entries(variables).filter(([name]) => !COMPANY_DATA_NAMES.includes(name)));
9
25
  }
10
26
  const IMAGE_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp"];
11
27
  export function PdfResourceSection({ resourceKeys, canUpload, filesAvailable, layoutName, onInsert, onUpload, }) {
@@ -13,3 +29,7 @@ export function PdfResourceSection({ resourceKeys, canUpload, filesAvailable, la
13
29
  const { Button } = usePrimitives();
14
30
  return (_jsxs(RailSection, { title: t("documentDesigner.resources.title"), testId: "pdf-editor-resources", children: [resourceKeys.length === 0 && _jsx(RailRow, { label: t("documentDesigner.resources.empty") }), resourceKeys.map((key) => (_jsx(RailRow, { testId: `pdf-resource-${key}`, symbol: "\u25A3", label: key, onPress: () => onInsert(key) }, key))), _jsx(RailField, { children: canUpload && filesAvailable ? (_jsx(UploadZone, { multiple: false, accept: IMAGE_TYPES, title: t("documentDesigner.resources.upload"), onUpload: onUpload, testId: "document-designer-upload" })) : (_jsx(Button, { variant: "secondary", size: "sm", disabled: true, children: t("documentDesigner.resources.upload") })) }), !filesAvailable && _jsx(RailHint, { children: t("documentDesigner.resources.noFilesFeature") }), filesAvailable && !canUpload && (_jsx(RailHint, { children: t("documentDesigner.resources.saveFirst") })), _jsx(RailHint, { children: t("documentDesigner.placeholders.layoutHint", { layout: layoutName }) })] }));
15
31
  }
32
+ // Company data of the tenant as placeholders, shown in the left rail beside the resources.
33
+ export function PdfCompanyDataSection({ onInsertPlaceholder, used, }) {
34
+ return _jsx(CompanyDataRail, { onInsert: onInsertPlaceholder, used: used });
35
+ }
@@ -1,6 +1,8 @@
1
- import type { ReactNode } from "react";
1
+ import { type DocumentFrameControl } from "@cosmicdrift/kumiko-bundled-features/template-resolver/web";
2
+ import { type ReactNode } from "react";
2
3
  import type { EditorVocabulary } from "./mail-edit-center.tsx";
3
4
  import type { OverviewRowView } from "./overview-row.ts";
5
+ import { type MarginsMm, type PaperSize } from "./paper-geometry.ts";
4
6
  import { type PdfMetaDraft } from "./pdf-meta.ts";
5
7
  import { type TemplateStatus } from "./types.ts";
6
8
  import type { InsertTargets } from "./use-insert-targets.ts";
@@ -17,7 +19,13 @@ export type SettingsPanelProps = {
17
19
  readonly version: VersionInfo;
18
20
  readonly vocabulary: EditorVocabulary;
19
21
  readonly insert: InsertTargets;
22
+ readonly frameControl: DocumentFrameControl | null;
23
+ readonly paper: PaperSize;
24
+ /** Margins as the renderer will apply them: template, then layout, then default. */
25
+ readonly marginsMm: MarginsMm;
26
+ /** Margins without the template's own values, i.e. what the layout hands down; null until the layout is loaded. */
27
+ readonly inheritedMarginsMm: MarginsMm | null;
20
28
  /** Changes when the draft is replaced from outside, so the single-line editors re-read it. */
21
29
  readonly draftKey: string;
22
30
  };
23
- export declare function SettingsPanel({ meta, onMetaChange, layouts, locale, version, vocabulary, insert, draftKey, }: SettingsPanelProps): ReactNode;
31
+ export declare function SettingsPanel({ meta, onMetaChange, layouts, locale, version, vocabulary, insert, frameControl, paper, marginsMm, inheritedMarginsMm, draftKey, }: SettingsPanelProps): ReactNode;
@@ -1,9 +1,12 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { PDF_ORIENTATIONS, PDF_PAGE_FORMATS, PlaceholderLineEditor, } from "@cosmicdrift/kumiko-bundled-features/template-resolver/web";
3
3
  import { usePrimitives, useTranslation } from "@cosmicdrift/kumiko-renderer";
4
+ import { useState } from "react";
5
+ import { FrameSettingsSection } from "./frame-settings-section.js";
4
6
  import { languageName } from "./mail-origin-bar.js";
5
7
  import { layoutOptionLabel } from "./mail-right-rail.js";
6
8
  import { VersionsSection } from "./mail-versions.js";
9
+ import { EMPTY_MARGIN_INPUT } from "./paper-geometry.js";
7
10
  import { MARGIN_SIDES } from "./pdf-meta.js";
8
11
  import { RailField, RailHint, RailSection } from "./rail.js";
9
12
  import { DEFAULT_LAYOUT_SLUG } from "./types.js";
@@ -13,23 +16,35 @@ const SIDE_LABEL_KEYS = {
13
16
  bottom: "documentDesigner.settings.marginBottom",
14
17
  left: "documentDesigner.settings.marginLeft",
15
18
  };
16
- export function SettingsPanel({ meta, onMetaChange, layouts, locale, version, vocabulary, insert, draftKey, }) {
19
+ export function SettingsPanel({ meta, onMetaChange, layouts, locale, version, vocabulary, insert, frameControl, paper, marginsMm, inheritedMarginsMm, draftKey, }) {
17
20
  const t = useTranslation();
18
- const { Field, Input, Text } = usePrimitives();
21
+ const { Button, Field, Input, Text } = usePrimitives();
22
+ const [overrideOpened, setOverrideOpened] = useState(false);
23
+ const hasMarginOverride = MARGIN_SIDES.some((side) => meta.margins[side] !== "");
24
+ const showMarginFields = overrideOpened || hasMarginOverride;
25
+ const recipientOptions = vocabulary.names.map((name) => ({
26
+ value: name,
27
+ label: vocabulary.labels[name] ?? name,
28
+ }));
19
29
  const patch = (changes) => onMetaChange({ ...meta, ...changes });
20
- return (_jsxs("div", { "data-testid": "document-designer-settings", children: [_jsx(RailSection, { title: t("documentDesigner.settings.page"), testId: "pdf-editor-document", children: _jsxs(RailField, { children: [_jsx(Field, { id: "dd-layout", label: t("documentDesigner.settings.layout"), children: _jsx(Input, { kind: "select", id: "dd-layout", name: "layoutSlug", value: meta.layoutSlug === "" ? DEFAULT_LAYOUT_SLUG : meta.layoutSlug, onChange: (layoutSlug) => patch({ layoutSlug }), display: "dropdown", options: layouts.map((layout) => ({
21
- value: layout.slug,
22
- label: layoutOptionLabel(t, layout),
23
- })) }) }), _jsx(Field, { id: "dd-file-name", label: t("documentDesigner.settings.fileName"), children: _jsx(Input, { kind: "text", id: "dd-file-name", name: "fileName", value: meta.fileName, onChange: (fileName) => patch({ fileName }), placeholder: t("documentDesigner.settings.fileNamePlaceholder") }) }), _jsxs("div", { className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1.5fr)] gap-2", children: [_jsx(Field, { id: "dd-page-format", label: t("documentDesigner.settings.pageFormat"), children: _jsx(Input, { kind: "select", id: "dd-page-format", name: "pageFormat", value: meta.pageFormat, onChange: (pageFormat) => {
24
- const next = PDF_PAGE_FORMATS.find((format) => format === pageFormat);
25
- if (next !== undefined)
26
- patch({ pageFormat: next });
27
- }, display: "dropdown", options: PDF_PAGE_FORMATS.map((value) => ({ value, label: value })) }) }), _jsx(Field, { id: "dd-orientation", label: t("documentDesigner.settings.orientation"), children: _jsx(Input, { kind: "select", id: "dd-orientation", name: "orientation", value: meta.orientation, onChange: (orientation) => {
28
- const next = PDF_ORIENTATIONS.find((candidate) => candidate === orientation);
29
- if (next !== undefined)
30
- patch({ orientation: next });
31
- }, display: "dropdown", options: PDF_ORIENTATIONS.map((value) => ({
32
- value,
33
- label: t(`documentDesigner.settings.${value}`),
34
- })) }) })] }), _jsxs("fieldset", { className: "m-0 flex min-w-0 flex-col gap-1 border-0 p-0", children: [_jsx("legend", { className: "p-0 text-sm font-medium", children: t("documentDesigner.settings.margins") }), _jsx("div", { className: "grid grid-cols-4 gap-1", children: MARGIN_SIDES.map((side) => (_jsx(Field, { id: `dd-margin-${side}`, label: t(SIDE_LABEL_KEYS[side]), hideLabel: true, children: _jsx(Input, { kind: "number", id: `dd-margin-${side}`, name: `margin-${side}`, value: meta.margins[side], onChange: (value) => patch({ margins: { ...meta.margins, [side]: value ?? "" } }), placeholder: "15" }) }, side))) })] }), _jsx(Field, { id: "dd-print-background", label: t("documentDesigner.settings.printBackground"), layout: "inline", children: _jsx(Input, { kind: "boolean", id: "dd-print-background", name: "printBackground", value: meta.printBackground, onChange: (printBackground) => patch({ printBackground }) }) }), _jsx(Field, { id: "dd-layout-header-footer", label: t("documentDesigner.settings.layoutHeaderFooter"), layout: "inline", children: _jsx(Input, { kind: "boolean", id: "dd-layout-header-footer", name: "layoutHeaderFooter", value: meta.layoutHeaderFooter, onChange: (layoutHeaderFooter) => patch({ layoutHeaderFooter }) }) })] }) }), _jsxs(RailSection, { title: t("documentDesigner.settings.metadata"), testId: "pdf-editor-metadata", children: [_jsxs(RailField, { children: [_jsx("div", { onFocusCapture: insert.markFocused("title"), "data-testid": "pdf-meta-title", children: _jsx(Field, { id: "dd-meta-title", label: t("documentDesigner.settings.metaTitle"), children: _jsx(PlaceholderLineEditor, { id: "dd-meta-title", value: meta.title, onChange: (title) => patch({ title }), variableLabels: vocabulary.labels, systemVariables: vocabulary.system, onEditorReady: insert.register("title"), "aria-label": t("documentDesigner.settings.metaTitle") }, `title:${draftKey}`) }) }), _jsx("div", { onFocusCapture: insert.markFocused("author"), "data-testid": "pdf-meta-author", children: _jsx(Field, { id: "dd-meta-author", label: t("documentDesigner.settings.metaAuthor"), children: _jsx(PlaceholderLineEditor, { id: "dd-meta-author", value: meta.author, onChange: (author) => patch({ author }), variableLabels: vocabulary.labels, systemVariables: vocabulary.system, onEditorReady: insert.register("author"), "aria-label": t("documentDesigner.settings.metaAuthor") }, `author:${draftKey}`) }) }), _jsxs("div", { className: "flex flex-col gap-1", "data-testid": "pdf-meta-language", children: [_jsx("span", { className: "text-sm font-medium", children: t("documentDesigner.settings.metaLanguage") }), _jsx(Text, { children: `${languageName(t, locale)} (${locale})` })] })] }), _jsx(RailHint, { children: t("documentDesigner.settings.metaLanguageHint") })] }), _jsx(RailSection, { title: t("documentDesigner.settings.storage"), testId: "pdf-editor-storage", children: _jsx(RailHint, { children: t("documentDesigner.settings.storageHint") }) }), _jsx(VersionsSection, { rowId: version.id, currentVersion: version.version, currentStatus: version.status })] }));
30
+ return (_jsxs("div", { "data-testid": "document-designer-settings", children: [frameControl !== null && (_jsx(FrameSettingsSection, { control: frameControl, paper: paper, margins: marginsMm, recipientOptions: recipientOptions })), _jsxs(RailSection, { title: t("documentDesigner.settings.page"), testId: "pdf-editor-document", children: [_jsxs(RailField, { children: [_jsx(Field, { id: "dd-layout", label: t("documentDesigner.settings.layout"), children: _jsx(Input, { kind: "select", id: "dd-layout", name: "layoutSlug", value: meta.layoutSlug === "" ? DEFAULT_LAYOUT_SLUG : meta.layoutSlug, onChange: (layoutSlug) => patch({ layoutSlug }), display: "dropdown", options: layouts.map((layout) => ({
31
+ value: layout.slug,
32
+ label: layoutOptionLabel(t, layout),
33
+ })) }) }), _jsx(Field, { id: "dd-file-name", label: t("documentDesigner.settings.fileName"), children: _jsx(Input, { kind: "text", id: "dd-file-name", name: "fileName", value: meta.fileName, onChange: (fileName) => patch({ fileName }), placeholder: t("documentDesigner.settings.fileNamePlaceholder") }) }), _jsxs("div", { className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1.5fr)] gap-2", children: [_jsx(Field, { id: "dd-page-format", label: t("documentDesigner.settings.pageFormat"), children: _jsx(Input, { kind: "select", id: "dd-page-format", name: "pageFormat", value: meta.pageFormat, onChange: (pageFormat) => {
34
+ const next = PDF_PAGE_FORMATS.find((format) => format === pageFormat);
35
+ if (next !== undefined)
36
+ patch({ pageFormat: next });
37
+ }, display: "dropdown", options: PDF_PAGE_FORMATS.map((value) => ({ value, label: value })) }) }), _jsx(Field, { id: "dd-orientation", label: t("documentDesigner.settings.orientation"), children: _jsx(Input, { kind: "select", id: "dd-orientation", name: "orientation", value: meta.orientation, onChange: (orientation) => {
38
+ const next = PDF_ORIENTATIONS.find((candidate) => candidate === orientation);
39
+ if (next !== undefined)
40
+ patch({ orientation: next });
41
+ }, display: "dropdown", options: PDF_ORIENTATIONS.map((value) => ({
42
+ value,
43
+ label: t(`documentDesigner.settings.${value}`),
44
+ })) }) })] }), showMarginFields ? (_jsxs("fieldset", { className: "m-0 flex min-w-0 flex-col gap-1 border-0 p-0", "data-testid": "pdf-override-margins", children: [_jsx("legend", { className: "p-0 text-sm font-medium", children: t("documentDesigner.margins.legendShort") }), _jsx("div", { className: "grid grid-cols-4 gap-1", children: MARGIN_SIDES.map((side) => (_jsx(Field, { id: `dd-margin-${side}`, label: t(SIDE_LABEL_KEYS[side]), hideLabel: true, children: _jsx(Input, { kind: "number", id: `dd-margin-${side}`, name: `margin-${side}`, value: meta.margins[side], onChange: (value) => patch({ margins: { ...meta.margins, [side]: value ?? "" } }), placeholder: inheritedMarginsMm === null ? "" : String(inheritedMarginsMm[side]) }) }, side))) }), _jsx(Button, { variant: "link", size: "sm", onClick: () => {
45
+ patch({ margins: EMPTY_MARGIN_INPUT });
46
+ setOverrideOpened(false);
47
+ }, testId: "pdf-reset-margins", children: t("documentDesigner.margins.useLayout") })] })) : (_jsxs("div", { className: "flex flex-col gap-1", "data-testid": "pdf-inherited-margins", children: [_jsx("span", { className: "text-sm font-medium", children: t("documentDesigner.margins.legendShort") }), inheritedMarginsMm !== null && (_jsx(Text, { children: t("documentDesigner.margins.fromLayout", {
48
+ margins: MARGIN_SIDES.map((side) => inheritedMarginsMm[side]).join(" · "),
49
+ }) })), _jsx("div", { className: "flex justify-start", children: _jsx(Button, { variant: "link", size: "sm", onClick: () => setOverrideOpened(true), testId: "pdf-override-margins-open", children: t("documentDesigner.margins.override") }) })] })), _jsx(Field, { id: "dd-print-background", label: t("documentDesigner.settings.printBackground"), layout: "inline", children: _jsx(Input, { kind: "boolean", id: "dd-print-background", name: "printBackground", value: meta.printBackground, onChange: (printBackground) => patch({ printBackground }) }) }), _jsx(Field, { id: "dd-layout-header-footer", label: t("documentDesigner.settings.layoutHeaderFooter"), layout: "inline", children: _jsx(Input, { kind: "boolean", id: "dd-layout-header-footer", name: "layoutHeaderFooter", value: meta.layoutHeaderFooter, onChange: (layoutHeaderFooter) => patch({ layoutHeaderFooter }) }) })] }), _jsx(RailHint, { children: t("documentDesigner.pdf.lookFromLayout") })] }), _jsxs(RailSection, { title: t("documentDesigner.settings.metadata"), testId: "pdf-editor-metadata", children: [_jsxs(RailField, { children: [_jsx("div", { onFocusCapture: insert.markFocused("title"), "data-testid": "pdf-meta-title", children: _jsx(Field, { id: "dd-meta-title", label: t("documentDesigner.settings.metaTitle"), children: _jsx(PlaceholderLineEditor, { id: "dd-meta-title", value: meta.title, onChange: (title) => patch({ title }), variableLabels: vocabulary.labels, systemVariables: vocabulary.system, onEditorReady: insert.register("title"), "aria-label": t("documentDesigner.settings.metaTitle") }, `title:${draftKey}`) }) }), _jsx("div", { onFocusCapture: insert.markFocused("author"), "data-testid": "pdf-meta-author", children: _jsx(Field, { id: "dd-meta-author", label: t("documentDesigner.settings.metaAuthor"), children: _jsx(PlaceholderLineEditor, { id: "dd-meta-author", value: meta.author, onChange: (author) => patch({ author }), variableLabels: vocabulary.labels, systemVariables: vocabulary.system, onEditorReady: insert.register("author"), "aria-label": t("documentDesigner.settings.metaAuthor") }, `author:${draftKey}`) }) }), _jsxs("div", { className: "flex flex-col gap-1", "data-testid": "pdf-meta-language", children: [_jsx("span", { className: "text-sm font-medium", children: t("documentDesigner.settings.metaLanguage") }), _jsx(Text, { children: `${languageName(t, locale)} (${locale})` })] })] }), _jsx(RailHint, { children: t("documentDesigner.settings.metaLanguageHint") })] }), _jsx(RailSection, { title: t("documentDesigner.settings.storage"), testId: "pdf-editor-storage", children: _jsx(RailHint, { children: t("documentDesigner.settings.storageHint") }) }), _jsx(VersionsSection, { rowId: version.id, currentVersion: version.version, currentStatus: version.status })] }));
35
50
  }