Add lifecycles to add image
This commit is contained in:
@ -13,10 +13,12 @@ import { IFileProperties } from "./file-properties";
|
||||
import { FooterWrapper, IDocumentFooter } from "./footer-wrapper";
|
||||
import { FootNotes } from "./footnotes";
|
||||
import { HeaderWrapper, IDocumentHeader } from "./header-wrapper";
|
||||
import { IOnCompile } from "./life-cycles";
|
||||
import { Image, Media } from "./media";
|
||||
import { Numbering } from "./numbering";
|
||||
import { Bookmark, Hyperlink, Paragraph } from "./paragraph";
|
||||
import { Relationships } from "./relationships";
|
||||
import { TargetModeType } from "./relationships/relationship/relationship";
|
||||
import { Settings } from "./settings";
|
||||
import { Styles } from "./styles";
|
||||
import { ExternalStylesFactory } from "./styles/external-styles-factory";
|
||||
@ -24,7 +26,7 @@ import { DefaultStylesFactory } from "./styles/factory";
|
||||
import { Table } from "./table";
|
||||
import { TableOfContents } from "./table-of-contents";
|
||||
|
||||
export class File {
|
||||
export class File implements IOnCompile {
|
||||
// tslint:disable-next-line:readonly-keyword
|
||||
private currentRelationshipId: number = 1;
|
||||
|
||||
@ -147,7 +149,7 @@ export class File {
|
||||
hyperlink.linkId,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
||||
link,
|
||||
"External",
|
||||
TargetModeType.EXTERNAL,
|
||||
);
|
||||
return hyperlink;
|
||||
}
|
||||
@ -221,6 +223,11 @@ export class File {
|
||||
}
|
||||
}
|
||||
|
||||
public onCompile(): void {
|
||||
// this.media.Array.forEach((media) => {
|
||||
// });
|
||||
}
|
||||
|
||||
private addHeaderToDocument(header: HeaderWrapper, type: HeaderReferenceType = HeaderReferenceType.DEFAULT): void {
|
||||
this.headers.push({ header, type });
|
||||
this.docRelationships.createRelationship(
|
||||
|
@ -2,7 +2,8 @@ import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { FooterReferenceType } from "./document";
|
||||
import { Footer } from "./footer/footer";
|
||||
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 IDocumentFooter {
|
||||
readonly type: FooterReferenceType;
|
||||
}
|
||||
|
||||
export class FooterWrapper {
|
||||
export class FooterWrapper implements IOnCompile {
|
||||
private readonly footer: Footer;
|
||||
private readonly relationships: Relationships;
|
||||
|
||||
@ -43,29 +44,8 @@ export class FooterWrapper {
|
||||
this.footer.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 FooterWrapper {
|
||||
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 Footer(): Footer {
|
||||
return this.footer;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
9
src/file/life-cycles.ts
Normal file
9
src/file/life-cycles.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export interface IOnCompile {
|
||||
readonly onCompile: () => void;
|
||||
}
|
||||
|
||||
// Needed because of: https://github.com/s-panferov/awesome-typescript-loader/issues/432
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
export const WORKAROUND4 = "";
|
@ -17,7 +17,9 @@ export type RelationshipType =
|
||||
| "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
|
||||
| "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes";
|
||||
|
||||
export type TargetModeType = "External";
|
||||
export enum TargetModeType {
|
||||
EXTERNAL = "External",
|
||||
}
|
||||
|
||||
export class Relationship extends XmlComponent {
|
||||
constructor(id: string, type: RelationshipType, target: string, targetMode?: TargetModeType) {
|
||||
|
Reference in New Issue
Block a user