-
Notifications
You must be signed in to change notification settings - Fork 146
/
TableCellSelection.ts
96 lines (90 loc) · 3.2 KB
/
TableCellSelection.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
import { clearState } from './utils/clearState';
import { DeleteTableContents } from './features/DeleteTableContents';
import { handleKeyDownEvent } from './keyUtils/handleKeyDownEvent';
import { handleKeyUpEvent } from './keyUtils/handleKeyUpEvent';
import { handleMouseDownEvent } from './mouseUtils/handleMouseDownEvent';
import { handleScrollEvent } from './mouseUtils/handleScrollEvent';
import { PluginEventType } from 'roosterjs-editor-types';
import type { TableCellSelectionState } from './TableCellSelectionState';
import type { EditorPlugin, IEditor, PluginEvent } from 'roosterjs-editor-types';
/**
* TableCellSelectionPlugin help highlight table cells
*/
export default class TableCellSelection implements EditorPlugin {
private editor: IEditor | null = null;
private state: TableCellSelectionState | null;
constructor() {
this.state = {
firstTable: null,
lastTarget: null,
firstTarget: null,
preventKeyUp: false,
startedSelection: false,
tableSelection: false,
targetTable: null,
vTable: null,
mouseMoveDisposer: null,
};
}
/**
* Get a friendly name of this plugin
*/
getName() {
return 'TableCellSelection';
}
/**
* Initialize this plugin. This should only be called from Editor
* @param editor Editor instance
*/
initialize(editor: IEditor) {
this.editor = editor;
this.editor.addContentEditFeature(DeleteTableContents);
}
/**
* Dispose this plugin
*/
dispose() {
clearState(this.state, this.editor);
this.state = null;
this.editor = null;
}
/**
* Handle events triggered from editor
* @param event PluginEvent object
*/
onPluginEvent(event: PluginEvent) {
if (this.editor && this.state) {
switch (event.eventType) {
case PluginEventType.MouseDown:
if (!this.state.startedSelection) {
handleMouseDownEvent(event, this.state, this.editor);
}
break;
case PluginEventType.KeyDown:
if (!this.state.startedSelection) {
handleKeyDownEvent(event, this.state, this.editor);
} else {
event.rawEvent.preventDefault();
}
break;
case PluginEventType.KeyUp:
if (!this.state.startedSelection) {
handleKeyUpEvent(event, this.state, this.editor);
} else {
event.rawEvent.preventDefault();
}
break;
case PluginEventType.Scroll:
if (this.state.startedSelection) {
handleScrollEvent(this.state, this.editor);
}
break;
case PluginEventType.BeforeSetContent:
this.state.firstTable = null;
this.state.tableSelection = false;
this.editor.select(null);
break;
}
}
}
}