Add table cell margin
This commit is contained in:
@ -2,6 +2,7 @@ import { expect } from "chai";
|
|||||||
|
|
||||||
import { Formatter } from "../../export/formatter";
|
import { Formatter } from "../../export/formatter";
|
||||||
import { TableProperties } from "./properties";
|
import { TableProperties } from "./properties";
|
||||||
|
import { WidthType } from "./table-cell";
|
||||||
|
|
||||||
describe("TableProperties", () => {
|
describe("TableProperties", () => {
|
||||||
describe("#constructor", () => {
|
describe("#constructor", () => {
|
||||||
@ -14,7 +15,7 @@ describe("TableProperties", () => {
|
|||||||
|
|
||||||
describe("#setWidth", () => {
|
describe("#setWidth", () => {
|
||||||
it("adds a table width property", () => {
|
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);
|
const tree = new Formatter().format(tp);
|
||||||
expect(tree).to.deep.equal({
|
expect(tree).to.deep.equal({
|
||||||
"w:tblPr": [{ "w:tblW": [{ _attr: { "w:type": "dxa", "w:w": 1234 } }] }],
|
"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 } }] }] }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||||
|
import { WidthType } from "./table-cell";
|
||||||
export type WidthTypes = "dxa" | "pct" | "nil" | "auto";
|
import { TableCellMargin } from "./table-cell-margin";
|
||||||
|
|
||||||
export class TableProperties extends XmlComponent {
|
export class TableProperties extends XmlComponent {
|
||||||
|
private readonly cellMargain: TableCellMargin;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super("w:tblPr");
|
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));
|
this.root.push(new PreferredTableWidth(type, w));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -21,10 +26,14 @@ export class TableProperties extends XmlComponent {
|
|||||||
this.root.push(new TableBorders());
|
this.root.push(new TableBorders());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get CellMargin(): TableCellMargin {
|
||||||
|
return this.cellMargain;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ITableWidth {
|
interface ITableWidth {
|
||||||
type: WidthTypes;
|
type: WidthType;
|
||||||
w: number | string;
|
w: number | string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +42,7 @@ class TableWidthAttributes extends XmlAttributeComponent<ITableWidth> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class PreferredTableWidth extends XmlComponent {
|
class PreferredTableWidth extends XmlComponent {
|
||||||
constructor(type: WidthTypes, w: number | string) {
|
constructor(type: WidthType, w: number | string) {
|
||||||
super("w:tblW");
|
super("w:tblW");
|
||||||
this.root.push(new TableWidthAttributes({ type, w }));
|
this.root.push(new TableWidthAttributes({ type, w }));
|
||||||
}
|
}
|
||||||
|
55
src/file/table/table-cell-margin.ts
Normal file
55
src/file/table/table-cell-margin.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user