forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.ts
154 lines (138 loc) · 3.87 KB
/
table.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
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
import Delta from 'quill-delta';
import Quill from '../core/quill';
import Module from '../core/module';
import {
TableCell,
TableRow,
TableBody,
TableContainer,
tableId,
} from '../formats/table';
class Table extends Module {
static register() {
Quill.register(TableCell);
Quill.register(TableRow);
Quill.register(TableBody);
Quill.register(TableContainer);
}
constructor(...args) {
// @ts-expect-error
super(...args);
this.listenBalanceCells();
}
balanceTables() {
// @ts-expect-error
this.quill.scroll.descendants(TableContainer).forEach(table => {
// @ts-expect-error
table.balanceCells();
});
}
deleteColumn() {
const [table, , cell] = this.getTable();
if (cell == null) return;
// @ts-expect-error
table.deleteColumn(cell.cellOffset());
this.quill.update(Quill.sources.USER);
}
deleteRow() {
const [, row] = this.getTable();
if (row == null) return;
row.remove();
this.quill.update(Quill.sources.USER);
}
deleteTable() {
const [table] = this.getTable();
if (table == null) return;
// @ts-expect-error
const offset = table.offset();
// @ts-expect-error
table.remove();
this.quill.update(Quill.sources.USER);
this.quill.setSelection(offset, Quill.sources.SILENT);
}
getTable(
range = this.quill.getSelection(),
): [null, null, null, -1] | [Table, TableRow, TableCell, number] {
if (range == null) return [null, null, null, -1];
const [cell, offset] = this.quill.getLine(range.index);
if (cell == null || cell.statics.blotName !== TableCell.blotName) {
return [null, null, null, -1];
}
const row = cell.parent;
const table = row.parent.parent;
// @ts-expect-error
return [table, row, cell, offset];
}
insertColumn(offset) {
const range = this.quill.getSelection();
const [table, row, cell] = this.getTable(range);
if (cell == null) return;
const column = cell.cellOffset();
table.insertColumn(column + offset);
this.quill.update(Quill.sources.USER);
let shift = row.rowOffset();
if (offset === 0) {
shift += 1;
}
this.quill.setSelection(
range.index + shift,
range.length,
Quill.sources.SILENT,
);
}
insertColumnLeft() {
this.insertColumn(0);
}
insertColumnRight() {
this.insertColumn(1);
}
insertRow(offset) {
const range = this.quill.getSelection();
const [table, row, cell] = this.getTable(range);
if (cell == null) return;
const index = row.rowOffset();
table.insertRow(index + offset);
this.quill.update(Quill.sources.USER);
if (offset > 0) {
this.quill.setSelection(range, Quill.sources.SILENT);
} else {
this.quill.setSelection(
range.index + row.children.length,
range.length,
Quill.sources.SILENT,
);
}
}
insertRowAbove() {
this.insertRow(0);
}
insertRowBelow() {
this.insertRow(1);
}
insertTable(rows, columns) {
const range = this.quill.getSelection();
if (range == null) return;
const delta = new Array(rows).fill(0).reduce(memo => {
const text = new Array(columns).fill('\n').join('');
return memo.insert(text, { table: tableId() });
}, new Delta().retain(range.index));
this.quill.updateContents(delta, Quill.sources.USER);
this.quill.setSelection(range.index, Quill.sources.SILENT);
this.balanceTables();
}
listenBalanceCells() {
this.quill.on(Quill.events.SCROLL_OPTIMIZE, mutations => {
mutations.some(mutation => {
if (['TD', 'TR', 'TBODY', 'TABLE'].includes(mutation.target.tagName)) {
this.quill.once(Quill.events.TEXT_CHANGE, (delta, old, source) => {
if (source !== Quill.sources.USER) return;
this.balanceTables();
});
return true;
}
return false;
});
});
}
}
export default Table;