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

371 lines
13 KiB
TypeScript
Raw Normal View History

2019-11-08 11:22:27 +01:00
import * as shortid from "shortid";
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 {
FooterReferenceType,
HeaderReferenceType,
2019-07-31 08:48:02 +01:00
IPageSizeAttributes,
2018-10-05 01:33:17 +01:00
SectionPropertiesOptions,
} from "./document/body/section-properties";
2019-07-31 08:48:02 +01:00
import { IPageMarginAttributes } from "./document/body/section-properties/page-margin/page-margin-attributes";
2018-09-19 23:04:34 +01:00
import { IFileProperties } from "./file-properties";
import { FooterWrapper, IDocumentFooter } from "./footer-wrapper";
import { FootNotes } from "./footnotes";
2019-07-31 08:48:02 +01:00
import { Footer, Header } from "./header";
2018-09-19 23:04:34 +01:00
import { HeaderWrapper, IDocumentHeader } from "./header-wrapper";
import { Media } from "./media";
2018-09-19 23:04:34 +01:00
import { Numbering } from "./numbering";
2019-12-21 03:59:40 +00:00
import { Hyperlink, HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph";
2018-09-19 23:04:34 +01:00
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
2019-07-07 03:54:29 +01:00
export interface ISectionOptions {
2019-07-31 08:48:02 +01:00
readonly headers?: {
readonly default?: Header;
readonly first?: Header;
readonly even?: Header;
};
readonly footers?: {
readonly default?: Footer;
readonly first?: Footer;
readonly even?: Footer;
};
readonly size?: IPageSizeAttributes;
readonly margins?: IPageMarginAttributes;
readonly properties?: SectionPropertiesOptions;
2020-08-01 17:58:16 +01:00
readonly children: (Paragraph | Table | TableOfContents | HyperlinkRef)[];
2019-07-07 03:54:29 +01:00
}
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;
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;
2019-10-04 01:20:41 +01:00
private readonly styles: Styles;
2019-12-24 16:31:35 +00:00
private readonly hyperlinkCache: { readonly [key: string]: Hyperlink } = {};
2018-09-19 00:37:11 +01:00
constructor(
options: IPropertiesOptions = {
creator: "Un-named",
revision: "1",
lastModifiedBy: "Un-named",
},
2018-09-19 23:04:34 +01:00
fileProperties: IFileProperties = {},
2019-07-07 03:54:29 +01:00
sections: ISectionOptions[] = [],
2018-09-19 00:37:11 +01:00
) {
2018-09-19 23:04:34 +01:00
this.coreProperties = new CoreProperties(options);
2019-11-08 03:11:19 +00:00
this.numbering = new Numbering(
options.numbering
? options.numbering
: {
config: [],
},
);
2018-09-19 23:04:34 +01:00
this.docRelationships = new Relationships();
this.fileRelationships = new Relationships();
this.appProperties = new AppProperties();
this.footNotes = new FootNotes();
this.contentTypes = new ContentTypes();
2019-07-31 08:48:02 +01:00
this.document = new Document();
this.settings = new Settings();
2018-09-19 23:04:34 +01:00
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);
2019-10-04 01:20:41 +01:00
} else if (options.styles) {
2019-10-04 02:37:22 +01:00
const stylesFactory = new DefaultStylesFactory();
const defaultStyles = stylesFactory.newInstance();
this.styles = new Styles({
...defaultStyles,
...options.styles,
});
2018-09-19 23:04:34 +01:00
} else {
const stylesFactory = new DefaultStylesFactory();
2019-10-04 02:37:22 +01:00
this.styles = new Styles(stylesFactory.newInstance());
2018-09-19 23:04:34 +01:00
}
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);
}
}
if (fileProperties.template && fileProperties.template.footers) {
for (const templateFooter of fileProperties.template.footers) {
this.addFooterToDocument(templateFooter.footer, templateFooter.type);
}
}
2018-09-04 17:16:31 +03:00
2019-07-07 03:54:29 +01:00
for (const section of sections) {
2019-07-31 08:48:02 +01:00
this.document.Body.addSection(section.properties ? section.properties : {});
2019-07-07 03:54:29 +01:00
for (const child of section.children) {
2019-12-18 21:11:15 +00:00
if (child instanceof HyperlinkRef) {
const hyperlink = this.hyperlinkCache[child.id];
this.document.add(hyperlink);
continue;
}
2019-07-31 08:48:02 +01:00
this.document.add(child);
2019-07-07 03:54:29 +01:00
}
}
2019-12-03 23:04:48 +00:00
if (options.footnotes) {
for (const paragraph of options.footnotes) {
this.footNotes.createFootNote(paragraph);
}
}
2019-12-18 21:11:15 +00:00
if (options.hyperlinks) {
const cache = {};
for (const key in options.hyperlinks) {
if (!options.hyperlinks[key]) {
continue;
}
2019-12-21 03:31:09 +00:00
const hyperlinkRef = options.hyperlinks[key];
const hyperlink =
hyperlinkRef.type === HyperlinkType.EXTERNAL
? this.createHyperlink(hyperlinkRef.link, hyperlinkRef.text)
: this.createInternalHyperLink(key, hyperlinkRef.text);
2019-12-18 21:11:15 +00:00
cache[key] = hyperlink;
}
this.hyperlinkCache = cache;
}
2018-09-19 23:04:34 +01:00
}
2019-07-31 08:48:02 +01:00
public addSection({
headers = { default: new Header() },
footers = { default: new Header() },
margins = {},
size = {},
properties,
children,
}: ISectionOptions): void {
this.document.Body.addSection({
...properties,
headers: {
default: headers.default ? this.createHeader(headers.default) : this.createHeader(new Header()),
first: headers.first ? this.createHeader(headers.first) : undefined,
even: headers.even ? this.createHeader(headers.even) : undefined,
},
footers: {
default: footers.default ? this.createFooter(footers.default) : this.createFooter(new Footer()),
first: footers.first ? this.createFooter(footers.first) : undefined,
even: footers.even ? this.createFooter(footers.even) : undefined,
},
...margins,
...size,
});
for (const child of children) {
2019-12-18 21:11:15 +00:00
if (child instanceof HyperlinkRef) {
const hyperlink = this.hyperlinkCache[child.id];
this.document.add(hyperlink);
continue;
}
2019-07-31 08:48:02 +01:00
this.document.add(child);
2019-07-07 20:23:42 +01:00
}
}
2019-07-31 08:48:02 +01:00
public verifyUpdateFields(): void {
if (this.document.getTablesOfContents().length) {
this.settings.addUpdateFields();
}
}
2019-12-21 03:31:09 +00:00
private createHyperlink(link: string, text: string = link): Hyperlink {
const hyperlink = new Hyperlink(text, shortid.generate().toLowerCase());
2019-12-18 21:11:15 +00:00
this.docRelationships.createRelationship(
hyperlink.linkId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
link,
TargetModeType.EXTERNAL,
);
return hyperlink;
}
2019-12-21 03:31:09 +00:00
private createInternalHyperLink(anchor: string, text: string = anchor): Hyperlink {
const hyperlink = new Hyperlink(text, shortid.generate().toLowerCase(), anchor);
// NOTE: unlike File#createHyperlink(), since the link is to an internal bookmark
// we don't need to create a new relationship.
return hyperlink;
}
2019-07-31 08:48:02 +01:00
private createHeader(header: Header): HeaderWrapper {
const wrapper = new HeaderWrapper(this.media, this.currentRelationshipId++);
2019-07-31 08:48:02 +01:00
for (const child of header.options.children) {
wrapper.add(child);
}
2019-07-31 08:48:02 +01:00
this.addHeaderToDocument(wrapper);
return wrapper;
}
2019-07-31 08:48:02 +01:00
private createFooter(footer: Footer): FooterWrapper {
const wrapper = new FooterWrapper(this.media, this.currentRelationshipId++);
2018-09-19 23:04:34 +01:00
2019-07-31 08:48:02 +01:00
for (const child of footer.options.children) {
wrapper.add(child);
2018-09-19 23:04:34 +01:00
}
2019-07-31 08:48:02 +01:00
this.addFooterToDocument(wrapper);
return wrapper;
2018-09-25 23:46:55 +01:00
}
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
}
public get Document(): Document {
return this.document;
}
public get Styles(): Styles {
return this.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 Headers(): HeaderWrapper[] {
return this.headers.map((item) => item.header);
}
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;
}
2019-12-18 21:11:15 +00:00
public get HyperlinkCache(): { readonly [key: string]: Hyperlink } {
return this.hyperlinkCache;
}
2017-12-15 01:16:04 +00:00
}