docx-rs/docx-wasm/js/table-cell.ts

112 lines
2.4 KiB
TypeScript
Raw Normal View History

import { Paragraph } from "./paragraph";
2020-04-30 07:39:56 +03:00
import { TableCellBorders, PositionKeys } from "./table-cell-borders";
import { BorderPosition, TableCellBorder } from "./table-cell-border";
import * as wasm from "./pkg";
export type VMergeType = "restart" | "continue";
export type VAlignType = "top" | "center" | "bottom";
export type TextDirectionType =
| "lr"
| "lrV"
| "rl"
| "rlV"
| "tb"
| "tbV"
| "tbRlV"
| "tbRl"
| "btLr"
| "lrTbV";
export const toTextDirectionWasmType = (
t: TextDirectionType
): wasm.TextDirectionType => {
switch (t) {
case "lr":
return wasm.TextDirectionType.Lr;
case "lrV":
return wasm.TextDirectionType.LrV;
case "rl":
return wasm.TextDirectionType.Rl;
case "rlV":
return wasm.TextDirectionType.RlV;
case "tb":
return wasm.TextDirectionType.Tb;
case "tbV":
return wasm.TextDirectionType.TbV;
case "tbRlV":
return wasm.TextDirectionType.TbRlV;
case "tbRl":
return wasm.TextDirectionType.TbRl;
case "btLr":
return wasm.TextDirectionType.BtLr;
case "lrTbV":
return wasm.TextDirectionType.LrTbV;
default:
throw new Error("unreachable");
}
};
export type CellProperty = {
2020-04-30 07:39:56 +03:00
borders: TableCellBorders;
verticalMerge?: VMergeType;
verticalAlign?: VAlignType;
gridSpan?: number;
width?: number;
textDirection?: TextDirectionType;
};
export class TableCell {
children: Paragraph[] = [];
2020-05-15 09:51:45 +03:00
hasNumberings = false;
2020-04-30 07:39:56 +03:00
property: CellProperty = {
borders: new TableCellBorders(),
};
addParagraph(p: Paragraph) {
2020-05-15 09:51:45 +03:00
if (p.hasNumberings) {
this.hasNumberings = true;
}
this.children.push(p);
return this;
}
verticalMerge(t: VMergeType) {
this.property.verticalMerge = t;
return this;
}
verticalAlign(t: VAlignType) {
this.property.verticalAlign = t;
return this;
}
gridSpan(v: number) {
this.property.gridSpan = v;
return this;
}
width(v: number) {
this.property.width = v;
return this;
}
textDirection(t: TextDirectionType) {
this.property.textDirection = t;
return this;
}
setBorder(position: BorderPosition, border: TableCellBorder) {
2020-04-30 07:39:56 +03:00
this.property.borders[position.toLowerCase() as PositionKeys] = border;
return this;
}
clearBorder(position: BorderPosition) {
2020-04-30 07:39:56 +03:00
this.property.borders[
position.toLowerCase() as PositionKeys
] = new TableCellBorder(position).border_type("nil");
return this;
}
}