Files
docx-js/src/file/table/properties.ts

98 lines
2.9 KiB
TypeScript
Raw Normal View History

import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
2018-08-23 00:55:33 +01:00
import { WidthType } from "./table-cell";
import { TableCellMargin } from "./table-cell-margin";
2017-03-07 19:22:10 +01:00
export class TableProperties extends XmlComponent {
2018-08-23 00:55:33 +01:00
private readonly cellMargain: TableCellMargin;
2017-03-07 19:22:10 +01:00
constructor() {
super("w:tblPr");
2018-08-23 00:55:33 +01:00
this.cellMargain = new TableCellMargin();
this.root.push(this.cellMargain);
2017-03-07 19:22:10 +01:00
}
2018-08-23 00:55:33 +01:00
public setWidth(type: WidthType, w: number | string): TableProperties {
this.root.push(new PreferredTableWidth(type, w));
return this;
2017-03-07 19:22:10 +01:00
}
2018-08-07 01:25:28 +01:00
public setFixedWidthLayout(): TableProperties {
this.root.push(new TableLayout("fixed"));
return this;
}
2018-03-22 23:04:46 +00:00
public setBorder(): TableProperties {
this.root.push(new TableBorders());
return this;
}
2018-08-23 00:55:33 +01:00
public get CellMargin(): TableCellMargin {
return this.cellMargain;
}
2017-03-07 19:22:10 +01:00
}
interface ITableWidth {
2018-08-23 00:55:33 +01:00
type: WidthType;
w: number | string;
}
class TableWidthAttributes extends XmlAttributeComponent<ITableWidth> {
2018-01-23 01:33:12 +00:00
protected xmlKeys = { type: "w:type", w: "w:w" };
}
2017-03-07 19:22:10 +01:00
class PreferredTableWidth extends XmlComponent {
2018-08-23 00:55:33 +01:00
constructor(type: WidthType, w: number | string) {
super("w:tblW");
2018-01-23 01:33:12 +00:00
this.root.push(new TableWidthAttributes({ type, w }));
2017-03-07 19:22:10 +01:00
}
}
type TableLayoutOptions = "autofit" | "fixed";
2018-01-23 01:33:12 +00:00
class TableLayoutAttributes extends XmlAttributeComponent<{ type: TableLayoutOptions }> {
protected xmlKeys = { type: "w:type" };
}
class TableLayout extends XmlComponent {
constructor(type: TableLayoutOptions) {
super("w:tblLayout");
2018-01-23 01:33:12 +00:00
this.root.push(new TableLayoutAttributes({ type }));
}
}
2018-03-22 22:55:33 +00:00
class TableBorders extends XmlComponent {
constructor() {
super("w:tblBorders");
this.root.push(new TableBordersElement("w:top", "single", 4, 0, "auto"));
this.root.push(new TableBordersElement("w:left", "single", 4, 0, "auto"));
this.root.push(new TableBordersElement("w:bottom", "single", 4, 0, "auto"));
this.root.push(new TableBordersElement("w:right", "single", 4, 0, "auto"));
this.root.push(new TableBordersElement("w:insideH", "single", 4, 0, "auto"));
this.root.push(new TableBordersElement("w:insideV", "single", 4, 0, "auto"));
}
}
class TableBordersElement extends XmlComponent {
constructor(elementName: string, value: string, size: number, space: number, color: string) {
super(elementName);
this.root.push(
new TableBordersAttributes({
value,
size,
space,
color,
}),
);
}
}
class TableBordersAttributes extends XmlAttributeComponent<{ value: string; size: number; space: number; color: string }> {
protected xmlKeys = {
value: "w:val",
size: "w:sz",
space: "w:space",
color: "w:color",
};
}