Fix naming

This commit is contained in:
Dolan
2018-09-17 20:06:51 +01:00
parent 5b00279996
commit b39c7ce323
4 changed files with 44 additions and 38 deletions

View File

@ -10,9 +10,9 @@ import {
IHeaderOptions, IHeaderOptions,
SectionPropertiesOptions, SectionPropertiesOptions,
} from "./document/body/section-properties"; } from "./document/body/section-properties";
import { FooterWrapper } from "./footer-wrapper"; import { FooterWrapper, IDocumentFooter } from "./footer-wrapper";
import { FootNotes } from "./footnotes"; import { FootNotes } from "./footnotes";
import { HeaderWrapper } from "./header-wrapper"; import { HeaderWrapper, IDocumentHeader } from "./header-wrapper";
import { Image, Media } from "./media"; import { Image, Media } from "./media";
import { Numbering } from "./numbering"; import { Numbering } from "./numbering";
import { Bookmark, Hyperlink, Paragraph } from "./paragraph"; import { Bookmark, Hyperlink, Paragraph } from "./paragraph";
@ -22,9 +22,6 @@ import { ExternalStylesFactory } from "./styles/external-styles-factory";
import { DefaultStylesFactory } from "./styles/factory"; import { DefaultStylesFactory } from "./styles/factory";
import { Table } from "./table"; import { Table } from "./table";
type DocumentHeaders = Array<{ header: HeaderWrapper; type: HeaderReferenceType }>;
type DocumentFooters = Array<{ footer: FooterWrapper; type: FooterReferenceType }>;
export class File { export class File {
private readonly document: Document; private readonly document: Document;
private readonly styles: Styles; private readonly styles: Styles;
@ -33,8 +30,8 @@ export class File {
private readonly media: Media; private readonly media: Media;
private readonly docRelationships: Relationships; private readonly docRelationships: Relationships;
private readonly fileRelationships: Relationships; private readonly fileRelationships: Relationships;
private readonly documentHeaders: DocumentHeaders = []; private readonly headers: IDocumentHeader[] = [];
private readonly documentFooters: DocumentFooters = []; private readonly footers: IDocumentFooter[] = [];
private readonly footNotes: FootNotes; private readonly footNotes: FootNotes;
@ -128,7 +125,7 @@ export class File {
this.footNotes = new FootNotes(); this.footNotes = new FootNotes();
const headersOptions: IHeaderOptions[] = []; const headersOptions: IHeaderOptions[] = [];
for (const documentHeader of this.documentHeaders) { for (const documentHeader of this.headers) {
headersOptions.push({ headersOptions.push({
headerId: documentHeader.header.Header.ReferenceId, headerId: documentHeader.header.Header.ReferenceId,
headerType: documentHeader.type, headerType: documentHeader.type,
@ -136,7 +133,7 @@ export class File {
} }
const footersOptions: IFooterOptions[] = []; const footersOptions: IFooterOptions[] = [];
for (const documentFooter of this.documentFooters) { for (const documentFooter of this.footers) {
footersOptions.push({ footersOptions.push({
footerId: documentFooter.footer.Footer.ReferenceId, footerId: documentFooter.footer.Footer.ReferenceId,
footerType: documentFooter.type, footerType: documentFooter.type,
@ -244,7 +241,7 @@ export class File {
} }
public getFooterByReferenceNumber(refId: number): FooterWrapper { public getFooterByReferenceNumber(refId: number): FooterWrapper {
const entry = this.documentFooters.map((item) => item.footer).find((h) => h.Footer.ReferenceId === refId); const entry = this.footers.map((item) => item.footer).find((h) => h.Footer.ReferenceId === refId);
if (entry) { if (entry) {
return entry; return entry;
} }
@ -252,7 +249,7 @@ export class File {
} }
public getHeaderByReferenceNumber(refId: number): HeaderWrapper { public getHeaderByReferenceNumber(refId: number): HeaderWrapper {
const entry = this.documentHeaders.map((item) => item.header).find((h) => h.Header.ReferenceId === refId); const entry = this.headers.map((item) => item.header).find((h) => h.Header.ReferenceId === refId);
if (entry) { if (entry) {
return entry; return entry;
} }
@ -260,23 +257,23 @@ export class File {
} }
private addHeaderToDocument(header: HeaderWrapper, type: HeaderReferenceType = HeaderReferenceType.DEFAULT): void { private addHeaderToDocument(header: HeaderWrapper, type: HeaderReferenceType = HeaderReferenceType.DEFAULT): void {
this.documentHeaders.push({ header, type }); this.headers.push({ header, type });
this.docRelationships.createRelationship( this.docRelationships.createRelationship(
header.Header.ReferenceId, header.Header.ReferenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header",
`header${this.documentHeaders.length}.xml`, `header${this.headers.length}.xml`,
); );
this.contentTypes.addHeader(this.documentHeaders.length); this.contentTypes.addHeader(this.headers.length);
} }
private addFooterToDocument(footer: FooterWrapper, type: FooterReferenceType = FooterReferenceType.DEFAULT): void { private addFooterToDocument(footer: FooterWrapper, type: FooterReferenceType = FooterReferenceType.DEFAULT): void {
this.documentFooters.push({ footer, type }); this.footers.push({ footer, type });
this.docRelationships.createRelationship( this.docRelationships.createRelationship(
footer.Footer.ReferenceId, footer.Footer.ReferenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer",
`footer${this.documentFooters.length}.xml`, `footer${this.footers.length}.xml`,
); );
this.contentTypes.addFooter(this.documentFooters.length); this.contentTypes.addFooter(this.footers.length);
} }
public get Document(): Document { public get Document(): Document {
@ -308,19 +305,19 @@ export class File {
} }
public get Header(): HeaderWrapper { public get Header(): HeaderWrapper {
return this.documentHeaders[0].header; return this.headers[0].header;
} }
public get Headers(): HeaderWrapper[] { public get Headers(): HeaderWrapper[] {
return this.documentHeaders.map((item) => item.header); return this.headers.map((item) => item.header);
} }
public get Footer(): FooterWrapper { public get Footer(): FooterWrapper {
return this.documentFooters[0].footer; return this.footers[0].footer;
} }
public get Footers(): FooterWrapper[] { public get Footers(): FooterWrapper[] {
return this.documentFooters.map((item) => item.footer); return this.footers.map((item) => item.footer);
} }
public get ContentTypes(): ContentTypes { public get ContentTypes(): ContentTypes {

View File

@ -1,11 +1,17 @@
import { IMediaData } from "file/media"; import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { FooterReferenceType } from "./document";
import { Footer } from "./footer/footer"; import { Footer } from "./footer/footer";
import { Image, Media } from "./media"; import { Image, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph"; import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships"; import { Relationships } from "./relationships";
import { Table } from "./table"; import { Table } from "./table";
export interface IDocumentFooter {
footer: FooterWrapper;
type: FooterReferenceType;
}
export class FooterWrapper { export class FooterWrapper {
private readonly footer: Footer; private readonly footer: Footer;
private readonly relationships: Relationships; private readonly relationships: Relationships;

View File

@ -1,11 +1,17 @@
import { IMediaData } from "file/media"; import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { HeaderReferenceType } from "./document";
import { Header } from "./header/header"; import { Header } from "./header/header";
import { Image, Media } from "./media"; import { Image, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph"; import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships"; import { Relationships } from "./relationships";
import { Table } from "./table"; import { Table } from "./table";
export interface IDocumentHeader {
header: HeaderWrapper;
type: HeaderReferenceType;
}
export class HeaderWrapper { export class HeaderWrapper {
private readonly header: Header; private readonly header: Header;
private readonly relationships: Relationships; private readonly relationships: Relationships;

View File

@ -3,8 +3,8 @@ import * as JSZip from "jszip";
import { FooterReferenceType } from "file/document/body/section-properties/footer-reference"; import { FooterReferenceType } from "file/document/body/section-properties/footer-reference";
import { HeaderReferenceType } from "file/document/body/section-properties/header-reference"; import { HeaderReferenceType } from "file/document/body/section-properties/header-reference";
import { FooterWrapper } from "file/footer-wrapper"; import { FooterWrapper, IDocumentFooter } from "file/footer-wrapper";
import { HeaderWrapper } from "file/header-wrapper"; import { HeaderWrapper, IDocumentHeader } from "file/header-wrapper";
import { convertToXmlComponent, ImportedXmlComponent, parseOptions } from "file/xml-components"; import { convertToXmlComponent, ImportedXmlComponent, parseOptions } from "file/xml-components";
const schemeToType = { const schemeToType = {
@ -18,19 +18,16 @@ interface IDocumentRefs {
footers: Array<{ id: number; type: FooterReferenceType }>; footers: Array<{ id: number; type: FooterReferenceType }>;
} }
interface IRelationFileInfo { interface IRelationshipFileInfo {
id: number; id: number;
targetFile: string; targetFile: string;
type: "header" | "footer" | "image"; type: "header" | "footer" | "image";
} }
type DocumentHeaders = Array<{ type: HeaderReferenceType; header: HeaderWrapper }>;
type DocumentFooters = Array<{ type: FooterReferenceType; footer: FooterWrapper }>;
export interface ITemplateDocument { export interface ITemplateDocument {
currentRelationshipId: number; currentRelationshipId: number;
headers: DocumentHeaders; headers: IDocumentHeader[];
footers: DocumentFooters; footers: IDocumentFooter[];
} }
export class ImportDocx { export class ImportDocx {
@ -47,12 +44,12 @@ export class ImportDocx {
const documentRefs: IDocumentRefs = this.extractDocumentRefs(await documentContent.async("text")); const documentRefs: IDocumentRefs = this.extractDocumentRefs(await documentContent.async("text"));
const relationshipContent = zipContent.files["word/_rels/document.xml.rels"]; const relationshipContent = zipContent.files["word/_rels/document.xml.rels"];
const documentRelations: IRelationFileInfo[] = this.findReferenceFiles(await relationshipContent.async("text")); const documentRelationships: IRelationshipFileInfo[] = this.findReferenceFiles(await relationshipContent.async("text"));
const headers: DocumentHeaders = []; const headers: IDocumentHeader[] = [];
for (const headerRef of documentRefs.headers) { for (const headerRef of documentRefs.headers) {
const headerKey = "w:hdr"; const headerKey = "w:hdr";
const relationFileInfo = documentRelations.find((rel) => rel.id === headerRef.id); const relationFileInfo = documentRelationships.find((rel) => rel.id === headerRef.id);
if (relationFileInfo === null || !relationFileInfo) { if (relationFileInfo === null || !relationFileInfo) {
throw new Error(`Can not find target file for id ${headerRef.id}`); throw new Error(`Can not find target file for id ${headerRef.id}`);
} }
@ -67,10 +64,10 @@ export class ImportDocx {
headers.push({ type: headerRef.type, header }); headers.push({ type: headerRef.type, header });
} }
const footers: DocumentFooters = []; const footers: IDocumentFooter[] = [];
for (const footerRef of documentRefs.footers) { for (const footerRef of documentRefs.footers) {
const footerKey = "w:ftr"; const footerKey = "w:ftr";
const relationFileInfo = documentRelations.find((rel) => rel.id === footerRef.id); const relationFileInfo = documentRelationships.find((rel) => rel.id === footerRef.id);
if (relationFileInfo === null || !relationFileInfo) { if (relationFileInfo === null || !relationFileInfo) {
throw new Error(`Can not find target file for id ${footerRef.id}`); throw new Error(`Can not find target file for id ${footerRef.id}`);
} }
@ -88,11 +85,11 @@ export class ImportDocx {
} }
public async addImagesToWrapper( public async addImagesToWrapper(
relationFile: IRelationFileInfo, relationFile: IRelationshipFileInfo,
zipContent: JSZip, zipContent: JSZip,
wrapper: HeaderWrapper | FooterWrapper, wrapper: HeaderWrapper | FooterWrapper,
): Promise<void> { ): Promise<void> {
let wrapperImagesReferences: IRelationFileInfo[] = []; let wrapperImagesReferences: IRelationshipFileInfo[] = [];
const refFile = zipContent.files[`word/_rels/${relationFile.targetFile}.rels`]; const refFile = zipContent.files[`word/_rels/${relationFile.targetFile}.rels`];
if (refFile) { if (refFile) {
const xmlRef = await refFile.async("text"); const xmlRef = await refFile.async("text");
@ -104,12 +101,12 @@ export class ImportDocx {
} }
} }
public findReferenceFiles(xmlData: string): IRelationFileInfo[] { public findReferenceFiles(xmlData: string): IRelationshipFileInfo[] {
const xmlObj = fastXmlParser.parse(xmlData, parseOptions); const xmlObj = fastXmlParser.parse(xmlData, parseOptions);
const relationXmlArray = Array.isArray(xmlObj.Relationships.Relationship) const relationXmlArray = Array.isArray(xmlObj.Relationships.Relationship)
? xmlObj.Relationships.Relationship ? xmlObj.Relationships.Relationship
: [xmlObj.Relationships.Relationship]; : [xmlObj.Relationships.Relationship];
const relations: IRelationFileInfo[] = relationXmlArray const relations: IRelationshipFileInfo[] = relationXmlArray
.map((item) => { .map((item) => {
return { return {
id: this.parseRefId(item._attr.Id), id: this.parseRefId(item._attr.Id),