Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

refactor(pdf-export): extract shared pdfExport model, style tokens, HTML converter and download helpers

Khuram Niaz90e8aeb5ce7b
diff --git a/__tests__/resume-pdf.spec.ts b/__tests__/resume-pdf.spec.ts
index bd61053..da9a8b4 100644
--- a/__tests__/resume-pdf.spec.ts
+++ b/__tests__/resume-pdf.spec.ts
@@ -1,9 +1,10 @@
import { describe, it, expect } from "vitest";
-import { sanitizeFilename, RESUME_LAYOUT_LABELS } from "@/components/profile/resume-pdf";
+import { sanitizeFilename } from "@/components/pdf-export/download";
+import { RESUME_LAYOUT_LABELS } from "@/models/resumeExport.model";
// DOMParser is available in jsdom (vitest environment)
-import { htmlToPdfNodes } from "@/components/profile/resume-pdf/html-to-pdf";
-import type { HtmlStyleSet } from "@/components/profile/resume-pdf/types";
+import { htmlToPdfNodes } from "@/components/pdf-export/html-to-pdf";
+import type { HtmlStyleSet } from "@/components/pdf-export/types";
// Minimal style sets — avoid @react-pdf StyleSheet.create in test env
const simpleStyleSet: HtmlStyleSet = {
@@ -25,33 +26,37 @@ const professionalStyleSet: HtmlStyleSet = {
describe("sanitizeFilename", () => {
it("returns 'resume' for empty string", () => {
- expect(sanitizeFilename("")).toBe("resume");
+ expect(sanitizeFilename("", "resume")).toBe("resume");
});
it("returns 'resume' for string of only unsafe chars", () => {
- expect(sanitizeFilename('???/\\:*?"<>|')).toBe("resume");
+ expect(sanitizeFilename('???/\\:*?"<>|', "resume")).toBe("resume");
});
it("strips null bytes and control characters", () => {
- expect(sanitizeFilename("my\x00resume\x1f")).toBe("myresume");
+ expect(sanitizeFilename("my\x00resume\x1f", "resume")).toBe("myresume");
});
it("truncates to 200 chars", () => {
const long = "a".repeat(250);
- expect(sanitizeFilename(long)).toHaveLength(200);
+ expect(sanitizeFilename(long, "resume")).toHaveLength(200);
});
it("leaves a clean string unchanged", () => {
- expect(sanitizeFilename("Senior Backend Resume")).toBe(
+ expect(sanitizeFilename("Senior Backend Resume", "resume")).toBe(
"Senior Backend Resume",
);
});
it("collapses extra whitespace", () => {
- expect(sanitizeFilename("Senior Backend Resume")).toBe(
+ expect(sanitizeFilename("Senior Backend Resume", "resume")).toBe(
"Senior Backend Resume",
);
});
+
+ it("uses the caller's fallback, not a hardcoded one", () => {
+ expect(sanitizeFilename(" ", "cover-letter")).toBe("cover-letter");
+ });
});
describe("htmlToPdfNodes", () => {
diff --git a/__tests__/resumeExportSettings.spec.ts b/__tests__/resumeExportSettings.spec.ts
index a6d151b..f68ef76 100644
--- a/__tests__/resumeExportSettings.spec.ts
+++ b/__tests__/resumeExportSettings.spec.ts
@@ -1,11 +1,14 @@
import { describe, it, expect } from "vitest";
import {
clampNumericField,
- defaultResumeExportSettings,
formatSettingValue,
- RESUME_FONT_LABELS,
- RESUME_NUMERIC_SETTINGS,
- type ResumeNumericSetting,
+ PDF_FONT_LABELS,
+ PDF_NUMERIC_SETTINGS,
+ settingsKey,
+} from "@/models/pdfExport.model";
+import {
+ defaultResumeExportSettings,
+ RESUME_NUMERIC_FIELDS,
} from "@/models/resumeExport.model";
describe("defaultResumeExportSettings", () => {
@@ -38,17 +41,18 @@ describe("defaultResumeExportSettings", () => {
});
it("puts every default inside its own field's range", () => {
- for (const [field, spec] of Object.entries(RESUME_NUMERIC_SETTINGS)) {
- const value = defaultResumeExportSettings[field as ResumeNumericSetting];
+ for (const field of RESUME_NUMERIC_FIELDS) {
+ const spec = PDF_NUMERIC_SETTINGS[field];
+ const value = defaultResumeExportSettings[field];
expect(value).toBeGreaterThanOrEqual(spec.min);
expect(value).toBeLessThanOrEqual(spec.max);
}
});
});
-describe("RESUME_FONT_LABELS", () => {
+describe("PDF_FONT_LABELS", () => {
it("labels the three standard-14 families", () => {
- expect(RESUME_FONT_LABELS).toEqual({
+ expect(PDF_FONT_LABELS).toEqual({
helvetica: "Helvetica",
times: "Times",
courier: "Courier",
@@ -56,20 +60,21 @@ describe("RESUME_FONT_LABELS", () => {
});
});
-describe("RESUME_NUMERIC_SETTINGS", () => {
- it("describes all six numeric settings", () => {
- expect(Object.keys(RESUME_NUMERIC_SETTINGS)).toEqual([
+describe("PDF_NUMERIC_SETTINGS", () => {
+ it("describes every numeric setting either document uses", () => {
+ expect(Object.keys(PDF_NUMERIC_SETTINGS)).toEqual([
"fontSize",
"lineHeight",
"marginVertical",
"marginHorizontal",
"sectionSpacing",
"entrySpacing",
+ "paragraphSpacing",
]);
});
it("gives font size half-point steps in points", () => {
- expect(RESUME_NUMERIC_SETTINGS.fontSize).toEqual({
+ expect(PDF_NUMERIC_SETTINGS.fontSize).toEqual({
label: "Font size",
min: 8,
max: 16,
@@ -80,23 +85,49 @@ describe("RESUME_NUMERIC_SETTINGS", () => {
});
it("gives line height a unitless two-decimal scale", () => {
- expect(RESUME_NUMERIC_SETTINGS.lineHeight.unit).toBe("");
- expect(RESUME_NUMERIC_SETTINGS.lineHeight.step).toBe(0.05);
- expect(RESUME_NUMERIC_SETTINGS.lineHeight.decimals).toBe(2);
+ expect(PDF_NUMERIC_SETTINGS.lineHeight.unit).toBe("");
+ expect(PDF_NUMERIC_SETTINGS.lineHeight.step).toBe(0.05);
+ expect(PDF_NUMERIC_SETTINGS.lineHeight.decimals).toBe(2);
});
it("gives every field a step it can reach from its own default", () => {
- for (const [field, spec] of Object.entries(RESUME_NUMERIC_SETTINGS)) {
+ for (const field of RESUME_NUMERIC_FIELDS) {
+ const spec = PDF_NUMERIC_SETTINGS[field];
expect(spec.step).toBeGreaterThan(0);
expect(spec.min).toBeLessThan(spec.max);
expect(spec.label.length).toBeGreaterThan(0);
- expect(defaultResumeExportSettings[field as ResumeNumericSetting]).toBeTypeOf(
- "number",
- );
+ expect(defaultResumeExportSettings[field]).toBeTypeOf("number");
}
});
});
+describe("PDF_NUMERIC_SETTINGS vs RESUME_NUMERIC_FIELDS", () => {
+ it("carries paragraphSpacing in the shared table", () => {
+ expect(PDF_NUMERIC_SETTINGS.paragraphSpacing).toEqual({
+ label: "Paragraph spacing",
+ min: 0,
+ max: 24,
+ step: 1,
+ decimals: 0,
+ unit: "pt",
+ });
+ });
+
+ // Iterating the merged table would add a paragraphSpacing key to every
+ // resume settings object, which nothing on the resume side reads.
+ it("keeps paragraphSpacing out of the resume's field list", () => {
+ expect(RESUME_NUMERIC_FIELDS).toEqual([
+ "fontSize",
+ "lineHeight",
+ "marginVertical",
+ "marginHorizontal",
+ "sectionSpacing",
+ "entrySpacing",
+ ]);
+ expect(RESUME_NUMERIC_FIELDS).not.toContain("paragraphSpacing");
+ });
+});
+
describe("clampNumericField", () => {
it("returns a value already in range unchanged", () => {
expect(clampNumericField("fontSize", 12.5)).toBe(12.5);
@@ -123,11 +154,22 @@ describe("clampNumericField", () => {
expect(clampNumericField("marginVertical", 41)).toBe(41);
});
- it("falls back to the field's default for anything that is not a number", () => {
+ it("returns the caller's fallback for anything that is not a number", () => {
for (const value of [null, undefined, "40", NaN, Infinity, {}, []]) {
- expect(clampNumericField("marginVertical", value)).toBe(40);
+ expect(clampNumericField("marginVertical", value, 40)).toBe(40);
}
});
+
+ // The shared model must not know about resume templates, so with no
+ // fallback a non-finite value lands on the field's own floor.
+ it("falls back to the field's min when no fallback is given", () => {
+ expect(clampNumericField("marginVertical", NaN)).toBe(18);
+ expect(clampNumericField("fontSize", "nope")).toBe(8);
+ });
+
+ it("accepts 0 as a fallback rather than treating it as absent", () => {
+ expect(clampNumericField("paragraphSpacing", NaN, 0)).toBe(0);
+ });
});
describe("formatSettingValue", () => {
@@ -140,3 +182,22 @@ describe("formatSettingValue", () => {
expect(formatSettingValue("entrySpacing", 8)).toBe("8 pt");
});
});
+
+describe("settingsKey", () => {
+ // This is what lets the rest of the code stop caring about key order.
+ it("is the same string whatever order the keys were written in", () => {
+ expect(settingsKey({ font: "times", fontSize: 12 })).toBe(
+ settingsKey({ fontSize: 12, font: "times" }),
+ );
+ });
+
+ it("still separates objects that differ in a value", () => {
+ expect(settingsKey({ fontSize: 12 })).not.toBe(settingsKey({ fontSize: 13 }));
+ });
+
+ it("separates objects that differ in which keys they carry", () => {
+ expect(settingsKey({ fontSize: 12 })).not.toBe(
+ settingsKey({ fontSize: 12, paragraphSpacing: 10 }),
+ );
+ });
+});
diff --git a/__tests__/resumeExportTokens.spec.ts b/__tests__/resumeExportTokens.spec.ts
index aeeae9b..2be97b9 100644
--- a/__tests__/resumeExportTokens.spec.ts
+++ b/__tests__/resumeExportTokens.spec.ts
@@ -3,11 +3,11 @@ import { defaultResumeExportSettings } from "@/models/resumeExport.model";
import {
BASE_FONT_SIZE,
FONT_QUARTETS,
- styleTokens,
-} from "@/components/profile/resume-pdf/styles/tokens";
+} from "@/components/pdf-export/tokens";
+import { resumeStyleTokens } from "@/components/profile/resume-pdf/styles/tokens";
const tokens = (patch: Partial<typeof defaultResumeExportSettings> = {}) =>
- styleTokens({ ...defaultResumeExportSettings, ...patch });
+ resumeStyleTokens({ ...defaultResumeExportSettings, ...patch });
describe("FONT_QUARTETS", () => {
it("names all four faces of each standard-14 family", () => {
diff --git a/src/components/pdf-export/download.ts b/src/components/pdf-export/download.ts
new file mode 100644
index 0000000..29fa93d
--- /dev/null
+++ b/src/components/pdf-export/download.ts
@@ -0,0 +1,18 @@
+export function sanitizeFilename(name: string, fallback: string): string {
+ const sanitized = name
+ .replace(/[\x00-\x1f\x7f]/g, "")
+ .replace(/[/\\:*?"<>|]/g, "")
+ .replace(/\s+/g, " ")
+ .trim()
+ .slice(0, 200);
+ return sanitized || fallback;
+}
+
+export function triggerDownload(blob: Blob, filename: string): void {
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ a.href = url;
+ a.download = filename;
+ a.click();
+ URL.revokeObjectURL(url);
+}
diff --git a/src/components/profile/resume-pdf/html-to-pdf.tsx b/src/components/pdf-export/html-to-pdf.tsx
similarity index 100%
rename from src/components/profile/resume-pdf/html-to-pdf.tsx
rename to src/components/pdf-export/html-to-pdf.tsx
diff --git a/src/components/pdf-export/tokens.ts b/src/components/pdf-export/tokens.ts
new file mode 100644
index 0000000..dee20da
--- /dev/null
+++ b/src/components/pdf-export/tokens.ts
@@ -0,0 +1,61 @@
+import type { BaseExportSettings, PdfFont } from "@/models/pdfExport.model";
+
+export type FontQuartet = {
+ regular: string;
+ bold: string;
+ italic: string;
+ boldItalic: string;
+};
+
+// The three standard-14 families @react-pdf/font ships; anything else
+// would need font files shipped or fetched.
+export const FONT_QUARTETS: Record<PdfFont, FontQuartet> = {
+ helvetica: {
+ regular: "Helvetica",
+ bold: "Helvetica-Bold",
+ italic: "Helvetica-Oblique",
+ boldItalic: "Helvetica-BoldOblique",
+ },
+ times: {
+ regular: "Times-Roman",
+ bold: "Times-Bold",
+ italic: "Times-Italic",
+ boldItalic: "Times-BoldItalic",
+ },
+ courier: {
+ regular: "Courier",
+ bold: "Courier-Bold",
+ italic: "Courier-Oblique",
+ boldItalic: "Courier-BoldOblique",
+ },
+};
+
+// Every derived size in every sheet is proportional to this.
+export const BASE_FONT_SIZE = 11;
+
+export type BaseStyleTokens = {
+ font: FontQuartet;
+ /** The base size itself. Assigned, never passed through `pt`. */
+ fontSize: number;
+ /** Sizes derived from the base, and font-metric widths. */
+ pt: (literal: number) => number;
+ lineHeight: number;
+ marginVertical: number;
+ marginHorizontal: number;
+};
+
+export function styleTokens(settings: BaseExportSettings): BaseStyleTokens {
+ const scale = settings.fontSize / BASE_FONT_SIZE;
+
+ return {
+ font: FONT_QUARTETS[settings.font],
+ fontSize: settings.fontSize,
+ // A scale of exactly 1 returns the literal untouched rather than
+ // rounding it, so the defaults reproduce each shipped sheet exactly.
+ pt: (literal) =>
+ scale === 1 ? literal : Math.round(literal * scale * 10) / 10,
+ lineHeight: settings.lineHeight,
+ marginVertical: settings.marginVertical,
+ marginHorizontal: settings.marginHorizontal,
+ };
+}
diff --git a/src/components/profile/resume-pdf/types.ts b/src/components/pdf-export/types.ts
similarity index 55%
rename from src/components/profile/resume-pdf/types.ts
rename to src/components/pdf-export/types.ts
index ff7b1f5..a733eb4 100644
--- a/src/components/profile/resume-pdf/types.ts
+++ b/src/components/pdf-export/types.ts
@@ -1,14 +1,5 @@
import type { Style } from "@react-pdf/types";
-export type {
- ResumeExportSettings,
- ResumeLayout,
-} from "@/models/resumeExport.model";
-export {
- RESUME_LAYOUT_LABELS,
- defaultResumeExportSettings,
-} from "@/models/resumeExport.model";
-
export type HtmlStyleSet = {
bodyText: Style;
bold: Style;
diff --git a/src/components/profile/export-pdf-dialog/ExportSettingsPanel.tsx b/src/components/profile/export-pdf-dialog/ExportSettingsPanel.tsx
index 05352a0..736cb4e 100644
--- a/src/components/profile/export-pdf-dialog/ExportSettingsPanel.tsx
+++ b/src/components/profile/export-pdf-dialog/ExportSettingsPanel.tsx
@@ -20,9 +20,11 @@ import {
import {
clampNumericField,
formatSettingValue,
- RESUME_FONT_LABELS,
+ PDF_FONT_LABELS,
+ PDF_NUMERIC_SETTINGS,
+} from "@/models/pdfExport.model";
+import {
RESUME_LAYOUT_LABELS,
- RESUME_NUMERIC_SETTINGS,
type ResumeExportSettings,
type ResumeNumericSetting,
} from "@/models/resumeExport.model";
@@ -87,7 +89,7 @@ function StepperRow({
value: number;
onCommit: (value: number) => void;
}) {
- const spec = RESUME_NUMERIC_SETTINGS[field];
+ const spec = PDF_NUMERIC_SETTINGS[field];
const id = `resume-export-${field}`;
const [draft, setDraft] = useState(() => String(value));
@@ -197,7 +199,7 @@ export function ExportSettingsPanel({
onChange({ ...settings, [field]: value });
const typographySummary = [
- RESUME_FONT_LABELS[settings.font],
+ PDF_FONT_LABELS[settings.font],
formatSettingValue("fontSize", settings.fontSize),
`${formatSettingValue("lineHeight", settings.lineHeight)} line`,
].join(" • ");
@@ -225,7 +227,7 @@ export function ExportSettingsPanel({
id="resume-export-font"
label="Font"
value={settings.font}
- labels={RESUME_FONT_LABELS}
+ labels={PDF_FONT_LABELS}
onSelect={(font) => onChange({ ...settings, font })}
/>
<StepperRow
diff --git a/src/components/profile/resume-container/useResumeExportSettings.ts b/src/components/profile/resume-container/useResumeExportSettings.ts
index eb8bb28..bca2372 100644
--- a/src/components/profile/resume-container/useResumeExportSettings.ts
+++ b/src/components/profile/resume-container/useResumeExportSettings.ts
@@ -3,15 +3,16 @@ import { useEffect, useState } from "react";
import { APP_CONSTANTS } from "@/lib/constants";
import {
clampNumericField,
+ PDF_FONT_LABELS,
+ type PdfFont,
+} from "@/models/pdfExport.model";
+import {
defaultResumeExportSettings,
- RESUME_FONT_LABELS,
RESUME_LAYOUT_LABELS,
- RESUME_NUMERIC_SETTINGS,
+ RESUME_NUMERIC_FIELDS,
RESUME_TEMPLATE_DEFAULTS,
type ResumeExportSettings,
- type ResumeFont,
type ResumeLayout,
- type ResumeNumericSetting,
} from "@/models/resumeExport.model";
import {
getFromLocalStorage,
@@ -38,16 +39,19 @@ export function coerceResumeExportSettings(
: defaultResumeExportSettings.template;
const next = { ...RESUME_TEMPLATE_DEFAULTS[template] };
- if (typeof raw.font === "string" && raw.font in RESUME_FONT_LABELS) {
- next.font = raw.font as ResumeFont;
+ if (typeof raw.font === "string" && raw.font in PDF_FONT_LABELS) {
+ next.font = raw.font as PdfFont;
}
- // Numeric settings: clampNumericField returns the field's default for
- // anything that is not a finite number, so a missing key needs no guard.
- for (const field of Object.keys(
- RESUME_NUMERIC_SETTINGS,
- ) as ResumeNumericSetting[]) {
- next[field] = clampNumericField(field, raw[field], template);
+ // Iterates the resume's own field list, never the merged spec table.
+ // clampNumericField returns the passed fallback for anything that is not
+ // a finite number, so a missing key needs no guard.
+ for (const field of RESUME_NUMERIC_FIELDS) {
+ next[field] = clampNumericField(
+ field,
+ raw[field],
+ RESUME_TEMPLATE_DEFAULTS[template][field],
+ );
}
return next;
diff --git a/src/components/profile/resume-container/useResumePdfExport.ts b/src/components/profile/resume-container/useResumePdfExport.ts
index 3f5176b..58309b9 100644
--- a/src/components/profile/resume-container/useResumePdfExport.ts
+++ b/src/components/profile/resume-container/useResumePdfExport.ts
@@ -1,6 +1,7 @@
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
+import { triggerDownload } from "@/components/pdf-export/download";
import { toastError, toastSuccess } from "@/lib/toast";
import type { Resume } from "@/models/profile.model";
import {
@@ -39,15 +40,6 @@ export function useResumePdfExport(resume: Resume) {
} | null>(null);
const [showAttachConfirm, setShowAttachConfirm] = useState(false);
- const triggerDownload = (blob: Blob, filename: string) => {
- const url = URL.createObjectURL(blob);
- const a = document.createElement("a");
- a.href = url;
- a.download = filename;
- a.click();
- URL.revokeObjectURL(url);
- };
-
const uploadPdfAsAttachment = async (
blob: Blob,
filename: string,
diff --git a/src/components/profile/resume-pdf/generateResumePdf.tsx b/src/components/profile/resume-pdf/generateResumePdf.tsx
index c69f7bd..11ad6eb 100644
--- a/src/components/profile/resume-pdf/generateResumePdf.tsx
+++ b/src/components/profile/resume-pdf/generateResumePdf.tsx
@@ -1,12 +1,13 @@
import React from "react";
import { Resume, SectionType } from "@/models/profile.model";
-import { htmlToPdfNodes } from "./html-to-pdf";
+import { htmlToPdfNodes } from "@/components/pdf-export/html-to-pdf";
+import { sanitizeFilename } from "@/components/pdf-export/download";
+import type { HtmlStyleSet } from "@/components/pdf-export/types";
import {
RESUME_LAYOUT_LABELS,
defaultResumeExportSettings,
type ResumeExportSettings,
-} from "./types";
-import type { HtmlStyleSet } from "./types";
+} from "@/models/resumeExport.model";
import { buildSimpleStyles } from "./styles/simple.styles";
import { buildProfessionalStyles } from "./styles/professional.styles";
@@ -16,16 +17,6 @@ export type ResumeHtmlNodes = {
educations: React.ReactElement[][];
};
-export function sanitizeFilename(name: string): string {
- const sanitized = name
- .replace(/[\x00-\x1f\x7f]/g, "")
- .replace(/[/\\:*?"<>|]/g, "")
- .replace(/\s+/g, " ")
- .trim()
- .slice(0, 200);
- return sanitized || "resume";
-}
-
// Parse HTML on the main thread before entering react-pdf's rendering context
function buildHtmlNodes(
resume: Resume,
@@ -92,16 +83,6 @@ export async function generateResumePdfBlob(
}
const blob = await pdf(document as any).toBlob();
- const filename = `${sanitizeFilename(resume.title)} - ${RESUME_LAYOUT_LABELS[layout]}.pdf`;
+ const filename = `${sanitizeFilename(resume.title, "resume")} - ${RESUME_LAYOUT_LABELS[layout]}.pdf`;
return { blob, filename };
}
-
-export async function downloadResumePdf(resume: Resume): Promise<void> {
- const { blob, filename } = await generateResumePdfBlob(resume);
- const url = URL.createObjectURL(blob);
- const a = document.createElement("a");
- a.href = url;
- a.download = filename;
- a.click();
- URL.revokeObjectURL(url);
-}
diff --git a/src/components/profile/resume-pdf/index.ts b/src/components/profile/resume-pdf/index.ts
index 84ec05e..991607b 100644
--- a/src/components/profile/resume-pdf/index.ts
+++ b/src/components/profile/resume-pdf/index.ts
@@ -1,4 +1 @@
-export { downloadResumePdf, generateResumePdfBlob, sanitizeFilename } from "./generateResumePdf";
-export { htmlToPdfNodes } from "./html-to-pdf";
-export type { ResumeLayout } from "./types";
-export { RESUME_LAYOUT_LABELS } from "./types";
+export { generateResumePdfBlob } from "./generateResumePdf";
diff --git a/src/components/profile/resume-pdf/styles/professional.styles.ts b/src/components/profile/resume-pdf/styles/professional.styles.ts
index 904593e..da90c1d 100644
--- a/src/components/profile/resume-pdf/styles/professional.styles.ts
+++ b/src/components/profile/resume-pdf/styles/professional.styles.ts
@@ -1,14 +1,14 @@
import { StyleSheet } from "@react-pdf/renderer";
-import { HtmlStyleSet } from "../types";
+import { HtmlStyleSet } from "@/components/pdf-export/types";
import type { ResumeExportSettings } from "@/models/resumeExport.model";
-import { styleTokens } from "./tokens";
+import { resumeStyleTokens } from "./tokens";
const ACCENT = "#34506e";
const NEAR_BLACK = "#1a1a1a";
const GRAY = "#555555";
export function buildProfessionalStyles(settings: ResumeExportSettings) {
- const t = styleTokens(settings);
+ const t = resumeStyleTokens(settings);
const styles = StyleSheet.create({
page: {
diff --git a/src/components/profile/resume-pdf/styles/simple.styles.ts b/src/components/profile/resume-pdf/styles/simple.styles.ts
index 84249c5..2c339ab 100644
--- a/src/components/profile/resume-pdf/styles/simple.styles.ts
+++ b/src/components/profile/resume-pdf/styles/simple.styles.ts
@@ -1,10 +1,10 @@
import { StyleSheet } from "@react-pdf/renderer";
-import { HtmlStyleSet } from "../types";
+import { HtmlStyleSet } from "@/components/pdf-export/types";
import type { ResumeExportSettings } from "@/models/resumeExport.model";
-import { styleTokens } from "./tokens";
+import { resumeStyleTokens } from "./tokens";
export function buildSimpleStyles(settings: ResumeExportSettings) {
- const t = styleTokens(settings);
+ const t = resumeStyleTokens(settings);
const styles = StyleSheet.create({
page: {
diff --git a/src/components/profile/resume-pdf/styles/tokens.ts b/src/components/profile/resume-pdf/styles/tokens.ts
index 69cf41a..08143d0 100644
--- a/src/components/profile/resume-pdf/styles/tokens.ts
+++ b/src/components/profile/resume-pdf/styles/tokens.ts
@@ -1,72 +1,24 @@
-import type {
- ResumeExportSettings,
- ResumeFont,
-} from "@/models/resumeExport.model";
-
-export type FontQuartet = {
- regular: string;
- bold: string;
- italic: string;
- boldItalic: string;
-};
-
-// The three standard-14 families @react-pdf/font ships; anything else
-// would need font files shipped or fetched.
-export const FONT_QUARTETS: Record<ResumeFont, FontQuartet> = {
- helvetica: {
- regular: "Helvetica",
- bold: "Helvetica-Bold",
- italic: "Helvetica-Oblique",
- boldItalic: "Helvetica-BoldOblique",
- },
- times: {
- regular: "Times-Roman",
- bold: "Times-Bold",
- italic: "Times-Italic",
- boldItalic: "Times-BoldItalic",
- },
- courier: {
- regular: "Courier",
- bold: "Courier-Bold",
- italic: "Courier-Oblique",
- boldItalic: "Courier-BoldOblique",
- },
-};
-
-// Every derived size in both sheets is proportional to this.
-export const BASE_FONT_SIZE = 11;
+import type { ResumeExportSettings } from "@/models/resumeExport.model";
+import {
+ styleTokens,
+ type BaseStyleTokens,
+} from "@/components/pdf-export/tokens";
// Certificate blocks sit at three quarters of an entry gap in both
// templates (6 against 8), so the ratio is identity at the default.
const CERT_SPACING_RATIO = 0.75;
-export type StyleTokens = {
- font: FontQuartet;
- /** The base size itself. Assigned, never passed through `pt`. */
- fontSize: number;
- /** Sizes derived from the base, and the two font-metric widths. */
- pt: (literal: number) => number;
- lineHeight: number;
- marginVertical: number;
- marginHorizontal: number;
+export type StyleTokens = BaseStyleTokens & {
sectionSpacing: number;
entrySpacing: number;
certSpacing: number;
};
-export function styleTokens(settings: ResumeExportSettings): StyleTokens {
- const scale = settings.fontSize / BASE_FONT_SIZE;
-
+export function resumeStyleTokens(
+ settings: ResumeExportSettings,
+): StyleTokens {
return {
- font: FONT_QUARTETS[settings.font],
- fontSize: settings.fontSize,
- // A scale of exactly 1 returns the literal untouched rather than
- // rounding it, so the defaults reproduce the Simple sheet exactly.
- pt: (literal) =>
- scale === 1 ? literal : Math.round(literal * scale * 10) / 10,
- lineHeight: settings.lineHeight,
- marginVertical: settings.marginVertical,
- marginHorizontal: settings.marginHorizontal,
+ ...styleTokens(settings),
sectionSpacing: settings.sectionSpacing,
entrySpacing: settings.entrySpacing,
certSpacing: Math.round(settings.entrySpacing * CERT_SPACING_RATIO),
diff --git a/src/models/pdfExport.model.ts b/src/models/pdfExport.model.ts
new file mode 100644
index 0000000..eb0e61c
--- /dev/null
+++ b/src/models/pdfExport.model.ts
@@ -0,0 +1,89 @@
+export type PdfFont = "helvetica" | "times" | "courier";
+
+export const PDF_FONT_LABELS: Record<PdfFont, string> = {
+ helvetica: "Helvetica",
+ times: "Times",
+ courier: "Courier",
+};
+
+// Every numeric field either document uses. A lookup table, never an
+// iteration source — each document owns its own ordered field list.
+export type NumericSetting =
+ | "fontSize"
+ | "lineHeight"
+ | "marginVertical"
+ | "marginHorizontal"
+ | "sectionSpacing"
+ | "entrySpacing"
+ | "paragraphSpacing";
+
+export type NumericSettingSpec = {
+ label: string;
+ min: number;
+ max: number;
+ step: number;
+ // Places a value is rounded to on write; kills IEEE-754 step drift.
+ decimals: number;
+ unit: string;
+};
+
+// One table: the stepper rows, the summaries and the storage validators all
+// read their bounds from here.
+export const PDF_NUMERIC_SETTINGS: Record<NumericSetting, NumericSettingSpec> = {
+ fontSize: { label: "Font size", min: 8, max: 16, step: 0.5, decimals: 1, unit: "pt" },
+ lineHeight: { label: "Line height", min: 1, max: 2, step: 0.05, decimals: 2, unit: "" },
+ marginVertical: { label: "Margin top/bottom", min: 18, max: 90, step: 2, decimals: 0, unit: "pt" },
+ marginHorizontal: { label: "Margin left/right", min: 18, max: 90, step: 2, decimals: 0, unit: "pt" },
+ sectionSpacing: { label: "Section spacing", min: 0, max: 32, step: 1, decimals: 0, unit: "pt" },
+ entrySpacing: { label: "Entry spacing", min: 0, max: 32, step: 1, decimals: 0, unit: "pt" },
+ paragraphSpacing: { label: "Paragraph spacing", min: 0, max: 24, step: 1, decimals: 0, unit: "pt" },
+};
+
+// The shape styleTokens consumes — the settings both documents share.
+export interface BaseExportSettings {
+ font: PdfFont;
+ fontSize: number;
+ lineHeight: number;
+ marginVertical: number;
+ marginHorizontal: number;
+}
+
+const roundTo = (value: number, decimals: number) => {
+ const factor = 10 ** decimals;
+ return Math.round(value * factor) / factor;
+};
+
+// Every write to a numeric setting goes through here — stepper buttons,
+// typed input and the stored-value validators alike. `fallback` is the
+// caller's own document default; the stepper passes none, since its value
+// is finite by construction.
+export function clampNumericField(
+ field: NumericSetting,
+ value: unknown,
+ fallback?: number,
+): number {
+ const spec = PDF_NUMERIC_SETTINGS[field];
+ if (typeof value !== "number" || !Number.isFinite(value)) {
+ return fallback ?? spec.min;
+ }
+ return roundTo(Math.min(spec.max, Math.max(spec.min, value)), spec.decimals);
+}
+
+// Reads the way the stepper input reads: a bare number, plus a unit when
+// the field has one. `decimals` is for rounding, not for display.
+export function formatSettingValue(
+ field: NumericSetting,
+ value: number,
+): string {
+ const spec = PDF_NUMERIC_SETTINGS[field];
+ return spec.unit ? `${value} ${spec.unit}` : String(value);
+}
+
+// The one way a settings object becomes a string — the preview cache key and
+// every "is this still the default?" check. Sorting the keys means the order
+// a literal was written in carries no meaning, so a rebuilt object compares
+// equal to a spread one. Settings are flat; the array replacer would drop the
+// keys of a nested object.
+export function settingsKey(settings: object): string {
+ return JSON.stringify(settings, Object.keys(settings).sort());
+}
diff --git a/src/models/resumeExport.model.ts b/src/models/resumeExport.model.ts
index daa92f0..4ef038f 100644
--- a/src/models/resumeExport.model.ts
+++ b/src/models/resumeExport.model.ts
@@ -1,54 +1,29 @@
+import type { NumericSetting, PdfFont } from "./pdfExport.model";
+
export type ResumeLayout = "simple" | "professional";
-export type ResumeFont = "helvetica" | "times" | "courier";
export const RESUME_LAYOUT_LABELS: Record<ResumeLayout, string> = {
simple: "Simple",
professional: "Professional",
};
-export const RESUME_FONT_LABELS: Record<ResumeFont, string> = {
- helvetica: "Helvetica",
- times: "Times",
- courier: "Courier",
-};
-
-export type ResumeNumericSetting =
- | "fontSize"
- | "lineHeight"
- | "marginVertical"
- | "marginHorizontal"
- | "sectionSpacing"
- | "entrySpacing";
+// The explicit list the resume's coercion iterates. Iterating the merged
+// PDF_NUMERIC_SETTINGS table instead would write paragraphSpacing into a
+// resume settings object, where nothing reads it and everything carries it.
+export const RESUME_NUMERIC_FIELDS = [
+ "fontSize",
+ "lineHeight",
+ "marginVertical",
+ "marginHorizontal",
+ "sectionSpacing",
+ "entrySpacing",
+] as const satisfies readonly NumericSetting[];
-export type NumericSettingSpec = {
- label: string;
- min: number;
- max: number;
- step: number;
- // Places a value is rounded to on write; kills IEEE-754 step drift.
- decimals: number;
- unit: string;
-};
-
-// One table: the stepper rows, the summaries and the storage validator all
-// read their bounds from here.
-export const RESUME_NUMERIC_SETTINGS: Record<
- ResumeNumericSetting,
- NumericSettingSpec
-> = {
- fontSize: { label: "Font size", min: 8, max: 16, step: 0.5, decimals: 1, unit: "pt" },
- lineHeight: { label: "Line height", min: 1, max: 2, step: 0.05, decimals: 2, unit: "" },
- marginVertical: { label: "Margin top/bottom", min: 18, max: 90, step: 2, decimals: 0, unit: "pt" },
- marginHorizontal: { label: "Margin left/right", min: 18, max: 90, step: 2, decimals: 0, unit: "pt" },
- sectionSpacing: { label: "Section spacing", min: 0, max: 32, step: 1, decimals: 0, unit: "pt" },
- entrySpacing: { label: "Entry spacing", min: 0, max: 32, step: 1, decimals: 0, unit: "pt" },
-};
+export type ResumeNumericSetting = (typeof RESUME_NUMERIC_FIELDS)[number];
-// The preview cache key is JSON.stringify(settings), so this literal's key
-// order is the order every settings object must keep.
export interface ResumeExportSettings {
template: ResumeLayout;
- font: ResumeFont;
+ font: PdfFont;
fontSize: number;
lineHeight: number;
marginVertical: number;
@@ -86,32 +61,3 @@ export const RESUME_TEMPLATE_DEFAULTS: Record<
};
export const defaultResumeExportSettings = RESUME_TEMPLATE_DEFAULTS.simple;
-
-const roundTo = (value: number, decimals: number) => {
- const factor = 10 ** decimals;
- return Math.round(value * factor) / factor;
-};
-
-// Every write to a numeric setting goes through here — stepper buttons,
-// typed input and the stored-value validator alike.
-export function clampNumericField(
- field: ResumeNumericSetting,
- value: unknown,
- template: ResumeLayout = "simple",
-): number {
- if (typeof value !== "number" || !Number.isFinite(value)) {
- return RESUME_TEMPLATE_DEFAULTS[template][field];
- }
- const spec = RESUME_NUMERIC_SETTINGS[field];
- return roundTo(Math.min(spec.max, Math.max(spec.min, value)), spec.decimals);
-}
-
-// Reads the way the stepper input reads: a bare number, plus a unit when
-// the field has one. `decimals` is for rounding, not for display.
-export function formatSettingValue(
- field: ResumeNumericSetting,
- value: number,
-): string {
- const spec = RESUME_NUMERIC_SETTINGS[field];
- return spec.unit ? `${value} ${spec.unit}` : String(value);
-}