UNPKG

prosemirror-docx-web

Version:

Export from a prosemirror document to Microsoft word forked from curvenote/prosemirror-docx

192 lines 7.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.coverColorToHex = exports.getLatexFromNode = exports.writeDocx = exports.createDocFromState = exports.createShortId = void 0; const docx_1 = require("docx"); const styles_1 = __importDefault(require("./styles")); function createShortId() { return Math.random().toString(36).substr(2, 9); } exports.createShortId = createShortId; function createDocFromState(state, footerText, footnotes = {}, pageOptions = null, getImageBuffer = async () => null, externalStyles = null) { // eslint-disable-next-line @typescript-eslint/no-use-before-define const pageMargin = getPageMargin(pageOptions?.margin || null); // 对多栏的支持 const sections = state.children.reduce((res, cur) => { if (!cur.properties?.column) { if (res[res.length - 1] && !res[res.length - 1].properties?.column) { res[res.length - 1].children.push(cur); } else { res.push({ properties: { type: docx_1.SectionType.CONTINUOUS, page: pageMargin || {}, }, children: [cur], }); } } else { res.push(cur); } return res; }, []); // eslint-disable-next-line @typescript-eslint/no-use-before-define const pageSection = getHeaderAndFooter(pageOptions, getImageBuffer); pageSection.properties = { page: pageMargin || {} }; sections.unshift(pageSection); return new docx_1.Document({ background: undefined, // eslint-disable-next-line @typescript-eslint/ban-ts-comment footnotes, comments: { children: state.comments }, styles: externalStyles || styles_1.default, numbering: { config: state.numbering, // eslint-disable-next-line @typescript-eslint/ban-ts-comment }, sections, // externalStyles: `<?xml version="1.0" encoding="UTF-8" standalone="yes"?> // <w:styles xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" // xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" // xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" // xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" // xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 w15"> // <w:style w:type="paragraph" w:customStyle="1" w:styleId="BlockCode"> // <w:name w:val="BlockCode" /> // <w:basedOn w:val="Normal" /> // <w:qFormat /> // <w:rsid w:val="00BE7EA6" /> // <w:pPr> // <w:widowControl w:val="0" /> // <w:shd w:val="clear" w:color="auto" w:fill="EDEDED" w:themeFill="accent3" // w:themeFillTint="33" /> // <w:spacing w:before="120" w:after="120" /> // <w:ind w:leftChars="240" w:rightChars="240" /> // </w:pPr> // <w:rPr> // <w:rFonts w:ascii="Menlo" w:eastAsia="Menlo" w:hAnsi="Menlo" /> // <w:color w:val="7B7B7B" w:themeColor="accent3" w:themeShade="BF" /> // <w:sz w:val="18" /> // </w:rPr> // </w:style> // </w:styles> // `, }); } exports.createDocFromState = createDocFromState; function writeDocx(doc, write) { docx_1.Packer.toBlob(doc).then(write); } exports.writeDocx = writeDocx; function getLatexFromNode(node) { let math = ''; node.forEach((child) => { if (child.isText) math += child.text; // TODO: improve this as we may have other things in the future }); return math; } exports.getLatexFromNode = getLatexFromNode; function coverColorToHex(color) { try { const el = document.createElement('div'); el.style.display = 'none'; el.style.position = 'fixed'; el.style.color = color; document.body.appendChild(el); const rgb = window.getComputedStyle(el).color.replace(/rgba?/, ''); document.body.removeChild(el); return `#${rgb .slice(1, -1) .split(',') .slice(0, 3) .map((c) => (+c).toString(16).padStart(2, '0')) .join('')}`; } catch (e) { return color; } } exports.coverColorToHex = coverColorToHex; function getPageMargin(margin) { if (margin) { return { margin: { top: (0, docx_1.convertMillimetersToTwip)(margin.top * 10), header: 50, bottom: (0, docx_1.convertMillimetersToTwip)(margin.bottom * 10), left: (0, docx_1.convertMillimetersToTwip)(margin.left * 10), right: (0, docx_1.convertMillimetersToTwip)(margin.right * 10), }, }; } return null; } function getHeaderAndFooter(pageOptions = {}, getImageBuffer) { const section = { children: [] }; if (pageOptions.header && pageOptions.header.isActive) { let image = null; if (pageOptions.header.image) { const { arrayBuffer, width: rawW, height: rawH } = getImageBuffer(pageOptions.header.image); const aspect = rawH / rawW; const height = 30; image = new docx_1.ImageRun({ data: arrayBuffer, transformation: { width: height / aspect, height, }, }); } section.headers = { default: new docx_1.Header({ children: [ new docx_1.Paragraph({ children: [ ...(image ? [image, new docx_1.TextRun(' ')] : []), new docx_1.TextRun(pageOptions.header.text), ], style: 'Header2', // eslint-disable-next-line @typescript-eslint/no-use-before-define alignment: getAlignment(pageOptions.header.position), }), ], }), }; } if (pageOptions.footer && pageOptions.footer.isActive) { section.footers = { default: new docx_1.Footer({ children: [ new docx_1.Paragraph({ children: [ new docx_1.TextRun({ children: [docx_1.PageNumber.CURRENT], }), ], style: 'Footer2', // eslint-disable-next-line @typescript-eslint/no-use-before-define alignment: getAlignment(pageOptions.footer.position), }), ], }), }; } return section; } function getAlignment(alignment = '') { switch (alignment) { case 'right': return docx_1.AlignmentType.RIGHT; case 'center': return docx_1.AlignmentType.CENTER; default: return docx_1.AlignmentType.LEFT; } } //# sourceMappingURL=utils.js.map