Skip to content

Commit 14b5dc3

Browse files
authored
Add minWidth cell to insertTable (#3239)
Add a new parameter, customCellFormat, to the insertTable function to allow custom styles for tables.
1 parent 7112947 commit 14b5dc3

File tree

5 files changed

+149
-5
lines changed

5 files changed

+149
-5
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
import { addBlock, createTable, createTableCell } from 'roosterjs-content-model-dom';
2-
import type { ContentModelBlockGroup, ContentModelTable } from 'roosterjs-content-model-types';
2+
import type {
3+
ContentModelBlockGroup,
4+
ContentModelTable,
5+
ContentModelTableCellFormat,
6+
} from 'roosterjs-content-model-types';
37

48
/**
59
* @internal
610
*/
711
export function createTableStructure(
812
parent: ContentModelBlockGroup,
913
columns: number,
10-
rows: number
14+
rows: number,
15+
cellFormat?: ContentModelTableCellFormat
1116
): ContentModelTable {
1217
const table = createTable(rows);
1318

1419
addBlock(parent, table);
1520

1621
table.rows.forEach(row => {
1722
for (let i = 0; i < columns; i++) {
18-
const cell = createTableCell();
23+
const cell = createTableCell(
24+
undefined /*spanLeftOrColSpan */,
25+
undefined /*spanAboveOrRowSpan */,
26+
undefined /* isHeader */,
27+
cellFormat
28+
);
1929

2030
row.cells.push(cell);
2131
}

packages/roosterjs-content-model-api/lib/publicApi/table/insertTable.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
ContentModelTableFormat,
1616
IEditor,
1717
TableMetadataFormat,
18+
ContentModelTableCellFormat,
1819
} from 'roosterjs-content-model-types';
1920

2021
/**
@@ -25,13 +26,15 @@ import type {
2526
* @param rows Number of rows in table
2627
* @param tableMetadataFormat (Optional) The table format that are stored as metadata. If not passed, the default format will be applied: background color: #FFF; border color: #ABABAB
2728
* @param format (Optional) The table format used for style attributes
29+
* @param cellFormat (Optional) custom format for table cells, except for borders styles, for borders use tableMetadataFormat
2830
*/
2931
export function insertTable(
3032
editor: IEditor,
3133
columns: number,
3234
rows: number,
3335
tableMetadataFormat?: Partial<TableMetadataFormat>,
34-
format?: ContentModelTableFormat
36+
format?: ContentModelTableFormat,
37+
customCellFormat?: ContentModelTableCellFormat
3538
) {
3639
editor.focus();
3740

@@ -41,7 +44,7 @@ export function insertTable(
4144

4245
if (insertPosition) {
4346
const doc = createContentModelDocument();
44-
const table = createTableStructure(doc, columns, rows);
47+
const table = createTableStructure(doc, columns, rows, customCellFormat);
4548
if (format) {
4649
table.format = { ...format };
4750
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,18 @@ describe('createTableStructure', () => {
4343
expect(table.rows.length).toBe(2);
4444
expect(table.rows[0].cells.length).toBe(3);
4545
});
46+
47+
it('Create 2*3 table with cell format', () => {
48+
const doc = createContentModelDocument();
49+
50+
createTableStructure(doc, 3, 2, {
51+
minWidth: '15px',
52+
});
53+
54+
const table = doc.blocks[0] as ContentModelTable;
55+
56+
expect(table.rows.length).toBe(2);
57+
expect(table.rows[0].cells.length).toBe(3);
58+
expect(table.rows[0].cells[0].format.minWidth).toBe('15px');
59+
});
4660
});

packages/roosterjs-content-model-api/test/publicApi/table/insertTableTest.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,118 @@ describe('insertTable', () => {
326326
],
327327
});
328328
});
329+
it('should insert table with minimum width', () => {
330+
// Arrange
331+
const model: ContentModelDocument = {
332+
blockGroupType: 'Document',
333+
blocks: [
334+
{
335+
blockType: 'Paragraph',
336+
segments: [
337+
{
338+
segmentType: 'SelectionMarker',
339+
isSelected: true,
340+
format: {},
341+
},
342+
],
343+
format: {},
344+
},
345+
],
346+
};
347+
348+
let resultModel: ContentModelDocument | null = null;
349+
350+
formatContentModelSpy.and.callFake((callback: any) => {
351+
const result = callback(model, {
352+
newEntities: [],
353+
deletedEntities: [],
354+
newImages: [],
355+
});
356+
resultModel = model;
357+
return result;
358+
});
359+
360+
// Act
361+
insertTable(
362+
editor,
363+
1,
364+
1,
365+
undefined,
366+
{
367+
marginBottom: '1px',
368+
},
369+
{
370+
minWidth: '15px',
371+
}
372+
);
373+
374+
// Assert
375+
expect(resultModel!).toEqual({
376+
blockGroupType: 'Document',
377+
blocks: [
378+
{
379+
blockType: 'Table',
380+
rows: [
381+
{
382+
format: {},
383+
height: jasmine.any(Number),
384+
cells: [
385+
{
386+
blockGroupType: 'TableCell',
387+
blocks: [
388+
{
389+
blockType: 'Paragraph',
390+
segments: [
391+
{
392+
segmentType: 'SelectionMarker',
393+
isSelected: true,
394+
format: {},
395+
},
396+
{
397+
segmentType: 'Br',
398+
format: {},
399+
},
400+
],
401+
format: {},
402+
},
403+
],
404+
format: {
405+
minWidth: '15px',
406+
useBorderBox: true,
407+
borderTop: '1px solid #ABABAB',
408+
borderRight: '1px solid #ABABAB',
409+
borderBottom: '1px solid #ABABAB',
410+
borderLeft: '1px solid #ABABAB',
411+
verticalAlign: 'top',
412+
},
413+
spanLeft: false,
414+
spanAbove: false,
415+
isHeader: false,
416+
dataset: {},
417+
},
418+
],
419+
},
420+
],
421+
format: {
422+
borderCollapse: true,
423+
useBorderBox: true,
424+
marginBottom: '1px',
425+
},
426+
widths: jasmine.any(Array),
427+
dataset: jasmine.any(Object),
428+
},
429+
{
430+
blockType: 'Paragraph',
431+
segments: [
432+
{
433+
segmentType: 'Br',
434+
format: {},
435+
},
436+
],
437+
format: {},
438+
},
439+
],
440+
});
441+
});
329442
});
330443
});

packages/roosterjs-react/lib/ribbon/buttons/insertTableButton.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export const insertTableButton: RibbonButton<InsertTableButtonStringKey> = {
7070
: {},
7171
{
7272
marginBottom: '1px',
73+
},
74+
{
75+
minWidth: '15px',
76+
borderBottom: '1px dotted red',
7377
}
7478
);
7579
},

0 commit comments

Comments
 (0)