/
insertTableRow.ts
39 lines (36 loc) · 1.39 KB
/
insertTableRow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { clearSelectedCells } from './clearSelectedCells';
import { createTableCell, getSelectedCells } from 'roosterjs-content-model-dom';
import type {
ContentModelTable,
TableVerticalInsertOperation,
} from 'roosterjs-content-model-types';
/**
* Insert a row to the table
* @param table The table model where the row is to be inserted
* @param operation The operation to be performed
*/
export function insertTableRow(table: ContentModelTable, operation: TableVerticalInsertOperation) {
const sel = getSelectedCells(table);
const insertAbove = operation == 'insertAbove';
if (sel) {
clearSelectedCells(table, sel);
for (let i = sel.firstRow; i <= sel.lastRow; i++) {
const sourceRow = table.rows[insertAbove ? sel.firstRow : sel.lastRow];
table.rows.splice(insertAbove ? sel.firstRow : sel.lastRow + 1, 0, {
format: { ...sourceRow.format },
cells: sourceRow.cells.map(cell => {
const newCell = createTableCell(
cell.spanLeft,
cell.spanAbove,
cell.isHeader,
cell.format,
cell.dataset
);
newCell.isSelected = true;
return newCell;
}),
height: sourceRow.height,
});
}
}
}