Merge branch 'master' into feat/declaritive

This commit is contained in:
Dolan
2019-06-26 00:38:21 +01:00
15 changed files with 137 additions and 9 deletions

View File

@ -71,7 +71,7 @@ describe("Body", () => {
},
},
},
{ "w:cols": { _attr: { "w:space": 708 } } },
{ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } },
{ "w:docGrid": { _attr: { "w:linePitch": 360 } } },
],
},
@ -96,7 +96,7 @@ describe("Body", () => {
},
},
},
{ "w:cols": { _attr: { "w:space": 708 } } },
{ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } },
{ "w:docGrid": { _attr: { "w:linePitch": 360 } } },
],
},

View File

@ -2,10 +2,12 @@ import { XmlAttributeComponent } from "file/xml-components";
export interface IColumnsAttributes {
readonly space?: number;
readonly num?: number;
}
export class ColumnsAttributes extends XmlAttributeComponent<IColumnsAttributes> {
protected readonly xmlKeys = {
space: "w:space",
num: "w:num",
};
}

View File

@ -2,11 +2,12 @@ import { XmlComponent } from "file/xml-components";
import { ColumnsAttributes } from "./columns-attributes";
export class Columns extends XmlComponent {
constructor(space: number) {
constructor(space: number, num: number) {
super("w:cols");
this.root.push(
new ColumnsAttributes({
space: space,
num: num,
}),
);
}

View File

@ -26,6 +26,7 @@ describe("SectionProperties", () => {
gutter: 0,
mirror: false,
space: 708,
num: 1,
linePitch: 360,
headers: {
default: new HeaderWrapper(media, 100),
@ -55,7 +56,7 @@ describe("SectionProperties", () => {
},
});
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708 } } });
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } });
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
expect(tree["w:sectPr"][4]).to.deep.equal({ "w:headerReference": { _attr: { "r:id": "rId100", "w:type": "default" } } });
expect(tree["w:sectPr"][5]).to.deep.equal({ "w:footerReference": { _attr: { "r:id": "rId200", "w:type": "even" } } });
@ -82,7 +83,7 @@ describe("SectionProperties", () => {
},
},
});
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708 } } });
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } });
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
});

View File

@ -67,6 +67,7 @@ export class SectionProperties extends XmlComponent {
gutter = 0,
mirror = false,
space = 708,
num = 1,
linePitch = 360,
orientation = PageOrientation.PORTRAIT,
headers,
@ -88,7 +89,7 @@ export class SectionProperties extends XmlComponent {
this.options = options;
this.root.push(new PageSize(width, height, orientation));
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror));
this.root.push(new Columns(space));
this.root.push(new Columns(space, num));
this.root.push(new DocumentGrid(linePitch));
this.addHeaders(headers);

View File

@ -9,3 +9,5 @@ export * from "./document";
export * from "./styles";
export * from "./table-of-contents";
export * from "./xml-components";
export * from "./header-wrapper";
export * from "./footer-wrapper";

View File

@ -1 +1,3 @@
export * from "./styles";
export * from "./style/character-style";
export * from "./style/paragraph-style";

View File

@ -2,3 +2,4 @@ export * from "./table";
export * from "./table-cell";
export * from "./table-properties";
export * from "./shading";
export * from "./table-row";

View File

@ -1,2 +1,3 @@
export * from "./table-row";
export * from "./table-row-properties";
export * from "./table-row-height";

View File

@ -0,0 +1,32 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export enum HeightRule {
/** Height is determined based on the content, so value is ignored. */
AUTO = "auto",
/** At least the value specified */
ATLEAST = "atLeast",
/** Exactly the value specified */
EXACT = "exact",
}
interface ITableRowHeight {
readonly height: number;
readonly rule: HeightRule;
}
export class TableRowHeightAttributes extends XmlAttributeComponent<ITableRowHeight> {
protected readonly xmlKeys = { height: "w:val", rule: "w:hRule" };
}
export class TableRowHeight extends XmlComponent {
constructor(value: number, rule: HeightRule) {
super("w:trHeight");
this.root.push(
new TableRowHeightAttributes({
height: value,
rule: rule,
}),
);
}
}

View File

@ -1,5 +1,6 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { HeightRule } from "file/table/table-row/table-row-height";
import { TableRowProperties } from "./table-row-properties";
describe("TableRowProperties", () => {
@ -31,4 +32,25 @@ describe("TableRowProperties", () => {
expect(tree).to.deep.equal({ "w:trPr": [{ "w:tblHeader": { _attr: { "w:val": true } } }] });
});
});
describe("#setHeight", () => {
it("sets row height exact", () => {
const rowProperties = new TableRowProperties();
rowProperties.setHeight(100, HeightRule.EXACT);
const tree = new Formatter().format(rowProperties);
expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "exact" } } }] });
});
it("sets row height auto", () => {
const rowProperties = new TableRowProperties();
rowProperties.setHeight(100, HeightRule.AUTO);
const tree = new Formatter().format(rowProperties);
expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "auto" } } }] });
});
it("sets row height at least", () => {
const rowProperties = new TableRowProperties();
rowProperties.setHeight(100, HeightRule.ATLEAST);
const tree = new Formatter().format(rowProperties);
expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "atLeast" } } }] });
});
});
});

View File

@ -1,3 +1,4 @@
import { HeightRule, TableRowHeight } from "file/table/table-row/table-row-height";
import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components";
export class TableRowProperties extends IgnoreIfEmptyXmlComponent {
@ -16,6 +17,12 @@ export class TableRowProperties extends IgnoreIfEmptyXmlComponent {
return this;
}
public setHeight(height: number, rule: HeightRule): TableRowProperties {
this.root.push(new TableRowHeight(height, rule));
return this;
}
}
class CantSplitAttributes extends XmlAttributeComponent<{ readonly val: boolean }> {

View File

@ -2,11 +2,11 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { HeightRule } from "file/table/table-row/table-row-height";
import { EMPTY_OBJECT } from "file/xml-components";
import { TableCell } from "../table-cell";
import { TableRow } from "./table-row";
import { EMPTY_OBJECT } from "file/xml-components";
describe("TableRow", () => {
describe("#constructor", () => {
it("should create with no cells", () => {
@ -67,4 +67,28 @@ describe("TableRow", () => {
expect(() => tableRow.getCell(1)).to.throw();
});
});
describe("#setHeight", () => {
it("should set row height", () => {
const tableRow = new TableRow([]);
tableRow.setHeight(100, HeightRule.EXACT);
const tree = new Formatter().format(tableRow);
expect(tree).to.deep.equal({
"w:tr": [
{
"w:trPr": [
{
"w:trHeight": {
_attr: {
"w:hRule": "exact",
"w:val": 100,
},
},
},
],
},
],
});
});
});
});

View File

@ -1,5 +1,5 @@
import { HeightRule } from "file/table/table-row/table-row-height";
import { XmlComponent } from "file/xml-components";
import { TableCell } from "../table-cell";
import { TableRowProperties } from "./table-row-properties";
@ -49,4 +49,10 @@ export class TableRow extends XmlComponent {
return this;
}
public setHeight(height: number, rule: HeightRule): TableRow {
this.properties.setHeight(height, rule);
return this;
}
}