Give finished Rules a print path, folded launch counts, and a two-day follow-up.
Export is PDF, RULE.md, and Print on US Letter, with every shared term, no member names, and a takeaway that points at Ask an organizer. Finished Rules keep type and place so the team can read folded counts without a database, Umami can sit beside the app with no cookies, and the person who holds the pen gets working PDF and RULE.md links 48 hours later.
This commit is contained in:
1 parent
0efc907633
commit
d9c475a967
74 files changed
+2401
-630
No files matched your search
@@ -0,0 +1,228 @@
|
||||
import { jsPDF } from "jspdf";
|
||||
|
||||
import exportCopy from "../../messages/en/create/export.json";
|
||||
import type {
|
||||
RuleExportCard,
|
||||
RuleExportDocument,
|
||||
RuleExportLine,
|
||||
RuleExportTerm,
|
||||
} from "./ruleExportModel";
|
||||
|
||||
const PAGE_W = 215.9;
|
||||
const PAGE_H = 279.4;
|
||||
const MARGIN_X = 16.9;
|
||||
const MARGIN_TOP = 12.7;
|
||||
const FOOTER_Y = PAGE_H - 9.5;
|
||||
const CONTENT_BOTTOM = PAGE_H - 16;
|
||||
const COL_GAP = 8.5;
|
||||
const COL_W = (PAGE_W - 2 * MARGIN_X - COL_GAP) / 2;
|
||||
|
||||
function lineText(line: RuleExportLine): string {
|
||||
return line.lead ? `${line.lead} ${line.body}` : line.body;
|
||||
}
|
||||
|
||||
export function ruleExportDocumentToPdfBytes(docModel: RuleExportDocument): Uint8Array {
|
||||
const doc = new jsPDF({ unit: "mm", format: "letter" });
|
||||
const maxW = PAGE_W - 2 * MARGIN_X;
|
||||
let y = MARGIN_TOP;
|
||||
|
||||
function addPage(): void {
|
||||
doc.addPage();
|
||||
y = MARGIN_TOP;
|
||||
}
|
||||
|
||||
function ensureSpace(height: number): void {
|
||||
if (y + height > CONTENT_BOTTOM) addPage();
|
||||
}
|
||||
|
||||
function writeWrapped(
|
||||
text: string,
|
||||
opts: {
|
||||
size: number;
|
||||
style: "normal" | "bold" | "italic";
|
||||
width?: number;
|
||||
x?: number;
|
||||
gap?: number;
|
||||
},
|
||||
): void {
|
||||
const width = opts.width ?? maxW;
|
||||
const x = opts.x ?? MARGIN_X;
|
||||
doc.setFont("helvetica", opts.style);
|
||||
doc.setFontSize(opts.size);
|
||||
const lines = doc.splitTextToSize(text, width) as string[];
|
||||
const dim = doc.getTextDimensions(lines.join("\n"), { maxWidth: width });
|
||||
ensureSpace(dim.h + 0.4);
|
||||
doc.text(lines, x, y);
|
||||
y += dim.h + (opts.gap ?? 1.6);
|
||||
}
|
||||
|
||||
function writeCard(card: RuleExportCard): void {
|
||||
writeWrapped(card.title, { size: 11, style: "bold", gap: 1.4 });
|
||||
for (const line of card.lines) {
|
||||
writeWrapped(lineText(line), { size: 9.5, style: "normal", gap: 2 });
|
||||
}
|
||||
y += 1;
|
||||
}
|
||||
|
||||
function termHeight(term: RuleExportTerm, width: number): number {
|
||||
doc.setFont("helvetica", "italic");
|
||||
doc.setFontSize(9);
|
||||
const termLines = doc.splitTextToSize(
|
||||
`${term.term} ${term.meaning}`,
|
||||
width,
|
||||
) as string[];
|
||||
const dim = doc.getTextDimensions(termLines.join("\n"), { maxWidth: width });
|
||||
return dim.h + 2.2;
|
||||
}
|
||||
|
||||
function writeTermAt(
|
||||
term: RuleExportTerm,
|
||||
x: number,
|
||||
top: number,
|
||||
width: number,
|
||||
): number {
|
||||
const text = `${term.term} ${term.meaning}`;
|
||||
doc.setFont("helvetica", "italic");
|
||||
doc.setFontSize(9);
|
||||
const lines = doc.splitTextToSize(text, width) as string[];
|
||||
const dim = doc.getTextDimensions(lines.join("\n"), { maxWidth: width });
|
||||
doc.text(lines, x, top);
|
||||
return dim.h + 2.2;
|
||||
}
|
||||
|
||||
function writeTerms(terms: RuleExportTerm[]): void {
|
||||
let index = 0;
|
||||
while (index < terms.length) {
|
||||
if (y > CONTENT_BOTTOM - 10) addPage();
|
||||
const colTop = y;
|
||||
const left: RuleExportTerm[] = [];
|
||||
let leftH = 0;
|
||||
while (index < terms.length) {
|
||||
const next = terms[index];
|
||||
if (!next) break;
|
||||
const height = termHeight(next, COL_W);
|
||||
if (leftH > 0 && colTop + leftH + height > CONTENT_BOTTOM) break;
|
||||
if (leftH === 0 && colTop + height > CONTENT_BOTTOM) {
|
||||
addPage();
|
||||
break;
|
||||
}
|
||||
left.push(next);
|
||||
leftH += height;
|
||||
index += 1;
|
||||
}
|
||||
if (left.length === 0) {
|
||||
const overflow = terms[index];
|
||||
if (!overflow) break;
|
||||
left.push(overflow);
|
||||
index += 1;
|
||||
}
|
||||
const right: RuleExportTerm[] = [];
|
||||
let rightH = 0;
|
||||
while (index < terms.length) {
|
||||
const next = terms[index];
|
||||
if (!next) break;
|
||||
const height = termHeight(next, COL_W);
|
||||
if (right.length > 0 && colTop + rightH + height > CONTENT_BOTTOM) {
|
||||
break;
|
||||
}
|
||||
if (right.length === 0 && colTop + height > CONTENT_BOTTOM) break;
|
||||
right.push(next);
|
||||
rightH += height;
|
||||
index += 1;
|
||||
}
|
||||
let leftY = colTop;
|
||||
for (const term of left) {
|
||||
leftY += writeTermAt(term, MARGIN_X, leftY, COL_W);
|
||||
}
|
||||
let rightY = colTop;
|
||||
for (const term of right) {
|
||||
rightY += writeTermAt(
|
||||
term,
|
||||
MARGIN_X + COL_W + COL_GAP,
|
||||
rightY,
|
||||
COL_W,
|
||||
);
|
||||
}
|
||||
y = Math.max(leftY, rightY);
|
||||
}
|
||||
}
|
||||
|
||||
doc.setTextColor(0, 0, 0);
|
||||
doc.setDrawColor(20, 20, 20);
|
||||
doc.setLineWidth(0.5);
|
||||
doc.circle(MARGIN_X + 9.5, y + 9.5, 9.5, "S");
|
||||
doc.setFont("helvetica", "bold");
|
||||
doc.setFontSize(11);
|
||||
doc.text(docModel.initials, MARGIN_X + 9.5, y + 11.2, { align: "center" });
|
||||
|
||||
const headerX = MARGIN_X + 25.4;
|
||||
const headerW = maxW - 25.4;
|
||||
const titleY = y;
|
||||
doc.setFont("helvetica", "bold");
|
||||
doc.setFontSize(16);
|
||||
const titleLines = doc.splitTextToSize(docModel.title, headerW) as string[];
|
||||
const titleDim = doc.getTextDimensions(titleLines.join("\n"), {
|
||||
maxWidth: headerW,
|
||||
});
|
||||
doc.text(titleLines, headerX, y + 6);
|
||||
let headerBottom = titleY + Math.max(19, titleDim.h + 4);
|
||||
if (docModel.summary.length > 0) {
|
||||
doc.setFont("helvetica", "normal");
|
||||
doc.setFontSize(10);
|
||||
const sumLines = doc.splitTextToSize(docModel.summary, headerW) as string[];
|
||||
const sumDim = doc.getTextDimensions(sumLines.join("\n"), {
|
||||
maxWidth: headerW,
|
||||
});
|
||||
doc.text(sumLines, headerX, titleY + titleDim.h + 8);
|
||||
headerBottom = titleY + titleDim.h + sumDim.h + 10;
|
||||
}
|
||||
y = Math.max(headerBottom, y + 22);
|
||||
|
||||
for (const section of docModel.sections) {
|
||||
doc.setDrawColor(20, 20, 20);
|
||||
doc.setLineWidth(0.25);
|
||||
ensureSpace(14);
|
||||
doc.line(MARGIN_X, y, PAGE_W - MARGIN_X, y);
|
||||
y += 5;
|
||||
writeWrapped(section.title, { size: 14, style: "bold", gap: 3 });
|
||||
for (const card of section.cards) {
|
||||
writeCard(card);
|
||||
}
|
||||
}
|
||||
|
||||
if (docModel.terms.length > 0) {
|
||||
doc.setDrawColor(20, 20, 20);
|
||||
doc.setLineWidth(0.25);
|
||||
ensureSpace(16);
|
||||
doc.line(MARGIN_X, y, PAGE_W - MARGIN_X, y);
|
||||
y += 5;
|
||||
writeWrapped(exportCopy.sharedTermsHeading, {
|
||||
size: 14,
|
||||
style: "bold",
|
||||
gap: 4,
|
||||
});
|
||||
writeTerms(docModel.terms);
|
||||
}
|
||||
|
||||
ensureSpace(8);
|
||||
y += 4;
|
||||
writeWrapped(`${docModel.takeaway} ${docModel.takeawayUrl}`, {
|
||||
size: 8,
|
||||
style: "normal",
|
||||
gap: 2,
|
||||
});
|
||||
|
||||
const pageCount = doc.getNumberOfPages();
|
||||
for (let i = 1; i <= pageCount; i += 1) {
|
||||
doc.setPage(i);
|
||||
doc.setFont("helvetica", "normal");
|
||||
doc.setFontSize(8);
|
||||
doc.setTextColor(70, 70, 70);
|
||||
doc.text(docModel.credit, MARGIN_X, FOOTER_Y);
|
||||
doc.text(String(i), PAGE_W - MARGIN_X, FOOTER_Y, { align: "right" });
|
||||
doc.setTextColor(0, 0, 0);
|
||||
}
|
||||
|
||||
const ab = doc.output("arraybuffer") as ArrayBuffer;
|
||||
return new Uint8Array(ab);
|
||||
}
|
||||
Reference in new issue
Block a user