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

51 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-06-22 23:04:03 +01:00
import { IXmlableObject, XmlComponent } from "file/xml-components";
import { Paragraph, ParagraphProperties, TableOfContents } from "../..";
2018-08-09 23:22:03 +01:00
import { SectionProperties, SectionPropertiesOptions } from "./section-properties/section-properties";
2017-09-19 15:39:14 +01:00
export class Body extends XmlComponent {
2018-08-07 01:38:15 +01:00
private readonly sections: SectionProperties[] = [];
2019-07-31 08:48:02 +01:00
constructor() {
2017-09-19 15:39:14 +01:00
super("w:body");
}
/**
* Adds new section properties.
* Note: Previous section is created in paragraph after the current element, and then new section will be added.
* The spec says:
* - section element should be in the last paragraph of the section
* - last section should be direct child of body
2019-07-31 08:48:02 +01:00
* @param options new section options
*/
2019-07-31 08:48:02 +01:00
public addSection(options: SectionPropertiesOptions): void {
const currentSection = this.sections.pop() as SectionProperties;
this.root.push(this.createSectionParagraph(currentSection));
2019-07-31 08:48:02 +01:00
this.sections.push(new SectionProperties(options));
}
2019-07-31 08:48:02 +01:00
public prepForXml(): IXmlableObject | undefined {
if (this.sections.length === 1) {
this.root.push(this.sections.pop() as SectionProperties);
}
return super.prepForXml();
2017-09-19 15:39:14 +01:00
}
public push(component: XmlComponent): void {
this.root.push(component);
}
public getTablesOfContents(): TableOfContents[] {
return this.root.filter((child) => child instanceof TableOfContents) as TableOfContents[];
}
private createSectionParagraph(section: SectionProperties): Paragraph {
const paragraph = new Paragraph({});
const properties = new ParagraphProperties({});
properties.addChildElement(section);
paragraph.addChildElement(properties);
return paragraph;
}
2017-09-19 15:39:14 +01:00
}