-
Notifications
You must be signed in to change notification settings - Fork 196
/
event-controller.ts
636 lines (568 loc) · 19 KB
/
event-controller.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import {
Canvas,
Event as CanvasEvent,
Group,
type LooseObject,
Shape,
} from '@antv/g-canvas';
import { each, get, hasIn, isEmpty, isNil } from 'lodash';
import { GuiIcon } from '../common';
import {
CellTypes,
InteractionKeyboardKey,
InterceptType,
OriginEventType,
S2Event,
SHAPE_STYLE_MAP,
} from '../common/constant';
import type { EmitterType, ResizeInfo } from '../common/interface';
import type { SpreadSheet } from '../sheet-type';
import { getSelectedData, keyEqualTo } from '../utils/export/copy';
import { verifyTheElementInTooltip } from '../utils/tooltip';
interface EventListener {
target: EventTarget;
type: string;
handler: EventListenerOrEventListenerObject;
options?: AddEventListenerOptions | boolean;
}
interface S2EventHandler {
type: keyof EmitterType;
handler: EmitterType[keyof EmitterType];
}
interface EventHandler {
type: string;
handler: (event: CanvasEvent) => void;
}
export class EventController {
public spreadsheet: SpreadSheet;
// 保存触发的元素
private target: LooseObject;
public canvasEventHandlers: EventHandler[] = [];
public s2EventHandlers: S2EventHandler[] = [];
public domEventListeners: EventListener[] = [];
public isCanvasEffect = false;
public canvasMousemoveEvent: CanvasEvent;
constructor(spreadsheet: SpreadSheet) {
this.spreadsheet = spreadsheet;
this.bindEvents();
}
public get canvasContainer(): Canvas {
return this.spreadsheet.container;
}
public get isAutoResetSheetStyle() {
return this.spreadsheet.options.interaction.autoResetSheetStyle;
}
public bindEvents() {
this.clearAllEvents();
// canvas events
this.addCanvasEvent(OriginEventType.CLICK, this.onCanvasClick);
this.addCanvasEvent(OriginEventType.MOUSE_DOWN, this.onCanvasMousedown);
this.addCanvasEvent(OriginEventType.MOUSE_MOVE, this.onCanvasMousemove);
this.addCanvasEvent(OriginEventType.MOUSE_OUT, this.onCanvasMouseout);
this.addCanvasEvent(OriginEventType.MOUSE_UP, this.onCanvasMouseup);
this.addCanvasEvent(OriginEventType.DOUBLE_CLICK, this.onCanvasDoubleClick);
this.addCanvasEvent(OriginEventType.CONTEXT_MENU, this.onCanvasContextMenu);
// spreadsheet events
this.addS2Event(S2Event.GLOBAL_ACTION_ICON_CLICK, () => {
this.spreadsheet.interaction.addIntercepts([InterceptType.HOVER]);
this.spreadsheet.interaction.clearState();
});
// dom events
this.addDomEventListener(
window,
OriginEventType.CLICK,
(event: MouseEvent) => {
this.resetSheetStyle(event);
this.isCanvasEffect = this.isMouseOnTheCanvasContainer(event);
},
);
this.addDomEventListener(
window,
OriginEventType.KEY_DOWN,
(event: KeyboardEvent) => {
this.onKeyboardCopy(event);
this.onKeyboardEsc(event);
this.spreadsheet.emit(S2Event.GLOBAL_KEYBOARD_DOWN, event);
},
);
this.addDomEventListener(
window,
OriginEventType.KEY_UP,
(event: KeyboardEvent) => {
this.spreadsheet.emit(S2Event.GLOBAL_KEYBOARD_UP, event);
},
);
this.addDomEventListener(
window,
OriginEventType.MOUSE_UP,
(event: MouseEvent) => {
this.spreadsheet.emit(S2Event.GLOBAL_MOUSE_UP, event);
},
);
this.addDomEventListener(
window,
OriginEventType.MOUSE_MOVE,
(event: MouseEvent) => {
this.spreadsheet.emit(S2Event.GLOBAL_MOUSE_MOVE, event);
},
);
}
// 不能单独判断是否 Image Shape, 用户如果自定义单元格绘制图片, 会导致判断错误
private isGuiIconShape = (target: LooseObject) => {
return target instanceof Shape.Image && target.attrs.type === GuiIcon.type;
};
private onKeyboardCopy(event: KeyboardEvent) {
// windows and macos copy
if (
this.isCanvasEffect &&
this.spreadsheet.options.interaction.enableCopy &&
// todo: 在copy header 时有问题
keyEqualTo(event.key, InteractionKeyboardKey.COPY) &&
(event.metaKey || event.ctrlKey)
) {
const copyData = getSelectedData(this.spreadsheet);
if (!isNil(copyData)) {
this.spreadsheet.emit(S2Event.GLOBAL_COPIED, copyData);
}
}
}
private onKeyboardEsc(event: KeyboardEvent) {
if (
this.isCanvasEffect &&
keyEqualTo(event.key, InteractionKeyboardKey.ESC)
) {
this.resetSheetStyle(event);
}
}
private resetSheetStyle(event: Event) {
if (!this.isAutoResetSheetStyle || !this.spreadsheet) {
return;
}
// 全局有 mouseUp 和 click 事件, 当刷选完成后会同时触发, 当选中单元格后, 会同时触发 click 对应的 reset 事件
// 所以如果是 刷选过程中 引起的 click(mousedown + mouseup) 事件, 则不需要重置
const { interaction } = this.spreadsheet;
if (
interaction.hasIntercepts([
InterceptType.BRUSH_SELECTION,
InterceptType.COL_BRUSH_SELECTION,
InterceptType.ROW_BRUSH_SELECTION,
])
) {
interaction.removeIntercepts([
InterceptType.BRUSH_SELECTION,
InterceptType.ROW_BRUSH_SELECTION,
InterceptType.COL_BRUSH_SELECTION,
]);
return;
}
if (
this.isMouseOnTheTooltip(event) ||
this.isMouseOnTheCanvasContainer(event)
) {
return;
}
interaction.reset();
this.spreadsheet.emit(S2Event.GLOBAL_RESET, event);
this.spreadsheet.emit(
S2Event.GLOBAL_SELECTED,
interaction.getActiveCells(),
);
}
private isMouseEvent(event: Event): event is MouseEvent {
// 通过 MouseEvent 特有属性判断,避免 instanceof 失效的问题
return hasIn(event, 'clientX') && hasIn(event, 'clientY');
}
private isMouseOnTheCanvasContainer(event: Event) {
if (this.isMouseEvent(event)) {
const canvas = this.spreadsheet.getCanvasElement();
if (!canvas) {
return false;
}
// 开启 CSS transform 时, 降级处理, 不做 canvas 内的空白检测: https://github.com/antvis/S2/issues/2879
if (this.spreadsheet.options.supportCSSTransform) {
return canvas.contains(event.target as HTMLElement);
}
const { x, y } = canvas.getBoundingClientRect() || {};
// 这里不能使用 bounding rect 的 width 和 height, 高清适配后 canvas 实际宽高会变
// 比如实际 400 * 300 => hd (800 * 600)
// 从视觉来看, 虽然点击了空白处, 但其实还是处于 放大后的 canvas 区域, 所以还需要额外判断一下坐标
const { width, height } = this.getContainerRect();
return (
canvas.contains(event.target as HTMLElement) &&
event.clientX <= x + width &&
event.clientY <= y + height
);
}
return false;
}
private getContainerRect() {
const { facet, options } = this.spreadsheet;
const scrollBar = facet?.hRowScrollBar || facet?.hScrollBar;
const { maxX, maxY } = facet?.panelBBox || {};
const { width, height } = options;
/**
* https://github.com/antvis/S2/issues/2376
* 横向的滚动条在表格外 (Canvas 内), 点击滚动条(含滑道区域) 不应该重置交互
*/
const trackHeight = scrollBar?.theme?.size || 0;
return {
width: Math.min(width, maxX),
height: Math.min(height, maxY + trackHeight),
};
}
private isMouseOnTheTooltip(event: Event) {
const { tooltip } = this.spreadsheet;
if (!tooltip?.visible) {
return false;
}
const { x, y, width, height } =
this.spreadsheet.tooltip?.container?.getBoundingClientRect?.() || {};
if (event.target instanceof Node) {
return verifyTheElementInTooltip(
this.spreadsheet.tooltip?.container,
event.target,
);
}
if (this.isMouseEvent(event)) {
return (
event.clientX >= x &&
event.clientX <= x + width &&
event.clientY >= y &&
event.clientY <= y + height
);
}
return false;
}
private isResizeArea(event: CanvasEvent) {
const appendInfo = get(event.target, 'attrs.appendInfo') as ResizeInfo;
return appendInfo?.isResizeArea;
}
private activeResizeArea(event: CanvasEvent) {
this.resetResizeArea();
const resizeArea = event.target as Group;
this.spreadsheet.store.set('activeResizeArea', resizeArea);
resizeArea.attr(
SHAPE_STYLE_MAP.backgroundOpacity,
this.spreadsheet.theme.resizeArea.interactionState.hover
.backgroundOpacity,
);
}
private resetResizeArea() {
const resizeArea = this.spreadsheet.store.get('activeResizeArea');
if (!isEmpty(resizeArea)) {
resizeArea.attr(
SHAPE_STYLE_MAP.backgroundOpacity,
this.spreadsheet.theme.resizeArea.backgroundOpacity,
);
}
this.spreadsheet.store.set('activeResizeArea', resizeArea);
}
private onCanvasMousedown = (event: CanvasEvent) => {
this.target = event.target;
// 点击时清除 hover focus 状态
this.spreadsheet.interaction.clearHoverTimer();
if (this.isResizeArea(event)) {
this.spreadsheet.emit(S2Event.LAYOUT_RESIZE_MOUSE_DOWN, event);
// 仅捕获在 canvas 之外触发的事件 https://github.com/antvis/S2/issues/1592
const resizeMouseMoveCapture = (mouseEvent: MouseEvent) => {
if (!this.spreadsheet.getCanvasElement()) {
return false;
}
if (this.spreadsheet.getCanvasElement() !== mouseEvent.target) {
event.clientX = mouseEvent.clientX;
event.clientY = mouseEvent.clientY;
event.originalEvent = mouseEvent;
this.spreadsheet.emit(S2Event.LAYOUT_RESIZE_MOUSE_MOVE, event);
}
};
window.addEventListener('mousemove', resizeMouseMoveCapture);
window.addEventListener(
'mouseup',
() => {
window.removeEventListener('mousemove', resizeMouseMoveCapture);
},
{ once: true },
);
return;
}
const cellType = this.spreadsheet.getCellType(event.target);
switch (cellType) {
case CellTypes.DATA_CELL:
this.spreadsheet.emit(S2Event.DATA_CELL_MOUSE_DOWN, event);
break;
case CellTypes.ROW_CELL:
this.spreadsheet.emit(S2Event.ROW_CELL_MOUSE_DOWN, event);
break;
case CellTypes.COL_CELL:
this.spreadsheet.emit(S2Event.COL_CELL_MOUSE_DOWN, event);
break;
case CellTypes.CORNER_CELL:
this.spreadsheet.emit(S2Event.CORNER_CELL_MOUSE_DOWN, event);
break;
case CellTypes.MERGED_CELL:
this.spreadsheet.emit(S2Event.MERGED_CELLS_MOUSE_DOWN, event);
break;
default:
break;
}
};
private onCanvasMousemove = (event: CanvasEvent) => {
this.canvasMousemoveEvent = event;
if (this.isResizeArea(event)) {
this.activeResizeArea(event);
this.spreadsheet.emit(S2Event.LAYOUT_RESIZE_MOUSE_MOVE, event);
return;
}
this.resetResizeArea();
const cell = this.spreadsheet.getCell(event.target);
if (cell) {
const cellType = cell.cellType;
switch (cellType) {
case CellTypes.DATA_CELL:
this.spreadsheet.emit(S2Event.DATA_CELL_MOUSE_MOVE, event);
break;
case CellTypes.ROW_CELL:
this.spreadsheet.emit(S2Event.ROW_CELL_MOUSE_MOVE, event);
break;
case CellTypes.COL_CELL:
this.spreadsheet.emit(S2Event.COL_CELL_MOUSE_MOVE, event);
break;
case CellTypes.CORNER_CELL:
this.spreadsheet.emit(S2Event.CORNER_CELL_MOUSE_MOVE, event);
break;
case CellTypes.MERGED_CELL:
this.spreadsheet.emit(S2Event.MERGED_CELLS_MOUSE_MOVE, event);
break;
default:
break;
}
if (!this.hasBrushSelectionIntercepts()) {
this.spreadsheet.emit(S2Event.GLOBAL_HOVER, event);
switch (cellType) {
case CellTypes.DATA_CELL:
this.spreadsheet.emit(S2Event.DATA_CELL_HOVER, event);
break;
case CellTypes.ROW_CELL:
this.spreadsheet.emit(S2Event.ROW_CELL_HOVER, event);
break;
case CellTypes.COL_CELL:
this.spreadsheet.emit(S2Event.COL_CELL_HOVER, event);
break;
case CellTypes.CORNER_CELL:
this.spreadsheet.emit(S2Event.CORNER_CELL_HOVER, event);
break;
case CellTypes.MERGED_CELL:
this.spreadsheet.emit(S2Event.MERGED_CELLS_HOVER, event);
break;
default:
break;
}
}
}
};
private hasBrushSelectionIntercepts() {
return this.spreadsheet.interaction.hasIntercepts([
InterceptType.HOVER,
InterceptType.BRUSH_SELECTION,
InterceptType.ROW_BRUSH_SELECTION,
InterceptType.COL_BRUSH_SELECTION,
]);
}
private onCanvasMouseup = (event: CanvasEvent) => {
if (this.isResizeArea(event)) {
this.spreadsheet.emit(S2Event.LAYOUT_RESIZE_MOUSE_UP, event);
return;
}
const cell = this.spreadsheet.getCell(event.target);
if (cell) {
const cellType = cell.cellType;
// target相同,说明是一个cell内的 click 事件
if (this.target === event.target) {
const isGuiIconShape = this.isGuiIconShape(event.target);
switch (cellType) {
case CellTypes.DATA_CELL:
this.spreadsheet.emit(S2Event.DATA_CELL_CLICK, event);
break;
case CellTypes.ROW_CELL:
// 屏蔽 actionIcons 的点击,只有 HeaderCells 需要, DataCell 有状态类 icon, 不需要屏蔽
if (isGuiIconShape) {
break;
}
this.spreadsheet.emit(S2Event.ROW_CELL_CLICK, event);
break;
case CellTypes.COL_CELL:
if (isGuiIconShape) {
break;
}
this.spreadsheet.emit(S2Event.COL_CELL_CLICK, event);
break;
case CellTypes.CORNER_CELL:
if (isGuiIconShape) {
break;
}
this.spreadsheet.emit(S2Event.CORNER_CELL_CLICK, event);
break;
case CellTypes.MERGED_CELL:
this.spreadsheet.emit(S2Event.MERGED_CELLS_CLICK, event);
break;
default:
break;
}
}
// 通用的mouseup事件
switch (cellType) {
case CellTypes.DATA_CELL:
this.spreadsheet.emit(S2Event.DATA_CELL_MOUSE_UP, event);
break;
case CellTypes.ROW_CELL:
this.spreadsheet.emit(S2Event.ROW_CELL_MOUSE_UP, event);
break;
case CellTypes.COL_CELL:
this.spreadsheet.emit(S2Event.COL_CELL_MOUSE_UP, event);
break;
case CellTypes.CORNER_CELL:
this.spreadsheet.emit(S2Event.CORNER_CELL_MOUSE_UP, event);
break;
case CellTypes.MERGED_CELL:
this.spreadsheet.emit(S2Event.MERGED_CELLS_MOUSE_UP, event);
break;
default:
break;
}
}
};
private onCanvasClick = (event: CanvasEvent) => {
this.spreadsheet.emit(S2Event.GLOBAL_CLICK, event);
};
private onCanvasDoubleClick = (event: CanvasEvent) => {
const spreadsheet = this.spreadsheet;
if (this.isResizeArea(event)) {
spreadsheet.emit(S2Event.LAYOUT_RESIZE_MOUSE_UP, event);
return;
}
spreadsheet.emit(S2Event.GLOBAL_DOUBLE_CLICK, event);
const cell = spreadsheet.getCell(event.target);
if (cell) {
const cellType = cell.cellType;
if (this.target === event.target) {
switch (cellType) {
case CellTypes.DATA_CELL:
spreadsheet.emit(S2Event.DATA_CELL_DOUBLE_CLICK, event);
break;
case CellTypes.ROW_CELL:
spreadsheet.emit(S2Event.ROW_CELL_DOUBLE_CLICK, event);
break;
case CellTypes.COL_CELL:
spreadsheet.emit(S2Event.COL_CELL_DOUBLE_CLICK, event);
break;
case CellTypes.CORNER_CELL:
spreadsheet.emit(S2Event.CORNER_CELL_DOUBLE_CLICK, event);
break;
case CellTypes.MERGED_CELL:
spreadsheet.emit(S2Event.MERGED_CELLS_DOUBLE_CLICK, event);
break;
default:
break;
}
}
}
};
private onCanvasMouseout = (event: CanvasEvent) => {
if (!this.isAutoResetSheetStyle || event?.shape) {
return;
}
const { interaction } = this.spreadsheet;
// 两种情况不能重置 1. 选中单元格 2. 有 intercepts 时(重置会清空 intercepts)
if (!interaction.isSelectedState() && !(interaction.intercepts.size > 0)) {
interaction.reset();
}
};
private onCanvasContextMenu = (event: CanvasEvent) => {
const spreadsheet = this.spreadsheet;
if (this.isResizeArea(event)) {
spreadsheet.emit(S2Event.LAYOUT_RESIZE_MOUSE_UP, event);
return;
}
spreadsheet.emit(S2Event.GLOBAL_CONTEXT_MENU, event);
const cellType = this.spreadsheet.getCellType(event.target);
switch (cellType) {
case CellTypes.DATA_CELL:
this.spreadsheet.emit(S2Event.DATA_CELL_CONTEXT_MENU, event);
break;
case CellTypes.ROW_CELL:
this.spreadsheet.emit(S2Event.ROW_CELL_CONTEXT_MENU, event);
break;
case CellTypes.COL_CELL:
this.spreadsheet.emit(S2Event.COL_CELL_CONTEXT_MENU, event);
break;
case CellTypes.CORNER_CELL:
this.spreadsheet.emit(S2Event.CORNER_CELL_CONTEXT_MENU, event);
break;
case CellTypes.MERGED_CELL:
this.spreadsheet.emit(S2Event.MERGED_CELLS_CONTEXT_MENU, event);
break;
default:
break;
}
};
public clear() {
this.unbindEvents();
}
private unbindEvents() {
this.clearAllEvents();
}
private addCanvasEvent(
eventType: string,
handler: (ev: CanvasEvent) => void,
) {
this.canvasContainer?.on(eventType, handler);
this.canvasEventHandlers.push({ type: eventType, handler });
}
private addS2Event<K extends keyof EmitterType>(
eventType: K,
handler: EmitterType[K],
) {
this.spreadsheet.on(eventType, handler);
this.s2EventHandlers.push({
type: eventType,
handler,
});
}
private addDomEventListener(
target: EventTarget,
type: string,
handler: EventListenerOrEventListenerObject,
) {
if (target.addEventListener) {
const { eventListenerOptions } = this.spreadsheet.options.interaction;
target.addEventListener(type, handler, eventListenerOptions);
this.domEventListeners.push({
target,
type,
handler,
options: eventListenerOptions,
});
} else {
// eslint-disable-next-line no-console
console.error(`Please make sure ${target} has addEventListener function`);
}
}
public clearAllEvents() {
each(this.canvasEventHandlers, ({ type, handler }) => {
this.canvasContainer?.off(type, handler);
});
each(this.s2EventHandlers, ({ type, handler }) => {
this.spreadsheet.off(type, handler);
});
each(this.domEventListeners, (listener) => {
listener.target.removeEventListener(
listener.type,
listener.handler,
listener.options,
);
});
this.canvasEventHandlers = [];
this.s2EventHandlers = [];
this.domEventListeners = [];
}
}