Add lifecycles to add image
This commit is contained in:
@ -2,7 +2,8 @@ import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { HeaderReferenceType } from "./document";
|
||||
import { Header } from "./header/header";
|
||||
import { Image, IMediaData, Media } from "./media";
|
||||
import { IOnCompile } from "./life-cycles";
|
||||
import { Image, Media } from "./media";
|
||||
import { ImageParagraph, Paragraph } from "./paragraph";
|
||||
import { Relationships } from "./relationships";
|
||||
import { Table } from "./table";
|
||||
@ -12,7 +13,7 @@ export interface IDocumentHeader {
|
||||
readonly type: HeaderReferenceType;
|
||||
}
|
||||
|
||||
export class HeaderWrapper {
|
||||
export class HeaderWrapper implements IOnCompile {
|
||||
private readonly header: Header;
|
||||
private readonly relationships: Relationships;
|
||||
|
||||
@ -43,29 +44,8 @@ export class HeaderWrapper {
|
||||
this.header.addChildElement(childElement);
|
||||
}
|
||||
|
||||
public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData {
|
||||
const mediaData = this.media.addMedia(image, refId, width, height);
|
||||
this.relationships.createRelationship(
|
||||
mediaData.referenceId,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||
`media/${mediaData.fileName}`,
|
||||
);
|
||||
return mediaData;
|
||||
}
|
||||
|
||||
public addHyperlinkRelationship(target: string, refId: number, targetMode?: "External" | undefined): void {
|
||||
this.relationships.createRelationship(
|
||||
refId,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
||||
target,
|
||||
targetMode,
|
||||
);
|
||||
}
|
||||
|
||||
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);
|
||||
const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount, width, height);
|
||||
this.addImage(new Image(new ImageParagraph(mediaData)));
|
||||
}
|
||||
|
||||
@ -74,6 +54,16 @@ export class HeaderWrapper {
|
||||
return this;
|
||||
}
|
||||
|
||||
public onCompile(): void {
|
||||
this.media.Array.forEach((mediaData) => {
|
||||
this.relationships.createRelationship(
|
||||
mediaData.referenceId,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||
`media/${mediaData.fileName}`,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public get Header(): Header {
|
||||
return this.header;
|
||||
}
|
||||
|
Reference in New Issue
Block a user