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

297 lines
11 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";
2021-03-07 21:27:38 +00:00
import { CustomProperties } from "./custom-properties";
import { DocumentWrapper } from "./document-wrapper";
import { HeaderFooterReferenceType, ISectionPropertiesOptions } from "./document/body/section-properties";
2018-09-19 23:04:34 +01:00
import { IFileProperties } from "./file-properties";
import { FooterWrapper, IDocumentFooter } from "./footer-wrapper";
2021-03-01 23:35:52 +00:00
import { FootnotesWrapper } from "./footnotes-wrapper";
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";
import { Paragraph } from "./paragraph";
2018-09-19 23:04:34 +01:00
import { Relationships } from "./relationships";
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;
};
2021-03-19 20:53:56 +00:00
readonly properties?: ISectionPropertiesOptions;
readonly children: (Paragraph | Table | TableOfContents)[];
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 documentWrapper: DocumentWrapper;
2018-09-19 23:04:34 +01:00
private readonly headers: IDocumentHeader[] = [];
private readonly footers: IDocumentFooter[] = [];
private readonly coreProperties: CoreProperties;
private readonly numbering: Numbering;
private readonly media: Media;
private readonly fileRelationships: Relationships;
2021-03-01 23:35:52 +00:00
private readonly footnotesWrapper: FootnotesWrapper;
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 customProperties: CustomProperties;
2018-09-19 23:04:34 +01:00
private readonly appProperties: AppProperties;
2019-10-04 01:20:41 +01:00
private readonly styles: Styles;
2018-09-19 00:37:11 +01:00
2021-03-19 20:53:56 +00:00
constructor(options: IPropertiesOptions, fileProperties: IFileProperties = {}) {
this.coreProperties = new CoreProperties({
...options,
creator: options.creator ?? "Un-named",
revision: options.revision ?? "1",
lastModifiedBy: options.lastModifiedBy ?? "Un-named",
});
2019-11-08 03:11:19 +00:00
this.numbering = new Numbering(
options.numbering
? options.numbering
: {
config: [],
},
);
2021-03-19 20:53:56 +00:00
2018-09-19 23:04:34 +01:00
this.fileRelationships = new Relationships();
2021-03-07 21:27:38 +00:00
this.customProperties = new CustomProperties(options.customProperties ?? []);
2018-09-19 23:04:34 +01:00
this.appProperties = new AppProperties();
2021-03-01 23:35:52 +00:00
this.footnotesWrapper = new FootnotesWrapper();
2018-09-19 23:04:34 +01:00
this.contentTypes = new ContentTypes();
this.documentWrapper = new DocumentWrapper({ background: options.background || {} });
2021-02-27 01:40:55 +00:00
this.settings = new Settings({
compatabilityModeVersion: options.compatabilityModeVersion,
2021-03-16 00:57:27 +00:00
evenAndOddHeaders: options.evenAndOddHeaderAndFooters ? true : false,
trackRevisions: options.features?.trackRevisions,
updateFields: options.features?.updateFields,
2021-02-27 01:40:55 +00:00
});
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(options.styles.default);
2019-10-04 02:37:22 +01:00
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
2021-03-19 20:53:56 +00:00
for (const section of options.sections) {
this.addSection(section);
2019-07-07 03:54:29 +01:00
}
2019-12-03 23:04:48 +00:00
if (options.footnotes) {
2021-03-08 04:33:15 +00:00
// tslint:disable-next-line: forin
2020-01-01 20:22:42 +00:00
for (const key in options.footnotes) {
this.footnotesWrapper.View.createFootNote(parseFloat(key), options.footnotes[key].children);
2019-12-03 23:04:48 +00:00
}
}
2021-03-19 20:53:56 +00:00
}
private addSection({ headers = {}, footers = {}, children, properties }: ISectionOptions): void {
this.documentWrapper.View.Body.addSection({
2019-07-31 08:48:02 +01:00
...properties,
2021-03-19 20:53:56 +00:00
headerWrapperGroup: {
default: headers.default ? this.createHeader(headers.default) : undefined,
2019-07-31 08:48:02 +01:00
first: headers.first ? this.createHeader(headers.first) : undefined,
even: headers.even ? this.createHeader(headers.even) : undefined,
},
2021-03-19 20:53:56 +00:00
footerWrapperGroup: {
default: footers.default ? this.createFooter(footers.default) : undefined,
2019-07-31 08:48:02 +01:00
first: footers.first ? this.createFooter(footers.first) : undefined,
even: footers.even ? this.createFooter(footers.even) : undefined,
},
});
for (const child of children) {
this.documentWrapper.View.add(child);
2019-07-07 20:23:42 +01:00
}
}
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
}
private addHeaderToDocument(header: HeaderWrapper, type: HeaderFooterReferenceType = HeaderFooterReferenceType.DEFAULT): void {
2018-09-19 23:04:34 +01:00
this.headers.push({ header, type });
this.documentWrapper.Relationships.createRelationship(
header.View.ReferenceId,
2018-09-19 23:04:34 +01:00
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header",
`header${this.headers.length}.xml`,
);
this.contentTypes.addHeader(this.headers.length);
}
private addFooterToDocument(footer: FooterWrapper, type: HeaderFooterReferenceType = HeaderFooterReferenceType.DEFAULT): void {
2018-09-19 23:04:34 +01:00
this.footers.push({ footer, type });
this.documentWrapper.Relationships.createRelationship(
footer.View.ReferenceId,
2018-09-19 23:04:34 +01:00
"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.fileRelationships.createRelationship(
4,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties",
"docProps/custom.xml",
);
this.documentWrapper.Relationships.createRelationship(
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
"styles.xml",
);
this.documentWrapper.Relationships.createRelationship(
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
"numbering.xml",
);
this.documentWrapper.Relationships.createRelationship(
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
"footnotes.xml",
);
this.documentWrapper.Relationships.createRelationship(
this.currentRelationshipId++,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings",
"settings.xml",
);
2018-09-19 23:04:34 +01:00
}
public get Document(): DocumentWrapper {
return this.documentWrapper;
2018-09-19 23:04:34 +01:00
}
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 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 CustomProperties(): CustomProperties {
return this.customProperties;
}
2018-09-19 23:04:34 +01:00
public get AppProperties(): AppProperties {
return this.appProperties;
}
2021-03-01 23:35:52 +00:00
public get FootNotes(): FootnotesWrapper {
return this.footnotesWrapper;
2018-09-19 23:04:34 +01:00
}
2018-09-20 10:11:59 -03:00
public get Settings(): Settings {
return this.settings;
}
2017-12-15 01:16:04 +00:00
}