Add lifecycles to add image
This commit is contained in:
@ -5,11 +5,11 @@ import { FooterReferenceType } from "file/document/body/section-properties/foote
|
||||
import { HeaderReferenceType } from "file/document/body/section-properties/header-reference";
|
||||
import { FooterWrapper, IDocumentFooter } from "file/footer-wrapper";
|
||||
import { HeaderWrapper, IDocumentHeader } from "file/header-wrapper";
|
||||
import { convertToXmlComponent, ImportedXmlComponent } from "file/xml-components";
|
||||
|
||||
import { Media } from "file/media";
|
||||
import { TargetModeType } from "file/relationships/relationship/relationship";
|
||||
import { Styles } from "file/styles";
|
||||
import { ExternalStylesFactory } from "file/styles/external-styles-factory";
|
||||
import { convertToXmlComponent, ImportedXmlComponent } from "file/xml-components";
|
||||
|
||||
const schemeToType = {
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header": "header",
|
||||
@ -23,10 +23,17 @@ interface IDocumentRefs {
|
||||
readonly footers: Array<{ readonly id: number; readonly type: FooterReferenceType }>;
|
||||
}
|
||||
|
||||
enum RelationshipType {
|
||||
HEADER = "header",
|
||||
FOOTER = "footer",
|
||||
IMAGE = "image",
|
||||
HYPERLINK = "hyperlink",
|
||||
}
|
||||
|
||||
interface IRelationshipFileInfo {
|
||||
readonly id: number;
|
||||
readonly target: string;
|
||||
readonly type: "header" | "footer" | "image" | "hyperlink";
|
||||
readonly type: RelationshipType;
|
||||
}
|
||||
|
||||
// Document Template
|
||||
@ -83,7 +90,7 @@ export class ImportDotx {
|
||||
}
|
||||
const importedComp = convertToXmlComponent(headerXmlElement) as ImportedXmlComponent;
|
||||
const header = new HeaderWrapper(media, this.currentRelationshipId++, importedComp);
|
||||
await this.addRelationToWrapper(relationFileInfo, zipContent, header);
|
||||
await this.addRelationshipToWrapper(relationFileInfo, zipContent, header, media);
|
||||
headers.push({ type: headerRef.type, header });
|
||||
}
|
||||
|
||||
@ -106,7 +113,7 @@ export class ImportDotx {
|
||||
}
|
||||
const importedComp = convertToXmlComponent(footerXmlElement) as ImportedXmlComponent;
|
||||
const footer = new FooterWrapper(media, this.currentRelationshipId++, importedComp);
|
||||
await this.addRelationToWrapper(relationFileInfo, zipContent, footer);
|
||||
await this.addRelationshipToWrapper(relationFileInfo, zipContent, footer, media);
|
||||
footers.push({ type: footerRef.type, footer });
|
||||
}
|
||||
|
||||
@ -120,29 +127,43 @@ export class ImportDotx {
|
||||
return templateDocument;
|
||||
}
|
||||
|
||||
public async addRelationToWrapper(
|
||||
private async addRelationshipToWrapper(
|
||||
relationhipFile: IRelationshipFileInfo,
|
||||
zipContent: JSZip,
|
||||
wrapper: HeaderWrapper | FooterWrapper,
|
||||
media: Media,
|
||||
): Promise<void> {
|
||||
let wrapperImagesReferences: IRelationshipFileInfo[] = [];
|
||||
let hyperLinkReferences: IRelationshipFileInfo[] = [];
|
||||
const refFile = zipContent.files[`word/_rels/${relationhipFile.target}.rels`];
|
||||
if (refFile) {
|
||||
const xmlRef = await refFile.async("text");
|
||||
wrapperImagesReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === "image");
|
||||
hyperLinkReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === "hyperlink");
|
||||
|
||||
if (!refFile) {
|
||||
return;
|
||||
}
|
||||
|
||||
const xmlRef = await refFile.async("text");
|
||||
const wrapperImagesReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === RelationshipType.IMAGE);
|
||||
const hyperLinkReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === RelationshipType.HYPERLINK);
|
||||
|
||||
for (const r of wrapperImagesReferences) {
|
||||
const buffer = await zipContent.files[`word/${r.target}`].async("nodebuffer");
|
||||
wrapper.addImageRelationship(buffer, r.id);
|
||||
const mediaData = media.addMedia(buffer, r.id);
|
||||
wrapper.Relationships.createRelationship(
|
||||
mediaData.referenceId,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||
`media/${mediaData.fileName}`,
|
||||
);
|
||||
}
|
||||
|
||||
for (const r of hyperLinkReferences) {
|
||||
wrapper.addHyperlinkRelationship(r.target, r.id, "External");
|
||||
wrapper.Relationships.createRelationship(
|
||||
r.id,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
||||
r.target,
|
||||
TargetModeType.EXTERNAL,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public findReferenceFiles(xmlData: string): IRelationshipFileInfo[] {
|
||||
private findReferenceFiles(xmlData: string): IRelationshipFileInfo[] {
|
||||
const xmlObj = xml2js(xmlData, { compact: true }) as XMLElementCompact;
|
||||
const relationXmlArray = Array.isArray(xmlObj.Relationships.Relationship)
|
||||
? xmlObj.Relationships.Relationship
|
||||
@ -162,7 +183,7 @@ export class ImportDotx {
|
||||
return relationships;
|
||||
}
|
||||
|
||||
public extractDocumentRefs(xmlData: string): IDocumentRefs {
|
||||
private extractDocumentRefs(xmlData: string): IDocumentRefs {
|
||||
const xmlObj = xml2js(xmlData, { compact: true }) as XMLElementCompact;
|
||||
const sectionProp = xmlObj["w:document"]["w:body"]["w:sectPr"];
|
||||
|
||||
@ -208,13 +229,13 @@ export class ImportDotx {
|
||||
return { headers, footers };
|
||||
}
|
||||
|
||||
public titlePageIsDefined(xmlData: string): boolean {
|
||||
private titlePageIsDefined(xmlData: string): boolean {
|
||||
const xmlObj = xml2js(xmlData, { compact: true }) as XMLElementCompact;
|
||||
const sectionProp = xmlObj["w:document"]["w:body"]["w:sectPr"];
|
||||
return sectionProp["w:titlePg"] !== undefined;
|
||||
}
|
||||
|
||||
public parseRefId(str: string): number {
|
||||
private parseRefId(str: string): number {
|
||||
const match = /^rId(\d+)$/.exec(str);
|
||||
if (match === null) {
|
||||
throw new Error("Invalid ref id");
|
||||
|
Reference in New Issue
Block a user