-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathtable-cell.ts
More file actions
74 lines (60 loc) · 1.76 KB
/
table-cell.ts
File metadata and controls
74 lines (60 loc) · 1.76 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import '../types.js'
import { mergeAttributes, Node } from '@tiptap/core'
import { createAlignAttribute } from '../utilities/parseAlign.js'
export interface TableCellOptions {
/**
* The HTML attributes for a table cell node.
* @default {}
* @example { class: 'foo' }
*/
HTMLAttributes: Record<string, any>
}
/**
* This extension allows you to create table cells.
* @see https://www.tiptap.dev/api/nodes/table-cell
*/
export const TableCell = Node.create<TableCellOptions>({
name: 'tableCell',
addOptions() {
return {
HTMLAttributes: {},
}
},
content: 'block+',
addAttributes() {
return {
colspan: {
default: 1,
},
rowspan: {
default: 1,
},
colwidth: {
default: null,
parseHTML: element => {
const colwidth = element.getAttribute('colwidth')
const value = colwidth ? colwidth.split(',').map(width => parseInt(width, 10)) : null
// if there is no colwidth attribute on the cell, try to get it from the colgroup
if (!value) {
const cols = element.closest('table')?.querySelectorAll('colgroup > col')
const cellIndex = Array.from(element.parentElement?.children || []).indexOf(element)
if (cellIndex && cellIndex > -1 && cols && cols[cellIndex]) {
const colWidth = cols[cellIndex].getAttribute('width')
return colWidth ? [parseInt(colWidth, 10)] : null
}
}
return value
},
},
align: createAlignAttribute(),
}
},
tableRole: 'cell',
isolating: true,
parseHTML() {
return [{ tag: 'td' }]
},
renderHTML({ HTMLAttributes }) {
return ['td', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
})