Merge pull request #133 from dolanmiu/feat/readme-update

Add table cell margin
This commit is contained in:
Dolan
2018-08-23 01:24:16 +01:00
committed by GitHub
9 changed files with 140 additions and 45 deletions

View File

@ -2,6 +2,7 @@ import { expect } from "chai";
import { Formatter } from "../../export/formatter";
import { TableProperties } from "./properties";
import { WidthType } from "./table-cell";
describe("TableProperties", () => {
describe("#constructor", () => {
@ -14,7 +15,7 @@ describe("TableProperties", () => {
describe("#setWidth", () => {
it("adds a table width property", () => {
const tp = new TableProperties().setWidth("dxa", 1234);
const tp = new TableProperties().setWidth(WidthType.DXA, 1234);
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblW": [{ _attr: { "w:type": "dxa", "w:w": 1234 } }] }],
@ -31,4 +32,15 @@ describe("TableProperties", () => {
});
});
});
describe("#cellMargin", () => {
it("adds a table cell top margin", () => {
const tp = new TableProperties();
tp.CellMargin.addTopMargin(1234, WidthType.DXA);
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblCellMar": [{ "w:top": [{ _attr: { "w:sz": "dxa", "w:w": 1234 } }] }] }],
});
});
});
});

View File

@ -1,13 +1,18 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export type WidthTypes = "dxa" | "pct" | "nil" | "auto";
import { WidthType } from "./table-cell";
import { TableCellMargin } from "./table-cell-margin";
export class TableProperties extends XmlComponent {
private readonly cellMargain: TableCellMargin;
constructor() {
super("w:tblPr");
this.cellMargain = new TableCellMargin();
this.root.push(this.cellMargain);
}
public setWidth(type: WidthTypes, w: number | string): TableProperties {
public setWidth(type: WidthType, w: number | string): TableProperties {
this.root.push(new PreferredTableWidth(type, w));
return this;
}
@ -21,10 +26,14 @@ export class TableProperties extends XmlComponent {
this.root.push(new TableBorders());
return this;
}
public get CellMargin(): TableCellMargin {
return this.cellMargain;
}
}
interface ITableWidth {
type: WidthTypes;
type: WidthType;
w: number | string;
}
@ -33,7 +42,7 @@ class TableWidthAttributes extends XmlAttributeComponent<ITableWidth> {
}
class PreferredTableWidth extends XmlComponent {
constructor(type: WidthTypes, w: number | string) {
constructor(type: WidthType, w: number | string) {
super("w:tblW");
this.root.push(new TableWidthAttributes({ type, w }));
}

View File

@ -0,0 +1,55 @@
import { IXmlableObject, XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { WidthType } from "./table-cell";
class TableCellMarginAttributes extends XmlAttributeComponent<{ type: WidthType; value: number }> {
protected xmlKeys = { value: "w:w", type: "w:sz" };
}
class BaseTableCellMargin extends XmlComponent {
public setProperties(value: number, type: WidthType = WidthType.DXA): void {
this.root.push(
new TableCellMarginAttributes({
type: type,
value: value,
}),
);
}
}
export class TableCellMargin extends XmlComponent {
constructor() {
super("w:tblCellMar");
}
public prepForXml(): IXmlableObject {
return this.root.length > 0 ? super.prepForXml() : "";
}
public addTopMargin(value: number, type: WidthType = WidthType.DXA): void {
const top = new BaseTableCellMargin("w:top");
top.setProperties(value, type);
this.root.push(top);
}
public addLeftMargin(value: number, type: WidthType = WidthType.DXA): void {
const left = new BaseTableCellMargin("w:left");
left.setProperties(value, type);
this.root.push(left);
}
public addBottomMargin(value: number, type: WidthType = WidthType.DXA): void {
const bottom = new BaseTableCellMargin("w:bottom");
bottom.setProperties(value, type);
this.root.push(bottom);
}
public addRightMargin(value: number, type: WidthType = WidthType.DXA): void {
const right = new BaseTableCellMargin("w:right");
right.setProperties(value, type);
this.root.push(right);
}
}

View File

@ -4,6 +4,7 @@ import { expect } from "chai";
import { Formatter } from "../../export/formatter";
import { Paragraph } from "../paragraph";
import { Table } from "./";
import { WidthType } from "./table-cell";
const DEFAULT_TABLE_PROPERTIES = {
"w:tblBorders": [
@ -174,7 +175,7 @@ describe("Table", () => {
describe("#setWidth", () => {
it("sets the preferred width on the table", () => {
const table = new Table(2, 2).setWidth("pct", 1000);
const table = new Table(2, 2).setWidth(WidthType.PERCENTAGE, 1000);
const tree = new Formatter().format(table);
expect(tree)
.to.have.property("w:tbl")

View File

@ -12,7 +12,7 @@ import {
import { IXmlableObject, XmlComponent } from "file/xml-components";
import { Paragraph } from "../paragraph";
import { TableGrid } from "./grid";
import { TableProperties, WidthTypes } from "./properties";
import { TableProperties } from "./properties";
export class Table extends XmlComponent {
private readonly properties: TableProperties;
@ -67,7 +67,7 @@ export class Table extends XmlComponent {
return this.getRow(row).getCell(col);
}
public setWidth(type: WidthTypes, width: number | string): Table {
public setWidth(type: WidthType, width: number | string): Table {
this.properties.setWidth(type, width);
return this;
}