Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

feat(export): render blocks as sections in PDF and DOCX

Luis Ortizcc0e03067b1b
diff --git a/src/core/export/docx-export.ts b/src/core/export/docx-export.ts
index 26be447..5a5daef 100644
--- a/src/core/export/docx-export.ts
+++ b/src/core/export/docx-export.ts
@@ -9,6 +9,7 @@ import {
Packer,
PageNumber,
Paragraph,
+ ShadingType,
Table,
TableCell,
TableLayoutType,
@@ -22,6 +23,7 @@ import { i18n } from '#imports';
import { type Branding, dataUrlToBytes, fitLogo, loadBranding } from '@/core/export/branding';
import { type ExportOptions, IMAGE_SCALE_FACTORS, loadExportOptions } from '@/core/export/options';
import { blobToArrayBuffer, extractDomain, formatDate } from '@/core/export/utils';
+import { actionSteps, calloutAccent, isBlock, stepNumbers, tint } from '@/core/guides/blocks';
import type { Guide, Screenshot, Step } from '@/core/guides/types';
import { logger } from '@/lib/logger';
@@ -44,6 +46,13 @@ const TEXT_COL_MM = CONTENT_MM - NUM_COL_MM - COL_GAP_MM;
const META_COL_MM = 43;
const LOGO_MAX_W = px(34);
const LOGO_MAX_H = px(15);
+const BLOCK_SPACING_AFTER = 200;
+const BLOCK_RULE_SIZE = 6;
+const HEADING_SIZE = 28;
+const CALLOUT_SIZE = 22;
+const CALLOUT_BAR_SIZE = 18;
+const CALLOUT_PAD_X_MM = 3;
+const CALLOUT_PAD_Y_MM = 2;
const INK = '1E1B4B';
const MUTED = '6B7280';
@@ -166,7 +175,7 @@ function buildCover(guide: Guide, steps: Step[], domain: string | null, brand: B
new Paragraph({
children: [
new TextRun({
- text: String(steps.length).padStart(2, '0'),
+ text: String(actionSteps(steps).length).padStart(2, '0'),
bold: true,
color: accent,
size: 60,
@@ -255,14 +264,62 @@ async function buildImageParagraph(
}
}
+function buildHeadingParagraph(step: Step): Paragraph {
+ return new Paragraph({
+ border: { bottom: { style: BorderStyle.SINGLE, size: BLOCK_RULE_SIZE, color: INK, space: 4 } },
+ children: [
+ new TextRun({ text: step.description, bold: true, color: INK, size: HEADING_SIZE, font: DOCX_FONT_FAMILY }),
+ ],
+ });
+}
+
+function buildCalloutTable(step: Step): Table {
+ const accent = calloutAccent(step);
+
+ return new Table({
+ width: { size: dxa(CONTENT_MM), type: WidthType.DXA },
+ layout: TableLayoutType.FIXED,
+ columnWidths: [dxa(CONTENT_MM)],
+ rows: [
+ new TableRow({
+ cantSplit: true,
+ children: [
+ new TableCell({
+ borders: {
+ ...NO_BORDERS,
+ left: { style: BorderStyle.SINGLE, size: CALLOUT_BAR_SIZE, color: bare(accent) },
+ },
+ shading: { type: ShadingType.CLEAR, color: 'auto', fill: bare(tint(accent)) },
+ width: { size: dxa(CONTENT_MM), type: WidthType.DXA },
+ margins: {
+ top: dxa(CALLOUT_PAD_Y_MM),
+ bottom: dxa(CALLOUT_PAD_Y_MM),
+ left: dxa(CALLOUT_PAD_X_MM),
+ right: dxa(CALLOUT_PAD_X_MM),
+ },
+ children: [
+ new Paragraph({
+ children: [
+ new TextRun({ text: step.description, color: INK, size: CALLOUT_SIZE, font: DOCX_FONT_FAMILY }),
+ ],
+ }),
+ ],
+ }),
+ ],
+ }),
+ ],
+ });
+}
+
async function buildStepTable(
step: Step,
+ number: number,
screenshot: Screenshot | undefined,
brand: Branding,
opts: ExportOptions,
): Promise<Table> {
const accent = bare(brand.accent);
- const stepNumber = String(step.index + 1).padStart(2, '0');
+ const stepNumber = String(number).padStart(2, '0');
const textChildren: Paragraph[] = [
new Paragraph({
@@ -424,12 +481,26 @@ export async function exportGuideAsDOCX(
const brand = await loadBranding();
const children: Array<Paragraph | Table> = opts.cover ? buildCover(guide, steps, domain, brand) : [];
+ const numbers = stepNumbers(steps);
for (const [i, step] of steps.entries()) {
if (i === 0 && opts.cover) {
children.push(new Paragraph({ pageBreakBefore: true, spacing: { after: 120 }, children: [] }));
}
- children.push(await buildStepTable(step, opts.screenshots ? screenshots.get(step.id) : undefined, brand, opts));
+ if (isBlock(step)) {
+ children.push(step.blockType === 'heading' ? buildHeadingParagraph(step) : buildCalloutTable(step));
+ children.push(new Paragraph({ spacing: { after: BLOCK_SPACING_AFTER }, children: [] }));
+ continue;
+ }
+ children.push(
+ await buildStepTable(
+ step,
+ numbers.get(step.id) ?? 0,
+ opts.screenshots ? screenshots.get(step.id) : undefined,
+ brand,
+ opts,
+ ),
+ );
children.push(new Paragraph({ spacing: { after: 300 }, children: [] }));
}
diff --git a/src/core/export/pdf-export.ts b/src/core/export/pdf-export.ts
index 0d9ca0b..2dd577c 100644
--- a/src/core/export/pdf-export.ts
+++ b/src/core/export/pdf-export.ts
@@ -3,6 +3,7 @@ import { i18n } from '#imports';
import { fitLogo, loadBranding } from '@/core/export/branding';
import { type ExportOptions, IMAGE_SCALE_FACTORS, loadExportOptions } from '@/core/export/options';
import { blobToDataUrl, extractDomain, fitImage, formatDate } from '@/core/export/utils';
+import { actionSteps, calloutAccent, isBlock, stepNumbers, tint } from '@/core/guides/blocks';
import type { Guide, Screenshot, Step } from '@/core/guides/types';
import { hexToRgb } from '@/core/screenshot/color';
import { resolveViewport } from '@/core/screenshot/geometry';
@@ -30,10 +31,23 @@ const HEAD_LOGO_W = 18;
const HEAD_LOGO_H = 7;
const META_COL_W = 43;
const META_DROP = 9;
+const BLOCK_GAP = 9;
+const HEADING_SIZE = 14;
+const HEADING_LINE_H = 6.5;
+const HEADING_BASELINE = 5;
+const HEADING_RULE_GAP = 3.2;
+const CALLOUT_SIZE = 11;
+const CALLOUT_LINE_H = 5;
+const CALLOUT_BASELINE = 4;
+const CALLOUT_PAD_X = 5;
+const CALLOUT_PAD_Y = 4;
+const CALLOUT_BAR_W = 2;
+const CALLOUT_RADIUS = 2;
const INK: [number, number, number] = [30, 27, 75];
const MUTED: [number, number, number] = [107, 114, 128];
const HAIR: [number, number, number] = [229, 231, 235];
+const PAPER: [number, number, number] = [255, 255, 255];
export async function exportGuideAsPDF(
guide: Guide,
@@ -43,6 +57,8 @@ export async function exportGuideAsPDF(
): Promise<Blob> {
const opts = options ?? (await loadExportOptions());
const doc = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' });
+ const actions = actionSteps(steps);
+ const numbers = stepNumbers(steps);
const domain = extractDomain(steps);
const brand = await loadBranding();
const accent = hexToRgb(brand.accent) ?? [79, 70, 229];
@@ -95,7 +111,7 @@ export async function exportGuideAsPDF(
doc.setFontSize(30);
doc.setFont('helvetica', 'bold');
doc.setTextColor(...accent);
- doc.text(String(steps.length).padStart(2, '0'), MARGIN, yValue);
+ doc.text(String(actions.length).padStart(2, '0'), MARGIN, yValue);
drawLabel(i18n.t('export.created').toUpperCase(), MARGIN + META_COL_W);
doc.setFontSize(11);
@@ -145,7 +161,11 @@ export async function exportGuideAsPDF(
let sy = startStepPage();
for (const step of steps) {
- const stepNum = String(step.index + 1).padStart(2, '0');
+ if (isBlock(step)) {
+ sy = drawBlock(doc, step, sy, startStepPage);
+ continue;
+ }
+ const stepNum = String(numbers.get(step.id) ?? 0).padStart(2, '0');
doc.setFontSize(11);
doc.setFont('helvetica', 'bold');
@@ -177,7 +197,7 @@ export async function exportGuideAsPDF(
if (sy + blockH > FOOTER_Y - 8 && sy > STEP_TOP) {
sy = startStepPage();
}
- pageSteps[pageSteps.length - 1].push(step.index + 1);
+ pageSteps[pageSteps.length - 1].push(numbers.get(step.id) ?? 0);
doc.setFontSize(30);
doc.setFont('helvetica', 'bold');
@@ -239,8 +259,8 @@ export async function exportGuideAsPDF(
if (onPage?.length) {
const range =
onPage.length > 1
- ? i18n.t('export.stepsRange', [String(onPage[0]), String(onPage[onPage.length - 1]), String(steps.length)])
- : i18n.t('export.stepOf', [String(onPage[0]), String(steps.length)]);
+ ? i18n.t('export.stepsRange', [String(onPage[0]), String(onPage[onPage.length - 1]), String(actions.length)])
+ : i18n.t('export.stepOf', [String(onPage[0]), String(actions.length)]);
doc.text(range, headRight, MARGIN + 3, { align: 'right' });
}
@@ -256,6 +276,42 @@ export async function exportGuideAsPDF(
return doc.output('blob');
}
+function drawBlock(doc: jsPDF, step: Step, y: number, nextPage: () => number): number {
+ if (step.blockType === 'heading') {
+ doc.setFontSize(HEADING_SIZE);
+ doc.setFont('helvetica', 'bold');
+ const lines = doc.splitTextToSize(step.description, CONTENT_W);
+ const blockH = HEADING_BASELINE + (lines.length - 1) * HEADING_LINE_H + HEADING_RULE_GAP;
+ let sy = y;
+ if (sy + blockH > FOOTER_Y - 8 && sy > STEP_TOP) sy = nextPage();
+ doc.setTextColor(...INK);
+ doc.text(lines, MARGIN, sy + HEADING_BASELINE);
+ const ruleY = sy + blockH;
+ doc.setDrawColor(...INK);
+ doc.setLineWidth(0.3);
+ doc.line(MARGIN, ruleY, RIGHT, ruleY);
+ return ruleY + BLOCK_GAP;
+ }
+
+ const accent = calloutAccent(step);
+ const bar = hexToRgb(accent) ?? INK;
+ const fill = hexToRgb(tint(accent)) ?? PAPER;
+ const textX = MARGIN + CALLOUT_BAR_W + CALLOUT_PAD_X;
+ doc.setFontSize(CALLOUT_SIZE);
+ doc.setFont('helvetica', 'normal');
+ const lines = doc.splitTextToSize(step.description, RIGHT - CALLOUT_PAD_X - textX);
+ const boxH = CALLOUT_PAD_Y * 2 + lines.length * CALLOUT_LINE_H;
+ let sy = y;
+ if (sy + boxH > FOOTER_Y - 8 && sy > STEP_TOP) sy = nextPage();
+ doc.setFillColor(...fill);
+ doc.roundedRect(MARGIN, sy, CONTENT_W, boxH, CALLOUT_RADIUS, CALLOUT_RADIUS, 'F');
+ doc.setFillColor(...bar);
+ doc.rect(MARGIN, sy + CALLOUT_RADIUS, CALLOUT_BAR_W, boxH - CALLOUT_RADIUS * 2, 'F');
+ doc.setTextColor(...INK);
+ doc.text(lines, textX, sy + CALLOUT_PAD_Y + CALLOUT_BASELINE);
+ return sy + boxH + BLOCK_GAP;
+}
+
function stepUrlLabel(url: string): string {
try {
const parsed = new URL(url);