import { getMHTdocument } from '../utils' import { defaultDocumentOptions, DocumentOptions, mergeOptions } from '../internal' import { defaultMargins, Margins, Orient } from '../templates' import IRelationFile from './IRelationFile' import WordFile from './wordFile' /** * 节点文件信息 */ export default class SectionFileInfo implements IRelationFile { /** * 编号 */ id: string = '' /** * 目标文件路径 */ target: string = '' /** * html内容 */ htmlContent: string = '' /** * 文档章节配置 * @private */ private _sectionOption: any /** * word文件上下文对象引用 */ _wordf: WordFile get mhtName() { return this.target.split('/').pop() } /** * 生成关联文件xml内容 */ getRelationXml(): string { return `` } public constructor(id: string | number, html: string, options: Partial, wordf: WordFile) { this._wordf = wordf this.id = `htmlChunk${id}` this.target = `/word/afchunk${id}.mht` this.htmlContent = html const documentOptions = mergeOptions(defaultDocumentOptions, options) const { orientation, margins } = documentOptions const marginsOptions = mergeOptions(defaultMargins, margins) let width = 0 let height = 0 if (orientation === 'landscape') { height = 12240 width = 15840 } else { width = 12240 height = 15840 } this._sectionOption = { height, width, orientation, marginsOptions } } /** * 生成MHT内容 */ generalMhtDocument() { return getMHTdocument(this.htmlContent) } /** * 生成document.xml文件中的openxml文本内容 */ documentOPENXML(isLast: boolean = false): string { const { width, height, orientation: orient, marginsOptions: margins } = this._sectionOption const headerXml = this.getHeaderDocumentXML(isLast) const footerXml = this.getFooterDocumentXML(isLast) const openXml = ` ` const sectPr = ` ${headerXml} ${footerXml} ` if (isLast) { return `${openXml}${sectPr}` } else { return `${openXml} ${sectPr} ` } } /** * 生成Content_types.xml内容类型引用定义 */ getContentTypeXml(): string { return `` } /** * 获取页眉xml内容 * @param isLast * @private */ private getHeaderDocumentXML(isLast: boolean = false): string { if (this._wordf.headerFile.id) { const curIndex = this._wordf.rFileArr.findIndex(r => r.id === this.id) if (this._wordf.headerFile.exceptSectionIdx !== -1) { if (curIndex === this._wordf.headerFile.exceptSectionIdx) { return `` } } return this._wordf.headerFile.documentOPENXML(isLast) } return '' } /** * 获取页脚xml内容 * @param isLast * @private */ private getFooterDocumentXML(isLast: boolean = false): string { if (this._wordf.footerFile.id) { const curIndex = this._wordf.rFileArr.findIndex(r => r.id === this.id) if (this._wordf.footerFile.exceptSectionIdx !== -1) { if (curIndex === this._wordf.footerFile.exceptSectionIdx) { return `` } } return this._wordf.footerFile.documentOPENXML(isLast) } return '' } }