/
formatTable.ts
37 lines (35 loc) · 1.29 KB
/
formatTable.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
import formatUndoSnapshot from '../utils/formatUndoSnapshot';
import { VTable } from 'roosterjs-editor-dom';
import type { IEditor, TableFormat } from 'roosterjs-editor-types';
/**
* Format table
* @param editor The editor which contains the table to format
* @param format A TableFormat object contains format information we want to apply to the table
* @param table The table to format. This is optional. When not passed, the current table (if any) will be formatted
*/
export default function formatTable(
editor: IEditor,
format: TableFormat,
table?: HTMLTableElement
) {
table = table || (editor.getElementAtCursor('TABLE') as HTMLTableElement);
if (table) {
formatUndoSnapshot(
editor,
(start, end) => {
if (!table) {
return;
}
const vtable = new VTable(table);
vtable.applyFormat(format);
vtable.writeBack(false /** skipApplyFormat */, editor.getDarkColorHandler());
editor.transformToDarkColor(vtable.table);
editor.focus();
if (start && end) {
editor.select(start, end);
}
},
'formatTable'
);
}
}