Use single media instead of multiple

This commit is contained in:
Dolan
2018-10-23 00:31:51 +01:00
parent a5bedf9a5b
commit 9c66db97ff
8 changed files with 49 additions and 44 deletions

View File

@ -3,12 +3,15 @@ import { expect } from "chai";
import { Formatter } from "../../../../export/formatter";
import { FooterWrapper } from "../../../footer-wrapper";
import { HeaderWrapper } from "../../../header-wrapper";
import { Media } from "../../../media";
import { PageBorderOffsetFrom, PageNumberFormat } from "./";
import { SectionProperties } from "./section-properties";
describe("SectionProperties", () => {
describe("#constructor()", () => {
it("should create section properties with options", () => {
const media = new Media();
const properties = new SectionProperties({
width: 11906,
height: 16838,
@ -23,10 +26,10 @@ describe("SectionProperties", () => {
space: 708,
linePitch: 360,
headers: {
default: new HeaderWrapper(100),
default: new HeaderWrapper(media, 100),
},
footers: {
even: new FooterWrapper(200),
even: new FooterWrapper(media, 200),
},
pageNumberStart: 10,
pageNumberFormatType: PageNumberFormat.CARDINAL_TEXT,

View File

@ -173,13 +173,13 @@ export class File {
}
public createHeader(): HeaderWrapper {
const header = new HeaderWrapper(this.currentRelationshipId++);
const header = new HeaderWrapper(this.media, this.currentRelationshipId++);
this.addHeaderToDocument(header);
return header;
}
public createFooter(): FooterWrapper {
const footer = new FooterWrapper(this.currentRelationshipId++);
const footer = new FooterWrapper(this.media, this.currentRelationshipId++);
this.addFooterToDocument(footer);
return footer;
}

View File

@ -1,8 +1,8 @@
import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components";
import { FooterReferenceType } from "./document";
import { Footer } from "./footer/footer";
import { Image, Media } from "./media";
import { Image, IMediaData, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { Table } from "./table";
@ -15,10 +15,8 @@ export interface IDocumentFooter {
export class FooterWrapper {
private readonly footer: Footer;
private readonly relationships: Relationships;
private readonly media: Media;
constructor(referenceId: number, initContent?: XmlComponent) {
this.media = new Media();
constructor(private readonly media: Media, referenceId: number, initContent?: XmlComponent) {
this.footer = new Footer(referenceId, initContent);
this.relationships = new Relationships();
}
@ -48,7 +46,7 @@ export class FooterWrapper {
public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData {
const mediaData = this.media.addMedia(image, refId, width, height);
this.relationships.createRelationship(
refId,
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
@ -64,8 +62,10 @@ export class FooterWrapper {
);
}
public createImage(image: Buffer, width?: number, height?: number): void {
const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height);
public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void {
// TODO
// tslint:disable-next-line:no-any
const mediaData = this.addImageRelationship(image as any, this.relationships.RelationshipCount, width, height);
this.addImage(new Image(new ImageParagraph(mediaData)));
}

View File

@ -1,8 +1,8 @@
import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components";
import { HeaderReferenceType } from "./document";
import { Header } from "./header/header";
import { Image, Media } from "./media";
import { Image, IMediaData, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { Table } from "./table";
@ -15,10 +15,8 @@ export interface IDocumentHeader {
export class HeaderWrapper {
private readonly header: Header;
private readonly relationships: Relationships;
private readonly media: Media;
constructor(referenceId: number, initContent?: XmlComponent) {
this.media = new Media();
constructor(private readonly media: Media, referenceId: number, initContent?: XmlComponent) {
this.header = new Header(referenceId, initContent);
this.relationships = new Relationships();
}
@ -48,7 +46,7 @@ export class HeaderWrapper {
public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData {
const mediaData = this.media.addMedia(image, refId, width, height);
this.relationships.createRelationship(
refId,
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
@ -64,8 +62,10 @@ export class HeaderWrapper {
);
}
public createImage(image: Buffer, width?: number, height?: number): void {
const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height);
public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void {
// TODO
// tslint:disable-next-line:no-any
const mediaData = this.addImageRelationship(image as any, this.relationships.RelationshipCount, width, height);
this.addImage(new Image(new ImageParagraph(mediaData)));
}

View File

@ -87,7 +87,7 @@ export class Media {
}
const imageData = {
referenceId: this.map.size + relationshipsCount + 1,
referenceId: relationshipsCount,
stream: data,
path: filePath,
fileName: key,