Consolidate TableWidth and TableCellMargins types; add Table indent (#954)

This commit is contained in:
Tom Hunkapiller
2021-05-23 21:17:20 +03:00
parent 528be93191
commit dd6d1bc039
18 changed files with 215 additions and 552 deletions

View File

@ -1,73 +0,0 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BottomCellMargin, LeftCellMargin, RightCellMargin, TopCellMargin } from "./cell-margin";
describe("TopCellMargin", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargin = new TopCellMargin(1);
const tree = new Formatter().format(cellMargin);
expect(tree).to.deep.equal({
"w:top": {
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
});
});
});
});
describe("BottomCellMargin", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargin = new BottomCellMargin(1);
const tree = new Formatter().format(cellMargin);
expect(tree).to.deep.equal({
"w:bottom": {
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
});
});
});
});
describe("LeftCellMargin", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargin = new LeftCellMargin(1);
const tree = new Formatter().format(cellMargin);
expect(tree).to.deep.equal({
"w:start": {
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
});
});
});
});
describe("RightCellMargin", () => {
describe("#constructor", () => {
it("should create", () => {
const cellMargin = new RightCellMargin(1);
const tree = new Formatter().format(cellMargin);
expect(tree).to.deep.equal({
"w:end": {
_attr: {
"w:type": "dxa",
"w:w": 1,
},
},
});
});
});
});

View File

@ -1,61 +0,0 @@
// http://officeopenxml.com/WPtableCellProperties-Margins.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class CellMarginAttributes extends XmlAttributeComponent<{
readonly type: string;
readonly width: number;
}> {
protected readonly xmlKeys = { width: "w:w", type: "w:type" };
}
export class TopCellMargin extends XmlComponent {
constructor(value: number) {
super("w:top");
this.root.push(
new CellMarginAttributes({
width: value,
type: "dxa",
}),
);
}
}
export class BottomCellMargin extends XmlComponent {
constructor(value: number) {
super("w:bottom");
this.root.push(
new CellMarginAttributes({
width: value,
type: "dxa",
}),
);
}
}
export class LeftCellMargin extends XmlComponent {
constructor(value: number) {
super("w:start");
this.root.push(
new CellMarginAttributes({
width: value,
type: "dxa",
}),
);
}
}
export class RightCellMargin extends XmlComponent {
constructor(value: number) {
super("w:end");
this.root.push(
new CellMarginAttributes({
width: value,
type: "dxa",
}),
);
}
}

View File

@ -1,96 +0,0 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { TableCellMargin } from "./table-cell-margins";
describe("TableCellMargin", () => {
describe("#constructor", () => {
it("should create with default values", () => {
const cellMargin = new TableCellMargin({});
const tree = new Formatter().format(cellMargin);
expect(tree).to.deep.equal({
"w:tcMar": [
{
"w:top": {
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
},
{
"w:start": {
_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,
},
},
},
],
});
});
it("should create with values", () => {
const cellMargin = new TableCellMargin({
top: 5,
bottom: 5,
left: 5,
right: 5,
});
const tree = new Formatter().format(cellMargin);
expect(tree).to.deep.equal({
"w:tcMar": [
{
"w:top": {
_attr: {
"w:type": "dxa",
"w:w": 5,
},
},
},
{
"w:start": {
_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,
},
},
},
],
});
});
});
});

View File

@ -1,21 +0,0 @@
// http://officeopenxml.com/WPtableCellProperties-Margins.php
import { XmlComponent } from "file/xml-components";
import { BottomCellMargin, LeftCellMargin, RightCellMargin, TopCellMargin } from "./cell-margin";
export interface ITableCellMarginOptions {
readonly top?: number;
readonly left?: number;
readonly bottom?: number;
readonly right?: number;
}
export class TableCellMargin extends XmlComponent {
constructor({ top = 0, left = 0, right = 0, bottom = 0 }: ITableCellMarginOptions) {
super("w:tcMar");
this.root.push(new TopCellMargin(top));
this.root.push(new LeftCellMargin(left));
this.root.push(new BottomCellMargin(bottom));
this.root.push(new RightCellMargin(right));
}
}

View File

@ -139,34 +139,3 @@ export class TDirection extends XmlComponent {
);
}
}
export enum WidthType {
/** Auto. */
AUTO = "auto",
/** Value is in twentieths of a point */
DXA = "dxa",
/** No (empty) value. */
NIL = "nil",
/** Value is in percentage. */
PERCENTAGE = "pct",
}
class TableCellWidthAttributes extends XmlAttributeComponent<{ readonly type: WidthType; readonly width: string | number }> {
protected readonly xmlKeys = { width: "w:w", type: "w:type" };
}
/**
* Table cell width element.
*/
export class TableCellWidth extends XmlComponent {
constructor(value: string | number, type: WidthType = WidthType.AUTO) {
super("w:tcW");
this.root.push(
new TableCellWidthAttributes({
width: value,
type: type,
}),
);
}
}

View File

@ -2,8 +2,9 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BorderStyle } from "file/border";
import { WidthType } from "../table-width";
import { VerticalAlign, VerticalMergeType, WidthType } from "./table-cell-components";
import { VerticalAlign, VerticalMergeType } from "./table-cell-components";
import { TableCellProperties } from "./table-cell-properties";
describe("TableCellProperties", () => {
@ -67,53 +68,6 @@ describe("TableCellProperties", () => {
expect(tree).to.deep.equal({ "w:tcPr": [{ "w:shd": { _attr: { "w:fill": "test", "w:color": "000" } } }] });
});
it("sets shading", () => {
const properties = new TableCellProperties({
margins: {},
});
const tree = new Formatter().format(properties);
expect(tree).to.deep.equal({
"w:tcPr": [
{
"w:tcMar": [
{
"w:top": {
_attr: {
"w:type": "dxa",
"w:w": 0,
},
},
},
{
"w:start": {
_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,
},
},
},
],
},
],
});
});
it("should set the TableCellBorders", () => {
const properties = new TableCellProperties({
borders: {
@ -131,5 +85,28 @@ describe("TableCellProperties", () => {
"w:tcBorders": [{ "w:top": { _attr: { "w:val": "dashDotStroked", "w:sz": 3, "w:color": "red" } } }],
});
});
it("should set the margins", () => {
const properties = new TableCellProperties({
margins: {
marginUnitType: WidthType.DXA,
top: 5,
left: 10,
bottom: 15,
right: 20,
},
});
const tree = new Formatter().format(properties);
expect(tree["w:tcPr"][0]).to.deep.equal({
"w:tcMar": [
{ "w:top": { _attr: { "w:type": "dxa", "w:w": 5 } } },
{ "w:left": { _attr: { "w:type": "dxa", "w:w": 10 } } },
{ "w:bottom": { _attr: { "w:type": "dxa", "w:w": 15 } } },
{ "w:right": { _attr: { "w:type": "dxa", "w:w": 20 } } },
],
});
});
});
});

View File

@ -1,19 +1,18 @@
import { IgnoreIfEmptyXmlComponent } from "file/xml-components";
import { IShadingAttributesProperties, Shading } from "../../shading";
import { ITableCellMarginOptions, TableCellMargin } from "./cell-margin/table-cell-margins";
import { ITableCellMarginOptions, TableCellMargin, TableCellMarginElementType } from "../table-properties/table-cell-margin";
import { ITableWidthProperties, TableWidthElement } from "../table-width";
import {
GridSpan,
ITableCellBorders,
TableCellBorders,
TableCellWidth,
TDirection,
TextDirection,
VAlign,
VerticalAlign,
VerticalMerge,
VerticalMergeType,
WidthType,
} from "./table-cell-components";
export interface ITableCellPropertiesOptions {
@ -22,10 +21,7 @@ export interface ITableCellPropertiesOptions {
readonly verticalAlign?: VerticalAlign;
readonly textDirection?: TextDirection;
readonly verticalMerge?: VerticalMergeType;
readonly width?: {
readonly size: number | string;
readonly type?: WidthType;
};
readonly width?: ITableWidthProperties;
readonly columnSpan?: number;
readonly rowSpan?: number;
readonly borders?: ITableCellBorders;
@ -36,7 +32,7 @@ export class TableCellProperties extends IgnoreIfEmptyXmlComponent {
super("w:tcPr");
if (options.width) {
this.root.push(new TableCellWidth(options.width.size, options.width.type));
this.root.push(new TableWidthElement("w:tcW", options.width));
}
if (options.columnSpan) {
@ -59,7 +55,7 @@ export class TableCellProperties extends IgnoreIfEmptyXmlComponent {
}
if (options.margins) {
this.root.push(new TableCellMargin(options.margins));
this.root.push(new TableCellMargin(TableCellMarginElementType.TABLE_CELL, options.margins));
}
if (options.textDirection) {

View File

@ -4,8 +4,9 @@ import { Formatter } from "export/formatter";
import { BorderStyle } from "file/border";
import { ShadingType } from "file/shading";
import { WidthType } from "../table-width";
import { TableCell } from "./table-cell";
import { TableCellBorders, TableCellWidth, TextDirection, VerticalAlign, VerticalMergeType, WidthType } from "./table-cell-components";
import { TableCellBorders, TextDirection, VerticalAlign, VerticalMergeType } from "./table-cell-components";
describe("TableCellBorders", () => {
describe("#prepForXml", () => {
@ -263,23 +264,6 @@ describe("TableCellBorders", () => {
});
});
describe("TableCellWidth", () => {
describe("#constructor", () => {
it("should create object", () => {
const tcWidth = new TableCellWidth(100, WidthType.DXA);
const tree = new Formatter().format(tcWidth);
expect(tree).to.deep.equal({
"w:tcW": {
_attr: {
"w:type": "dxa",
"w:w": 100,
},
},
});
});
});
});
describe("TableCell", () => {
describe("#constructor", () => {
it("should create", () => {
@ -410,7 +394,7 @@ describe("TableCell", () => {
},
},
{
"w:start": {
"w:left": {
_attr: {
"w:type": "dxa",
"w:w": 1,
@ -426,7 +410,7 @@ describe("TableCell", () => {
},
},
{
"w:end": {
"w:right": {
_attr: {
"w:type": "dxa",
"w:w": 1,