#339 Add Section Type
This commit is contained in:
93
demo/58-section-types.ts
Normal file
93
demo/58-section-types.ts
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
// Usage of different Section Types
|
||||||
|
// Import from 'docx' rather than '../build' if you install from npm
|
||||||
|
import * as fs from "fs";
|
||||||
|
import { Document, Packer, Paragraph, TextRun, SectionType } from "../build";
|
||||||
|
|
||||||
|
const doc = new Document();
|
||||||
|
|
||||||
|
doc.addSection({
|
||||||
|
properties: {},
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
children: [
|
||||||
|
new TextRun("Hello World"),
|
||||||
|
new TextRun({
|
||||||
|
text: "Foo Bar",
|
||||||
|
bold: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
doc.addSection({
|
||||||
|
properties: {
|
||||||
|
type: SectionType.CONTINUOUS,
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
children: [
|
||||||
|
new TextRun("Hello World"),
|
||||||
|
new TextRun({
|
||||||
|
text: "Foo Bar",
|
||||||
|
bold: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
doc.addSection({
|
||||||
|
properties: {
|
||||||
|
type: SectionType.ODD_PAGE,
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
children: [
|
||||||
|
new TextRun("Hello World"),
|
||||||
|
new TextRun({
|
||||||
|
text: "Foo Bar",
|
||||||
|
bold: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
doc.addSection({
|
||||||
|
properties: {
|
||||||
|
type: SectionType.EVEN_PAGE,
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
children: [
|
||||||
|
new TextRun("Hello World"),
|
||||||
|
new TextRun({
|
||||||
|
text: "Foo Bar",
|
||||||
|
bold: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
doc.addSection({
|
||||||
|
properties: {
|
||||||
|
type: SectionType.NEXT_PAGE,
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
children: [
|
||||||
|
new TextRun("Hello World"),
|
||||||
|
new TextRun({
|
||||||
|
text: "Foo Bar",
|
||||||
|
bold: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
Packer.toBuffer(doc).then((buffer) => {
|
||||||
|
fs.writeFileSync("My Document.docx", buffer);
|
||||||
|
});
|
@ -19,3 +19,30 @@ doc.addSection({
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Properties
|
||||||
|
|
||||||
|
You can specify additional properties to the section, by providing a `properties` attribute.
|
||||||
|
|
||||||
|
### Section Type
|
||||||
|
|
||||||
|
Setting the section type determines how the contents of the section will be placed relative to the previous section. E.g., There are five different types:
|
||||||
|
|
||||||
|
- `CONTINUOUS`
|
||||||
|
- `EVEN_PAGE`
|
||||||
|
- `NEXT_COLUMN`
|
||||||
|
- `NEXT_PAGE`
|
||||||
|
- `ODD_PAGE`
|
||||||
|
|
||||||
|
```ts
|
||||||
|
doc.addSection({
|
||||||
|
properties: {
|
||||||
|
type: SectionType.CONTINUOUS,
|
||||||
|
}
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
children: [new TextRun("Hello World")],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
```
|
||||||
|
@ -6,3 +6,4 @@ export * from "./page-number";
|
|||||||
export * from "./page-border";
|
export * from "./page-border";
|
||||||
export * from "./line-number";
|
export * from "./line-number";
|
||||||
export * from "./vertical-align";
|
export * from "./vertical-align";
|
||||||
|
export * from "./type";
|
||||||
|
@ -9,6 +9,7 @@ import { Media } from "file/media";
|
|||||||
import { PageBorderOffsetFrom } from "./page-border";
|
import { PageBorderOffsetFrom } from "./page-border";
|
||||||
import { PageNumberFormat } from "./page-number";
|
import { PageNumberFormat } from "./page-number";
|
||||||
import { SectionProperties } from "./section-properties";
|
import { SectionProperties } from "./section-properties";
|
||||||
|
import { SectionType } from "./type/section-type-attributes";
|
||||||
import { SectionVerticalAlignValue } from "./vertical-align";
|
import { SectionVerticalAlignValue } from "./vertical-align";
|
||||||
|
|
||||||
describe("SectionProperties", () => {
|
describe("SectionProperties", () => {
|
||||||
@ -199,5 +200,17 @@ describe("SectionProperties", () => {
|
|||||||
const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined);
|
const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined);
|
||||||
expect(pgNumType).to.equal(undefined);
|
expect(pgNumType).to.equal(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should create section properties with section type", () => {
|
||||||
|
const properties = new SectionProperties({
|
||||||
|
type: SectionType.CONTINUOUS,
|
||||||
|
});
|
||||||
|
const tree = new Formatter().format(properties);
|
||||||
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
||||||
|
const type = tree["w:sectPr"].find((item) => item["w:type"] !== undefined);
|
||||||
|
expect(type).to.deep.equal({
|
||||||
|
"w:type": { _attr: { "w:val": "continuous" } },
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -18,6 +18,8 @@ import { IPageMarginAttributes } from "./page-margin/page-margin-attributes";
|
|||||||
import { IPageNumberTypeAttributes, PageNumberType } from "./page-number";
|
import { IPageNumberTypeAttributes, PageNumberType } from "./page-number";
|
||||||
import { PageSize } from "./page-size/page-size";
|
import { PageSize } from "./page-size/page-size";
|
||||||
import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes";
|
import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes";
|
||||||
|
import { Type } from "./type/section-type";
|
||||||
|
import { SectionType } from "./type/section-type-attributes";
|
||||||
import { TitlePage } from "./title-page/title-page";
|
import { TitlePage } from "./title-page/title-page";
|
||||||
import { ISectionVerticalAlignAttributes, SectionVerticalAlign } from "./vertical-align";
|
import { ISectionVerticalAlignAttributes, SectionVerticalAlign } from "./vertical-align";
|
||||||
|
|
||||||
@ -53,6 +55,7 @@ export type SectionPropertiesOptions = IPageSizeAttributes &
|
|||||||
readonly space?: number;
|
readonly space?: number;
|
||||||
readonly count?: number;
|
readonly count?: number;
|
||||||
};
|
};
|
||||||
|
readonly type?: SectionType;
|
||||||
};
|
};
|
||||||
// Need to decouple this from the attributes
|
// Need to decouple this from the attributes
|
||||||
|
|
||||||
@ -91,6 +94,7 @@ export class SectionProperties extends XmlComponent {
|
|||||||
pageBorderLeft,
|
pageBorderLeft,
|
||||||
titlePage = false,
|
titlePage = false,
|
||||||
verticalAlign,
|
verticalAlign,
|
||||||
|
type,
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
this.options = options;
|
this.options = options;
|
||||||
@ -129,6 +133,10 @@ export class SectionProperties extends XmlComponent {
|
|||||||
if (verticalAlign) {
|
if (verticalAlign) {
|
||||||
this.root.push(new SectionVerticalAlign(verticalAlign));
|
this.root.push(new SectionVerticalAlign(verticalAlign));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type) {
|
||||||
|
this.root.push(new Type(type));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private addHeaders(headers?: IHeaderFooterGroup<HeaderWrapper>): void {
|
private addHeaders(headers?: IHeaderFooterGroup<HeaderWrapper>): void {
|
||||||
|
2
src/file/document/body/section-properties/type/index.ts
Normal file
2
src/file/document/body/section-properties/type/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export * from "./section-type";
|
||||||
|
export * from "./section-type-attributes";
|
@ -0,0 +1,17 @@
|
|||||||
|
import { XmlAttributeComponent } from "file/xml-components";
|
||||||
|
|
||||||
|
export enum SectionType {
|
||||||
|
CONTINUOUS = "continuous",
|
||||||
|
EVEN_PAGE = "evenPage",
|
||||||
|
NEXT_COLUMN = "nextColumn",
|
||||||
|
NEXT_PAGE = "nextPage",
|
||||||
|
ODD_PAGE = "oddPage",
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SectionTypeAttributes extends XmlAttributeComponent<{
|
||||||
|
readonly val: SectionType;
|
||||||
|
}> {
|
||||||
|
protected readonly xmlKeys = {
|
||||||
|
val: "w:val",
|
||||||
|
};
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
import { Formatter } from "export/formatter";
|
||||||
|
import { Type } from "./section-type";
|
||||||
|
import { SectionType } from "./section-type-attributes";
|
||||||
|
|
||||||
|
describe("Type", () => {
|
||||||
|
it("should create with even page section type", () => {
|
||||||
|
const sectionType = new Type(SectionType.EVEN_PAGE);
|
||||||
|
|
||||||
|
const tree = new Formatter().format(sectionType);
|
||||||
|
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:type": {
|
||||||
|
_attr: {
|
||||||
|
"w:val": "evenPage",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should create with continuous section type", () => {
|
||||||
|
const sectionType = new Type(SectionType.CONTINUOUS);
|
||||||
|
|
||||||
|
const tree = new Formatter().format(sectionType);
|
||||||
|
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:type": {
|
||||||
|
_attr: {
|
||||||
|
"w:val": "continuous",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,10 @@
|
|||||||
|
// http://officeopenxml.com/WPsection.php
|
||||||
|
import { XmlComponent } from "file/xml-components";
|
||||||
|
import { SectionType, SectionTypeAttributes } from "./section-type-attributes";
|
||||||
|
|
||||||
|
export class Type extends XmlComponent {
|
||||||
|
constructor(value: SectionType) {
|
||||||
|
super("w:type");
|
||||||
|
this.root.push(new SectionTypeAttributes({ val: value }));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user