Use single media instead of multiple
This commit is contained in:
@ -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,
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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)));
|
||||
}
|
||||
|
||||
|
@ -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)));
|
||||
}
|
||||
|
||||
|
@ -87,7 +87,7 @@ export class Media {
|
||||
}
|
||||
|
||||
const imageData = {
|
||||
referenceId: this.map.size + relationshipsCount + 1,
|
||||
referenceId: relationshipsCount,
|
||||
stream: data,
|
||||
path: filePath,
|
||||
fileName: key,
|
||||
|
@ -5,10 +5,10 @@ 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, parseOptions } from "file/xml-components";
|
||||
|
||||
import { Media } from "file/Media";
|
||||
import { Styles } from "file/styles";
|
||||
import { ExternalStylesFactory } from "file/styles/external-styles-factory";
|
||||
import { convertToXmlComponent, ImportedXmlComponent, parseOptions } from "file/xml-components";
|
||||
|
||||
const importParseOptions = {
|
||||
...parseOptions,
|
||||
@ -65,37 +65,39 @@ export class ImportDotx {
|
||||
const relationshipContent = zipContent.files["word/_rels/document.xml.rels"];
|
||||
const documentRelationships: IRelationshipFileInfo[] = this.findReferenceFiles(await relationshipContent.async("text"));
|
||||
|
||||
const media = new Media();
|
||||
|
||||
const headers: IDocumentHeader[] = [];
|
||||
for (const headerRef of documentRefs.headers) {
|
||||
const headerKey = "w:hdr";
|
||||
const relationFileInfo = documentRelationships.find((rel) => rel.id === headerRef.id);
|
||||
if (relationFileInfo === null || !relationFileInfo) {
|
||||
const relationshipFileInfo = documentRelationships.find((rel) => rel.id === headerRef.id);
|
||||
if (!relationshipFileInfo) {
|
||||
throw new Error(`Can not find target file for id ${headerRef.id}`);
|
||||
}
|
||||
|
||||
const xmlData = await zipContent.files[`word/${relationFileInfo.target}`].async("text");
|
||||
const xmlData = await zipContent.files[`word/${relationshipFileInfo.target}`].async("text");
|
||||
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
|
||||
|
||||
const importedComp = convertToXmlComponent(headerKey, xmlObj[headerKey]) as ImportedXmlComponent;
|
||||
|
||||
const header = new HeaderWrapper(this.currentRelationshipId++, importedComp);
|
||||
await this.addRelationToWrapper(relationFileInfo, zipContent, header);
|
||||
const header = new HeaderWrapper(media, this.currentRelationshipId++, importedComp);
|
||||
await this.addRelationToWrapper(relationshipFileInfo, zipContent, header);
|
||||
headers.push({ type: headerRef.type, header });
|
||||
}
|
||||
|
||||
const footers: IDocumentFooter[] = [];
|
||||
for (const footerRef of documentRefs.footers) {
|
||||
const footerKey = "w:ftr";
|
||||
const relationFileInfo = documentRelationships.find((rel) => rel.id === footerRef.id);
|
||||
if (relationFileInfo === null || !relationFileInfo) {
|
||||
const relationshipFileInfo = documentRelationships.find((rel) => rel.id === footerRef.id);
|
||||
if (!relationshipFileInfo) {
|
||||
throw new Error(`Can not find target file for id ${footerRef.id}`);
|
||||
}
|
||||
const xmlData = await zipContent.files[`word/${relationFileInfo.target}`].async("text");
|
||||
const xmlData = await zipContent.files[`word/${relationshipFileInfo.target}`].async("text");
|
||||
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
|
||||
const importedComp = convertToXmlComponent(footerKey, xmlObj[footerKey]) as ImportedXmlComponent;
|
||||
|
||||
const footer = new FooterWrapper(this.currentRelationshipId++, importedComp);
|
||||
await this.addRelationToWrapper(relationFileInfo, zipContent, footer);
|
||||
const footer = new FooterWrapper(media, this.currentRelationshipId++, importedComp);
|
||||
await this.addRelationToWrapper(relationshipFileInfo, zipContent, footer);
|
||||
footers.push({ type: footerRef.type, footer });
|
||||
}
|
||||
|
||||
@ -110,13 +112,13 @@ export class ImportDotx {
|
||||
}
|
||||
|
||||
public async addRelationToWrapper(
|
||||
relationFile: IRelationshipFileInfo,
|
||||
relationhipFile: IRelationshipFileInfo,
|
||||
zipContent: JSZip,
|
||||
wrapper: HeaderWrapper | FooterWrapper,
|
||||
): Promise<void> {
|
||||
let wrapperImagesReferences: IRelationshipFileInfo[] = [];
|
||||
let hyperLinkReferences: IRelationshipFileInfo[] = [];
|
||||
const refFile = zipContent.files[`word/_rels/${relationFile.target}.rels`];
|
||||
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");
|
||||
@ -133,10 +135,10 @@ export class ImportDotx {
|
||||
|
||||
public findReferenceFiles(xmlData: string): IRelationshipFileInfo[] {
|
||||
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
|
||||
const relationXmlArray = Array.isArray(xmlObj.Relationships.Relationship)
|
||||
const relationshipXmlArray = Array.isArray(xmlObj.Relationships.Relationship)
|
||||
? xmlObj.Relationships.Relationship
|
||||
: [xmlObj.Relationships.Relationship];
|
||||
const relations: IRelationshipFileInfo[] = relationXmlArray
|
||||
const relationships: IRelationshipFileInfo[] = relationshipXmlArray
|
||||
.map((item) => {
|
||||
return {
|
||||
id: this.parseRefId(item._attr.Id),
|
||||
@ -145,7 +147,7 @@ export class ImportDotx {
|
||||
};
|
||||
})
|
||||
.filter((item) => item.type !== null);
|
||||
return relations;
|
||||
return relationships;
|
||||
}
|
||||
|
||||
public extractDocumentRefs(xmlData: string): IDocumentRefs {
|
||||
|
Reference in New Issue
Block a user