diff --git a/src/export/packer/next-compiler.ts b/src/export/packer/next-compiler.ts index 197edf2b49..aefa7091ea 100644 --- a/src/export/packer/next-compiler.ts +++ b/src/export/packer/next-compiler.ts @@ -59,13 +59,13 @@ export class Compiler { } for (const header of file.Headers) { - for (const data of header.media.Array) { + for (const data of header.Media.Array) { zip.file(`word/media/${data.fileName}`, data.stream); } } for (const footer of file.Footers) { - for (const data of footer.media.Array) { + for (const data of footer.Media.Array) { zip.file(`word/media/${data.fileName}`, data.stream); } } diff --git a/src/file/document/body/section-properties/section-properties.spec.ts b/src/file/document/body/section-properties/section-properties.spec.ts index 8da53c8e57..12bc6ab61b 100644 --- a/src/file/document/body/section-properties/section-properties.spec.ts +++ b/src/file/document/body/section-properties/section-properties.spec.ts @@ -19,8 +19,8 @@ describe("SectionProperties", () => { gutter: 0, space: 708, linePitch: 360, - headers: [{headerId: 100, headerType: HeaderReferenceType.DEFAULT}], - footers: [{footerId: 200, footerType: FooterReferenceType.EVEN}], + headers: [{ headerId: 100, headerType: HeaderReferenceType.DEFAULT }], + footers: [{ footerId: 200, footerType: FooterReferenceType.EVEN }], pageNumberStart: 10, pageNumberFormatType: PageNumberFormat.CARDINAL_TEXT, }); diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index aa2cc9a664..9aa6b58fde 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -13,8 +13,13 @@ import { PageSize } from "./page-size/page-size"; import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes"; import { TitlePage } from "./title-page/title-page"; -type IHeadersOptions = {headers? : IHeaderOptions[]} -type IFootersOptions = {footers? : IFooterOptions[]} +interface IHeadersOptions { + headers?: IHeaderOptions[]; +} + +interface IFootersOptions { + footers?: IFooterOptions[]; +} export type SectionPropertiesOptions = IPageSizeAttributes & IPageMarginAttributes & @@ -53,7 +58,7 @@ export class SectionProperties extends XmlComponent { pageBorderRight: undefined, pageBorderBottom: undefined, pageBorderLeft: undefined, - titlePage : true + titlePage: true, }; const mergedOptions = { @@ -61,7 +66,6 @@ export class SectionProperties extends XmlComponent { ...options, }; - this.root.push(new PageSize(mergedOptions.width, mergedOptions.height, mergedOptions.orientation)); this.root.push( new PageMargin( @@ -77,7 +81,7 @@ export class SectionProperties extends XmlComponent { this.root.push(new Columns(mergedOptions.space)); this.root.push(new DocumentGrid(mergedOptions.linePitch)); - for (let header of mergedOptions.headers) { + for (const header of mergedOptions.headers) { this.root.push( new HeaderReference({ headerType: header.headerType, @@ -86,7 +90,7 @@ export class SectionProperties extends XmlComponent { ); } - for (let footer of mergedOptions.footers) { + for (const footer of mergedOptions.footers) { this.root.push( new FooterReference({ footerType: footer.footerType, @@ -94,7 +98,7 @@ export class SectionProperties extends XmlComponent { }), ); } - + this.root.push(new PageNumberType(mergedOptions.pageNumberStart, mergedOptions.pageNumberFormatType)); if ( diff --git a/src/file/file.ts b/src/file/file.ts index 71848926e7..ead83c4554 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -224,32 +224,12 @@ export class File { return header; } - private addHeaderToDocument(header: HeaderWrapper, type: HeaderReferenceType = HeaderReferenceType.DEFAULT): void { - this.documentHeaders.push({ header, type }); - this.docRelationships.createRelationship( - header.Header.ReferenceId, - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header", - `header${this.documentHeaders.length}.xml`, - ); - this.contentTypes.addHeader(this.documentHeaders.length); - } - public createFooter(): FooterWrapper { const footer = new FooterWrapper(this.currentRelationshipId++); this.addFooterToDocument(footer); return footer; } - private addFooterToDocument(footer: FooterWrapper, type: FooterReferenceType = FooterReferenceType.DEFAULT): void { - this.documentFooters.push({ footer, type }); - this.docRelationships.createRelationship( - footer.Footer.ReferenceId, - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer", - `footer${this.documentFooters.length}.xml`, - ); - this.contentTypes.addFooter(this.documentFooters.length); - } - public createFirstPageHeader(): HeaderWrapper { const headerWrapper = this.createHeader(); @@ -263,6 +243,42 @@ export class File { return headerWrapper; } + public getFooterByRefNumber(refId: number): FooterWrapper { + const entry = this.documentFooters.map((item) => item.footer).find((h) => h.Footer.ReferenceId === refId); + if (entry) { + return entry; + } + throw new Error(`There is no footer with given reference id ${refId}`); + } + + public getHeaderByRefNumber(refId: number): HeaderWrapper { + const entry = this.documentHeaders.map((item) => item.header).find((h) => h.Header.ReferenceId === refId); + if (entry) { + return entry; + } + throw new Error(`There is no header with given reference id ${refId}`); + } + + private addHeaderToDocument(header: HeaderWrapper, type: HeaderReferenceType = HeaderReferenceType.DEFAULT): void { + this.documentHeaders.push({ header, type }); + this.docRelationships.createRelationship( + header.Header.ReferenceId, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header", + `header${this.documentHeaders.length}.xml`, + ); + this.contentTypes.addHeader(this.documentHeaders.length); + } + + private addFooterToDocument(footer: FooterWrapper, type: FooterReferenceType = FooterReferenceType.DEFAULT): void { + this.documentFooters.push({ footer, type }); + this.docRelationships.createRelationship( + footer.Footer.ReferenceId, + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer", + `footer${this.documentFooters.length}.xml`, + ); + this.contentTypes.addFooter(this.documentFooters.length); + } + public get Document(): Document { return this.document; } @@ -299,14 +315,6 @@ export class File { return this.documentHeaders.map((item) => item.header); } - public HeaderByRefNumber(refId: number): HeaderWrapper { - const entry = this.documentHeaders.map((item) => item.header).find((h) => h.Header.ReferenceId === refId); - if (entry) { - return entry; - } - throw new Error(`There is no header with given reference id ${refId}`); - } - public get Footer(): FooterWrapper { return this.documentFooters[0].footer; } @@ -315,14 +323,6 @@ export class File { return this.documentFooters.map((item) => item.footer); } - public FooterByRefNumber(refId: number): FooterWrapper { - const entry = this.documentFooters.map((item) => item.footer).find((h) => h.Footer.ReferenceId === refId); - if (entry) { - return entry; - } - throw new Error(`There is no footer with given reference id ${refId}`); - } - public get ContentTypes(): ContentTypes { return this.contentTypes; } diff --git a/src/file/footer-wrapper.ts b/src/file/footer-wrapper.ts index f738d92a27..87adb97c47 100644 --- a/src/file/footer-wrapper.ts +++ b/src/file/footer-wrapper.ts @@ -1,20 +1,19 @@ +import { IMediaData } from "file/media"; import { XmlComponent } from "file/xml-components"; import { Footer } from "./footer/footer"; import { Image, Media } from "./media"; import { ImageParagraph, Paragraph } from "./paragraph"; import { Relationships } from "./relationships"; import { Table } from "./table"; -import { IMediaData } from 'file/media'; export class FooterWrapper { private readonly footer: Footer; private readonly relationships: Relationships; - public readonly media = new Media(); + private readonly media = new Media(); - constructor(referenceId: number, initContent? : XmlComponent) { + constructor(referenceId: number, initContent?: XmlComponent) { this.footer = new Footer(referenceId, initContent); this.relationships = new Relationships(); - } public addParagraph(paragraph: Paragraph): void { @@ -39,7 +38,7 @@ export class FooterWrapper { this.footer.addChildElement(childElement); } - public addImageRelation(image: Buffer, refId : number, width?: number, height?: number) : IMediaData { + public addImageRelation(image: Buffer, refId: number, width?: number, height?: number): IMediaData { const mediaData = this.media.addMedia(image, refId, width, height); this.relationships.createRelationship( refId, @@ -48,13 +47,12 @@ export class FooterWrapper { ); return mediaData; } - + public createImage(image: Buffer, width?: number, height?: number): void { - let mediaData = this.addImageRelation(image, this.relationships.RelationshipCount, width, height); + const mediaData = this.addImageRelation(image, this.relationships.RelationshipCount, width, height); this.addImage(new Image(new ImageParagraph(mediaData))); } - public addImage(image: Image): FooterWrapper { this.footer.addParagraph(image.Paragraph); return this; @@ -67,4 +65,8 @@ export class FooterWrapper { public get Relationships(): Relationships { return this.relationships; } + + public get Media(): Media { + return this.media; + } } diff --git a/src/file/footer/footer.ts b/src/file/footer/footer.ts index 959540d083..448df0e0dc 100644 --- a/src/file/footer/footer.ts +++ b/src/file/footer/footer.ts @@ -7,7 +7,7 @@ import { FooterAttributes } from "./footer-attributes"; export class Footer extends XmlComponent { private readonly refId: number; - constructor(referenceNumber: number, initContent? : XmlComponent) { + constructor(referenceNumber: number, initContent?: XmlComponent) { super("w:ftr", initContent); this.refId = referenceNumber; this.root.push( diff --git a/src/file/header-wrapper.ts b/src/file/header-wrapper.ts index 11e7587edb..7d42643158 100644 --- a/src/file/header-wrapper.ts +++ b/src/file/header-wrapper.ts @@ -9,7 +9,7 @@ import { Table } from "./table"; export class HeaderWrapper { private readonly header: Header; private readonly relationships: Relationships; - public readonly media = new Media(); + private readonly media = new Media(); // constructor(private readonly media: Media, referenceId: number, initContent? : XmlComponent) { constructor(referenceId: number, initContent?: XmlComponent) { @@ -66,4 +66,8 @@ export class HeaderWrapper { public get Relationships(): Relationships { return this.relationships; } + + public get Media(): Media { + return this.media; + } } diff --git a/src/file/header/header-attributes.ts b/src/file/header/header-attributes.ts index 9d7f8a7801..3e46b4ca94 100644 --- a/src/file/header/header-attributes.ts +++ b/src/file/header/header-attributes.ts @@ -23,17 +23,17 @@ export interface IHeaderAttributesProperties { dcmitype?: string; xsi?: string; type?: string; - cx? : string, - cx1? : string, - cx2? : string, - cx3? : string, - cx4? : string, - cx5? : string, - cx6? : string, - cx7? : string, - cx8? : string, - w16cid: string, - w16se: string + cx?: string; + cx1?: string; + cx2?: string; + cx3?: string; + cx4?: string; + cx5?: string; + cx6?: string; + cx7?: string; + cx8?: string; + w16cid: string; + w16se: string; } export class HeaderAttributes extends XmlAttributeComponent { @@ -60,7 +60,7 @@ export class HeaderAttributes extends XmlAttributeComponent { + public async addImagesToWrapper( + relationFile: IRelationFileInfo, + zipContent: JSZip, + wrapper: HeaderWrapper | FooterWrapper, + ): Promise { let wrapperImagesReferences: IRelationFileInfo[] = []; const refFile = zipContent.files[`word/_rels/${relationFile.targetFile}.rels`]; if (refFile) {