import React from "react"; import { renderToStaticMarkup } from ".."; import { w } from "./tags"; import styles from "./styles"; import type { DOCXOptions } from "./component"; import { Relationships, Types } from "./component"; import { makePDFOLE } from "./ole"; import type { AsyncZippable } from "../../../util/zip"; import { zip } from "../../../util/zip"; export const renderDocxAsync = async (bodyEL: React.JSX.Element, docxOptions?: DOCXOptions): Promise => { const media: {Id: string, Type: string, fileName: string, buf: ArrayBuffer}[] = []; const embeddings: {Id: string, Type: string, fileName: string, buf: ArrayBuffer}[] = []; const types = new Map(); const figDataManager = docxOptions?.figDataManager; if (figDataManager) { for (const [, figData] of figDataManager.getFigDataItems()) { if ("image" in figData) { media.push({ Id: figData.image.rId, Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", fileName: figData.image.name, buf: figData.image.blob.buf, }); types.set(figData.image.name.split(".").slice(-1)[0], figData.image.blob.type); } if ("file" in figData) { embeddings.push({ Id: figData.file.rId, Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject", fileName: `${figData.file.name}.bin`, buf: makePDFOLE(figData.file.blob.buf), }); types.set("bin", "application/vnd.openxmlformats-officedocument.oleObject"); if (!media.find(m => m.Id === figDataManager.pdfIcon.rId)) { media.push({ Id: figDataManager.pdfIcon.rId, Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", fileName: figDataManager.pdfIcon.fileName, buf: figDataManager.pdfIcon.buf, }); types.set("emf", "image/x-emf"); } } if ("pages" in figData) { for (const page of figData.pages) { media.push({ Id: page.rId, Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", fileName: page.name, buf: page.blob.buf, }); types.set(page.name.split(".").slice(-1)[0], page.blob.type); } } } } types.set("rels", "application/vnd.openxmlformats-package.relationships+xml"); types.set("xml", "application/xml"); const document = ( {bodyEL} ); const zipData: AsyncZippable = {}; zipData["[Content_Types].xml"] = /*xml*/`\ ${renderToStaticMarkup( ({ tag: "Default" as const, Extension: ext, ContentType: type, }))), ]} />)} `; zipData["_rels/.rels"] = /*xml*/`\ ${renderToStaticMarkup()} `; zipData["word/_rels/document.xml.rels"] = /*xml*/`\ ${renderToStaticMarkup( ({ Id: m.Id, Type: m.Type, Target: `media/${m.fileName}`, }))), ...(embeddings.map(m => ({ Id: m.Id, Type: m.Type, Target: `embeddings/${m.fileName}`, }))), ]} />)} `; for (const m of media) { zipData[`word/media/${m.fileName}`] = m.buf; } for (const m of embeddings) { zipData[`word/embeddings/${m.fileName}`] = m.buf; } zipData["word/document.xml"] = /*xml*/`\ ${renderToStaticMarkup(document)} `; zipData["word/styles.xml"] = /*xml*/`\ ${renderToStaticMarkup(styles)} `; zipData["word/settings.xml"] = /*xml*/`\ ${renderToStaticMarkup(( ))} `; const zipBuf = await zip(zipData, { level: 9 }); return zipBuf; };