Files
docx-js/src/file/file.ts

447 lines
15 KiB
TypeScript
Raw Normal View History

2018-09-19 23:04:34 +01:00
import { AppProperties } from "./app-properties/app-properties";
import { ContentTypes } from "./content-types/content-types";
import { CoreProperties, IPropertiesOptions } from "./core-properties";
2017-12-15 01:16:04 +00:00
import { Document } from "./document";
2018-10-05 01:33:17 +01:00
import {
FooterReference,
2018-10-05 01:33:17 +01:00
FooterReferenceType,
HeaderReference,
HeaderReferenceType,
IHeaderFooterGroup,
SectionPropertiesOptions,
} from "./document/body/section-properties";
import { IDrawingOptions } from "./drawing";
2018-09-19 23:04:34 +01:00
import { IFileProperties } from "./file-properties";
import { FooterWrapper, IDocumentFooter } from "./footer-wrapper";
import { FootNotes } from "./footnotes";
import { HeaderWrapper, IDocumentHeader } from "./header-wrapper";
import { Media } from "./media";
2018-09-19 23:04:34 +01:00
import { Numbering } from "./numbering";
import { Bookmark, Hyperlink, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
2018-12-05 00:05:11 +00:00
import { TargetModeType } from "./relationships/relationship/relationship";
2018-09-20 10:11:59 -03:00
import { Settings } from "./settings";
2018-09-19 23:04:34 +01:00
import { Styles } from "./styles";
import { ExternalStylesFactory } from "./styles/external-styles-factory";
import { DefaultStylesFactory } from "./styles/factory";
2019-06-25 01:21:28 +01:00
import { Table } from "./table";
2018-09-21 11:16:14 -03:00
import { TableOfContents } from "./table-of-contents";
2018-09-19 23:04:34 +01:00
export class File {
// tslint:disable-next-line:readonly-keyword
2018-09-19 23:04:34 +01:00
private currentRelationshipId: number = 1;
2019-06-17 01:51:57 +01:00
// tslint:disable-next-line:readonly-keyword
private styles: Styles;
2018-09-19 23:04:34 +01:00
private readonly document: Document;
private readonly headers: IDocumentHeader[] = [];
private readonly footers: IDocumentFooter[] = [];
private readonly docRelationships: Relationships;
private readonly coreProperties: CoreProperties;
private readonly numbering: Numbering;
private readonly media: Media;
private readonly fileRelationships: Relationships;
private readonly footNotes: FootNotes;
2018-09-20 10:11:59 -03:00
private readonly settings: Settings;
2018-09-19 23:04:34 +01:00
private readonly contentTypes: ContentTypes;
private readonly appProperties: AppProperties;
2018-09-19 00:37:11 +01:00
constructor(
options: IPropertiesOptions = {
creator: "Un-named",
revision: "1",
lastModifiedBy: "Un-named",
},
sectionPropertiesOptions: SectionPropertiesOptions = {},
2018-09-19 23:04:34 +01:00
fileProperties: IFileProperties = {},
2018-09-19 00:37:11 +01:00
) {
2018-09-19 23:04:34 +01:00
this.coreProperties = new CoreProperties(options);
this.numbering = new Numbering();
this.docRelationships = new Relationships();
this.fileRelationships = new Relationships();
this.appProperties = new AppProperties();
this.footNotes = new FootNotes();
this.contentTypes = new ContentTypes();
2019-01-04 00:11:50 +00:00
this.media = fileProperties.template && fileProperties.template.media ? fileProperties.template.media : new Media();
2018-09-26 13:10:21 +03:00
if (fileProperties.template) {
this.currentRelationshipId = fileProperties.template.currentRelationshipId + 1;
}
2018-09-26 14:33:05 +03:00
// set up styles
if (fileProperties.template && options.externalStyles) {
throw Error("can not use both template and external styles");
}
if (fileProperties.template) {
2019-01-07 21:43:04 +00:00
const stylesFactory = new ExternalStylesFactory();
this.styles = stylesFactory.newInstance(fileProperties.template.styles);
2018-09-26 14:33:05 +03:00
} else if (options.externalStyles) {
2018-09-19 23:04:34 +01:00
const stylesFactory = new ExternalStylesFactory();
this.styles = stylesFactory.newInstance(options.externalStyles);
} else {
const stylesFactory = new DefaultStylesFactory();
this.styles = stylesFactory.newInstance();
}
2018-09-19 00:37:11 +01:00
this.addDefaultRelationships();
2018-09-19 23:04:34 +01:00
if (fileProperties.template && fileProperties.template.headers) {
for (const templateHeader of fileProperties.template.headers) {
this.addHeaderToDocument(templateHeader.header, templateHeader.type);
}
} else {
this.createHeader();
}
if (fileProperties.template && fileProperties.template.footers) {
for (const templateFooter of fileProperties.template.footers) {
this.addFooterToDocument(templateFooter.footer, templateFooter.type);
}
} else {
this.createFooter();
}
2018-09-04 17:16:31 +03:00
const newSectionPropertiesOptions = {
2018-09-17 20:27:43 +01:00
...sectionPropertiesOptions,
2018-10-05 01:33:17 +01:00
headers: this.groupHeaders(this.headers, sectionPropertiesOptions.headers),
footers: this.groupFooters(this.footers, sectionPropertiesOptions.footers),
2018-09-17 20:27:43 +01:00
};
2018-09-04 17:16:31 +03:00
this.document = new Document(newSectionPropertiesOptions);
2018-09-20 10:11:59 -03:00
this.settings = new Settings();
}
public add(item: Paragraph | Table | TableOfContents): File {
if (item instanceof Paragraph) {
2019-06-25 23:17:56 +01:00
this.document.add(item);
}
2018-09-19 23:04:34 +01:00
if (item instanceof Table) {
2019-06-25 23:17:56 +01:00
this.document.add(item);
}
2018-09-19 23:04:34 +01:00
if (item instanceof TableOfContents) {
this.document.addTableOfContents(item);
}
2018-09-19 23:04:34 +01:00
return this;
}
public createImage(
buffer: Buffer | string | Uint8Array | ArrayBuffer,
width?: number,
height?: number,
drawingOptions?: IDrawingOptions,
): Paragraph {
const image = Media.addImage(this, buffer, width, height, drawingOptions);
const paragraph = new Paragraph(image);
2019-06-25 23:17:56 +01:00
this.document.add(paragraph);
2018-09-19 23:04:34 +01:00
return paragraph;
2018-09-19 23:04:34 +01:00
}
public createHyperlink(link: string, text?: string): Hyperlink {
const newText = text === undefined ? link : text;
const hyperlink = new Hyperlink(newText, this.docRelationships.RelationshipCount);
2018-09-19 23:04:34 +01:00
this.docRelationships.createRelationship(
hyperlink.linkId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
link,
2018-12-05 00:05:11 +00:00
TargetModeType.EXTERNAL,
2018-09-19 23:04:34 +01:00
);
return hyperlink;
}
public createInternalHyperLink(anchor: string, text?: string): Hyperlink {
const newText = text === undefined ? anchor : text;
const hyperlink = new Hyperlink(newText, this.docRelationships.RelationshipCount, anchor);
2018-09-19 23:04:34 +01:00
// NOTE: unlike File#createHyperlink(), since the link is to an internal bookmark
// we don't need to create a new relationship.
return hyperlink;
}
public createBookmark(name: string, text?: string): Bookmark {
const newText = text === undefined ? name : text;
const bookmark = new Bookmark(name, newText, this.docRelationships.RelationshipCount);
2018-09-19 23:04:34 +01:00
return bookmark;
}
public addSection(sectionPropertiesOptions: SectionPropertiesOptions): void {
this.document.Body.addSection(sectionPropertiesOptions);
}
public createFootnote(paragraph: Paragraph): void {
this.footNotes.createFootNote(paragraph);
}
public createHeader(): HeaderWrapper {
2018-10-23 00:31:51 +01:00
const header = new HeaderWrapper(this.media, this.currentRelationshipId++);
2018-09-19 23:04:34 +01:00
this.addHeaderToDocument(header);
return header;
}
public createFooter(): FooterWrapper {
2018-10-23 00:31:51 +01:00
const footer = new FooterWrapper(this.media, this.currentRelationshipId++);
2018-09-19 23:04:34 +01:00
this.addFooterToDocument(footer);
return footer;
}
public createFirstPageHeader(): HeaderWrapper {
const headerWrapper = this.createHeader();
this.document.Body.DefaultSection.addChildElement(
new HeaderReference({
headerType: HeaderReferenceType.FIRST,
headerId: headerWrapper.Header.ReferenceId,
}),
);
return headerWrapper;
}
2019-01-09 01:52:20 +00:00
public createEvenPageHeader(): HeaderWrapper {
const headerWrapper = this.createHeader();
this.document.Body.DefaultSection.addChildElement(
new HeaderReference({
headerType: HeaderReferenceType.EVEN,
headerId: headerWrapper.Header.ReferenceId,
}),
);
return headerWrapper;
}
public createFirstPageFooter(): FooterWrapper {
const footerWrapper = this.createFooter();
this.document.Body.DefaultSection.addChildElement(
new FooterReference({
footerType: FooterReferenceType.FIRST,
footerId: footerWrapper.Footer.ReferenceId,
}),
);
return footerWrapper;
}
public createEvenPageFooter(): FooterWrapper {
const footerWrapper = this.createFooter();
this.document.Body.DefaultSection.addChildElement(
new FooterReference({
footerType: FooterReferenceType.EVEN,
footerId: footerWrapper.Footer.ReferenceId,
}),
);
return footerWrapper;
}
2018-09-19 23:04:34 +01:00
public getFooterByReferenceNumber(refId: number): FooterWrapper {
const entry = this.footers.map((item) => item.footer).find((h) => h.Footer.ReferenceId === refId);
if (entry) {
return entry;
}
throw new Error(`There is no footer with given reference id ${refId}`);
}
public getHeaderByReferenceNumber(refId: number): HeaderWrapper {
const entry = this.headers.map((item) => item.header).find((h) => h.Header.ReferenceId === refId);
if (entry) {
return entry;
}
throw new Error(`There is no header with given reference id ${refId}`);
}
2018-09-25 23:46:55 +01:00
public verifyUpdateFields(): void {
if (this.document.getTablesOfContents().length) {
this.settings.addUpdateFields();
}
}
2018-10-05 00:20:43 +01:00
private addHeaderToDocument(header: HeaderWrapper, type: HeaderReferenceType = HeaderReferenceType.DEFAULT): void {
2018-09-19 23:04:34 +01:00
this.headers.push({ header, type });
this.docRelationships.createRelationship(
header.Header.ReferenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header",
`header${this.headers.length}.xml`,
);
this.contentTypes.addHeader(this.headers.length);
}
2018-10-05 00:20:43 +01:00
private addFooterToDocument(footer: FooterWrapper, type: FooterReferenceType = FooterReferenceType.DEFAULT): void {
2018-09-19 23:04:34 +01:00
this.footers.push({ footer, type });
this.docRelationships.createRelationship(
footer.Footer.ReferenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer",
`footer${this.footers.length}.xml`,
);
this.contentTypes.addFooter(this.footers.length);
}
2018-10-05 00:20:43 +01:00
private addDefaultRelationships(): void {
2018-09-19 23:04:34 +01:00
this.fileRelationships.createRelationship(
1,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
"word/document.xml",
);
this.fileRelationships.createRelationship(
2,
"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties",
"docProps/core.xml",
);
this.fileRelationships.createRelationship(
3,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties",
"docProps/app.xml",
);
this.docRelationships.createRelationship(
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
"styles.xml",
);
this.docRelationships.createRelationship(
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
"numbering.xml",
);
this.docRelationships.createRelationship(
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
"footnotes.xml",
);
this.docRelationships.createRelationship(
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings",
"settings.xml",
);
2018-09-19 23:04:34 +01:00
}
2018-10-05 01:33:17 +01:00
private groupHeaders(headers: IDocumentHeader[], group: IHeaderFooterGroup<HeaderWrapper> = {}): IHeaderFooterGroup<HeaderWrapper> {
let newGroup = group;
for (const header of headers) {
switch (header.type) {
case HeaderReferenceType.FIRST:
newGroup = {
...newGroup,
first: header.header,
};
2018-10-17 01:40:17 +01:00
break;
2018-10-05 01:33:17 +01:00
case HeaderReferenceType.EVEN:
newGroup = {
...newGroup,
even: header.header,
};
2018-10-17 01:40:17 +01:00
break;
2019-01-03 02:36:56 +00:00
case HeaderReferenceType.DEFAULT:
2018-10-05 01:33:17 +01:00
default:
newGroup = {
...newGroup,
default: header.header,
};
2018-10-17 01:40:17 +01:00
break;
2018-10-05 01:33:17 +01:00
}
}
return newGroup;
}
private groupFooters(footers: IDocumentFooter[], group: IHeaderFooterGroup<FooterWrapper> = {}): IHeaderFooterGroup<FooterWrapper> {
let newGroup = group;
for (const footer of footers) {
switch (footer.type) {
case FooterReferenceType.FIRST:
newGroup = {
...newGroup,
first: footer.footer,
};
2018-10-17 01:40:17 +01:00
break;
2018-10-05 01:33:17 +01:00
case FooterReferenceType.EVEN:
newGroup = {
...newGroup,
even: footer.footer,
};
2018-10-17 01:40:17 +01:00
break;
2019-01-03 02:36:56 +00:00
case FooterReferenceType.DEFAULT:
2018-10-05 01:33:17 +01:00
default:
newGroup = {
...newGroup,
default: footer.footer,
};
2018-10-17 01:40:17 +01:00
break;
2018-10-05 01:33:17 +01:00
}
}
return newGroup;
}
2018-09-19 23:04:34 +01:00
public get Document(): Document {
return this.document;
}
public get Styles(): Styles {
return this.styles;
}
public set Styles(styles: Styles) {
this.styles = styles;
}
public get CoreProperties(): CoreProperties {
return this.coreProperties;
}
public get Numbering(): Numbering {
return this.numbering;
}
public get Media(): Media {
return this.media;
}
public get DocumentRelationships(): Relationships {
return this.docRelationships;
}
public get FileRelationships(): Relationships {
return this.fileRelationships;
}
public get Header(): HeaderWrapper {
return this.headers[0].header;
}
public get Headers(): HeaderWrapper[] {
return this.headers.map((item) => item.header);
}
public get Footer(): FooterWrapper {
return this.footers[0].footer;
}
public get Footers(): FooterWrapper[] {
return this.footers.map((item) => item.footer);
}
public get ContentTypes(): ContentTypes {
return this.contentTypes;
}
public get AppProperties(): AppProperties {
return this.appProperties;
}
public get FootNotes(): FootNotes {
return this.footNotes;
}
2018-09-20 10:11:59 -03:00
public get Settings(): Settings {
return this.settings;
}
2017-12-15 01:16:04 +00:00
}