Add tests and margain

This commit is contained in:
Dolan
2019-03-18 23:50:21 +00:00
parent 639842332f
commit e67fd9cb2b
19 changed files with 611 additions and 107 deletions

View File

@ -0,0 +1,81 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BottomCellMargain, LeftCellMargain, RightCellMargain, TopCellMargain } from "./cell-margain";
describe("TopCellMargain", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargain = new TopCellMargain(1);
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:top": [
{
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
],
});
});
});
});
describe("BottomCellMargain", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargain = new BottomCellMargain(1);
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:bottom": [
{
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
],
});
});
});
});
describe("LeftCellMargain", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargain = new LeftCellMargain(1);
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:start": [
{
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
],
});
});
});
});
describe("RightCellMargain", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargain = new RightCellMargain(1);
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:end": [
{
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
],
});
});
});
});

View File

@ -0,0 +1,63 @@
// http://officeopenxml.com/WPtableCellProperties-Margins.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export interface ICellMargainProperties {
readonly type: string;
readonly width: number;
}
class CellMargainAttributes extends XmlAttributeComponent<ICellMargainProperties> {
protected readonly xmlKeys = { width: "w:w", type: "w:type" };
}
export class TopCellMargain extends XmlComponent {
constructor(value: number) {
super("w:top");
this.root.push(
new CellMargainAttributes({
width: value,
type: "dxa",
}),
);
}
}
export class BottomCellMargain extends XmlComponent {
constructor(value: number) {
super("w:bottom");
this.root.push(
new CellMargainAttributes({
width: value,
type: "dxa",
}),
);
}
}
export class LeftCellMargain extends XmlComponent {
constructor(value: number) {
super("w:start");
this.root.push(
new CellMargainAttributes({
width: value,
type: "dxa",
}),
);
}
}
export class RightCellMargain extends XmlComponent {
constructor(value: number) {
super("w:end");
this.root.push(
new CellMargainAttributes({
width: value,
type: "dxa",
}),
);
}
}

View File

@ -0,0 +1,112 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { TableCellMargain } from "./table-cell-margains";
describe("TableCellMargain", () => {
describe("#constructor", () => {
it("should create with default values", () => {
const cellMargain = new TableCellMargain({});
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:tcMar": [
{
"w:top": [
{
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
],
},
{
"w:bottom": [
{
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
],
},
{
"w:end": [
{
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
],
},
{
"w:start": [
{
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
],
},
],
});
});
it("should create with values", () => {
const cellMargain = new TableCellMargain({
top: 5,
bottom: 5,
left: 5,
right: 5,
});
const tree = new Formatter().format(cellMargain);
expect(tree).to.deep.equal({
"w:tcMar": [
{
"w:top": [
{
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
],
},
{
"w:bottom": [
{
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
],
},
{
"w:end": [
{
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
],
},
{
"w:start": [
{
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,21 @@
// http://officeopenxml.com/WPtableCellProperties-Margins.php
import { XmlComponent } from "file/xml-components";
import { BottomCellMargain, LeftCellMargain, RightCellMargain, TopCellMargain } from "./cell-margain";
export interface ITableCellMargainOptions {
readonly top?: number;
readonly left?: number;
readonly bottom?: number;
readonly right?: number;
}
export class TableCellMargain extends XmlComponent {
constructor({ top = 0, left = 0, right = 0, bottom = 0 }: ITableCellMargainOptions) {
super("w:tcMar");
this.root.push(new TopCellMargain(top));
this.root.push(new BottomCellMargain(bottom));
this.root.push(new RightCellMargain(right));
this.root.push(new LeftCellMargain(left));
}
}

View File

@ -1,5 +1,6 @@
import { XmlComponent } from "file/xml-components";
import { ITableCellMargainOptions, TableCellMargain } from "./cell-margain/table-cell-margains";
import {
GridSpan,
ITableCellShadingAttributesProperties,
@ -55,4 +56,10 @@ export class TableCellProperties extends XmlComponent {
return this;
}
public addMargains(options: ITableCellMargainOptions): TableCellProperties {
this.root.push(new TableCellMargain(options));
return this;
}
}

View File

@ -3,6 +3,7 @@ import { Paragraph } from "file/paragraph";
import { IXmlableObject, XmlComponent } from "file/xml-components";
import { Table } from "../table";
import { ITableCellMargainOptions } from "./cell-margain/table-cell-margains";
import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components";
import { TableCellProperties } from "./table-cell-properties";
@ -11,6 +12,7 @@ export class TableCell extends XmlComponent {
constructor() {
super("w:tc");
this.properties = new TableCellProperties();
this.root.push(this.properties);
}
@ -64,6 +66,12 @@ export class TableCell extends XmlComponent {
return this;
}
public setMargains(margains: ITableCellMargainOptions): TableCell {
this.properties.addMargains(margains);
return this;
}
public get Borders(): TableCellBorders {
return this.properties.Borders;
}