2022-06-26 23:26:42 +01:00
|
|
|
import { Paragraph, ParagraphProperties } from "@file/paragraph";
|
|
|
|
import { IContext, IXmlableObject, XmlComponent } from "@file/xml-components";
|
2021-03-01 03:28:35 +00:00
|
|
|
|
2021-03-19 20:53:56 +00:00
|
|
|
import { ISectionPropertiesOptions, SectionProperties } from "./section-properties/section-properties";
|
2017-09-19 15:39:14 +01:00
|
|
|
|
|
|
|
export class Body extends XmlComponent {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
2018-08-07 01:38:15 +01:00
|
|
|
private readonly sections: SectionProperties[] = [];
|
2018-06-21 12:03:34 +02:00
|
|
|
|
2022-08-31 07:52:27 +01:00
|
|
|
public 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
|
2022-09-15 20:00:50 +01:00
|
|
|
*
|
2019-07-31 08:48:02 +01:00
|
|
|
* @param options new section options
|
2018-06-21 12:03:34 +02:00
|
|
|
*/
|
2021-03-19 20:53:56 +00:00
|
|
|
public addSection(options: ISectionPropertiesOptions): 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-11 01:06:55 +00:00
|
|
|
public prepForXml(context: IContext): 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
|
|
|
}
|
|
|
|
|
2021-03-11 01:06:55 +00:00
|
|
|
return super.prepForXml(context);
|
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
|
|
|
|
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
|
|
|
}
|