Mandatory Sections
This commit is contained in:
@ -11,35 +11,7 @@ describe("Body", () => {
|
||||
body = new Body();
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create default section", () => {
|
||||
const formatted = new Formatter().format(body)["w:body"][0];
|
||||
expect(formatted)
|
||||
.to.have.property("w:sectPr")
|
||||
.and.to.be.an.instanceof(Array);
|
||||
expect(formatted["w:sectPr"]).to.have.length(4);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#addSection", () => {
|
||||
it("should add section with options", () => {
|
||||
body.addSection({
|
||||
width: 10000,
|
||||
height: 10000,
|
||||
});
|
||||
|
||||
const formatted = new Formatter().format(body)["w:body"];
|
||||
expect(formatted).to.be.an.instanceof(Array);
|
||||
const defaultSectionPr = formatted[0]["w:p"][0]["w:pPr"][0]["w:sectPr"];
|
||||
|
||||
// check that this is the default section and added first in paragraph
|
||||
expect(defaultSectionPr[0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
|
||||
|
||||
// check for new section (since it's the last one, it's direct child of body)
|
||||
const newSection = formatted[1]["w:sectPr"];
|
||||
expect(newSection[0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 10000, "w:w": 10000, "w:orient": "portrait" } } });
|
||||
});
|
||||
|
||||
it("should add section with default parameters", () => {
|
||||
body.addSection({
|
||||
width: 10000,
|
||||
@ -51,33 +23,7 @@ describe("Body", () => {
|
||||
expect(tree).to.deep.equal({
|
||||
"w:body": [
|
||||
{
|
||||
"w:p": [
|
||||
{
|
||||
"w:pPr": [
|
||||
{
|
||||
"w:sectPr": [
|
||||
{ "w:pgSz": { _attr: { "w:w": 11906, "w:h": 16838, "w:orient": "portrait" } } },
|
||||
{
|
||||
"w:pgMar": {
|
||||
_attr: {
|
||||
"w:top": 1440,
|
||||
"w:right": 1440,
|
||||
"w:bottom": 1440,
|
||||
"w:left": 1440,
|
||||
"w:header": 708,
|
||||
"w:footer": 708,
|
||||
"w:gutter": 0,
|
||||
"w:mirrorMargins": false,
|
||||
},
|
||||
},
|
||||
},
|
||||
{ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } },
|
||||
{ "w:docGrid": { _attr: { "w:linePitch": 360 } } },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
"w:p": {},
|
||||
},
|
||||
{
|
||||
"w:sectPr": [
|
||||
@ -104,21 +50,4 @@ describe("Body", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#getParagraphs", () => {
|
||||
it("should get no paragraphs", () => {
|
||||
const paragraphs = body.getParagraphs();
|
||||
|
||||
expect(paragraphs).to.be.an.instanceof(Array);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#DefaultSection", () => {
|
||||
it("should get section", () => {
|
||||
const section = body.DefaultSection;
|
||||
|
||||
const tree = new Formatter().format(section);
|
||||
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -3,15 +3,10 @@ import { Paragraph, ParagraphProperties, TableOfContents } from "../..";
|
||||
import { SectionProperties, SectionPropertiesOptions } from "./section-properties/section-properties";
|
||||
|
||||
export class Body extends XmlComponent {
|
||||
private readonly defaultSection: SectionProperties;
|
||||
|
||||
private readonly sections: SectionProperties[] = [];
|
||||
|
||||
constructor(sectionPropertiesOptions?: SectionPropertiesOptions) {
|
||||
constructor() {
|
||||
super("w:body");
|
||||
|
||||
this.defaultSection = new SectionProperties(sectionPropertiesOptions);
|
||||
this.sections.push(this.defaultSection);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -20,21 +15,15 @@ export class Body extends XmlComponent {
|
||||
* The spec says:
|
||||
* - section element should be in the last paragraph of the section
|
||||
* - last section should be direct child of body
|
||||
* @param section new section
|
||||
* @param options new section options
|
||||
*/
|
||||
public addSection(section: SectionPropertiesOptions | SectionProperties): void {
|
||||
public addSection(options: SectionPropertiesOptions): void {
|
||||
const currentSection = this.sections.pop() as SectionProperties;
|
||||
this.root.push(this.createSectionParagraph(currentSection));
|
||||
if (section instanceof SectionProperties) {
|
||||
this.sections.push(section);
|
||||
} else {
|
||||
const params = {
|
||||
...this.defaultSection.Options,
|
||||
...section,
|
||||
};
|
||||
this.sections.push(new SectionProperties(params));
|
||||
}
|
||||
|
||||
this.sections.push(new SectionProperties(options));
|
||||
}
|
||||
|
||||
public prepForXml(): IXmlableObject | undefined {
|
||||
if (this.sections.length === 1) {
|
||||
this.root.push(this.sections.pop() as SectionProperties);
|
||||
@ -47,18 +36,10 @@ export class Body extends XmlComponent {
|
||||
this.root.push(component);
|
||||
}
|
||||
|
||||
public get DefaultSection(): SectionProperties {
|
||||
return this.defaultSection;
|
||||
}
|
||||
|
||||
public getTablesOfContents(): TableOfContents[] {
|
||||
return this.root.filter((child) => child instanceof TableOfContents) as TableOfContents[];
|
||||
}
|
||||
|
||||
public getParagraphs(): Paragraph[] {
|
||||
return this.root.filter((child) => child instanceof Paragraph) as Paragraph[];
|
||||
}
|
||||
|
||||
private createSectionParagraph(section: SectionProperties): Paragraph {
|
||||
const paragraph = new Paragraph({});
|
||||
const properties = new ParagraphProperties({});
|
||||
|
@ -1,13 +1,13 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { HeaderReferenceAttributes, HeaderReferenceType } from "./header-reference-attributes";
|
||||
|
||||
export interface IHeaderOptions {
|
||||
export interface IHeaderReferenceOptions {
|
||||
readonly headerType?: HeaderReferenceType;
|
||||
readonly headerId?: number;
|
||||
}
|
||||
|
||||
export class HeaderReference extends XmlComponent {
|
||||
constructor(options: IHeaderOptions) {
|
||||
constructor(options: IHeaderReferenceOptions) {
|
||||
super("w:headerReference");
|
||||
this.root.push(
|
||||
new HeaderReferenceAttributes({
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { assert, expect } from "chai";
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
@ -13,19 +13,34 @@ describe("Document", () => {
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create valid JSON", () => {
|
||||
const stringifiedJson = JSON.stringify(document);
|
||||
const tree = new Formatter().format(document);
|
||||
|
||||
try {
|
||||
JSON.parse(stringifiedJson);
|
||||
} catch (e) {
|
||||
assert.isTrue(false);
|
||||
}
|
||||
assert.isTrue(true);
|
||||
});
|
||||
|
||||
it("should create default section", () => {
|
||||
const body = new Formatter().format(document)["w:document"][1]["w:body"];
|
||||
expect(body[0]).to.have.property("w:sectPr");
|
||||
expect(tree).to.deep.equal({
|
||||
"w:document": [
|
||||
{
|
||||
_attr: {
|
||||
"xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
|
||||
"xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
|
||||
"xmlns:o": "urn:schemas-microsoft-com:office:office",
|
||||
"xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
||||
"xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
|
||||
"xmlns:v": "urn:schemas-microsoft-com:vml",
|
||||
"xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
|
||||
"xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
|
||||
"xmlns:w10": "urn:schemas-microsoft-com:office:word",
|
||||
"xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
|
||||
"xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml",
|
||||
"xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml",
|
||||
"xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
|
||||
"xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
|
||||
"xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml",
|
||||
"xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
|
||||
"mc:Ignorable": "w14 w15 wp14",
|
||||
},
|
||||
},
|
||||
{ "w:body": {} },
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -4,13 +4,12 @@ import { Paragraph } from "../paragraph";
|
||||
import { Table } from "../table";
|
||||
import { TableOfContents } from "../table-of-contents";
|
||||
import { Body } from "./body";
|
||||
import { SectionPropertiesOptions } from "./body/section-properties";
|
||||
import { DocumentAttributes } from "./document-attributes";
|
||||
|
||||
export class Document extends XmlComponent {
|
||||
private readonly body: Body;
|
||||
|
||||
constructor(sectionPropertiesOptions?: SectionPropertiesOptions) {
|
||||
constructor() {
|
||||
super("w:document");
|
||||
this.root.push(
|
||||
new DocumentAttributes({
|
||||
@ -33,17 +32,12 @@ export class Document extends XmlComponent {
|
||||
Ignorable: "w14 w15 wp14",
|
||||
}),
|
||||
);
|
||||
this.body = new Body(sectionPropertiesOptions);
|
||||
this.body = new Body();
|
||||
this.root.push(this.body);
|
||||
}
|
||||
|
||||
public add(paragraph: Paragraph | Table): Document {
|
||||
this.body.push(paragraph);
|
||||
return this;
|
||||
}
|
||||
|
||||
public addTableOfContents(toc: TableOfContents): Document {
|
||||
this.body.push(toc);
|
||||
public add(item: Paragraph | Table | TableOfContents): Document {
|
||||
this.body.push(item);
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -54,8 +48,4 @@ export class Document extends XmlComponent {
|
||||
public getTablesOfContents(): TableOfContents[] {
|
||||
return this.body.getTablesOfContents();
|
||||
}
|
||||
|
||||
public getParagraphs(): Paragraph[] {
|
||||
return this.body.getParagraphs();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user