diff --git a/src/file/document/body/section-properties/properties/index.ts b/src/file/document/body/section-properties/properties/index.ts index 3349b2595f..8cc32e6e23 100644 --- a/src/file/document/body/section-properties/properties/index.ts +++ b/src/file/document/body/section-properties/properties/index.ts @@ -7,6 +7,7 @@ export * from "./page-number"; export * from "./page-borders"; export * from "./page-margin"; export * from "./page-borders"; +export * from "./page-text-direction"; export * from "./line-number"; export * from "./section-type"; export * from "./header-footer-reference"; diff --git a/src/file/document/body/section-properties/properties/page-text-direction.spec.ts b/src/file/document/body/section-properties/properties/page-text-direction.spec.ts new file mode 100644 index 0000000000..da6a06e4a4 --- /dev/null +++ b/src/file/document/body/section-properties/properties/page-text-direction.spec.ts @@ -0,0 +1,22 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; +import { PageTDirection, PageTextDirection } from "./page-text-direction"; + +describe("PageTextDirection", () => { + describe("#constructor()", () => { + it("should set the direction of the text flow to top-to-bottom-right-to-left", () => { + const textDirection = new PageTDirection(PageTextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT); + + const tree = new Formatter().format(textDirection); + + expect(tree).to.deep.equal({ + "w:textDirection": { + _attr: { + "w:val": "tbRl", + }, + }, + }); + }); + }); +}); diff --git a/src/file/document/body/section-properties/properties/page-text-direction.ts b/src/file/document/body/section-properties/properties/page-text-direction.ts new file mode 100644 index 0000000000..6edd300980 --- /dev/null +++ b/src/file/document/body/section-properties/properties/page-text-direction.ts @@ -0,0 +1,22 @@ +import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; + +export enum PageTextDirection { + LEFT_TO_RIGHT_TOP_TO_BOTTOM = "lrTb", + TOP_TO_BOTTOM_RIGHT_TO_LEFT = "tbRl", +} + +class PageTextDirectionAttributes extends XmlAttributeComponent<{ readonly val: PageTextDirection }> { + protected readonly xmlKeys = { val: "w:val" }; +} + +export class PageTDirection extends XmlComponent { + constructor(value: PageTextDirection) { + super("w:textDirection"); + + this.root.push( + new PageTextDirectionAttributes({ + val: value, + }), + ); + } +} diff --git a/src/file/document/body/section-properties/section-properties.spec.ts b/src/file/document/body/section-properties/section-properties.spec.ts index d0c7e7fc5a..9460f16d9b 100644 --- a/src/file/document/body/section-properties/section-properties.spec.ts +++ b/src/file/document/body/section-properties/section-properties.spec.ts @@ -11,6 +11,7 @@ import { VerticalAlign } from "file/vertical-align"; import { PageOrientation } from "./properties"; import { LineNumberRestartFormat } from "./properties/line-number"; import { PageBorderOffsetFrom } from "./properties/page-borders"; +import { PageTextDirection } from "./properties/page-text-direction"; import { SectionType } from "./properties/section-type"; import { sectionMarginDefaults, sectionPageSizeDefaults, SectionProperties } from "./section-properties"; @@ -258,5 +259,19 @@ describe("SectionProperties", () => { "w:lnNumType": { _attr: { "w:countBy": 2, "w:distance": 4, "w:restart": "continuous", "w:start": 2 } }, }); }); + + it("should create section properties with text flow direction", () => { + const properties = new SectionProperties({ + page: { + textDirection: PageTextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT, + }, + }); + const tree = new Formatter().format(properties); + expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); + const type = tree["w:sectPr"].find((item) => item["w:textDirection"] !== undefined); + expect(type).to.deep.equal({ + "w:textDirection": { _attr: { "w:val": "tbRl" } }, + }); + }); }); }); diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index ba155a61c4..22591ede9d 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -15,6 +15,7 @@ import { IPageBordersOptions, PageBorders } from "./properties/page-borders"; import { IPageMarginAttributes, PageMargin } from "./properties/page-margin"; import { IPageNumberTypeAttributes, PageNumberType } from "./properties/page-number"; import { IPageSizeAttributes, PageOrientation, PageSize } from "./properties/page-size"; +import { PageTDirection, PageTextDirection } from "./properties/page-text-direction"; import { SectionType, Type } from "./properties/section-type"; export interface IHeaderFooterGroup { @@ -29,6 +30,7 @@ export interface ISectionPropertiesOptions { readonly margin?: IPageMarginAttributes; readonly pageNumbers?: IPageNumberTypeAttributes; readonly borders?: IPageBordersOptions; + readonly textDirection?: PageTextDirection; }; readonly grid?: IDocGridAttributesProperties; readonly headerWrapperGroup?: IHeaderFooterGroup; @@ -108,6 +110,7 @@ export class SectionProperties extends XmlComponent { } = {}, pageNumbers = {}, borders, + textDirection, } = {}, grid: { linePitch = 360 } = {}, headerWrapperGroup = {}, @@ -152,6 +155,10 @@ export class SectionProperties extends XmlComponent { this.root.push(new OnOffElement("w:titlePg", titlePage)); } + if (textDirection) { + this.root.push(new PageTDirection(textDirection)); + } + this.root.push(new DocumentGrid(linePitch)); }