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: support export doccx

Luiz Henriquef82251b112d0
diff --git a/src/core/export/docx-export.ts b/src/core/export/docx-export.ts
new file mode 100644
index 0000000..768ba8f
--- /dev/null
+++ b/src/core/export/docx-export.ts
@@ -0,0 +1,136 @@
+import { AlignmentType, BorderStyle, Document, ImageRun, Packer, Paragraph, TextRun } from 'docx';
+import { i18n } from '#imports';
+import { blobToArrayBuffer, extractDomain, formatDate } from '@/core/export/utils';
+import type { Guide, Screenshot, Step } from '@/core/guides/types';
+import { logger } from '@/lib/logger';
+
+type SupportedDocxImageType = 'bmp' | 'gif' | 'jpg' | 'png';
+
+const DOCX_MAX_IMAGE_WIDTH = 520;
+
+function getDocxImageType(mimeType: string): SupportedDocxImageType | null {
+ switch (mimeType) {
+ case 'image/png':
+ return 'png';
+ case 'image/jpeg':
+ case 'image/jpg':
+ return 'jpg';
+ case 'image/gif':
+ return 'gif';
+ case 'image/bmp':
+ return 'bmp';
+ default:
+ return null;
+ }
+}
+
+async function buildImageParagraph(screenshot: Screenshot, stepIndex: number): Promise<Paragraph | null> {
+ const imageType = getDocxImageType(screenshot.mimeType);
+ if (!imageType) {
+ logger.warn('DOCX: unsupported screenshot mime type', screenshot.mimeType, 'for step', stepIndex);
+ return null;
+ }
+
+ try {
+ const arrayBuffer = await blobToArrayBuffer(screenshot.blob);
+ const width = Math.min(screenshot.width, DOCX_MAX_IMAGE_WIDTH);
+ const height = Math.max(1, Math.round((screenshot.height / screenshot.width) * width));
+
+ return new Paragraph({
+ alignment: AlignmentType.CENTER,
+ spacing: { after: 280 },
+ children: [
+ new ImageRun({
+ type: imageType,
+ data: new Uint8Array(arrayBuffer),
+ transformation: { width, height },
+ }),
+ ],
+ });
+ } catch (err) {
+ logger.warn('DOCX: failed to load screenshot for step', stepIndex, err);
+ return null;
+ }
+}
+
+export async function exportGuideAsDOCX(
+ guide: Guide,
+ steps: Step[],
+ 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[] = [
+ new Paragraph({
+ alignment: AlignmentType.CENTER,
+ spacing: { after: 140 },
+ children: [
+ new TextRun({ text: i18n.t('export.stepsCount', [String(steps.length)]), bold: true, color: '4F46E5' }),
+ ],
+ }),
+ 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' })],
+ }),
+ ];
+
+ for (const step of steps) {
+ const stepNumber = String(step.index + 1).padStart(2, '0');
+
+ children.push(
+ new Paragraph({
+ border: {
+ top: {
+ color: 'C7D2FE',
+ size: 6,
+ space: 1,
+ style: BorderStyle.SINGLE,
+ },
+ },
+ spacing: { before: 240, after: 100 },
+ children: [
+ new TextRun({
+ text: i18n.t('export.stepLabel', [stepNumber]),
+ bold: true,
+ color: '818CF8',
+ size: 24,
+ }),
+ ],
+ }),
+ new Paragraph({
+ spacing: { after: 160 },
+ children: [new TextRun({ text: step.description, color: '1E1B4B', size: 24 })],
+ }),
+ );
+
+ const screenshot = screenshots.get(step.id);
+ if (screenshot) {
+ const imageParagraph = await buildImageParagraph(screenshot, step.index);
+ if (imageParagraph) {
+ children.push(imageParagraph);
+ }
+ }
+ }
+
+ const doc = new Document({
+ sections: [
+ {
+ properties: {},
+ children,
+ },
+ ],
+ });
+
+ return await Packer.toBlob(doc);
+}
diff --git a/src/core/export/utils.ts b/src/core/export/utils.ts
index 465464e..4241007 100644
--- a/src/core/export/utils.ts
+++ b/src/core/export/utils.ts
@@ -22,6 +22,10 @@ export function blobToDataUrl(blob: Blob): Promise<string> {
});
}
+export async function blobToArrayBuffer(blob: Blob): Promise<ArrayBuffer> {
+ return await blob.arrayBuffer();
+}
+
export function extractDomain(steps: Step[]): string | null {
const stepWithUrl = steps.find((s) => s.url);
if (!stepWithUrl) return null;
diff --git a/src/locales/en.yml b/src/locales/en.yml
index fc1fa67..f66dff9 100644
--- a/src/locales/en.yml
+++ b/src/locales/en.yml
@@ -162,6 +162,7 @@ blurPanel:
resetAll: Reset All
exportMenu:
+ docx: DOCX
html: HTML
markdown: Markdown
pdf: PDF
diff --git a/src/locales/es.yml b/src/locales/es.yml
index 46fa5ca..ecbb2db 100644
--- a/src/locales/es.yml
+++ b/src/locales/es.yml
@@ -162,6 +162,7 @@ blurPanel:
resetAll: Restablecer todo
exportMenu:
+ docx: DOCX
html: HTML
markdown: Markdown
pdf: PDF
diff --git a/src/locales/fr.yml b/src/locales/fr.yml
index c3cf21e..a3115c9 100644
--- a/src/locales/fr.yml
+++ b/src/locales/fr.yml
@@ -162,6 +162,7 @@ blurPanel:
resetAll: Tout réinitialiser
exportMenu:
+ docx: DOCX
html: HTML
markdown: Markdown
pdf: PDF
diff --git a/src/locales/pt-BR.yml b/src/locales/pt-BR.yml
index a3c5a8f..ff43dd6 100644
--- a/src/locales/pt-BR.yml
+++ b/src/locales/pt-BR.yml
@@ -162,6 +162,7 @@ blurPanel:
resetAll: Limpar tudo
exportMenu:
+ docx: DOCX
html: HTML
markdown: Markdown
pdf: PDF
diff --git a/src/ui/sidepanel/ExportMenu.tsx b/src/ui/sidepanel/ExportMenu.tsx
index 961e72f..e7fa8bc 100644
--- a/src/ui/sidepanel/ExportMenu.tsx
+++ b/src/ui/sidepanel/ExportMenu.tsx
@@ -16,6 +16,10 @@ interface ExportMenuProps {
function downloadFile(content: string, filename: string, mimeType: string) {
const blob = new Blob([content], { type: mimeType });
+ downloadBlob(blob, filename);
+}
+
+function downloadBlob(blob: Blob, filename: string) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
@@ -24,6 +28,8 @@ function downloadFile(content: string, filename: string, mimeType: string) {
URL.revokeObjectURL(url);
}
+type ExportType = 'docx' | 'html' | 'markdown' | 'pdf';
+
export default function ExportMenu({ guide, steps, screenshots }: ExportMenuProps) {
const [open, setOpen] = useState(false);
const [exporting, setExporting] = useState(false);
@@ -37,24 +43,23 @@ export default function ExportMenu({ guide, steps, screenshots }: ExportMenuProp
return () => document.removeEventListener('mousedown', handleClickOutside);
}, [open]);
- async function handleExport(type: 'html' | 'markdown' | 'pdf') {
+ async function handleExport(type: ExportType) {
setOpen(false);
setExporting(true);
try {
if (type === 'html') {
const html = await exportGuideAsHTML(guide, steps, screenshots);
downloadFile(html, `${guide.title}.html`, 'text/html');
+ } else if (type === 'docx') {
+ const { exportGuideAsDOCX } = await import('@/core/export/docx-export');
+ const blob = await exportGuideAsDOCX(guide, steps, screenshots);
+ downloadBlob(blob, `${guide.title}.docx`);
} else if (type === 'markdown') {
const md = await exportGuideAsMarkdown(guide, steps, screenshots);
downloadFile(md, `${guide.title}.md`, 'text/markdown');
} else {
const blob = await exportGuideAsPDF(guide, steps, screenshots);
- const url = URL.createObjectURL(blob);
- const a = document.createElement('a');
- a.href = url;
- a.download = `${guide.title}.pdf`;
- a.click();
- URL.revokeObjectURL(url);
+ downloadBlob(blob, `${guide.title}.pdf`);
}
} finally {
setExporting(false);
@@ -62,6 +67,7 @@ export default function ExportMenu({ guide, steps, screenshots }: ExportMenuProp
}
const items = [
+ { type: 'docx' as const, icon: FileText, label: i18n.t('exportMenu.docx') },
{ type: 'html' as const, icon: FileCode, label: i18n.t('exportMenu.html') },
{ type: 'markdown' as const, icon: FileText, label: i18n.t('exportMenu.markdown') },
{ type: 'pdf' as const, icon: FileDown, label: i18n.t('exportMenu.pdf') },