2021-03-01 03:28:35 +00:00
|
|
|
import { IViewWrapper } from "file/document-wrapper";
|
2018-06-22 23:04:03 +01:00
|
|
|
import { IXmlableObject, XmlComponent } from "file/xml-components";
|
2021-03-01 03:28:35 +00:00
|
|
|
|
2019-10-03 22:12:23 -05:00
|
|
|
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[] = [];
|
2018-06-21 12:03:34 +02:00
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
constructor() {
|
2017-09-19 15:39:14 +01:00
|
|
|
super("w:body");
|
2018-06-21 12:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-06-21 12:03:34 +02:00
|
|
|
*/
|
2019-07-31 08:48:02 +01:00
|
|
|
public addSection(options: SectionPropertiesOptions): void {
|
2019-10-03 22:12:23 -05:00
|
|
|
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));
|
2018-06-21 12:03:34 +02:00
|
|
|
}
|
2019-07-31 08:48:02 +01:00
|
|
|
|
2021-03-01 03:28:35 +00:00
|
|
|
public prepForXml(file?: IViewWrapper): IXmlableObject | undefined {
|
2018-06-21 12:03:34 +02:00
|
|
|
if (this.sections.length === 1) {
|
2019-12-15 22:56:24 +02:00
|
|
|
this.root.splice(0, 1);
|
2019-04-04 17:43:54 +02:00
|
|
|
this.root.push(this.sections.pop() as SectionProperties);
|
2018-06-21 12:03:34 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 21:11:15 +00:00
|
|
|
return super.prepForXml(file);
|
2017-09-19 15:39:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public push(component: XmlComponent): void {
|
|
|
|
this.root.push(component);
|
|
|
|
}
|
2018-06-21 12:03:34 +02:00
|
|
|
|
2018-09-18 05:24:19 -03:00
|
|
|
public getTablesOfContents(): TableOfContents[] {
|
|
|
|
return this.root.filter((child) => child instanceof TableOfContents) as TableOfContents[];
|
|
|
|
}
|
2019-10-03 22:12:23 -05:00
|
|
|
|
|
|
|
private createSectionParagraph(section: SectionProperties): Paragraph {
|
|
|
|
const paragraph = new Paragraph({});
|
|
|
|
const properties = new ParagraphProperties({});
|
2020-07-11 17:01:32 +08:00
|
|
|
properties.push(section);
|
2019-10-03 22:12:23 -05:00
|
|
|
paragraph.addChildElement(properties);
|
|
|
|
return paragraph;
|
|
|
|
}
|
2017-09-19 15:39:14 +01:00
|
|
|
}
|