Skip to content

Commit eb4f463

Browse files
committed
align cell RTL
1 parent febf50f commit eb4f463

File tree

2 files changed

+123
-16
lines changed

2 files changed

+123
-16
lines changed

packages/roosterjs-content-model-api/lib/modelApi/table/alignTableCell.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ import type {
1010
TableCellVerticalAlignOperation,
1111
} from 'roosterjs-content-model-types';
1212

13-
const TextAlignValueMap: Partial<Record<
13+
const TextAlignValueMap: Record<
1414
TableCellHorizontalAlignOperation,
15-
'start' | 'center' | 'end'
16-
>> = {
17-
alignCellLeft: 'start',
18-
alignCellCenter: 'center',
19-
alignCellRight: 'end',
15+
Record<'ltr' | 'rtl', 'start' | 'center' | 'end' | 'initial' | 'justify'>
16+
> = {
17+
alignCellLeft: {
18+
ltr: 'start',
19+
rtl: 'end',
20+
},
21+
alignCellCenter: {
22+
ltr: 'center',
23+
rtl: 'center',
24+
},
25+
alignCellRight: {
26+
ltr: 'end',
27+
rtl: 'start',
28+
},
2029
};
2130

2231
const VerticalAlignValueMap: Partial<Record<
@@ -36,7 +45,8 @@ export function alignTableCellHorizontally(
3645
operation: TableCellHorizontalAlignOperation
3746
) {
3847
alignTableCellInternal(table, cell => {
39-
cell.format.textAlign = TextAlignValueMap[operation];
48+
cell.format.textAlign =
49+
TextAlignValueMap[operation][cell.format.direction == 'rtl' ? 'rtl' : 'ltr'];
4050
});
4151
}
4252

packages/roosterjs-content-model-api/test/modelApi/table/alignTableCellTest.ts

Lines changed: 106 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,82 @@ import {
1212
describe('alignTableCellHorizontally', () => {
1313
function runTest(
1414
operation: TableCellHorizontalAlignOperation,
15-
expectedFormat: ContentModelTableCellFormat
15+
expectedFormat: ContentModelTableCellFormat,
16+
isRTL?: boolean
1617
) {
1718
const table = createTable(2);
18-
table.rows[0].cells.push(createTableCell(1, 1, false));
19-
table.rows[0].cells.push(createTableCell(1, 1, false));
20-
table.rows[0].cells.push(createTableCell(1, 1, false));
21-
table.rows[1].cells.push(createTableCell(1, 1, false));
22-
table.rows[1].cells.push(createTableCell(1, 1, false));
23-
table.rows[1].cells.push(createTableCell(1, 1, false));
19+
table.rows[0].cells.push(
20+
createTableCell(
21+
1,
22+
1,
23+
false,
24+
isRTL
25+
? {
26+
direction: 'rtl',
27+
}
28+
: undefined
29+
)
30+
);
31+
table.rows[0].cells.push(
32+
createTableCell(
33+
1,
34+
1,
35+
false,
36+
isRTL
37+
? {
38+
direction: 'rtl',
39+
}
40+
: undefined
41+
)
42+
);
43+
table.rows[0].cells.push(
44+
createTableCell(
45+
1,
46+
1,
47+
false,
48+
isRTL
49+
? {
50+
direction: 'rtl',
51+
}
52+
: undefined
53+
)
54+
);
55+
table.rows[1].cells.push(
56+
createTableCell(
57+
1,
58+
1,
59+
false,
60+
isRTL
61+
? {
62+
direction: 'rtl',
63+
}
64+
: undefined
65+
)
66+
);
67+
table.rows[1].cells.push(
68+
createTableCell(
69+
1,
70+
1,
71+
false,
72+
isRTL
73+
? {
74+
direction: 'rtl',
75+
}
76+
: undefined
77+
)
78+
);
79+
table.rows[1].cells.push(
80+
createTableCell(
81+
1,
82+
1,
83+
false,
84+
isRTL
85+
? {
86+
direction: 'rtl',
87+
}
88+
: undefined
89+
)
90+
);
2491
table.rows[0].cells[1].isSelected = true;
2592
table.rows[0].cells[2].isSelected = true;
2693
table.rows[1].cells[1].isSelected = true;
@@ -36,12 +103,20 @@ describe('alignTableCellHorizontally', () => {
36103
alignTableCellHorizontally(table, operation);
37104

38105
expect(table.rows[0].cells.map(c => c.format)).toEqual([
39-
{},
106+
isRTL
107+
? {
108+
direction: 'rtl',
109+
}
110+
: {},
40111
expectedFormat,
41112
expectedFormat,
42113
]);
43114
expect(table.rows[1].cells.map(c => c.format)).toEqual([
44-
{},
115+
isRTL
116+
? {
117+
direction: 'rtl',
118+
}
119+
: {},
45120
expectedFormat,
46121
expectedFormat,
47122
]);
@@ -87,6 +162,28 @@ describe('alignTableCellHorizontally', () => {
87162
textAlign: 'end',
88163
});
89164
});
165+
166+
it('align to left - RTL', () => {
167+
runTest(
168+
'alignCellLeft',
169+
{
170+
direction: 'rtl',
171+
textAlign: 'end',
172+
},
173+
true /* isRTL */
174+
);
175+
});
176+
177+
it('align to right - RTL ', () => {
178+
runTest(
179+
'alignCellRight',
180+
{
181+
direction: 'rtl',
182+
textAlign: 'start',
183+
},
184+
true /* isRTL */
185+
);
186+
});
90187
});
91188

92189
describe('alignTableCellVertically', () => {

0 commit comments

Comments
 (0)