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

303 lines
10 KiB
TypeScript
Raw Normal View History

2018-02-08 00:12:59 +00:00
import { AppProperties } from "./app-properties/app-properties";
2018-02-03 20:56:20 +00:00
import { ContentTypes } from "./content-types/content-types";
2018-02-04 01:43:03 +00:00
import { CoreProperties, IPropertiesOptions } from "./core-properties";
2017-12-15 01:16:04 +00:00
import { Document } from "./document";
2018-08-09 23:22:03 +01:00
import { FooterReferenceType, HeaderReference, HeaderReferenceType, SectionPropertiesOptions } from "./document/body/section-properties";
import { FooterWrapper } from "./footer-wrapper";
2018-06-03 02:11:21 +01:00
import { FootNotes } from "./footnotes";
import { HeaderWrapper } from "./header-wrapper";
2018-08-09 01:55:50 +01:00
import { Image, Media } from "./media";
2017-12-20 00:01:23 +00:00
import { Numbering } from "./numbering";
2018-09-21 11:16:14 -03:00
import { Bookmark, Hyperlink, Paragraph } from "./paragraph";
2018-01-29 01:54:10 +00:00
import { Relationships } from "./relationships";
2018-09-20 10:11:59 -03:00
import { Settings } from "./settings";
2017-12-20 00:52:41 +00:00
import { Styles } from "./styles";
import { ExternalStylesFactory } from "./styles/external-styles-factory";
2018-05-06 02:58:16 +01:00
import { DefaultStylesFactory } from "./styles/factory";
2017-12-20 00:01:23 +00:00
import { Table } from "./table";
2018-09-21 11:16:14 -03:00
import { TableOfContents } from "./table-of-contents";
2017-12-15 01:16:04 +00:00
export class File {
2018-01-29 01:54:10 +00:00
private readonly document: Document;
private styles: Styles;
2018-02-04 01:43:03 +00:00
private readonly coreProperties: CoreProperties;
2018-01-29 01:54:10 +00:00
private readonly numbering: Numbering;
private readonly media: Media;
2018-02-04 00:58:34 +00:00
private readonly docRelationships: Relationships;
private readonly fileRelationships: Relationships;
private readonly headerWrapper: HeaderWrapper[] = [];
private readonly footerWrapper: FooterWrapper[] = [];
2018-06-03 02:11:21 +01:00
private readonly footNotes: FootNotes;
2018-09-20 10:11:59 -03:00
private readonly settings: Settings;
2018-05-12 20:04:54 -04:00
2018-02-03 20:56:20 +00:00
private readonly contentTypes: ContentTypes;
2018-02-08 00:12:59 +00:00
private readonly appProperties: AppProperties;
2017-12-15 01:16:04 +00:00
private currentRelationshipId: number = 1;
2017-12-15 02:15:44 +00:00
constructor(options?: IPropertiesOptions, sectionPropertiesOptions?: SectionPropertiesOptions) {
2017-12-15 02:15:44 +00:00
if (!options) {
options = {
creator: "Un-named",
revision: "1",
lastModifiedBy: "Un-named",
};
}
if (options.externalStyles) {
const stylesFactory = new ExternalStylesFactory();
this.styles = stylesFactory.newInstance(options.externalStyles);
} else {
const stylesFactory = new DefaultStylesFactory();
this.styles = stylesFactory.newInstance();
}
2018-02-04 01:43:03 +00:00
this.coreProperties = new CoreProperties(options);
2017-12-15 01:16:04 +00:00
this.numbering = new Numbering();
2018-02-04 00:58:34 +00:00
this.docRelationships = new Relationships();
2018-02-05 01:44:28 +00:00
this.docRelationships.createRelationship(
this.currentRelationshipId++,
2018-02-05 01:44:28 +00:00
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
"styles.xml",
);
this.docRelationships.createRelationship(
this.currentRelationshipId++,
2018-02-05 01:44:28 +00:00
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
"numbering.xml",
);
this.contentTypes = new ContentTypes();
2018-06-11 00:48:50 +01:00
this.docRelationships.createRelationship(
this.currentRelationshipId++,
2018-06-11 00:48:50 +01:00
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
"footnotes.xml",
);
2018-01-29 01:54:10 +00:00
this.media = new Media();
2018-05-12 20:04:54 -04:00
const header = this.createHeader();
const footer = this.createFooter();
2018-05-12 20:04:54 -04:00
2018-02-04 00:58:34 +00:00
this.fileRelationships = new Relationships();
2018-02-05 01:44:28 +00: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",
);
2018-02-08 00:12:59 +00:00
this.appProperties = new AppProperties();
2018-06-03 02:11:21 +01:00
this.footNotes = new FootNotes();
if (!sectionPropertiesOptions) {
sectionPropertiesOptions = {
footerType: FooterReferenceType.DEFAULT,
headerType: HeaderReferenceType.DEFAULT,
2018-08-07 01:25:28 +01:00
headerId: header.Header.ReferenceId,
footerId: footer.Footer.ReferenceId,
};
} else {
2018-08-07 01:25:28 +01:00
sectionPropertiesOptions.headerId = header.Header.ReferenceId;
sectionPropertiesOptions.footerId = footer.Footer.ReferenceId;
}
this.document = new Document(sectionPropertiesOptions);
2018-09-20 10:11:59 -03:00
this.settings = new Settings();
}
public addTableOfContents(toc: TableOfContents): void {
this.document.addTableOfContents(toc);
2017-12-15 01:16:04 +00:00
}
2017-12-15 02:15:44 +00:00
public addParagraph(paragraph: Paragraph): void {
this.document.addParagraph(paragraph);
}
public createParagraph(text?: string): Paragraph {
2018-01-29 02:56:35 +00:00
return this.document.createParagraph(text);
2017-12-15 02:15:44 +00:00
}
public addTable(table: Table): void {
return this.document.addTable(table);
}
public createTable(rows: number, cols: number): Table {
return this.document.createTable(rows, cols);
}
2018-08-07 01:25:28 +01:00
public addImage(image: Image): File {
2018-08-09 01:55:50 +01:00
this.document.addParagraph(image.Paragraph);
return this;
}
2018-08-15 22:20:43 +01:00
public createImage(buffer: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): Image {
2018-08-12 23:07:31 +01:00
const image = Media.addImage(this, buffer, width, height);
2018-08-09 01:55:50 +01:00
this.document.addParagraph(image.Paragraph);
return image;
}
2018-05-06 22:24:16 -05:00
public createHyperlink(link: string, text?: string): Hyperlink {
text = text === undefined ? link : text;
const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount);
this.docRelationships.createRelationship(
hyperlink.linkId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
link,
"External",
);
return hyperlink;
}
2018-07-24 15:56:13 -04:00
public createInternalHyperLink(anchor: string, text?: string): Hyperlink {
text = text === undefined ? anchor : text;
const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount, anchor);
2018-07-24 16:56:27 -04:00
// NOTE: unlike File#createHyperlink(), since the link is to an internal bookmark
// we don't need to create a new relationship.
2018-07-24 15:56:13 -04:00
return hyperlink;
}
public createBookmark(name: string, text?: string): Bookmark {
text = text === undefined ? name : text;
const bookmark = new Bookmark(name, text, this.docRelationships.RelationshipCount);
return bookmark;
}
public addSection(sectionPropertiesOptions: SectionPropertiesOptions): void {
this.document.Body.addSection(sectionPropertiesOptions);
}
2018-06-25 19:49:46 +01:00
public createFootnote(paragraph: Paragraph): void {
this.footNotes.createFootNote(paragraph);
}
public createHeader(): HeaderWrapper {
const header = new HeaderWrapper(this.media, this.currentRelationshipId++);
this.headerWrapper.push(header);
this.docRelationships.createRelationship(
2018-08-07 01:25:28 +01:00
header.Header.ReferenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header",
`header${this.headerWrapper.length}.xml`,
);
this.contentTypes.addHeader(this.headerWrapper.length);
return header;
}
public createFooter(): FooterWrapper {
const footer = new FooterWrapper(this.media, this.currentRelationshipId++);
this.footerWrapper.push(footer);
this.docRelationships.createRelationship(
2018-08-07 01:25:28 +01:00
footer.Footer.ReferenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer",
`footer${this.footerWrapper.length}.xml`,
);
this.contentTypes.addFooter(this.footerWrapper.length);
return footer;
}
public createFirstPageHeader(): HeaderWrapper {
const headerWrapper = this.createHeader();
2018-06-26 00:10:08 +01:00
this.document.Body.DefaultSection.addChildElement(
new HeaderReference({
headerType: HeaderReferenceType.FIRST,
2018-08-07 01:25:28 +01:00
headerId: headerWrapper.Header.ReferenceId,
2018-06-26 00:10:08 +01:00
}),
);
return headerWrapper;
}
2017-12-15 02:15:44 +00:00
public get Document(): Document {
return this.document;
}
public get Styles(): Styles {
return this.styles;
}
public set Styles(styles: Styles) {
this.styles = styles;
}
2018-02-04 01:43:03 +00:00
public get CoreProperties(): CoreProperties {
return this.coreProperties;
2017-12-15 02:15:44 +00:00
}
public get Numbering(): Numbering {
return this.numbering;
}
public get Media(): Media {
return this.media;
}
2018-01-10 00:29:17 +00:00
2018-02-04 00:58:34 +00:00
public get DocumentRelationships(): Relationships {
return this.docRelationships;
}
public get FileRelationships(): Relationships {
return this.fileRelationships;
2018-01-10 00:29:17 +00:00
}
2018-01-29 01:54:10 +00:00
public get Header(): HeaderWrapper {
return this.headerWrapper[0];
}
public get Headers(): HeaderWrapper[] {
return this.headerWrapper;
2018-01-29 01:54:10 +00:00
}
2018-01-29 21:53:22 +00:00
public HeaderByRefNumber(refId: number): HeaderWrapper {
2018-08-07 01:25:28 +01:00
const entry = this.headerWrapper.find((h) => h.Header.ReferenceId === refId);
if (entry) {
return entry;
}
throw new Error(`There is no header with given reference id ${refId}`);
2018-05-12 20:04:54 -04:00
}
public get Footer(): FooterWrapper {
return this.footerWrapper[0];
}
public get Footers(): FooterWrapper[] {
return this.footerWrapper;
2018-01-29 21:53:22 +00:00
}
2018-02-03 20:56:20 +00:00
public FooterByRefNumber(refId: number): FooterWrapper {
2018-08-07 01:25:28 +01:00
const entry = this.footerWrapper.find((h) => h.Footer.ReferenceId === refId);
if (entry) {
return entry;
}
throw new Error(`There is no footer with given reference id ${refId}`);
}
2018-02-03 20:56:20 +00:00
public get ContentTypes(): ContentTypes {
return this.contentTypes;
}
2018-02-08 00:12:59 +00:00
public get AppProperties(): AppProperties {
return this.appProperties;
}
2018-06-03 02:11:21 +01:00
public get FootNotes(): FootNotes {
return this.footNotes;
}
2018-09-20 10:11:59 -03:00
public get Settings(): Settings {
return this.settings;
}
2018-09-21 11:16:14 -03:00
public verifyUpdateFields(): void {
if (this.document.getTablesOfContents().length) {
2018-09-20 10:11:59 -03:00
this.settings.addUpdateFields();
}
}
2017-12-15 01:16:04 +00:00
}