2020-01-30 16:14:25 +02:00
|
|
|
import { Paragraph } from "./paragraph";
|
2024-03-25 08:37:32 +02:00
|
|
|
import { Table, convertWidthType } from "./table";
|
2021-03-18 12:02:28 +02:00
|
|
|
import { Shading } from "./shading";
|
2020-04-30 07:39:56 +03:00
|
|
|
import { TableCellBorders, PositionKeys } from "./table-cell-borders";
|
2021-03-16 09:09:39 +02:00
|
|
|
import { TableCellBorderPosition, TableCellBorder } from "./table-cell-border";
|
2021-03-03 03:35:50 +02:00
|
|
|
import * as wasm from "./pkg";
|
2024-11-22 03:58:49 +02:00
|
|
|
import { convertBorderType } from "./run-property";
|
2023-03-16 11:48:24 +02:00
|
|
|
import { TableOfContents } from "./table-of-contents";
|
2023-02-06 11:42:25 +02:00
|
|
|
import { build } from "./builder";
|
2024-03-25 08:37:32 +02:00
|
|
|
import { WidthType } from ".";
|
2020-01-30 16:14:25 +02:00
|
|
|
|
|
|
|
export type VMergeType = "restart" | "continue";
|
|
|
|
|
2020-03-19 18:19:39 +02:00
|
|
|
export type VAlignType = "top" | "center" | "bottom";
|
|
|
|
|
2021-03-03 03:35:50 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
export type CellProperty = {
|
2020-04-30 07:39:56 +03:00
|
|
|
borders: TableCellBorders;
|
2020-01-30 16:14:25 +02:00
|
|
|
verticalMerge?: VMergeType;
|
2020-03-19 18:19:39 +02:00
|
|
|
verticalAlign?: VAlignType;
|
2020-01-30 16:14:25 +02:00
|
|
|
gridSpan?: number;
|
|
|
|
width?: number;
|
2021-03-03 03:35:50 +02:00
|
|
|
textDirection?: TextDirectionType;
|
2021-03-18 12:02:28 +02:00
|
|
|
shading?: Shading;
|
2024-03-25 08:37:32 +02:00
|
|
|
margins?: {
|
|
|
|
top?: { val: number; type: WidthType };
|
|
|
|
left?: { val: number; type: WidthType };
|
|
|
|
bottom?: { val: number; type: WidthType };
|
|
|
|
right?: { val: number; type: WidthType };
|
|
|
|
};
|
2020-01-30 16:14:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export class TableCell {
|
2023-03-16 11:48:24 +02:00
|
|
|
children: (Paragraph | Table | TableOfContents)[] = [];
|
2020-05-15 09:51:45 +03:00
|
|
|
hasNumberings = false;
|
2020-04-30 07:39:56 +03:00
|
|
|
property: CellProperty = {
|
|
|
|
borders: new TableCellBorders(),
|
|
|
|
};
|
2020-01-30 16:14:25 +02:00
|
|
|
|
|
|
|
addParagraph(p: Paragraph) {
|
2020-05-15 09:51:45 +03:00
|
|
|
if (p.hasNumberings) {
|
|
|
|
this.hasNumberings = true;
|
|
|
|
}
|
2020-01-30 16:14:25 +02:00
|
|
|
this.children.push(p);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-03-16 11:48:24 +02:00
|
|
|
addTableOfContents(t: TableOfContents) {
|
|
|
|
this.children.push(t);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:47:22 +02:00
|
|
|
addTable(t: Table) {
|
|
|
|
if (t.hasNumberings) {
|
|
|
|
this.hasNumberings = true;
|
|
|
|
}
|
|
|
|
this.children.push(t);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
verticalMerge(t: VMergeType) {
|
|
|
|
this.property.verticalMerge = t;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-03-19 18:19:39 +02:00
|
|
|
verticalAlign(t: VAlignType) {
|
|
|
|
this.property.verticalAlign = t;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
gridSpan(v: number) {
|
|
|
|
this.property.gridSpan = v;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
width(v: number) {
|
|
|
|
this.property.width = v;
|
|
|
|
return this;
|
|
|
|
}
|
2020-04-27 04:41:23 +03:00
|
|
|
|
2021-03-19 05:31:36 +02:00
|
|
|
shading(type: string, color: string, fill: string) {
|
2021-03-18 12:02:28 +02:00
|
|
|
const s = new Shading();
|
|
|
|
s.color(color);
|
|
|
|
s.fill(fill);
|
2021-03-19 05:31:36 +02:00
|
|
|
s.type(type);
|
2021-03-18 12:02:28 +02:00
|
|
|
this.property.shading = s;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-03-03 03:35:50 +02:00
|
|
|
textDirection(t: TextDirectionType) {
|
|
|
|
this.property.textDirection = t;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:09:39 +02:00
|
|
|
setBorder(border: TableCellBorder) {
|
2022-12-13 06:33:48 +02:00
|
|
|
this.property.borders[border.position.toLowerCase() as PositionKeys] =
|
|
|
|
border;
|
2020-04-27 04:41:23 +03:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:09:39 +02:00
|
|
|
clearBorder(position: TableCellBorderPosition) {
|
2022-12-13 06:33:48 +02:00
|
|
|
this.property.borders[position.toLowerCase() as PositionKeys] =
|
|
|
|
new TableCellBorder(position).border_type("nil");
|
2020-04-27 04:41:23 +03:00
|
|
|
return this;
|
|
|
|
}
|
2022-12-13 06:33:48 +02:00
|
|
|
|
2024-03-25 08:37:32 +02:00
|
|
|
marginTop(v: number, t: WidthType) {
|
|
|
|
this.property.margins = {
|
|
|
|
...this.property.margins,
|
|
|
|
top: { val: v, type: t },
|
|
|
|
};
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
marginLeft(v: number, t: WidthType) {
|
|
|
|
this.property.margins = {
|
|
|
|
...this.property.margins,
|
|
|
|
left: { val: v, type: t },
|
|
|
|
};
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
marginRight(v: number, t: WidthType) {
|
|
|
|
this.property.margins = {
|
|
|
|
...this.property.margins,
|
|
|
|
right: { val: v, type: t },
|
|
|
|
};
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
marginBottom(v: number, t: WidthType) {
|
|
|
|
this.property.margins = {
|
|
|
|
...this.property.margins,
|
|
|
|
bottom: { val: v, type: t },
|
|
|
|
};
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-12-13 06:33:48 +02:00
|
|
|
buildCellBorders(cell: wasm.TableCell): wasm.TableCell {
|
|
|
|
if (this.property.borders.top) {
|
|
|
|
const border = wasm
|
|
|
|
.createTableCellBorder(wasm.TableCellBorderPosition.Top)
|
|
|
|
.size(this.property.borders.top._size)
|
|
|
|
.color(this.property.borders.top._color)
|
|
|
|
.border_type(convertBorderType(this.property.borders.top._border_type));
|
|
|
|
cell = cell.set_border(border);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.property.borders.right) {
|
|
|
|
const border = wasm
|
|
|
|
.createTableCellBorder(wasm.TableCellBorderPosition.Right)
|
|
|
|
.size(this.property.borders.right._size)
|
|
|
|
.color(this.property.borders.right._color)
|
|
|
|
.border_type(
|
|
|
|
convertBorderType(this.property.borders.right._border_type)
|
|
|
|
);
|
|
|
|
cell = cell.set_border(border);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.property.borders.bottom) {
|
|
|
|
const border = wasm
|
|
|
|
.createTableCellBorder(wasm.TableCellBorderPosition.Bottom)
|
|
|
|
.size(this.property.borders.bottom._size)
|
|
|
|
.color(this.property.borders.bottom._color)
|
|
|
|
.border_type(
|
|
|
|
convertBorderType(this.property.borders.bottom._border_type)
|
|
|
|
);
|
|
|
|
cell = cell.set_border(border);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.property.borders.left) {
|
|
|
|
const border = wasm
|
|
|
|
.createTableCellBorder(wasm.TableCellBorderPosition.Left)
|
|
|
|
.size(this.property.borders.left._size)
|
|
|
|
.color(this.property.borders.left._color)
|
|
|
|
.border_type(
|
|
|
|
convertBorderType(this.property.borders.left._border_type)
|
|
|
|
);
|
|
|
|
cell = cell.set_border(border);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.property.borders.insideH) {
|
|
|
|
const border = wasm
|
|
|
|
.createTableCellBorder(wasm.TableCellBorderPosition.InsideH)
|
|
|
|
.size(this.property.borders.insideH._size)
|
|
|
|
.color(this.property.borders.insideH._color)
|
|
|
|
.border_type(
|
|
|
|
convertBorderType(this.property.borders.insideH._border_type)
|
|
|
|
);
|
|
|
|
cell = cell.set_border(border);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.property.borders.insideV) {
|
|
|
|
const border = wasm
|
|
|
|
.createTableCellBorder(wasm.TableCellBorderPosition.InsideV)
|
|
|
|
.size(this.property.borders.insideV._size)
|
|
|
|
.color(this.property.borders.insideV._color)
|
|
|
|
.border_type(
|
|
|
|
convertBorderType(this.property.borders.insideV._border_type)
|
|
|
|
);
|
|
|
|
cell = cell.set_border(border);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.property.borders.tl2br) {
|
|
|
|
const border = wasm
|
|
|
|
.createTableCellBorder(wasm.TableCellBorderPosition.Tl2br)
|
|
|
|
.size(this.property.borders.tl2br._size)
|
|
|
|
.color(this.property.borders.tl2br._color)
|
|
|
|
.border_type(
|
|
|
|
convertBorderType(this.property.borders.tl2br._border_type)
|
|
|
|
);
|
|
|
|
cell = cell.set_border(border);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.property.borders.tr2bl) {
|
|
|
|
const border = wasm
|
|
|
|
.createTableCellBorder(wasm.TableCellBorderPosition.Tr2bl)
|
|
|
|
.size(this.property.borders.tr2bl._size)
|
|
|
|
.color(this.property.borders.tr2bl._color)
|
|
|
|
.border_type(
|
|
|
|
convertBorderType(this.property.borders.tr2bl._border_type)
|
|
|
|
);
|
|
|
|
cell = cell.set_border(border);
|
|
|
|
}
|
|
|
|
|
2024-03-25 08:37:32 +02:00
|
|
|
if (this.property.margins?.top) {
|
|
|
|
cell = cell.margin_top(
|
|
|
|
this.property.margins?.top.val,
|
|
|
|
convertWidthType(this.property.margins?.top.type)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.property.margins?.right) {
|
|
|
|
cell = cell.margin_right(
|
|
|
|
this.property.margins?.right.val,
|
|
|
|
convertWidthType(this.property.margins?.right.type)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.property.margins?.bottom) {
|
|
|
|
cell = cell.margin_bottom(
|
|
|
|
this.property.margins?.bottom.val,
|
|
|
|
convertWidthType(this.property.margins?.bottom.type)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.property.margins?.left) {
|
|
|
|
cell = cell.margin_left(
|
|
|
|
this.property.margins?.left.val,
|
|
|
|
convertWidthType(this.property.margins?.left.type)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-13 06:33:48 +02:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
build() {
|
|
|
|
let cell = wasm.createTableCell();
|
|
|
|
this.children.forEach((c) => {
|
|
|
|
if (c instanceof Paragraph) {
|
2023-02-06 11:42:25 +02:00
|
|
|
cell = cell.add_paragraph(build(c));
|
2022-12-13 06:33:48 +02:00
|
|
|
} else if (c instanceof Table) {
|
|
|
|
const table = c.build();
|
|
|
|
cell = cell.add_table(table);
|
2023-03-16 11:48:24 +02:00
|
|
|
} else if (c instanceof TableOfContents) {
|
|
|
|
cell = cell.add_table_of_contents(c.buildWasmObject());
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
const _: never = c;
|
|
|
|
console.error(_);
|
2022-12-13 06:33:48 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.property.verticalMerge === "continue") {
|
|
|
|
cell = cell.vertical_merge(wasm.VMergeType.Continue);
|
|
|
|
} else if (this.property.verticalMerge === "restart") {
|
|
|
|
cell = cell.vertical_merge(wasm.VMergeType.Restart);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (this.property.verticalAlign) {
|
|
|
|
case "top": {
|
|
|
|
cell = cell.vertical_align(wasm.VAlignType.Top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "center": {
|
|
|
|
cell = cell.vertical_align(wasm.VAlignType.Center);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "bottom": {
|
|
|
|
cell = cell.vertical_align(wasm.VAlignType.Bottom);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof this.property.gridSpan !== "undefined") {
|
|
|
|
cell = cell.grid_span(this.property.gridSpan);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof this.property.width !== "undefined") {
|
|
|
|
cell = cell.width(this.property.width);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof this.property.textDirection !== "undefined") {
|
|
|
|
cell = cell.text_direction(
|
|
|
|
toTextDirectionWasmType(this.property.textDirection)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof this.property.borders !== "undefined") {
|
|
|
|
cell = this.buildCellBorders(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof this.property.shading !== "undefined") {
|
|
|
|
cell = cell.shading(
|
|
|
|
this.property.shading._type,
|
|
|
|
this.property.shading._color,
|
|
|
|
this.property.shading._fill
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
2020-01-30 16:14:25 +02:00
|
|
|
}
|