quill-table-up 3.1.2 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/dist/index.css +1 -1
- package/dist/index.d.ts +168 -146
- package/dist/index.js +47 -47
- package/dist/index.js.map +1 -1
- package/dist/index.umd.js +52 -52
- package/dist/index.umd.js.map +1 -1
- package/package.json +22 -24
- package/src/__tests__/e2e/custom-creator.test.ts +44 -44
- package/src/__tests__/e2e/editor-page.ts +77 -77
- package/src/__tests__/e2e/table-align.test.ts +104 -104
- package/src/__tests__/e2e/table-blots.test.ts +169 -169
- package/src/__tests__/e2e/table-caption.test.ts +134 -134
- package/src/__tests__/e2e/table-clipboard.test.ts +20 -20
- package/src/__tests__/e2e/table-hack.test.ts +151 -151
- package/src/__tests__/e2e/table-keyboard-handler.test.ts +12 -3
- package/src/__tests__/e2e/table-menu.test.ts +172 -172
- package/src/__tests__/e2e/table-resize.test.ts +654 -9
- package/src/__tests__/e2e/table-scrollbar.test.ts +144 -144
- package/src/__tests__/e2e/table-selection.test.ts +563 -563
- package/src/__tests__/e2e/types.d.ts +7 -7
- package/src/__tests__/e2e/utils.ts +52 -52
- package/src/__tests__/unit/table-blots.test.ts +720 -720
- package/src/__tests__/unit/table-caption.test.ts +234 -234
- package/src/__tests__/unit/table-cell-merge.test.ts +724 -724
- package/src/__tests__/unit/table-clipboard.test.ts +2176 -2176
- package/src/__tests__/unit/table-hack.test.ts +1014 -1014
- package/src/__tests__/unit/table-insert.test.ts +926 -926
- package/src/__tests__/unit/table-redo-undo.test.ts +2429 -2429
- package/src/__tests__/unit/table-remove.test.ts +343 -343
- package/src/__tests__/unit/utils.test-d.ts +49 -49
- package/src/__tests__/unit/utils.test.ts +711 -711
- package/src/__tests__/unit/utils.ts +307 -307
- package/src/__tests__/unit/vitest.d.ts +14 -14
- package/src/formats/container-format.ts +107 -107
- package/src/formats/overrides/block-embed.ts +72 -72
- package/src/formats/overrides/block.ts +95 -95
- package/src/formats/overrides/index.ts +3 -3
- package/src/formats/overrides/scroll.ts +70 -70
- package/src/formats/table-body-format.ts +52 -52
- package/src/formats/table-caption-format.ts +116 -116
- package/src/formats/table-cell-format.ts +304 -304
- package/src/formats/table-cell-inner-format.ts +403 -398
- package/src/formats/table-colgroup-format.ts +136 -136
- package/src/formats/table-foot-format.ts +7 -7
- package/src/formats/table-head-format.ts +7 -7
- package/src/formats/table-main-format.ts +1 -1
- package/src/formats/table-row-format.ts +218 -210
- package/src/formats/utils.ts +6 -6
- package/src/index.ts +19 -19
- package/src/modules/index.ts +7 -7
- package/src/modules/table-align.ts +131 -131
- package/src/modules/table-clipboard/table-clipboard.ts +6 -8
- package/src/modules/table-dom-selector.ts +33 -33
- package/src/modules/table-menu/constants.ts +223 -223
- package/src/modules/table-menu/index.ts +4 -4
- package/src/modules/table-menu/table-menu-common.ts +330 -329
- package/src/modules/table-menu/table-menu-contextmenu.ts +111 -118
- package/src/modules/table-menu/table-menu-select.ts +96 -94
- package/src/modules/table-resize/index.ts +5 -5
- package/src/modules/table-resize/table-resize-box.ts +714 -363
- package/src/modules/table-resize/table-resize-common.ts +246 -382
- package/src/modules/table-resize/table-resize-drag.ts +241 -0
- package/src/modules/table-resize/table-resize-line.ts +244 -182
- package/src/modules/table-resize/table-resize-scale.ts +174 -173
- package/src/modules/table-resize/utils.ts +84 -3
- package/src/modules/table-scrollbar.ts +292 -292
- package/src/modules/table-selection.ts +613 -669
- package/src/style/button.less +45 -45
- package/src/style/color-picker.less +136 -136
- package/src/style/dialog.less +53 -53
- package/src/style/functions.less +9 -9
- package/src/style/index.less +120 -120
- package/src/style/input.less +64 -64
- package/src/style/select-box.less +52 -52
- package/src/style/table-creator.less +56 -56
- package/src/style/table-menu.less +125 -125
- package/src/style/table-resize-scale.less +31 -31
- package/src/style/table-resize.less +249 -202
- package/src/style/table-scrollbar.less +49 -49
- package/src/style/table-selection.less +23 -23
- package/src/style/tooltip.less +19 -19
- package/src/style/variables.less +1 -1
- package/src/svg/arrow-up-down.svg +11 -11
- package/src/svg/convert-cell.svg +7 -7
- package/src/table-up.ts +1360 -1360
- package/src/types.d.ts +4 -4
- package/src/utils/bem.ts +23 -23
- package/src/utils/blot-helper.ts +101 -105
- package/src/utils/color.ts +109 -109
- package/src/utils/components/button.ts +22 -22
- package/src/utils/components/color-picker.ts +236 -236
- package/src/utils/components/dialog.ts +83 -41
- package/src/utils/components/index.ts +6 -6
- package/src/utils/components/input.ts +74 -74
- package/src/utils/components/table/creator.ts +89 -89
- package/src/utils/components/table/index.ts +2 -2
- package/src/utils/components/table/select-box.ts +78 -78
- package/src/utils/components/tooltip.ts +179 -189
- package/src/utils/constants.ts +125 -124
- package/src/utils/drag-helper.ts +112 -0
- package/src/utils/index.ts +15 -14
- package/src/utils/is.ts +9 -9
- package/src/utils/position.ts +60 -60
- package/src/utils/resize-observer-helper.ts +47 -47
- package/src/utils/scroll.ts +145 -47
- package/src/utils/style-helper.ts +47 -47
- package/src/utils/transformer.ts +10 -10
- package/src/utils/transition-event-helper.ts +8 -8
- package/src/utils/types.ts +156 -157
- package/src/utils/utils.ts +12 -12
|
@@ -1,292 +1,292 @@
|
|
|
1
|
-
import type { TableMainFormat } from '../formats';
|
|
2
|
-
import type { TableUp } from '../table-up';
|
|
3
|
-
import Quill from 'quill';
|
|
4
|
-
import { getTableMainRect } from '../formats';
|
|
5
|
-
import { addScrollEvent, clearScrollEvent, createBEM, debounce } from '../utils';
|
|
6
|
-
import { TableDomSelector } from './table-dom-selector';
|
|
7
|
-
|
|
8
|
-
export class Scrollbar {
|
|
9
|
-
minSize: number = 20;
|
|
10
|
-
gap: number = 4;
|
|
11
|
-
move: number = 0;
|
|
12
|
-
cursorDown: boolean = false;
|
|
13
|
-
cursorLeave: boolean = false;
|
|
14
|
-
ratioY: number = 1;
|
|
15
|
-
ratioX: number = 1;
|
|
16
|
-
sizeWidth: string = '';
|
|
17
|
-
sizeHeight: string = '';
|
|
18
|
-
size: string = '';
|
|
19
|
-
thumbState: {
|
|
20
|
-
X: number;
|
|
21
|
-
Y: number;
|
|
22
|
-
} = {
|
|
23
|
-
X: 0,
|
|
24
|
-
Y: 0,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
ob: ResizeObserver;
|
|
28
|
-
container: HTMLElement;
|
|
29
|
-
scrollbar: HTMLElement;
|
|
30
|
-
thumb: HTMLElement = document.createElement('div');
|
|
31
|
-
scrollHandler: [HTMLElement, (e: Event) => void][] = [];
|
|
32
|
-
propertyMap: { readonly size: 'height'; readonly offset: 'offsetHeight'; readonly scrollDirection: 'scrollTop'; readonly scrollSize: 'scrollHeight'; readonly axis: 'Y'; readonly direction: 'top'; readonly client: 'clientY' } | { readonly size: 'width'; readonly offset: 'offsetWidth'; readonly scrollDirection: 'scrollLeft'; readonly scrollSize: 'scrollWidth'; readonly axis: 'X'; readonly direction: 'left'; readonly client: 'clientX' };
|
|
33
|
-
bem = createBEM('scrollbar');
|
|
34
|
-
tableMainBlot: TableMainFormat;
|
|
35
|
-
|
|
36
|
-
get isVertical() {
|
|
37
|
-
return this.options.isVertical;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
constructor(public quill: Quill, public table: HTMLElement, public options: { isVertical: boolean }) {
|
|
41
|
-
this.tableMainBlot = Quill.find(this.table) as TableMainFormat;
|
|
42
|
-
this.container = table.parentElement!;
|
|
43
|
-
this.propertyMap = this.isVertical
|
|
44
|
-
? {
|
|
45
|
-
size: 'height',
|
|
46
|
-
offset: 'offsetHeight',
|
|
47
|
-
scrollDirection: 'scrollTop',
|
|
48
|
-
scrollSize: 'scrollHeight',
|
|
49
|
-
axis: 'Y',
|
|
50
|
-
direction: 'top',
|
|
51
|
-
client: 'clientY',
|
|
52
|
-
} as const
|
|
53
|
-
: {
|
|
54
|
-
size: 'width',
|
|
55
|
-
offset: 'offsetWidth',
|
|
56
|
-
scrollDirection: 'scrollLeft',
|
|
57
|
-
scrollSize: 'scrollWidth',
|
|
58
|
-
axis: 'X',
|
|
59
|
-
direction: 'left',
|
|
60
|
-
client: 'clientX',
|
|
61
|
-
} as const;
|
|
62
|
-
this.calculateSize();
|
|
63
|
-
this.ob = new ResizeObserver(() => this.update());
|
|
64
|
-
this.ob.observe(table);
|
|
65
|
-
this.scrollbar = this.createScrollbar();
|
|
66
|
-
this.setScrollbarPosition();
|
|
67
|
-
addScrollEvent.call(this, this.quill.root, () => this.setScrollbarPosition());
|
|
68
|
-
this.showScrollbar();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
update() {
|
|
72
|
-
this.calculateSize();
|
|
73
|
-
this.setScrollbarPosition();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
setScrollbarPosition() {
|
|
77
|
-
const { rect: tableRect, head: tableHeadBlot, body: tableBodyBlot, foot: tableFootBlot } = getTableMainRect(this.tableMainBlot);
|
|
78
|
-
const tableMainContentBlot = tableHeadBlot || tableBodyBlot || tableFootBlot;
|
|
79
|
-
if (!tableMainContentBlot || !tableRect) return;
|
|
80
|
-
const { scrollLeft: editorScrollX, scrollTop: editorScrollY, offsetLeft: rootOffsetLeft, offsetTop: rootOffsetTop } = this.quill.root;
|
|
81
|
-
const { offsetLeft: containerOffsetLeft, offsetTop: containerOffsetTop } = this.container;
|
|
82
|
-
const { offsetLeft: tableOffsetLeft, offsetTop: tableOffsetTop } = tableMainContentBlot.domNode;
|
|
83
|
-
const { width: containerWidth, height: containerHeight } = this.container.getBoundingClientRect();
|
|
84
|
-
|
|
85
|
-
let x = containerOffsetLeft + tableOffsetLeft - rootOffsetLeft;
|
|
86
|
-
let y = containerOffsetTop + tableOffsetTop - rootOffsetTop;
|
|
87
|
-
if (this.isVertical) {
|
|
88
|
-
x += Math.min(containerWidth, tableRect.width);
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
y += Math.min(containerHeight, tableRect.height);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// table align right effect
|
|
95
|
-
if (this.tableMainBlot && this.tableMainBlot.align !== 'left') {
|
|
96
|
-
x += this.table.offsetLeft - containerOffsetLeft;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
Object.assign(this.scrollbar.style, {
|
|
100
|
-
[this.propertyMap.size]: `${this.isVertical ? Math.min(containerHeight, tableRect.height) : containerWidth}px`,
|
|
101
|
-
transform: `translate(${x - editorScrollX}px, ${y - editorScrollY}px)`,
|
|
102
|
-
});
|
|
103
|
-
this.containerScrollHandler(this.container);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
calculateSize() {
|
|
107
|
-
const offsetHeight = this.container.offsetHeight - this.gap;
|
|
108
|
-
const offsetWidth = this.container.offsetWidth - this.gap;
|
|
109
|
-
const originalHeight = offsetHeight ** 2 / this.container.scrollHeight;
|
|
110
|
-
const originalWidth = offsetWidth ** 2 / this.container.scrollWidth;
|
|
111
|
-
const height = Math.max(originalHeight, this.minSize);
|
|
112
|
-
const width = Math.max(originalWidth, this.minSize);
|
|
113
|
-
this.ratioY = originalHeight / (offsetHeight - originalHeight) / (height / (offsetHeight - height));
|
|
114
|
-
this.ratioX = originalWidth / (offsetWidth - originalWidth) / (width / (offsetWidth - width));
|
|
115
|
-
|
|
116
|
-
this.sizeWidth = width + this.gap < offsetWidth ? `${width}px` : '';
|
|
117
|
-
this.sizeHeight = height + this.gap < offsetHeight ? `${height}px` : '';
|
|
118
|
-
this.size = this.isVertical ? this.sizeHeight : this.sizeWidth;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
createScrollbar() {
|
|
122
|
-
const scrollbar = document.createElement('div');
|
|
123
|
-
scrollbar.classList.add(this.bem.b(), this.isVertical ? this.bem.is('vertical') : this.bem.is('horizontal'), this.bem.is('transparent'));
|
|
124
|
-
Object.assign(scrollbar.style, {
|
|
125
|
-
display: 'none',
|
|
126
|
-
});
|
|
127
|
-
this.thumb.classList.add(this.bem.be('thumb'));
|
|
128
|
-
let originDocSelect: typeof document.onselectstart = null;
|
|
129
|
-
const mouseMoveDocumentHandler = (e: MouseEvent) => {
|
|
130
|
-
if (this.cursorDown === false) return;
|
|
131
|
-
const prevPage = this.thumbState[this.propertyMap.axis];
|
|
132
|
-
if (!prevPage) return;
|
|
133
|
-
|
|
134
|
-
const offsetRatio = this.scrollbar[this.propertyMap.offset] ** 2
|
|
135
|
-
/ this.container[this.propertyMap.scrollSize] / (this.isVertical ? this.ratioY : this.ratioX)
|
|
136
|
-
/ this.thumb[this.propertyMap.offset];
|
|
137
|
-
const offset = (this.scrollbar.getBoundingClientRect()[this.propertyMap.direction] - e[this.propertyMap.client]) * -1;
|
|
138
|
-
const thumbClickPosition = this.thumb[this.propertyMap.offset] - prevPage;
|
|
139
|
-
const thumbPositionPercentage = ((offset - thumbClickPosition) * 100 * offsetRatio) / this.scrollbar[this.propertyMap.offset];
|
|
140
|
-
this.container[this.propertyMap.scrollDirection] = (thumbPositionPercentage * this.container[this.propertyMap.scrollSize]) / 100;
|
|
141
|
-
};
|
|
142
|
-
const mouseUpDocumentHandler = () => {
|
|
143
|
-
this.thumbState[this.propertyMap.axis] = 0;
|
|
144
|
-
this.cursorDown = false;
|
|
145
|
-
document.removeEventListener('mousemove', mouseMoveDocumentHandler);
|
|
146
|
-
document.removeEventListener('mouseup', mouseUpDocumentHandler);
|
|
147
|
-
if (this.cursorLeave) {
|
|
148
|
-
this.hideScrollbar();
|
|
149
|
-
}
|
|
150
|
-
// eslint-disable-next-line unicorn/prefer-add-event-listener
|
|
151
|
-
document.onselectstart = originDocSelect;
|
|
152
|
-
};
|
|
153
|
-
const startDrag = (e: MouseEvent) => {
|
|
154
|
-
e.stopImmediatePropagation();
|
|
155
|
-
this.cursorDown = true;
|
|
156
|
-
document.addEventListener('mousemove', mouseMoveDocumentHandler);
|
|
157
|
-
document.addEventListener('mouseup', mouseUpDocumentHandler);
|
|
158
|
-
originDocSelect = document.onselectstart;
|
|
159
|
-
// eslint-disable-next-line unicorn/prefer-add-event-listener
|
|
160
|
-
document.onselectstart = () => false;
|
|
161
|
-
};
|
|
162
|
-
this.thumb.addEventListener('mousedown', (e: MouseEvent) => {
|
|
163
|
-
e.stopPropagation();
|
|
164
|
-
if (e.ctrlKey || [1, 2].includes(e.button)) return;
|
|
165
|
-
|
|
166
|
-
window.getSelection()?.removeAllRanges();
|
|
167
|
-
startDrag(e);
|
|
168
|
-
|
|
169
|
-
const el = e.currentTarget as HTMLElement;
|
|
170
|
-
if (!el) return;
|
|
171
|
-
this.thumbState[this.propertyMap.axis] = el[this.propertyMap.offset] - (e[this.propertyMap.client] - el.getBoundingClientRect()[this.propertyMap.direction]);
|
|
172
|
-
});
|
|
173
|
-
const displayListener = [this.table, scrollbar];
|
|
174
|
-
for (const el of displayListener) {
|
|
175
|
-
el.addEventListener('mouseenter', this.showScrollbar);
|
|
176
|
-
el.addEventListener('mouseleave', this.hideScrollbar);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
addScrollEvent.call(this, this.container, () => {
|
|
180
|
-
this.containerScrollHandler(this.container);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
scrollbar.appendChild(this.thumb);
|
|
184
|
-
return scrollbar;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
containerScrollHandler(wrap: HTMLElement) {
|
|
188
|
-
const offset = wrap[this.propertyMap.offset] - this.gap;
|
|
189
|
-
this.move = wrap[this.propertyMap.scrollDirection] * 100 / offset * (this.isVertical ? this.ratioY : this.ratioX);
|
|
190
|
-
Object.assign(this.thumb.style, {
|
|
191
|
-
[this.propertyMap.size]: this.size,
|
|
192
|
-
transform: `translate${this.propertyMap.axis}(${this.move}%)`,
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
showScrollbar = debounce(() => {
|
|
197
|
-
this.cursorLeave = false;
|
|
198
|
-
this.scrollbar.removeEventListener('transitionend', this.hideScrollbarTransitionend);
|
|
199
|
-
this.scrollbar.style.display = this.size ? 'block' : 'none';
|
|
200
|
-
requestAnimationFrame(() => {
|
|
201
|
-
this.scrollbar.classList.remove(this.bem.is('transparent'));
|
|
202
|
-
});
|
|
203
|
-
}, 200);
|
|
204
|
-
|
|
205
|
-
hideScrollbar = debounce(() => {
|
|
206
|
-
this.cursorLeave = true;
|
|
207
|
-
if (this.cursorDown) return;
|
|
208
|
-
this.scrollbar.removeEventListener('transitionend', this.hideScrollbarTransitionend);
|
|
209
|
-
this.scrollbar.addEventListener('transitionend', this.hideScrollbarTransitionend, { once: true });
|
|
210
|
-
this.scrollbar.classList.add(this.bem.is('transparent'));
|
|
211
|
-
}, 200);
|
|
212
|
-
|
|
213
|
-
hideScrollbarTransitionend = () => {
|
|
214
|
-
this.scrollbar.style.display = (this.cursorDown && this.size) ? 'block' : 'none';
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
destroy() {
|
|
218
|
-
this.ob.disconnect();
|
|
219
|
-
clearScrollEvent.call(this);
|
|
220
|
-
this.table.removeEventListener('mouseenter', this.showScrollbar);
|
|
221
|
-
this.table.removeEventListener('mouseleave', this.hideScrollbar);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export class TableVirtualScrollbar extends TableDomSelector {
|
|
226
|
-
static moduleName: string = 'table-scrollbar';
|
|
227
|
-
|
|
228
|
-
scrollbarContainer: HTMLElement;
|
|
229
|
-
scrollbar: Scrollbar[] = [];
|
|
230
|
-
bem = createBEM('scrollbar');
|
|
231
|
-
constructor(public tableModule: TableUp, public quill: Quill, _options: any) {
|
|
232
|
-
super(tableModule, quill);
|
|
233
|
-
|
|
234
|
-
const scrollbarBEM = createBEM('scrollbar');
|
|
235
|
-
this.quill.container.classList.add(scrollbarBEM.bm('virtual'));
|
|
236
|
-
|
|
237
|
-
this.scrollbarContainer = this.tableModule.addContainer(this.bem.be('container'));
|
|
238
|
-
this.quill.on(Quill.events.EDITOR_CHANGE, this.updateWhenTextChange);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
updateWhenTextChange = (eventName: string) => {
|
|
242
|
-
if (eventName === Quill.events.TEXT_CHANGE) {
|
|
243
|
-
if (this.table && !this.quill.root.contains(this.table)) {
|
|
244
|
-
this.setSelectionTable(undefined);
|
|
245
|
-
}
|
|
246
|
-
else {
|
|
247
|
-
this.update();
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
hide() {
|
|
253
|
-
for (const scrollbar of this.scrollbar) {
|
|
254
|
-
scrollbar.destroy();
|
|
255
|
-
}
|
|
256
|
-
this.scrollbar = [];
|
|
257
|
-
this.scrollbarContainer.innerHTML = '';
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
show() {
|
|
261
|
-
if (!this.table) return;
|
|
262
|
-
this.scrollbar = [
|
|
263
|
-
new Scrollbar(this.quill, this.table, { isVertical: true }),
|
|
264
|
-
new Scrollbar(this.quill, this.table, { isVertical: false }),
|
|
265
|
-
];
|
|
266
|
-
for (const item of this.scrollbar) {
|
|
267
|
-
this.scrollbarContainer.appendChild(item.scrollbar);
|
|
268
|
-
item.showScrollbar();
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
update() {
|
|
273
|
-
if (this.table) {
|
|
274
|
-
if (this.scrollbar.length <= 0) {
|
|
275
|
-
this.show();
|
|
276
|
-
}
|
|
277
|
-
for (const scrollbar of this.scrollbar) {
|
|
278
|
-
scrollbar.calculateSize();
|
|
279
|
-
scrollbar.setScrollbarPosition();
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
else if (this.scrollbar.length > 0) {
|
|
283
|
-
this.hide();
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
destroy() {
|
|
288
|
-
this.hide();
|
|
289
|
-
this.scrollbarContainer.remove();
|
|
290
|
-
this.quill.off(Quill.events.TEXT_CHANGE, this.updateWhenTextChange);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
1
|
+
import type { TableMainFormat } from '../formats';
|
|
2
|
+
import type { TableUp } from '../table-up';
|
|
3
|
+
import Quill from 'quill';
|
|
4
|
+
import { getTableMainRect } from '../formats';
|
|
5
|
+
import { addScrollEvent, clearScrollEvent, createBEM, debounce } from '../utils';
|
|
6
|
+
import { TableDomSelector } from './table-dom-selector';
|
|
7
|
+
|
|
8
|
+
export class Scrollbar {
|
|
9
|
+
minSize: number = 20;
|
|
10
|
+
gap: number = 4;
|
|
11
|
+
move: number = 0;
|
|
12
|
+
cursorDown: boolean = false;
|
|
13
|
+
cursorLeave: boolean = false;
|
|
14
|
+
ratioY: number = 1;
|
|
15
|
+
ratioX: number = 1;
|
|
16
|
+
sizeWidth: string = '';
|
|
17
|
+
sizeHeight: string = '';
|
|
18
|
+
size: string = '';
|
|
19
|
+
thumbState: {
|
|
20
|
+
X: number;
|
|
21
|
+
Y: number;
|
|
22
|
+
} = {
|
|
23
|
+
X: 0,
|
|
24
|
+
Y: 0,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
ob: ResizeObserver;
|
|
28
|
+
container: HTMLElement;
|
|
29
|
+
scrollbar: HTMLElement;
|
|
30
|
+
thumb: HTMLElement = document.createElement('div');
|
|
31
|
+
scrollHandler: [HTMLElement, (e: Event) => void][] = [];
|
|
32
|
+
propertyMap: { readonly size: 'height'; readonly offset: 'offsetHeight'; readonly scrollDirection: 'scrollTop'; readonly scrollSize: 'scrollHeight'; readonly axis: 'Y'; readonly direction: 'top'; readonly client: 'clientY' } | { readonly size: 'width'; readonly offset: 'offsetWidth'; readonly scrollDirection: 'scrollLeft'; readonly scrollSize: 'scrollWidth'; readonly axis: 'X'; readonly direction: 'left'; readonly client: 'clientX' };
|
|
33
|
+
bem = createBEM('scrollbar');
|
|
34
|
+
tableMainBlot: TableMainFormat;
|
|
35
|
+
|
|
36
|
+
get isVertical() {
|
|
37
|
+
return this.options.isVertical;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
constructor(public quill: Quill, public table: HTMLElement, public options: { isVertical: boolean }) {
|
|
41
|
+
this.tableMainBlot = Quill.find(this.table) as TableMainFormat;
|
|
42
|
+
this.container = table.parentElement!;
|
|
43
|
+
this.propertyMap = this.isVertical
|
|
44
|
+
? {
|
|
45
|
+
size: 'height',
|
|
46
|
+
offset: 'offsetHeight',
|
|
47
|
+
scrollDirection: 'scrollTop',
|
|
48
|
+
scrollSize: 'scrollHeight',
|
|
49
|
+
axis: 'Y',
|
|
50
|
+
direction: 'top',
|
|
51
|
+
client: 'clientY',
|
|
52
|
+
} as const
|
|
53
|
+
: {
|
|
54
|
+
size: 'width',
|
|
55
|
+
offset: 'offsetWidth',
|
|
56
|
+
scrollDirection: 'scrollLeft',
|
|
57
|
+
scrollSize: 'scrollWidth',
|
|
58
|
+
axis: 'X',
|
|
59
|
+
direction: 'left',
|
|
60
|
+
client: 'clientX',
|
|
61
|
+
} as const;
|
|
62
|
+
this.calculateSize();
|
|
63
|
+
this.ob = new ResizeObserver(() => this.update());
|
|
64
|
+
this.ob.observe(table);
|
|
65
|
+
this.scrollbar = this.createScrollbar();
|
|
66
|
+
this.setScrollbarPosition();
|
|
67
|
+
addScrollEvent.call(this, this.quill.root, () => this.setScrollbarPosition());
|
|
68
|
+
this.showScrollbar();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
update() {
|
|
72
|
+
this.calculateSize();
|
|
73
|
+
this.setScrollbarPosition();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
setScrollbarPosition() {
|
|
77
|
+
const { rect: tableRect, head: tableHeadBlot, body: tableBodyBlot, foot: tableFootBlot } = getTableMainRect(this.tableMainBlot);
|
|
78
|
+
const tableMainContentBlot = tableHeadBlot || tableBodyBlot || tableFootBlot;
|
|
79
|
+
if (!tableMainContentBlot || !tableRect) return;
|
|
80
|
+
const { scrollLeft: editorScrollX, scrollTop: editorScrollY, offsetLeft: rootOffsetLeft, offsetTop: rootOffsetTop } = this.quill.root;
|
|
81
|
+
const { offsetLeft: containerOffsetLeft, offsetTop: containerOffsetTop } = this.container;
|
|
82
|
+
const { offsetLeft: tableOffsetLeft, offsetTop: tableOffsetTop } = tableMainContentBlot.domNode;
|
|
83
|
+
const { width: containerWidth, height: containerHeight } = this.container.getBoundingClientRect();
|
|
84
|
+
|
|
85
|
+
let x = containerOffsetLeft + tableOffsetLeft - rootOffsetLeft;
|
|
86
|
+
let y = containerOffsetTop + tableOffsetTop - rootOffsetTop;
|
|
87
|
+
if (this.isVertical) {
|
|
88
|
+
x += Math.min(containerWidth, tableRect.width);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
y += Math.min(containerHeight, tableRect.height);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// table align right effect
|
|
95
|
+
if (this.tableMainBlot && this.tableMainBlot.align !== 'left') {
|
|
96
|
+
x += this.table.offsetLeft - containerOffsetLeft;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
Object.assign(this.scrollbar.style, {
|
|
100
|
+
[this.propertyMap.size]: `${this.isVertical ? Math.min(containerHeight, tableRect.height) : containerWidth}px`,
|
|
101
|
+
transform: `translate(${x - editorScrollX}px, ${y - editorScrollY}px)`,
|
|
102
|
+
});
|
|
103
|
+
this.containerScrollHandler(this.container);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
calculateSize() {
|
|
107
|
+
const offsetHeight = this.container.offsetHeight - this.gap;
|
|
108
|
+
const offsetWidth = this.container.offsetWidth - this.gap;
|
|
109
|
+
const originalHeight = offsetHeight ** 2 / this.container.scrollHeight;
|
|
110
|
+
const originalWidth = offsetWidth ** 2 / this.container.scrollWidth;
|
|
111
|
+
const height = Math.max(originalHeight, this.minSize);
|
|
112
|
+
const width = Math.max(originalWidth, this.minSize);
|
|
113
|
+
this.ratioY = originalHeight / (offsetHeight - originalHeight) / (height / (offsetHeight - height));
|
|
114
|
+
this.ratioX = originalWidth / (offsetWidth - originalWidth) / (width / (offsetWidth - width));
|
|
115
|
+
|
|
116
|
+
this.sizeWidth = width + this.gap < offsetWidth ? `${width}px` : '';
|
|
117
|
+
this.sizeHeight = height + this.gap < offsetHeight ? `${height}px` : '';
|
|
118
|
+
this.size = this.isVertical ? this.sizeHeight : this.sizeWidth;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
createScrollbar() {
|
|
122
|
+
const scrollbar = document.createElement('div');
|
|
123
|
+
scrollbar.classList.add(this.bem.b(), this.isVertical ? this.bem.is('vertical') : this.bem.is('horizontal'), this.bem.is('transparent'));
|
|
124
|
+
Object.assign(scrollbar.style, {
|
|
125
|
+
display: 'none',
|
|
126
|
+
});
|
|
127
|
+
this.thumb.classList.add(this.bem.be('thumb'));
|
|
128
|
+
let originDocSelect: typeof document.onselectstart = null;
|
|
129
|
+
const mouseMoveDocumentHandler = (e: MouseEvent) => {
|
|
130
|
+
if (this.cursorDown === false) return;
|
|
131
|
+
const prevPage = this.thumbState[this.propertyMap.axis];
|
|
132
|
+
if (!prevPage) return;
|
|
133
|
+
|
|
134
|
+
const offsetRatio = this.scrollbar[this.propertyMap.offset] ** 2
|
|
135
|
+
/ this.container[this.propertyMap.scrollSize] / (this.isVertical ? this.ratioY : this.ratioX)
|
|
136
|
+
/ this.thumb[this.propertyMap.offset];
|
|
137
|
+
const offset = (this.scrollbar.getBoundingClientRect()[this.propertyMap.direction] - e[this.propertyMap.client]) * -1;
|
|
138
|
+
const thumbClickPosition = this.thumb[this.propertyMap.offset] - prevPage;
|
|
139
|
+
const thumbPositionPercentage = ((offset - thumbClickPosition) * 100 * offsetRatio) / this.scrollbar[this.propertyMap.offset];
|
|
140
|
+
this.container[this.propertyMap.scrollDirection] = (thumbPositionPercentage * this.container[this.propertyMap.scrollSize]) / 100;
|
|
141
|
+
};
|
|
142
|
+
const mouseUpDocumentHandler = () => {
|
|
143
|
+
this.thumbState[this.propertyMap.axis] = 0;
|
|
144
|
+
this.cursorDown = false;
|
|
145
|
+
document.removeEventListener('mousemove', mouseMoveDocumentHandler);
|
|
146
|
+
document.removeEventListener('mouseup', mouseUpDocumentHandler);
|
|
147
|
+
if (this.cursorLeave) {
|
|
148
|
+
this.hideScrollbar();
|
|
149
|
+
}
|
|
150
|
+
// eslint-disable-next-line unicorn/prefer-add-event-listener
|
|
151
|
+
document.onselectstart = originDocSelect;
|
|
152
|
+
};
|
|
153
|
+
const startDrag = (e: MouseEvent) => {
|
|
154
|
+
e.stopImmediatePropagation();
|
|
155
|
+
this.cursorDown = true;
|
|
156
|
+
document.addEventListener('mousemove', mouseMoveDocumentHandler);
|
|
157
|
+
document.addEventListener('mouseup', mouseUpDocumentHandler);
|
|
158
|
+
originDocSelect = document.onselectstart;
|
|
159
|
+
// eslint-disable-next-line unicorn/prefer-add-event-listener
|
|
160
|
+
document.onselectstart = () => false;
|
|
161
|
+
};
|
|
162
|
+
this.thumb.addEventListener('mousedown', (e: MouseEvent) => {
|
|
163
|
+
e.stopPropagation();
|
|
164
|
+
if (e.ctrlKey || [1, 2].includes(e.button)) return;
|
|
165
|
+
|
|
166
|
+
window.getSelection()?.removeAllRanges();
|
|
167
|
+
startDrag(e);
|
|
168
|
+
|
|
169
|
+
const el = e.currentTarget as HTMLElement;
|
|
170
|
+
if (!el) return;
|
|
171
|
+
this.thumbState[this.propertyMap.axis] = el[this.propertyMap.offset] - (e[this.propertyMap.client] - el.getBoundingClientRect()[this.propertyMap.direction]);
|
|
172
|
+
});
|
|
173
|
+
const displayListener = [this.table, scrollbar];
|
|
174
|
+
for (const el of displayListener) {
|
|
175
|
+
el.addEventListener('mouseenter', this.showScrollbar);
|
|
176
|
+
el.addEventListener('mouseleave', this.hideScrollbar);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
addScrollEvent.call(this, this.container, () => {
|
|
180
|
+
this.containerScrollHandler(this.container);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
scrollbar.appendChild(this.thumb);
|
|
184
|
+
return scrollbar;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
containerScrollHandler(wrap: HTMLElement) {
|
|
188
|
+
const offset = wrap[this.propertyMap.offset] - this.gap;
|
|
189
|
+
this.move = wrap[this.propertyMap.scrollDirection] * 100 / offset * (this.isVertical ? this.ratioY : this.ratioX);
|
|
190
|
+
Object.assign(this.thumb.style, {
|
|
191
|
+
[this.propertyMap.size]: this.size,
|
|
192
|
+
transform: `translate${this.propertyMap.axis}(${this.move}%)`,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
showScrollbar = debounce(() => {
|
|
197
|
+
this.cursorLeave = false;
|
|
198
|
+
this.scrollbar.removeEventListener('transitionend', this.hideScrollbarTransitionend);
|
|
199
|
+
this.scrollbar.style.display = this.size ? 'block' : 'none';
|
|
200
|
+
requestAnimationFrame(() => {
|
|
201
|
+
this.scrollbar.classList.remove(this.bem.is('transparent'));
|
|
202
|
+
});
|
|
203
|
+
}, 200);
|
|
204
|
+
|
|
205
|
+
hideScrollbar = debounce(() => {
|
|
206
|
+
this.cursorLeave = true;
|
|
207
|
+
if (this.cursorDown) return;
|
|
208
|
+
this.scrollbar.removeEventListener('transitionend', this.hideScrollbarTransitionend);
|
|
209
|
+
this.scrollbar.addEventListener('transitionend', this.hideScrollbarTransitionend, { once: true });
|
|
210
|
+
this.scrollbar.classList.add(this.bem.is('transparent'));
|
|
211
|
+
}, 200);
|
|
212
|
+
|
|
213
|
+
hideScrollbarTransitionend = () => {
|
|
214
|
+
this.scrollbar.style.display = (this.cursorDown && this.size) ? 'block' : 'none';
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
destroy() {
|
|
218
|
+
this.ob.disconnect();
|
|
219
|
+
clearScrollEvent.call(this);
|
|
220
|
+
this.table.removeEventListener('mouseenter', this.showScrollbar);
|
|
221
|
+
this.table.removeEventListener('mouseleave', this.hideScrollbar);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export class TableVirtualScrollbar extends TableDomSelector {
|
|
226
|
+
static moduleName: string = 'table-scrollbar';
|
|
227
|
+
|
|
228
|
+
scrollbarContainer: HTMLElement;
|
|
229
|
+
scrollbar: Scrollbar[] = [];
|
|
230
|
+
bem = createBEM('scrollbar');
|
|
231
|
+
constructor(public tableModule: TableUp, public quill: Quill, _options: any) {
|
|
232
|
+
super(tableModule, quill);
|
|
233
|
+
|
|
234
|
+
const scrollbarBEM = createBEM('scrollbar');
|
|
235
|
+
this.quill.container.classList.add(scrollbarBEM.bm('virtual'));
|
|
236
|
+
|
|
237
|
+
this.scrollbarContainer = this.tableModule.addContainer(this.bem.be('container'));
|
|
238
|
+
this.quill.on(Quill.events.EDITOR_CHANGE, this.updateWhenTextChange);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
updateWhenTextChange = (eventName: string) => {
|
|
242
|
+
if (eventName === Quill.events.TEXT_CHANGE) {
|
|
243
|
+
if (this.table && !this.quill.root.contains(this.table)) {
|
|
244
|
+
this.setSelectionTable(undefined);
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
this.update();
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
hide() {
|
|
253
|
+
for (const scrollbar of this.scrollbar) {
|
|
254
|
+
scrollbar.destroy();
|
|
255
|
+
}
|
|
256
|
+
this.scrollbar = [];
|
|
257
|
+
this.scrollbarContainer.innerHTML = '';
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
show() {
|
|
261
|
+
if (!this.table) return;
|
|
262
|
+
this.scrollbar = [
|
|
263
|
+
new Scrollbar(this.quill, this.table, { isVertical: true }),
|
|
264
|
+
new Scrollbar(this.quill, this.table, { isVertical: false }),
|
|
265
|
+
];
|
|
266
|
+
for (const item of this.scrollbar) {
|
|
267
|
+
this.scrollbarContainer.appendChild(item.scrollbar);
|
|
268
|
+
item.showScrollbar();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
update() {
|
|
273
|
+
if (this.table) {
|
|
274
|
+
if (this.scrollbar.length <= 0) {
|
|
275
|
+
this.show();
|
|
276
|
+
}
|
|
277
|
+
for (const scrollbar of this.scrollbar) {
|
|
278
|
+
scrollbar.calculateSize();
|
|
279
|
+
scrollbar.setScrollbarPosition();
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
else if (this.scrollbar.length > 0) {
|
|
283
|
+
this.hide();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
destroy() {
|
|
288
|
+
this.hide();
|
|
289
|
+
this.scrollbarContainer.remove();
|
|
290
|
+
this.quill.off(Quill.events.TEXT_CHANGE, this.updateWhenTextChange);
|
|
291
|
+
}
|
|
292
|
+
}
|