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 πŸ“–

qfe: Enhance UI for DOCX

Luiz Henrique3d0b6bdaa66d
diff --git a/src/core/export/__tests__/docx-export.test.ts b/src/core/export/__tests__/docx-export.test.ts
index 36c4c0e..1c94cf7 100644
--- a/src/core/export/__tests__/docx-export.test.ts
+++ b/src/core/export/__tests__/docx-export.test.ts
@@ -63,9 +63,10 @@ describe('exportGuideAsDOCX', () => {
const xml = await readDocumentXml(blob);
expect(xml).toContain('My Guide');
expect(xml).toContain('export.stepsCount[1]');
- expect(xml).toContain('export.createdLabel[');
- expect(xml).toContain('export.sourceLabel[example.com]');
- expect(xml).toContain('export.stepLabel[01]');
+ expect(xml).toContain('EXPORT.CREATED');
+ expect(xml).toContain('EXPORT.SOURCE');
+ expect(xml).toContain('example.com');
+ expect(xml).toContain('<w:pageBreakBefore/>');
expect(xml).toContain('Click the button');
});
@@ -75,7 +76,7 @@ describe('exportGuideAsDOCX', () => {
expect(xml).toContain('Test Guide');
expect(xml).toContain('export.stepsCount[0]');
- expect(xml).not.toContain('export.stepLabel[');
+ expect(xml).not.toContain('Click the button');
});
it('embeds screenshot media when steps have screenshots', async () => {
diff --git a/src/core/export/docx-export.ts b/src/core/export/docx-export.ts
index 768ba8f..cb94be5 100644
--- a/src/core/export/docx-export.ts
+++ b/src/core/export/docx-export.ts
@@ -1,4 +1,16 @@
-import { AlignmentType, BorderStyle, Document, ImageRun, Packer, Paragraph, TextRun } from 'docx';
+import {
+ AlignmentType,
+ BorderStyle,
+ Document,
+ ImageRun,
+ Packer,
+ Paragraph,
+ Table,
+ TableCell,
+ TableRow,
+ TextRun,
+ WidthType,
+} from 'docx';
import { i18n } from '#imports';
import { blobToArrayBuffer, extractDomain, formatDate } from '@/core/export/utils';
import type { Guide, Screenshot, Step } from '@/core/guides/types';
@@ -7,6 +19,102 @@ import { logger } from '@/lib/logger';
type SupportedDocxImageType = 'bmp' | 'gif' | 'jpg' | 'png';
const DOCX_MAX_IMAGE_WIDTH = 520;
+const DOCX_STEP_INDENT = 900;
+const DOCX_FONT_FAMILY = 'Helvetica';
+
+function buildGradientDividerParagraph(): Paragraph {
+ const gradientSegments = ['4F46E5', '635BED', '8178F4', 'A4A1F9', 'C7D2FE', '9BD2FE', '60C8FB', '38BDF8'];
+
+ return new Paragraph({
+ alignment: AlignmentType.CENTER,
+ spacing: { after: 260 },
+ children: gradientSegments.map(
+ (color) =>
+ new TextRun({
+ text: '--------',
+ font: DOCX_FONT_FAMILY,
+ color,
+ bold: true,
+ size: 16,
+ }),
+ ),
+ });
+}
+
+function buildMetaTable(guide: Guide, domain: string | null): Table {
+ const baseCell = {
+ borders: {
+ top: { style: BorderStyle.NONE, size: 0, color: 'FFFFFF' },
+ right: { style: BorderStyle.NONE, size: 0, color: 'FFFFFF' },
+ bottom: { style: BorderStyle.NONE, size: 0, color: 'FFFFFF' },
+ left: { style: BorderStyle.NONE, size: 0, color: 'FFFFFF' },
+ },
+ margins: { top: 0, bottom: 0, left: 80, right: 80 },
+ };
+
+ return new Table({
+ width: { size: 70, type: WidthType.PERCENTAGE },
+ alignment: AlignmentType.CENTER,
+ rows: [
+ new TableRow({
+ children: [
+ new TableCell({
+ ...baseCell,
+ width: { size: domain ? 50 : 100, type: WidthType.PERCENTAGE },
+ children: [
+ new Paragraph({
+ alignment: AlignmentType.LEFT,
+ spacing: { after: 40 },
+ children: [
+ new TextRun({
+ text: i18n.t('export.created').toUpperCase(),
+ font: DOCX_FONT_FAMILY,
+ bold: true,
+ color: '6B7280',
+ size: 14,
+ }),
+ ],
+ }),
+ new Paragraph({
+ alignment: AlignmentType.LEFT,
+ children: [
+ new TextRun({ text: formatDate(guide.createdAt), font: DOCX_FONT_FAMILY, color: '1E1B4B', size: 24 }),
+ ],
+ }),
+ ],
+ }),
+ ...(domain
+ ? [
+ new TableCell({
+ ...baseCell,
+ width: { size: 50, type: WidthType.PERCENTAGE },
+ children: [
+ new Paragraph({
+ alignment: AlignmentType.LEFT,
+ spacing: { after: 40 },
+ children: [
+ new TextRun({
+ text: i18n.t('export.source').toUpperCase(),
+ font: DOCX_FONT_FAMILY,
+ bold: true,
+ color: '6B7280',
+ size: 14,
+ }),
+ ],
+ }),
+ new Paragraph({
+ alignment: AlignmentType.LEFT,
+ children: [new TextRun({ text: domain, font: DOCX_FONT_FAMILY, color: '4F46E5', size: 24 })],
+ }),
+ ],
+ }),
+ ]
+ : []),
+ ],
+ }),
+ ],
+ });
+}
function getDocxImageType(mimeType: string): SupportedDocxImageType | null {
switch (mimeType) {
@@ -24,7 +132,11 @@ function getDocxImageType(mimeType: string): SupportedDocxImageType | null {
}
}
-async function buildImageParagraph(screenshot: Screenshot, stepIndex: number): Promise<Paragraph | null> {
+async function buildImageParagraph(
+ screenshot: Screenshot,
+ stepIndex: number,
+ leftIndent = 0,
+): Promise<Paragraph | null> {
const imageType = getDocxImageType(screenshot.mimeType);
if (!imageType) {
logger.warn('DOCX: unsupported screenshot mime type', screenshot.mimeType, 'for step', stepIndex);
@@ -33,11 +145,12 @@ async function buildImageParagraph(screenshot: Screenshot, stepIndex: number): P
try {
const arrayBuffer = await blobToArrayBuffer(screenshot.blob);
- const width = Math.min(screenshot.width, DOCX_MAX_IMAGE_WIDTH);
+ const width = Math.min(screenshot.width, DOCX_MAX_IMAGE_WIDTH - Math.round(leftIndent / 20));
const height = Math.max(1, Math.round((screenshot.height / screenshot.width) * width));
return new Paragraph({
- alignment: AlignmentType.CENTER,
+ alignment: AlignmentType.LEFT,
+ indent: { left: leftIndent },
spacing: { after: 280 },
children: [
new ImageRun({
@@ -59,32 +172,40 @@ export async function exportGuideAsDOCX(
screenshots: Map<string, Screenshot>,
): Promise<Blob> {
const domain = extractDomain(steps);
- const meta = [
- i18n.t('export.stepsCount', [String(steps.length)]),
- i18n.t('export.createdLabel', [formatDate(guide.createdAt)]),
- ...(domain ? [i18n.t('export.sourceLabel', [domain])] : []),
- ].join(' Β· ');
-
- const children: Paragraph[] = [
+ const coverChildren: Array<Paragraph | Table> = [
new Paragraph({
alignment: AlignmentType.CENTER,
- spacing: { after: 140 },
+ spacing: { before: 3300, after: 200 },
children: [
- new TextRun({ text: i18n.t('export.stepsCount', [String(steps.length)]), bold: true, color: '4F46E5' }),
+ new TextRun({
+ text: i18n.t('export.stepsCount', [String(steps.length)]),
+ font: DOCX_FONT_FAMILY,
+ bold: true,
+ color: '4F46E5',
+ size: 20,
+ }),
],
}),
+ buildGradientDividerParagraph(),
new Paragraph({
alignment: AlignmentType.CENTER,
- spacing: { after: 220 },
- children: [new TextRun({ text: guide.title, bold: true, size: 36, color: '1E1B4B' })],
- }),
- new Paragraph({
- alignment: AlignmentType.CENTER,
- spacing: { after: steps.length > 0 ? 360 : 0 },
- children: [new TextRun({ text: meta, italics: true, color: '6B7280' })],
+ spacing: { after: 280 },
+ children: [new TextRun({ text: guide.title, font: DOCX_FONT_FAMILY, bold: true, size: 44, color: '1E1B4B' })],
}),
+ buildMetaTable(guide, domain),
+ new Paragraph({ spacing: { after: 420 } }),
];
+ const children: Array<Paragraph | Table> = [...coverChildren];
+
+ if (steps.length > 0) {
+ children.push(
+ new Paragraph({
+ pageBreakBefore: true,
+ }),
+ );
+ }
+
for (const step of steps) {
const stepNumber = String(step.index + 1).padStart(2, '0');
@@ -98,25 +219,23 @@ export async function exportGuideAsDOCX(
style: BorderStyle.SINGLE,
},
},
- spacing: { before: 240, after: 100 },
- children: [
- new TextRun({
- text: i18n.t('export.stepLabel', [stepNumber]),
- bold: true,
- color: '818CF8',
- size: 24,
- }),
- ],
+ spacing: { before: 240, after: 120 },
+ children: [new TextRun({ text: '', font: DOCX_FONT_FAMILY })],
}),
new Paragraph({
- spacing: { after: 160 },
- children: [new TextRun({ text: step.description, color: '1E1B4B', size: 24 })],
+ indent: { left: DOCX_STEP_INDENT / 2 },
+ spacing: { after: 180 },
+ children: [
+ new TextRun({ text: stepNumber, font: DOCX_FONT_FAMILY, bold: true, color: '818CF8', size: 32 }),
+ new TextRun({ text: ' ', font: DOCX_FONT_FAMILY }),
+ new TextRun({ text: step.description, font: DOCX_FONT_FAMILY, color: '1E1B4B', size: 24 }),
+ ],
}),
);
const screenshot = screenshots.get(step.id);
if (screenshot) {
- const imageParagraph = await buildImageParagraph(screenshot, step.index);
+ const imageParagraph = await buildImageParagraph(screenshot, step.index, DOCX_STEP_INDENT / 2);
if (imageParagraph) {
children.push(imageParagraph);
}
@@ -124,6 +243,15 @@ export async function exportGuideAsDOCX(
}
const doc = new Document({
+ styles: {
+ default: {
+ document: {
+ run: {
+ font: DOCX_FONT_FAMILY,
+ },
+ },
+ },
+ },
sections: [
{
properties: {},