Add Header and Footer wrapper for their own relationship classes

This commit is contained in:
Dolan
2018-01-31 00:31:54 +00:00
parent df701be572
commit 6d0a267ba4
8 changed files with 138 additions and 38 deletions

View File

@ -42,8 +42,10 @@ export class Compiler {
}); });
const xmlNumbering = xml(this.formatter.format(this.file.Numbering)); const xmlNumbering = xml(this.formatter.format(this.file.Numbering));
const xmlRelationships = xml(this.formatter.format(this.file.Relationships)); const xmlRelationships = xml(this.formatter.format(this.file.Relationships));
const xmlHeader = xml(this.formatter.format(this.file.Header)); const xmlHeader = xml(this.formatter.format(this.file.Header.Header));
const xmlFooter = xml(this.formatter.format(this.file.Footer)); const xmlFooter = xml(this.formatter.format(this.file.Footer.Footer));
const xmlHeaderRelationships = xml(this.formatter.format(this.file.Header.Relationships));
const xmlFooterRelationships = xml(this.formatter.format(this.file.Footer.Relationships));
this.archive.append(xmlDocument, { this.archive.append(xmlDocument, {
name: "word/document.xml", name: "word/document.xml",
@ -73,6 +75,14 @@ export class Compiler {
name: "word/_rels/document.xml.rels", name: "word/_rels/document.xml.rels",
}); });
this.archive.append(xmlHeaderRelationships, {
name: "word/_rels/header1.xml.rels",
});
this.archive.append(xmlFooterRelationships, {
name: "word/_rels/footer1.xml.rels",
});
for (const data of this.file.Media.array) { for (const data of this.file.Media.array) {
this.archive.append(data.stream, { this.archive.append(data.stream, {
name: `word/media/${data.fileName}`, name: `word/media/${data.fileName}`,

View File

@ -1,7 +1,7 @@
import { Document } from "./document"; import { Document } from "./document";
import { SectionPropertiesOptions } from "./document/body/section-properties/section-properties"; import { SectionPropertiesOptions } from "./document/body/section-properties/section-properties";
import { Footer } from "./footer/footer"; import { FooterWrapper } from "./footer-wrapper";
import { Header } from "./header/header"; import { HeaderWrapper } from "./header-wrapper";
import { Media } from "./media"; import { Media } from "./media";
import { Numbering } from "./numbering"; import { Numbering } from "./numbering";
import { Paragraph } from "./paragraph"; import { Paragraph } from "./paragraph";
@ -18,8 +18,8 @@ export class File {
private readonly numbering: Numbering; private readonly numbering: Numbering;
private readonly media: Media; private readonly media: Media;
private readonly relationships: Relationships; private readonly relationships: Relationships;
private readonly header: Header; private readonly headerWrapper: HeaderWrapper;
private readonly footer: Footer; private readonly footerWrapper: FooterWrapper;
constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) { constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) {
this.document = new Document(sectionPropertiesOptions); this.document = new Document(sectionPropertiesOptions);
@ -38,8 +38,8 @@ export class File {
this.numbering = new Numbering(); this.numbering = new Numbering();
this.relationships = new Relationships(); this.relationships = new Relationships();
this.media = new Media(); this.media = new Media();
this.header = new Header(this.media, this.relationships); this.headerWrapper = new HeaderWrapper(this.media);
this.footer = new Footer(this.media, this.relationships); this.footerWrapper = new FooterWrapper(this.media);
} }
public addParagraph(paragraph: Paragraph): void { public addParagraph(paragraph: Paragraph): void {
@ -92,11 +92,11 @@ export class File {
return this.relationships; return this.relationships;
} }
public get Header(): Header { public get Header(): HeaderWrapper {
return this.header; return this.headerWrapper;
} }
public get Footer(): Footer { public get Footer(): FooterWrapper {
return this.footer; return this.footerWrapper;
} }
} }

View File

@ -0,0 +1,55 @@
import { Footer } from "./footer/footer";
import { IMediaData, Media } from "./media";
import { Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { Table } from "./table";
export class FooterWrapper {
private readonly footer: Footer;
private readonly relationships: Relationships;
constructor(private readonly media: Media) {
this.footer = new Footer();
this.relationships = new Relationships();
}
public addParagraph(paragraph: Paragraph): void {
this.footer.addParagraph(paragraph);
}
public createParagraph(text?: string): Paragraph {
const para = new Paragraph(text);
this.addParagraph(para);
return para;
}
public addTable(table: Table): void {
this.footer.addTable(table);
}
public createTable(rows: number, cols: number): Table {
return this.footer.createTable(rows, cols);
}
public addDrawing(imageData: IMediaData): void {
this.footer.addDrawing(imageData);
}
public createImage(image: string): void {
const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount);
this.relationships.createRelationship(
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.addDrawing(mediaData);
}
public get Footer(): Footer {
return this.footer;
}
public get Relationships(): Relationships {
return this.relationships;
}
}

View File

@ -1,13 +1,12 @@
// http://officeopenxml.com/WPfooters.php // http://officeopenxml.com/WPfooters.php
import { IMediaData, Media } from "file/media"; import { IMediaData } from "file/media";
import { Relationships } from "file/relationships";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Paragraph, PictureRun } from "../paragraph"; import { Paragraph, PictureRun } from "../paragraph";
import { Table } from "../table"; import { Table } from "../table";
import { FooterAttributes } from "./footer-attributes"; import { FooterAttributes } from "./footer-attributes";
export class Footer extends XmlComponent { export class Footer extends XmlComponent {
constructor(private readonly media: Media, private readonly relationships: Relationships) { constructor() {
super("w:ftr"); super("w:ftr");
this.root.push( this.root.push(
new FooterAttributes({ new FooterAttributes({
@ -58,14 +57,4 @@ export class Footer extends XmlComponent {
this.root.push(paragraph); this.root.push(paragraph);
} }
public createImage(image: string): void {
const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount);
this.relationships.createRelationship(
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.addDrawing(mediaData);
}
} }

1
src/file/footer/index.ts Normal file
View File

@ -0,0 +1 @@
export * from "./footer";

View File

@ -0,0 +1,55 @@
import { Header } from "./header/header";
import { IMediaData, Media } from "./media";
import { Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { Table } from "./table";
export class HeaderWrapper {
private readonly header: Header;
private readonly relationships: Relationships;
constructor(private readonly media: Media) {
this.header = new Header();
this.relationships = new Relationships();
}
public addParagraph(paragraph: Paragraph): void {
this.header.addParagraph(paragraph);
}
public createParagraph(text?: string): Paragraph {
const para = new Paragraph(text);
this.addParagraph(para);
return para;
}
public addTable(table: Table): void {
this.header.addTable(table);
}
public createTable(rows: number, cols: number): Table {
return this.header.createTable(rows, cols);
}
public addDrawing(imageData: IMediaData): void {
this.header.addDrawing(imageData);
}
public createImage(image: string): void {
const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount);
this.relationships.createRelationship(
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.addDrawing(mediaData);
}
public get Header(): Header {
return this.header;
}
public get Relationships(): Relationships {
return this.relationships;
}
}

View File

@ -1,13 +1,12 @@
// http://officeopenxml.com/WPheaders.php // http://officeopenxml.com/WPheaders.php
import { IMediaData, Media } from "file/media"; import { IMediaData } from "file/media";
import { Relationships } from "file/relationships";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Paragraph, PictureRun } from "../paragraph"; import { Paragraph, PictureRun } from "../paragraph";
import { Table } from "../table"; import { Table } from "../table";
import { HeaderAttributes } from "./header-attributes"; import { HeaderAttributes } from "./header-attributes";
export class Header extends XmlComponent { export class Header extends XmlComponent {
constructor(private readonly media: Media, private readonly relationships: Relationships) { constructor() {
super("w:hdr"); super("w:hdr");
this.root.push( this.root.push(
new HeaderAttributes({ new HeaderAttributes({
@ -58,14 +57,4 @@ export class Header extends XmlComponent {
this.root.push(paragraph); this.root.push(paragraph);
} }
public createImage(image: string): void {
const mediaData = this.media.addMedia(image, this.relationships.RelationshipCount);
this.relationships.createRelationship(
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.addDrawing(mediaData);
}
} }

1
src/file/header/index.ts Normal file
View File

@ -0,0 +1 @@
export * from "./header";