add an ability to specify charSpace and type for DocumentGrid

This commit is contained in:
Katz Sakai
2021-11-30 21:11:06 +09:00
parent 75d62723b6
commit e08ad990a8
4 changed files with 50 additions and 4 deletions

View File

@ -0,0 +1,30 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { DocumentGrid, DocumentGridType } from ".";
describe("DocumentGrid", () => {
describe("#constructor()", () => {
it("should create documentGrid with specified linePitch", () => {
const docGrid = new DocumentGrid(360);
const tree = new Formatter().format(docGrid);
expect(tree["w:docGrid"]).to.deep.equal({ _attr: { "w:linePitch": 360 } });
});
it("should create documentGrid with specified linePitch and type", () => {
const docGrid = new DocumentGrid(360, undefined, DocumentGridType.LINES);
const tree = new Formatter().format(docGrid);
expect(tree["w:docGrid"]).to.deep.equal({ _attr: { "w:linePitch": 360, "w:type": "lines" } });
});
it("should create documentGrid with specified linePitch,charSpace and type", () => {
const docGrid = new DocumentGrid(346, -1541, DocumentGridType.LINES_AND_CHARS);
const tree = new Formatter().format(docGrid);
expect(tree["w:docGrid"]).to.deep.equal({ _attr: { "w:linePitch": 346, "w:charSpace": -1541, "w:type": "linesAndChars" } });
});
});
});

View File

@ -16,22 +16,36 @@ import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
// <xsd:attribute name="linePitch" type="ST_DecimalNumber"/> // <xsd:attribute name="linePitch" type="ST_DecimalNumber"/>
// <xsd:attribute name="charSpace" type="ST_DecimalNumber"/> // <xsd:attribute name="charSpace" type="ST_DecimalNumber"/>
// </xsd:complexType> // </xsd:complexType>
export enum DocumentGridType {
DEFAULT = "default",
LINES = "lines",
LINES_AND_CHARS = "linesAndChars",
SNAP_TO_CHARS = "snapToChars",
}
export interface IDocGridAttributesProperties { export interface IDocGridAttributesProperties {
readonly type?: DocumentGridType;
readonly linePitch?: number; readonly linePitch?: number;
readonly charSpace?: number;
} }
export class DocGridAttributes extends XmlAttributeComponent<IDocGridAttributesProperties> { export class DocGridAttributes extends XmlAttributeComponent<IDocGridAttributesProperties> {
protected readonly xmlKeys = { protected readonly xmlKeys = {
type: "w:type",
linePitch: "w:linePitch", linePitch: "w:linePitch",
charSpace: "w:charSpace",
}; };
} }
export class DocumentGrid extends XmlComponent { export class DocumentGrid extends XmlComponent {
constructor(linePitch: number) { constructor(linePitch: number, charSpace?: number, type?: DocumentGridType) {
super("w:docGrid"); super("w:docGrid");
this.root.push( this.root.push(
new DocGridAttributes({ new DocGridAttributes({
type: type,
linePitch: decimalNumber(linePitch), linePitch: decimalNumber(linePitch),
charSpace: charSpace ? decimalNumber(charSpace) : undefined,
}), }),
); );
} }

View File

@ -9,6 +9,7 @@ import { NumberFormat } from "file/shared/number-format";
import { VerticalAlign } from "file/vertical-align"; import { VerticalAlign } from "file/vertical-align";
import { PageOrientation } from "./properties"; import { PageOrientation } from "./properties";
import { DocumentGridType } from "./properties/doc-grid";
import { LineNumberRestartFormat } from "./properties/line-number"; import { LineNumberRestartFormat } from "./properties/line-number";
import { PageBorderOffsetFrom } from "./properties/page-borders"; import { PageBorderOffsetFrom } from "./properties/page-borders";
import { PageTextDirectionType } from "./properties/page-text-direction"; import { PageTextDirectionType } from "./properties/page-text-direction";
@ -64,6 +65,7 @@ describe("SectionProperties", () => {
}, },
grid: { grid: {
linePitch: convertInchesToTwip(0.25), linePitch: convertInchesToTwip(0.25),
type: DocumentGridType.LINES,
}, },
headerWrapperGroup: { headerWrapperGroup: {
default: new HeaderWrapper(media, 100), default: new HeaderWrapper(media, 100),
@ -100,7 +102,7 @@ describe("SectionProperties", () => {
expect(tree["w:sectPr"][5]).to.deep.equal({ "w:cols": { _attr: { "w:space": 208, "w:sep": true, "w:num": 2 } } }); expect(tree["w:sectPr"][5]).to.deep.equal({ "w:cols": { _attr: { "w:space": 208, "w:sep": true, "w:num": 2 } } });
expect(tree["w:sectPr"][6]).to.deep.equal({ "w:vAlign": { _attr: { "w:val": "top" } } }); expect(tree["w:sectPr"][6]).to.deep.equal({ "w:vAlign": { _attr: { "w:val": "top" } } });
expect(tree["w:sectPr"][7]).to.deep.equal({ "w:titlePg": {} }); expect(tree["w:sectPr"][7]).to.deep.equal({ "w:titlePg": {} });
expect(tree["w:sectPr"][8]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } }); expect(tree["w:sectPr"][8]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360, "w:type": "lines" } } });
}); });
it("should create section properties with no options", () => { it("should create section properties with no options", () => {

View File

@ -112,7 +112,7 @@ export class SectionProperties extends XmlComponent {
borders, borders,
textDirection, textDirection,
} = {}, } = {},
grid: { linePitch = 360 } = {}, grid: { linePitch = 360, charSpace, type: gridType } = {},
headerWrapperGroup = {}, headerWrapperGroup = {},
footerWrapperGroup = {}, footerWrapperGroup = {},
lineNumbers, lineNumbers,
@ -159,7 +159,7 @@ export class SectionProperties extends XmlComponent {
this.root.push(new PageTextDirection(textDirection)); this.root.push(new PageTextDirection(textDirection));
} }
this.root.push(new DocumentGrid(linePitch)); this.root.push(new DocumentGrid(linePitch, charSpace, gridType));
} }
private addHeaderFooterGroup( private addHeaderFooterGroup(