-
Notifications
You must be signed in to change notification settings - Fork 146
/
TableResize.ts
187 lines (166 loc) · 6.24 KB
/
TableResize.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
import TableEditor from './editors/TableEditor';
import { contains, normalizeRect, safeInstanceOf } from 'roosterjs-editor-dom';
import { PluginEventType } from 'roosterjs-editor-types';
import type {
CreateElementData,
EditorPlugin,
IEditor,
PluginEvent,
Rect,
} from 'roosterjs-editor-types';
const TABLE_RESIZER_LENGTH = 12;
/**
* TableResize plugin, provides the ability to resize a table by drag-and-drop
*/
export default class TableResize implements EditorPlugin {
private editor: IEditor | null = null;
private onMouseMoveDisposer: (() => void) | null = null;
private tableRectMap: { table: HTMLTableElement; rect: Rect }[] | null = null;
private tableEditor: TableEditor | null = null;
/**
* Construct a new instance of TableResize plugin
* @param onShowHelperElement An optional callback to allow customize helper element of table resizing.
* To customize the helper element, add this callback and change the attributes of elementData then it
* will be picked up by TableResize code
* @param anchorContainerSelector An optional selector string to specify the container to host the plugin.
* The container must not be affected by transform: scale(), otherwise the position calculation will be wrong.
* If not specified, the plugin will be inserted in document.body
*/
constructor(
private onShowHelperElement?: (
elementData: CreateElementData,
helperType: 'CellResizer' | 'TableInserter' | 'TableResizer' | 'TableSelector',
tableOrTd: HTMLTableElement | HTMLTableCellElement
) => void,
private anchorContainerSelector?: string
) {}
/**
* Get a friendly name of this plugin
*/
getName() {
return 'TableResize';
}
/**
* Initialize this plugin. This should only be called from Editor
* @param editor Editor instance
*/
initialize(editor: IEditor) {
this.editor = editor;
this.onMouseMoveDisposer = this.editor.addDomEventHandler({
mousemove: this.onMouseMove,
});
const scrollContainer = this.editor.getScrollContainer();
scrollContainer.addEventListener('mouseout', this.onMouseOut);
}
private onMouseOut = ({ relatedTarget, currentTarget }: MouseEvent) => {
if (
safeInstanceOf(relatedTarget, 'HTMLElement') &&
safeInstanceOf(currentTarget, 'HTMLElement') &&
this.tableEditor &&
!this.tableEditor.isOwnedElement(relatedTarget) &&
!contains(currentTarget, relatedTarget)
) {
this.setTableEditor(null);
}
};
/**
* Dispose this plugin
*/
dispose() {
const scrollContainer = this.editor?.getScrollContainer();
scrollContainer?.removeEventListener('mouseout', this.onMouseOut);
this.onMouseMoveDisposer?.();
this.invalidateTableRects();
this.disposeTableEditor();
this.editor = null;
this.onMouseMoveDisposer = null;
}
/**
* Handle events triggered from editor
* @param event PluginEvent object
*/
onPluginEvent(e: PluginEvent) {
switch (e.eventType) {
case PluginEventType.Input:
case PluginEventType.ContentChanged:
case PluginEventType.Scroll:
case PluginEventType.ZoomChanged:
this.setTableEditor(null);
this.invalidateTableRects();
break;
}
}
private onMouseMove = (event: Event) => {
const e = event as MouseEvent;
if (e.buttons > 0 || !this.editor) {
return;
}
this.ensureTableRects();
const editorWindow = this.editor.getDocument().defaultView || window;
const x = e.pageX - editorWindow.scrollX;
const y = e.pageY - editorWindow.scrollY;
let currentTable: HTMLTableElement | null = null;
if (this.tableRectMap) {
for (let i = this.tableRectMap.length - 1; i >= 0; i--) {
const { table, rect } = this.tableRectMap[i];
if (
x >= rect.left - TABLE_RESIZER_LENGTH &&
x <= rect.right + TABLE_RESIZER_LENGTH &&
y >= rect.top - TABLE_RESIZER_LENGTH &&
y <= rect.bottom + TABLE_RESIZER_LENGTH
) {
currentTable = table;
break;
}
}
}
this.setTableEditor(currentTable, e);
this.tableEditor?.onMouseMove(x, y);
};
/**
* @internal Public only for unit test
* @param table Table to use when setting the Editors
* @param event (Optional) Mouse event
*/
public setTableEditor(table: HTMLTableElement | null, event?: MouseEvent) {
if (this.tableEditor && !this.tableEditor.isEditing() && table != this.tableEditor.table) {
this.disposeTableEditor();
}
if (!this.tableEditor && table && this.editor && table.rows.length > 0) {
const container = this.anchorContainerSelector
? this.editor.getDocument().querySelector(this.anchorContainerSelector)
: undefined;
this.tableEditor = new TableEditor(
this.editor,
table,
this.invalidateTableRects,
this.onShowHelperElement,
safeInstanceOf(container, 'HTMLElement') ? container : undefined,
event?.currentTarget
);
}
}
private invalidateTableRects = () => {
this.tableRectMap = null;
};
private disposeTableEditor() {
this.tableEditor?.dispose();
this.tableEditor = null;
}
private ensureTableRects() {
if (!this.tableRectMap && this.editor) {
this.tableRectMap = [];
this.editor.queryElements('table', table => {
if (table.isContentEditable) {
const rect = normalizeRect(table.getBoundingClientRect());
if (rect && this.tableRectMap) {
this.tableRectMap.push({
table,
rect,
});
}
}
});
}
}
}