forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.js
191 lines (168 loc) · 4.73 KB
/
table.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import Block from '../blots/block';
import Container from '../blots/container';
class TableCell extends Block {
static create(value) {
const node = super.create();
if (value) {
node.setAttribute('data-row', value);
} else {
node.setAttribute('data-row', tableId());
}
return node;
}
static formats(domNode) {
if (domNode.hasAttribute('data-row')) {
return domNode.getAttribute('data-row');
}
return undefined;
}
cellOffset() {
if (this.parent) {
return this.parent.children.indexOf(this);
}
return -1;
}
format(name, value) {
if (name === TableCell.blotName && value) {
this.domNode.setAttribute('data-row', value);
} else {
super.format(name, value);
}
}
row() {
return this.parent;
}
rowOffset() {
if (this.row()) {
return this.row().rowOffset();
}
return -1;
}
table() {
return this.row() && this.row().table();
}
}
TableCell.blotName = 'table';
TableCell.tagName = 'TD';
class TableRow extends Container {
checkMerge() {
if (super.checkMerge() && this.next.children.head != null) {
const thisHead = this.children.head.formats();
const thisTail = this.children.tail.formats();
const nextHead = this.next.children.head.formats();
const nextTail = this.next.children.tail.formats();
return (
thisHead.table === thisTail.table &&
thisHead.table === nextHead.table &&
thisHead.table === nextTail.table
);
}
return false;
}
optimize(...args) {
super.optimize(...args);
this.children.forEach(child => {
if (child.next == null) return;
const childFormats = child.formats();
const nextFormats = child.next.formats();
if (childFormats.table !== nextFormats.table) {
const next = this.splitAfter(child);
if (next) {
next.optimize();
}
// We might be able to merge with prev now
if (this.prev) {
this.prev.optimize();
}
}
});
}
rowOffset() {
if (this.parent) {
return this.parent.children.indexOf(this);
}
return -1;
}
table() {
return this.parent && this.parent.parent;
}
}
TableRow.blotName = 'table-row';
TableRow.tagName = 'TR';
class TableBody extends Container {}
TableBody.blotName = 'table-body';
TableBody.tagName = 'TBODY';
class TableContainer extends Container {
balanceCells() {
const rows = this.descendants(TableRow);
const maxColumns = rows.reduce((max, row) => {
return Math.max(row.children.length, max);
}, 0);
rows.forEach(row => {
new Array(maxColumns - row.children.length).fill(0).forEach(() => {
let value;
if (row.children.head != null) {
value = TableCell.formats(row.children.head.domNode);
}
const blot = this.scroll.create(TableCell.blotName, value);
row.appendChild(blot);
blot.optimize(); // Add break blot
});
});
}
cells(column) {
return this.rows().map(row => row.children.at(column));
}
deleteColumn(index) {
const [body] = this.descendant(TableBody);
if (body == null || body.children.head == null) return;
body.children.forEach(row => {
const cell = row.children.at(index);
if (cell != null) {
cell.remove();
}
});
}
insertColumn(index) {
const [body] = this.descendant(TableBody);
if (body == null || body.children.head == null) return;
body.children.forEach(row => {
const ref = row.children.at(index);
const value = TableCell.formats(row.children.head.domNode);
const cell = this.scroll.create(TableCell.blotName, value);
row.insertBefore(cell, ref);
});
}
insertRow(index) {
const [body] = this.descendant(TableBody);
if (body == null || body.children.head == null) return;
const id = tableId();
const row = this.scroll.create(TableRow.blotName);
body.children.head.children.forEach(() => {
const cell = this.scroll.create(TableCell.blotName, id);
row.appendChild(cell);
});
const ref = body.children.at(index);
body.insertBefore(row, ref);
}
rows() {
const body = this.children.head;
if (body == null) return [];
return body.children.map(row => row);
}
}
TableContainer.blotName = 'table-container';
TableContainer.tagName = 'TABLE';
TableContainer.allowedChildren = [TableBody];
TableBody.requiredContainer = TableContainer;
TableBody.allowedChildren = [TableRow];
TableRow.requiredContainer = TableBody;
TableRow.allowedChildren = [TableCell];
TableCell.requiredContainer = TableRow;
function tableId() {
const id = Math.random()
.toString(36)
.slice(2, 6);
return `row-${id}`;
}
export { TableCell, TableRow, TableBody, TableContainer, tableId };