Amend table documentation

This commit is contained in:
Dolan
2019-09-26 02:03:17 +01:00
parent 2842619196
commit bd888219fc
6 changed files with 229 additions and 167 deletions

View File

@ -103,7 +103,7 @@ export class GridSpan extends XmlComponent {
/**
* Vertical merge types.
*/
export enum VMergeType {
export enum VerticalMergeType {
/**
* Cell that is merged with upper one.
*/
@ -114,19 +114,19 @@ export enum VMergeType {
RESTART = "restart",
}
class VMergeAttributes extends XmlAttributeComponent<{ readonly val: VMergeType }> {
class VerticalMergeAttributes extends XmlAttributeComponent<{ readonly val: VerticalMergeType }> {
protected readonly xmlKeys = { val: "w:val" };
}
/**
* Vertical merge element. Should be used in a table cell.
*/
export class VMerge extends XmlComponent {
constructor(value: VMergeType) {
export class VerticalMerge extends XmlComponent {
constructor(value: VerticalMergeType) {
super("w:vMerge");
this.root.push(
new VMergeAttributes({
new VerticalMergeAttributes({
val: value,
}),
);

View File

@ -3,7 +3,7 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BorderStyle } from "file/styles";
import { VerticalAlign, VMergeType, WidthType } from "./table-cell-components";
import { VerticalAlign, VerticalMergeType, WidthType } from "./table-cell-components";
import { TableCellProperties } from "./table-cell-properties";
describe("TableCellProperties", () => {
@ -30,7 +30,7 @@ describe("TableCellProperties", () => {
describe("#addVerticalMerge", () => {
it("adds vertical merge", () => {
const properties = new TableCellProperties();
properties.addVerticalMerge(VMergeType.CONTINUE);
properties.addVerticalMerge(VerticalMergeType.CONTINUE);
const tree = new Formatter().format(properties);
expect(tree).to.deep.equal({ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "continue" } } }] });
});

View File

@ -2,7 +2,16 @@ import { IgnoreIfEmptyXmlComponent } from "file/xml-components";
import { ITableShadingAttributesProperties, TableShading } from "../shading";
import { ITableCellMarginOptions, TableCellMargin } from "./cell-margin/table-cell-margins";
import { GridSpan, TableCellBorders, TableCellWidth, VAlign, VerticalAlign, VMerge, VMergeType, WidthType } from "./table-cell-components";
import {
GridSpan,
TableCellBorders,
TableCellWidth,
VAlign,
VerticalAlign,
VerticalMerge,
VerticalMergeType,
WidthType,
} from "./table-cell-components";
export class TableCellProperties extends IgnoreIfEmptyXmlComponent {
private readonly cellBorder: TableCellBorders;
@ -23,8 +32,8 @@ export class TableCellProperties extends IgnoreIfEmptyXmlComponent {
return this;
}
public addVerticalMerge(type: VMergeType): TableCellProperties {
this.root.push(new VMerge(type));
public addVerticalMerge(type: VerticalMergeType): TableCellProperties {
this.root.push(new VerticalMerge(type));
return this;
}

View File

@ -6,14 +6,14 @@ import { IXmlableObject, XmlComponent } from "file/xml-components";
import { ITableShadingAttributesProperties } from "../shading";
import { Table } from "../table";
import { ITableCellMarginOptions } from "./cell-margin/table-cell-margins";
import { VerticalAlign, VMergeType } from "./table-cell-components";
import { VerticalAlign, VerticalMergeType } from "./table-cell-components";
import { TableCellProperties } from "./table-cell-properties";
export interface ITableCellOptions {
readonly shading?: ITableShadingAttributesProperties;
readonly margins?: ITableCellMarginOptions;
readonly verticalAlign?: VerticalAlign;
readonly verticalMerge?: VMergeType;
readonly verticalMerge?: VerticalMergeType;
readonly columnSpan?: number;
readonly rowSpan?: number;
readonly borders?: {
@ -75,7 +75,7 @@ export class TableCell extends XmlComponent {
}
if (options.rowSpan && options.rowSpan > 1) {
this.properties.addVerticalMerge(VMergeType.RESTART);
this.properties.addVerticalMerge(VerticalMergeType.RESTART);
}
if (options.borders) {

View File

@ -1,7 +1,7 @@
// http://officeopenxml.com/WPtableGrid.php
import { XmlComponent } from "file/xml-components";
import { TableGrid } from "./grid";
import { TableCell, VMergeType, WidthType } from "./table-cell";
import { TableCell, VerticalMergeType, WidthType } from "./table-cell";
import { ITableFloatOptions, TableProperties } from "./table-properties";
import { TableLayoutType } from "./table-properties/table-layout";
import { TableRow } from "./table-row";
@ -18,8 +18,10 @@ import { TableRow } from "./table-row";
*/
export interface ITableOptions {
readonly rows: TableRow[];
readonly width?: number;
readonly widthUnitType?: WidthType;
readonly width?: {
readonly size: number;
readonly type?: WidthType;
};
readonly columnWidths?: number[];
readonly margins?: {
readonly marginUnitType?: WidthType;
@ -37,8 +39,7 @@ export class Table extends XmlComponent {
constructor({
rows,
width = 100,
widthUnitType = WidthType.AUTO,
width,
columnWidths = Array<number>(Math.max(...rows.map((row) => row.CellCount))).fill(100),
margins: { marginUnitType, top, bottom, right, left } = { marginUnitType: WidthType.AUTO, top: 0, bottom: 0, right: 0, left: 0 },
float,
@ -48,7 +49,13 @@ export class Table extends XmlComponent {
this.properties = new TableProperties();
this.root.push(this.properties);
this.properties.setBorder();
this.properties.setWidth(width, widthUnitType);
if (width) {
this.properties.setWidth(width.size, width.type);
} else {
this.properties.setWidth(100);
}
this.properties.CellMargin.addBottomMargin(bottom || 0, marginUnitType);
this.properties.CellMargin.addTopMargin(top || 0, marginUnitType);
this.properties.CellMargin.addLeftMargin(left || 0, marginUnitType);
@ -73,7 +80,7 @@ export class Table extends XmlComponent {
rows[i].addCellToIndex(
new TableCell({
children: [],
verticalMerge: VMergeType.CONTINUE,
verticalMerge: VerticalMergeType.CONTINUE,
}),
i,
);