Skip to content

Commit 7d11299

Browse files
authored
Fix: separate vertical alignment enums for ITableCellOptions and ISectionPropertiesOptions (#3079)
* fix: separate vertical alignment enums for table and section properties - Introduced `VerticalAlignTable` for table-cell vertical alignment with valid values: `top`, `center`, and `bottom`. - Added `VerticalAlignSection` for section properties, extending `VerticalAlignTable` with an additional value `both`. - Marked the original `VerticalAlign` as deprecated for backward compatibility, directing users to the new enums. - Updated type definitions for better clarity on valid vertical alignments. * refactor: update vertical alignment imports and types for section and table properties - Renamed `VerticalAlign` to `VerticalAlignSection` in section properties and `VerticalAlignTable` in table-cell properties for clarity. - Updated type definitions to reflect the new enum names, ensuring better organization and understanding of vertical alignment options. - Adjusted related test cases to utilize the new imports and types. * refactor: update demos to use new enums for table and section properties for vertical alignment
1 parent 1e6c9e1 commit 7d11299

14 files changed

+81
-52
lines changed

demo/31-tables.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Example of how you would create a table and add data to it
22

33
import * as fs from "fs";
4-
import { Document, HeadingLevel, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign, TextDirection } from "docx";
4+
import { Document, HeadingLevel, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlignTable, TextDirection } from "docx";
55

66
const doc = new Document({
77
sections: [
@@ -13,11 +13,11 @@ const doc = new Document({
1313
children: [
1414
new TableCell({
1515
children: [new Paragraph({}), new Paragraph({})],
16-
verticalAlign: VerticalAlign.CENTER,
16+
verticalAlign: VerticalAlignTable.CENTER,
1717
}),
1818
new TableCell({
1919
children: [new Paragraph({}), new Paragraph({})],
20-
verticalAlign: VerticalAlign.CENTER,
20+
verticalAlign: VerticalAlignTable.CENTER,
2121
}),
2222
new TableCell({
2323
children: [new Paragraph({ text: "bottom to top" }), new Paragraph({})],
@@ -45,23 +45,23 @@ const doc = new Document({
4545
text: "This text should be in the middle of the cell",
4646
}),
4747
],
48-
verticalAlign: VerticalAlign.CENTER,
48+
verticalAlign: VerticalAlignTable.CENTER,
4949
}),
5050
new TableCell({
5151
children: [
5252
new Paragraph({
5353
text: "Text above should be vertical from bottom to top",
5454
}),
5555
],
56-
verticalAlign: VerticalAlign.CENTER,
56+
verticalAlign: VerticalAlignTable.CENTER,
5757
}),
5858
new TableCell({
5959
children: [
6060
new Paragraph({
6161
text: "Text above should be vertical from top to bottom",
6262
}),
6363
],
64-
verticalAlign: VerticalAlign.CENTER,
64+
verticalAlign: VerticalAlignTable.CENTER,
6565
}),
6666
],
6767
}),

demo/48-vertical-align.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Example of making content of section vertically aligned
22

33
import * as fs from "fs";
4-
import { Document, Packer, Paragraph, VerticalAlign, TextRun, Tab } from "docx";
4+
import { Document, Packer, Paragraph, VerticalAlignSection, TextRun, Tab } from "docx";
55

66
const doc = new Document({
77
sections: [
88
{
99
properties: {
10-
verticalAlign: VerticalAlign.CENTER,
10+
verticalAlign: VerticalAlignSection.CENTER,
1111
},
1212
children: [
1313
new Paragraph({

demo/49-table-borders.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
TableCell,
1313
TableRow,
1414
TextDirection,
15-
VerticalAlign,
15+
VerticalAlignTable,
1616
} from "docx";
1717

1818
const table = new Table({
@@ -102,11 +102,11 @@ const noBorderTable = new Table({
102102
children: [
103103
new TableCell({
104104
children: [new Paragraph({}), new Paragraph({})],
105-
verticalAlign: VerticalAlign.CENTER,
105+
verticalAlign: VerticalAlignTable.CENTER,
106106
}),
107107
new TableCell({
108108
children: [new Paragraph({}), new Paragraph({})],
109-
verticalAlign: VerticalAlign.CENTER,
109+
verticalAlign: VerticalAlignTable.CENTER,
110110
}),
111111
new TableCell({
112112
children: [new Paragraph({ text: "bottom to top" }), new Paragraph({})],
@@ -134,23 +134,23 @@ const noBorderTable = new Table({
134134
text: "This text should be in the middle of the cell",
135135
}),
136136
],
137-
verticalAlign: VerticalAlign.CENTER,
137+
verticalAlign: VerticalAlignTable.CENTER,
138138
}),
139139
new TableCell({
140140
children: [
141141
new Paragraph({
142142
text: "Text above should be vertical from bottom to top",
143143
}),
144144
],
145-
verticalAlign: VerticalAlign.CENTER,
145+
verticalAlign: VerticalAlignTable.CENTER,
146146
}),
147147
new TableCell({
148148
children: [
149149
new Paragraph({
150150
text: "Text above should be vertical from top to bottom",
151151
}),
152152
],
153-
verticalAlign: VerticalAlign.CENTER,
153+
verticalAlign: VerticalAlignTable.CENTER,
154154
}),
155155
],
156156
}),

demo/50-readme-demo.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// The demo on the README.md
22

33
import * as fs from "fs";
4-
import { Document, HeadingLevel, ImageRun, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign } from "docx";
4+
import { Document, HeadingLevel, ImageRun, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlignTable } from "docx";
55

66
const table = new Table({
77
rows: [
@@ -13,6 +13,7 @@ const table = new Table({
1313
children: [
1414
new ImageRun({
1515
data: fs.readFileSync("./demo/images/image1.jpeg"),
16+
type: "jpg",
1617
transformation: {
1718
width: 100,
1819
height: 100,
@@ -21,7 +22,7 @@ const table = new Table({
2122
],
2223
}),
2324
],
24-
verticalAlign: VerticalAlign.CENTER,
25+
verticalAlign: VerticalAlignTable.CENTER,
2526
}),
2627
new TableCell({
2728
children: [
@@ -30,7 +31,7 @@ const table = new Table({
3031
heading: HeadingLevel.HEADING_1,
3132
}),
3233
],
33-
verticalAlign: VerticalAlign.CENTER,
34+
verticalAlign: VerticalAlignTable.CENTER,
3435
}),
3536
],
3637
}),
@@ -50,6 +51,7 @@ const table = new Table({
5051
children: [
5152
new ImageRun({
5253
data: fs.readFileSync("./demo/images/image1.jpeg"),
54+
type: "jpg",
5355
transformation: {
5456
width: 100,
5557
height: 100,
@@ -77,6 +79,7 @@ const doc = new Document({
7779
children: [
7880
new ImageRun({
7981
data: fs.readFileSync("./demo/images/pizza.gif"),
82+
type: "gif",
8083
transformation: {
8184
width: 100,
8285
height: 100,

demo/79-table-from-data-source.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
Table,
1010
TableCell,
1111
TableRow,
12-
VerticalAlign,
12+
VerticalAlignTable,
1313
TextDirection,
1414
TextRun,
1515
WidthType,
@@ -101,17 +101,17 @@ const generateRows = (prices: StockPrice[]): TableRow[] =>
101101
children: [
102102
new TableCell({
103103
children: [new Paragraph(date.toString())],
104-
verticalAlign: VerticalAlign.CENTER,
104+
verticalAlign: VerticalAlignTable.CENTER,
105105
textDirection: TextDirection.LEFT_TO_RIGHT_TOP_TO_BOTTOM,
106106
}),
107107
new TableCell({
108108
children: [new Paragraph(ticker)],
109-
verticalAlign: VerticalAlign.CENTER,
109+
verticalAlign: VerticalAlignTable.CENTER,
110110
textDirection: TextDirection.LEFT_TO_RIGHT_TOP_TO_BOTTOM,
111111
}),
112112
new TableCell({
113113
children: [new Paragraph(price.toString())],
114-
verticalAlign: VerticalAlign.CENTER,
114+
verticalAlign: VerticalAlignTable.CENTER,
115115
textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT,
116116
}),
117117
],
@@ -143,7 +143,7 @@ const doc = new Document({
143143
],
144144
}),
145145
],
146-
verticalAlign: VerticalAlign.CENTER,
146+
verticalAlign: VerticalAlignTable.CENTER,
147147
textDirection: TextDirection.LEFT_TO_RIGHT_TOP_TO_BOTTOM,
148148
}),
149149
new TableCell({
@@ -159,7 +159,7 @@ const doc = new Document({
159159
],
160160
}),
161161
],
162-
verticalAlign: VerticalAlign.CENTER,
162+
verticalAlign: VerticalAlignTable.CENTER,
163163
textDirection: TextDirection.LEFT_TO_RIGHT_TOP_TO_BOTTOM,
164164
}),
165165
new TableCell({
@@ -175,7 +175,7 @@ const doc = new Document({
175175
],
176176
}),
177177
],
178-
verticalAlign: VerticalAlign.CENTER,
178+
verticalAlign: VerticalAlignTable.CENTER,
179179
textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT,
180180
}),
181181
],

demo/85-template-document.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
TableRow,
1414
TextDirection,
1515
TextRun,
16-
VerticalAlign,
16+
VerticalAlignTable,
1717
} from "docx";
1818

1919
patchDocument({
@@ -105,11 +105,11 @@ patchDocument({
105105
children: [
106106
new TableCell({
107107
children: [new Paragraph({}), new Paragraph({})],
108-
verticalAlign: VerticalAlign.CENTER,
108+
verticalAlign: VerticalAlignTable.CENTER,
109109
}),
110110
new TableCell({
111111
children: [new Paragraph({}), new Paragraph({})],
112-
verticalAlign: VerticalAlign.CENTER,
112+
verticalAlign: VerticalAlignTable.CENTER,
113113
}),
114114
new TableCell({
115115
children: [new Paragraph({ text: "bottom to top" }), new Paragraph({})],
@@ -137,23 +137,23 @@ patchDocument({
137137
text: "This text should be in the middle of the cell",
138138
}),
139139
],
140-
verticalAlign: VerticalAlign.CENTER,
140+
verticalAlign: VerticalAlignTable.CENTER,
141141
}),
142142
new TableCell({
143143
children: [
144144
new Paragraph({
145145
text: "Text above should be vertical from bottom to top",
146146
}),
147147
],
148-
verticalAlign: VerticalAlign.CENTER,
148+
verticalAlign: VerticalAlignTable.CENTER,
149149
}),
150150
new TableCell({
151151
children: [
152152
new Paragraph({
153153
text: "Text above should be vertical from top to bottom",
154154
}),
155155
],
156-
verticalAlign: VerticalAlign.CENTER,
156+
verticalAlign: VerticalAlignTable.CENTER,
157157
}),
158158
],
159159
}),

demo/96-template-document.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
TableRow,
1414
TextDirection,
1515
TextRun,
16-
VerticalAlign,
16+
VerticalAlignTable,
1717
} from "docx";
1818

1919
patchDocument({
@@ -105,11 +105,11 @@ patchDocument({
105105
children: [
106106
new TableCell({
107107
children: [new Paragraph({}), new Paragraph({})],
108-
verticalAlign: VerticalAlign.CENTER,
108+
verticalAlign: VerticalAlignTable.CENTER,
109109
}),
110110
new TableCell({
111111
children: [new Paragraph({}), new Paragraph({})],
112-
verticalAlign: VerticalAlign.CENTER,
112+
verticalAlign: VerticalAlignTable.CENTER,
113113
}),
114114
new TableCell({
115115
children: [new Paragraph({ text: "bottom to top" }), new Paragraph({})],
@@ -137,23 +137,23 @@ patchDocument({
137137
text: "This text should be in the middle of the cell",
138138
}),
139139
],
140-
verticalAlign: VerticalAlign.CENTER,
140+
verticalAlign: VerticalAlignTable.CENTER,
141141
}),
142142
new TableCell({
143143
children: [
144144
new Paragraph({
145145
text: "Text above should be vertical from bottom to top",
146146
}),
147147
],
148-
verticalAlign: VerticalAlign.CENTER,
148+
verticalAlign: VerticalAlignTable.CENTER,
149149
}),
150150
new TableCell({
151151
children: [
152152
new Paragraph({
153153
text: "Text above should be vertical from top to bottom",
154154
}),
155155
],
156-
verticalAlign: VerticalAlign.CENTER,
156+
verticalAlign: VerticalAlignTable.CENTER,
157157
}),
158158
],
159159
}),

docs/usage/tables.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const tableRow = new TableRow({
159159
| children | `Array<Paragraph or Table>` | Required. You can nest tables by adding a table into a cell |
160160
| shading | `IShadingAttributesProperties` | Optional |
161161
| margins | `ITableCellMarginOptions` | Optional |
162-
| verticalAlign | `VerticalAlign` | Optional |
162+
| verticalAlign | `VerticalAlignTable` | Optional |
163163
| columnSpan | `number` | Optional |
164164
| rowSpan | `number` | Optional |
165165
| borders | `BorderOptions` | Optional |
@@ -266,7 +266,7 @@ Sets the vertical alignment of the contents of the cell
266266
```ts
267267
const cell = new TableCell({
268268
...,
269-
verticalAlign: VerticalAlign,
269+
verticalAlign: VerticalAlignTable,
270270
});
271271
```
272272

@@ -282,7 +282,7 @@ For example, to center align a cell:
282282

283283
```ts
284284
const cell = new TableCell({
285-
verticalAlign: VerticalAlign.CENTER,
285+
verticalAlign: VerticalAlignTable.CENTER,
286286
});
287287
```
288288

src/file/document/body/section-properties/section-properties.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { FooterWrapper } from "@file/footer-wrapper";
66
import { HeaderWrapper } from "@file/header-wrapper";
77
import { Media } from "@file/media";
88
import { NumberFormat } from "@file/shared/number-format";
9-
import { VerticalAlign } from "@file/vertical-align";
9+
import { VerticalAlignSection } from "@file/vertical-align";
1010
import { convertInchesToTwip } from "@util/convenience-functions";
1111

1212
import { PageOrientation } from "./properties";
@@ -75,7 +75,7 @@ describe("SectionProperties", () => {
7575
even: new FooterWrapper(media, 200),
7676
},
7777
titlePage: true,
78-
verticalAlign: VerticalAlign.TOP,
78+
verticalAlign: VerticalAlignSection.TOP,
7979
});
8080

8181
const tree = new Formatter().format(properties);

src/file/document/body/section-properties/section-properties.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { FooterWrapper } from "@file/footer-wrapper";
44
import { HeaderWrapper } from "@file/header-wrapper";
5-
import { VerticalAlign, VerticalAlignElement } from "@file/vertical-align";
5+
import { SectionVerticalAlign, VerticalAlignElement } from "@file/vertical-align";
66
import { OnOffElement, XmlComponent } from "@file/xml-components";
77

88
import { Columns, IColumnsAttributes } from "./properties/columns";
@@ -35,7 +35,7 @@ export type ISectionPropertiesOptions = {
3535
readonly footerWrapperGroup?: IHeaderFooterGroup<FooterWrapper>;
3636
readonly lineNumbers?: ILineNumberAttributes;
3737
readonly titlePage?: boolean;
38-
readonly verticalAlign?: (typeof VerticalAlign)[keyof typeof VerticalAlign];
38+
readonly verticalAlign?: SectionVerticalAlign;
3939
readonly column?: IColumnsAttributes;
4040
readonly type?: (typeof SectionType)[keyof typeof SectionType];
4141
};

0 commit comments

Comments
 (0)