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";
|
2021-05-25 03:41:12 +03:00
|
|
|
import { HeaderFooterReferenceType, ISectionPropertiesOptions } from "./document/body/section-properties";
|
2024-10-21 03:57:15 +01:00
|
|
|
import { DocumentWrapper } from "./document-wrapper";
|
|
|
|
import { FileChild } from "./file-child";
|
|
|
|
import { FontWrapper } from "./fonts/font-wrapper";
|
2018-09-19 23:04:34 +01:00
|
|
|
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";
|
2019-06-25 01:52:02 +01:00
|
|
|
import { Media } from "./media";
|
2018-09-19 23:04:34 +01:00
|
|
|
import { Numbering } from "./numbering";
|
2022-06-19 00:31:36 +01:00
|
|
|
import { Comments } from "./paragraph/run/comment-run";
|
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";
|
|
|
|
|
2024-10-21 03:57:15 +01:00
|
|
|
export type 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;
|
2023-01-23 14:14:05 +00:00
|
|
|
readonly children: readonly FileChild[];
|
2024-10-21 03:57:15 +01:00
|
|
|
};
|
2019-07-07 03:54:29 +01:00
|
|
|
|
2018-09-19 23:04:34 +01:00
|
|
|
export class File {
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
2018-09-19 23:04:34 +01:00
|
|
|
private currentRelationshipId: number = 1;
|
|
|
|
|
2021-02-28 16:04:21 +00:00
|
|
|
private readonly documentWrapper: DocumentWrapper;
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
2018-09-19 23:04:34 +01:00
|
|
|
private readonly headers: IDocumentHeader[] = [];
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
2018-09-19 23:04:34 +01:00
|
|
|
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;
|
2020-08-03 14:58:30 +12:00
|
|
|
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;
|
2022-03-03 09:59:09 +08:00
|
|
|
private readonly comments: Comments;
|
2023-12-30 02:23:54 +00:00
|
|
|
private readonly fontWrapper: FontWrapper;
|
2018-09-19 00:37:11 +01:00
|
|
|
|
2023-03-17 00:20:55 +00:00
|
|
|
public constructor(options: IPropertiesOptions) {
|
2021-03-19 20:53:56 +00:00
|
|
|
this.coreProperties = new CoreProperties({
|
|
|
|
...options,
|
|
|
|
creator: options.creator ?? "Un-named",
|
2021-10-08 15:52:12 -06:00
|
|
|
revision: options.revision ?? 1,
|
2021-03-19 20:53:56 +00:00
|
|
|
lastModifiedBy: options.lastModifiedBy ?? "Un-named",
|
|
|
|
});
|
|
|
|
|
2022-08-31 08:59:27 +01:00
|
|
|
this.numbering = new Numbering(options.numbering ? options.numbering : { config: [] });
|
2021-03-19 20:53:56 +00:00
|
|
|
|
2022-07-12 16:57:25 +01:00
|
|
|
this.comments = new Comments(options.comments ?? { children: [] });
|
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();
|
2022-12-09 21:07:08 +00:00
|
|
|
this.documentWrapper = new DocumentWrapper({ background: options.background });
|
2021-02-27 01:40:55 +00:00
|
|
|
this.settings = new Settings({
|
2022-10-29 15:10:02 +01:00
|
|
|
compatibilityModeVersion: options.compatabilityModeVersion,
|
|
|
|
compatibility: options.compatibility,
|
2021-03-16 00:57:27 +00:00
|
|
|
evenAndOddHeaders: options.evenAndOddHeaderAndFooters ? true : false,
|
2021-05-26 08:36:05 +03:00
|
|
|
trackRevisions: options.features?.trackRevisions,
|
|
|
|
updateFields: options.features?.updateFields,
|
2023-12-24 03:37:36 +00:00
|
|
|
defaultTabStop: options.defaultTabStop,
|
2021-02-27 01:40:55 +00:00
|
|
|
});
|
2018-09-19 23:04:34 +01:00
|
|
|
|
2023-03-17 00:20:55 +00:00
|
|
|
this.media = new Media();
|
2019-01-04 00:11:50 +00:00
|
|
|
|
2023-12-31 18:54:35 +00:00
|
|
|
if (options.externalStyles !== undefined) {
|
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();
|
2020-11-30 10:25:58 -06:00
|
|
|
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
|
|
|
|
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) {
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line guard-for-in
|
2020-01-01 20:22:42 +00:00
|
|
|
for (const key in options.footnotes) {
|
2021-03-08 03:40:11 +00:00
|
|
|
this.footnotesWrapper.View.createFootNote(parseFloat(key), options.footnotes[key].children);
|
2019-12-03 23:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-30 02:23:54 +00:00
|
|
|
|
|
|
|
this.fontWrapper = new FontWrapper(options.fonts ?? []);
|
2021-03-19 20:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private addSection({ headers = {}, footers = {}, children, properties }: ISectionOptions): void {
|
2021-02-28 16:04:21 +00:00
|
|
|
this.documentWrapper.View.Body.addSection({
|
2019-07-31 08:48:02 +01:00
|
|
|
...properties,
|
2021-03-19 20:53:56 +00:00
|
|
|
headerWrapperGroup: {
|
2021-03-16 01:23:23 +00:00
|
|
|
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: {
|
2021-03-16 01:23:23 +00:00
|
|
|
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) {
|
2021-02-28 16:04:21 +00:00
|
|
|
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 {
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2019-07-31 08:48:02 +01:00
|
|
|
const wrapper = new HeaderWrapper(this.media, this.currentRelationshipId++);
|
2019-01-10 02:36:42 +00:00
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
for (const child of header.options.children) {
|
|
|
|
wrapper.add(child);
|
|
|
|
}
|
2019-01-10 02:36:42 +00:00
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
this.addHeaderToDocument(wrapper);
|
|
|
|
return wrapper;
|
2019-01-10 02:36:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
private createFooter(footer: Footer): FooterWrapper {
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2019-07-31 08:48:02 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
private addHeaderToDocument(
|
|
|
|
header: HeaderWrapper,
|
|
|
|
type: (typeof HeaderFooterReferenceType)[keyof typeof HeaderFooterReferenceType] = HeaderFooterReferenceType.DEFAULT,
|
|
|
|
): void {
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2018-09-19 23:04:34 +01:00
|
|
|
this.headers.push({ header, type });
|
2021-02-28 16:04:21 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
private addFooterToDocument(
|
|
|
|
footer: FooterWrapper,
|
|
|
|
type: (typeof HeaderFooterReferenceType)[keyof typeof HeaderFooterReferenceType] = HeaderFooterReferenceType.DEFAULT,
|
|
|
|
): void {
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2018-09-19 23:04:34 +01:00
|
|
|
this.footers.push({ footer, type });
|
2021-02-28 16:04:21 +00:00
|
|
|
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",
|
|
|
|
);
|
2020-08-03 14:58:30 +12:00
|
|
|
this.fileRelationships.createRelationship(
|
|
|
|
4,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties",
|
|
|
|
"docProps/custom.xml",
|
|
|
|
);
|
2018-09-26 12:03:52 +03:00
|
|
|
|
2021-02-28 16:04:21 +00:00
|
|
|
this.documentWrapper.Relationships.createRelationship(
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2018-09-26 12:03:52 +03:00
|
|
|
this.currentRelationshipId++,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
|
|
|
|
"styles.xml",
|
|
|
|
);
|
2021-02-28 16:04:21 +00:00
|
|
|
this.documentWrapper.Relationships.createRelationship(
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2018-09-26 12:03:52 +03:00
|
|
|
this.currentRelationshipId++,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
|
|
|
|
"numbering.xml",
|
|
|
|
);
|
2021-02-28 16:04:21 +00:00
|
|
|
this.documentWrapper.Relationships.createRelationship(
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2018-09-26 12:03:52 +03:00
|
|
|
this.currentRelationshipId++,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
|
|
|
|
"footnotes.xml",
|
|
|
|
);
|
2021-02-28 16:04:21 +00:00
|
|
|
this.documentWrapper.Relationships.createRelationship(
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2019-03-01 16:12:15 +01:00
|
|
|
this.currentRelationshipId++,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings",
|
|
|
|
"settings.xml",
|
|
|
|
);
|
2022-03-03 09:59:09 +08:00
|
|
|
this.documentWrapper.Relationships.createRelationship(
|
2022-09-19 20:48:50 +01:00
|
|
|
// eslint-disable-next-line functional/immutable-data
|
2022-03-03 09:59:09 +08:00
|
|
|
this.currentRelationshipId++,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
|
|
|
|
"comments.xml",
|
|
|
|
);
|
2018-09-19 23:04:34 +01:00
|
|
|
}
|
|
|
|
|
2021-02-28 16:04:21 +00: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;
|
|
|
|
}
|
|
|
|
|
2022-09-15 20:00:50 +01:00
|
|
|
public get Headers(): readonly HeaderWrapper[] {
|
2018-09-19 23:04:34 +01:00
|
|
|
return this.headers.map((item) => item.header);
|
|
|
|
}
|
|
|
|
|
2022-09-15 20:00:50 +01:00
|
|
|
public get Footers(): readonly FooterWrapper[] {
|
2018-09-19 23:04:34 +01:00
|
|
|
return this.footers.map((item) => item.footer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public get ContentTypes(): ContentTypes {
|
|
|
|
return this.contentTypes;
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:58:30 +12:00
|
|
|
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-10 10:01:26 -03:00
|
|
|
|
2018-09-20 10:11:59 -03:00
|
|
|
public get Settings(): Settings {
|
|
|
|
return this.settings;
|
|
|
|
}
|
2022-03-03 09:59:09 +08:00
|
|
|
|
|
|
|
public get Comments(): Comments {
|
|
|
|
return this.comments;
|
|
|
|
}
|
2023-12-30 02:23:54 +00:00
|
|
|
|
|
|
|
public get FontTable(): FontWrapper {
|
|
|
|
return this.fontWrapper;
|
|
|
|
}
|
2017-12-15 01:16:04 +00:00
|
|
|
}
|