-
Notifications
You must be signed in to change notification settings - Fork 3k
/
textEditor.js
445 lines (368 loc) · 14.7 KB
/
textEditor.js
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
import {
addClass,
getCaretPosition,
getComputedStyle,
getCssTransform,
getScrollableElement,
getScrollbarWidth,
innerWidth,
offset,
resetCssTransform,
setCaretPosition,
hasVerticalScrollbar,
hasHorizontalScrollbar
} from './../helpers/dom/element';
import autoResize from './../../lib/autoResize/autoResize';
import BaseEditor, {EditorState} from './_baseEditor';
import EventManager from './../eventManager';
import {KEY_CODES} from './../helpers/unicode';
import {isMSBrowser} from './../helpers/browser';
import {stopPropagation, stopImmediatePropagation, isImmediatePropagationStopped} from './../helpers/dom/event';
const TextEditor = BaseEditor.prototype.extend();
/**
* @private
* @editor TextEditor
* @class TextEditor
* @dependencies autoResize
*/
TextEditor.prototype.init = function() {
var that = this;
this.createElements();
this.eventManager = new EventManager(this);
this.bindEvents();
this.autoResize = autoResize();
this.holderZIndex = -1;
this.instance.addHook('afterDestroy', () => {
that.destroy();
});
};
TextEditor.prototype.prepare = function(row, col, prop, td, originalValue, cellProperties) {
const previousState = this.state;
BaseEditor.prototype.prepare.apply(this, arguments);
if (!cellProperties.readOnly) {
this.refreshDimensions(true);
const {
allowInvalid,
fragmentSelection,
} = cellProperties;
if (allowInvalid) {
this.TEXTAREA.value = ''; // Remove an empty space from texarea (added by copyPaste plugin to make copy/paste functionality work with IME)
}
if (previousState !== EditorState.FINISHED) {
this.hideEditableElement();
}
// @TODO: The fragmentSelection functionality is conflicted with IME. For this feature refocus has to
// be disabled (to make IME working).
const restoreFocus = !fragmentSelection;
if (restoreFocus) {
this.instance._registerImmediate(() => this.focus());
}
}
};
TextEditor.prototype.hideEditableElement = function() {
this.textareaParentStyle.top = '-9999px';
this.textareaParentStyle.left = '-9999px';
this.textareaParentStyle.zIndex = '-1';
};
TextEditor.prototype.showEditableElement = function() {
this.textareaParentStyle.zIndex = this.holderZIndex >= 0 ? this.holderZIndex : '';
};
TextEditor.prototype.getValue = function() {
return this.TEXTAREA.value;
};
TextEditor.prototype.setValue = function(newValue) {
this.TEXTAREA.value = newValue;
};
TextEditor.prototype.beginEditing = function(newInitialValue, event) {
if (this.state !== EditorState.VIRGIN) {
return;
}
this.TEXTAREA.value = ''; // Remove an empty space from texarea (added by copyPaste plugin to make copy/paste functionality work with IME).
BaseEditor.prototype.beginEditing.apply(this, arguments);
};
var onBeforeKeyDown = function onBeforeKeyDown(event) {
var
instance = this,
that = instance.getActiveEditor(),
ctrlDown;
// catch CTRL but not right ALT (which in some systems triggers ALT+CTRL)
ctrlDown = (event.ctrlKey || event.metaKey) && !event.altKey;
// Process only events that have been fired in the editor
if (event.target !== that.TEXTAREA || isImmediatePropagationStopped(event)) {
return;
}
if (event.keyCode === 17 || event.keyCode === 224 || event.keyCode === 91 || event.keyCode === 93) {
// when CTRL or its equivalent is pressed and cell is edited, don't prepare selectable text in textarea
stopImmediatePropagation(event);
return;
}
switch (event.keyCode) {
case KEY_CODES.ARROW_RIGHT:
if (that.isInFullEditMode()) {
if ((!that.isWaiting() && !that.allowKeyEventPropagation) ||
(!that.isWaiting() && that.allowKeyEventPropagation && !that.allowKeyEventPropagation(event.keyCode))) {
stopImmediatePropagation(event);
}
}
break;
case KEY_CODES.ARROW_LEFT:
if (that.isInFullEditMode()) {
if ((!that.isWaiting() && !that.allowKeyEventPropagation) ||
(!that.isWaiting() && that.allowKeyEventPropagation && !that.allowKeyEventPropagation(event.keyCode))) {
stopImmediatePropagation(event);
}
}
break;
case KEY_CODES.ARROW_UP:
case KEY_CODES.ARROW_DOWN:
if (that.isInFullEditMode()) {
if ((!that.isWaiting() && !that.allowKeyEventPropagation) ||
(!that.isWaiting() && that.allowKeyEventPropagation && !that.allowKeyEventPropagation(event.keyCode))) {
stopImmediatePropagation(event);
}
}
break;
case KEY_CODES.ENTER: {
let isMultipleSelection = this.selection.isMultiple();
if ((ctrlDown && !isMultipleSelection) || event.altKey) { // if ctrl+enter or alt+enter, add new line
if (that.isOpened()) {
let caretPosition = getCaretPosition(that.TEXTAREA);
let value = that.getValue();
let newValue = `${value.slice(0, caretPosition)}\n${value.slice(caretPosition)}`;
that.setValue(newValue);
setCaretPosition(that.TEXTAREA, caretPosition + 1);
} else {
that.beginEditing(`${that.originalValue}\n`);
}
stopImmediatePropagation(event);
}
event.preventDefault(); // don't add newline to field
break;
}
case KEY_CODES.A:
case KEY_CODES.X:
case KEY_CODES.C:
case KEY_CODES.V:
if (ctrlDown) {
stopImmediatePropagation(event); // CTRL+A, CTRL+C, CTRL+V, CTRL+X should only work locally when cell is edited (not in table context)
}
break;
case KEY_CODES.BACKSPACE:
case KEY_CODES.DELETE:
case KEY_CODES.HOME:
case KEY_CODES.END:
stopImmediatePropagation(event); // backspace, delete, home, end should only work locally when cell is edited (not in table context)
break;
default:
break;
}
if ([KEY_CODES.ARROW_UP, KEY_CODES.ARROW_RIGHT, KEY_CODES.ARROW_DOWN, KEY_CODES.ARROW_LEFT].indexOf(event.keyCode) === -1) {
that.autoResize.resize(String.fromCharCode(event.keyCode));
}
};
TextEditor.prototype.open = function() {
this.refreshDimensions(); // need it instantly, to prevent https://github.com/handsontable/handsontable/issues/348
this.showEditableElement();
this.instance.addHook('beforeKeyDown', onBeforeKeyDown);
};
TextEditor.prototype.close = function(tdOutside) {
this.autoResize.unObserve();
if (document.activeElement === this.TEXTAREA) {
this.instance.listen(); // don't refocus the table if user focused some cell outside of HT on purpose
}
this.hideEditableElement();
this.setValue('');
this.instance.removeHook('beforeKeyDown', onBeforeKeyDown);
};
TextEditor.prototype.focus = function() {
// For IME editor textarea element must be focused using ".select" method. Using ".focus" browser automatically scroll into
// the focused element which is undesire effect.
this.TEXTAREA.select();
setCaretPosition(this.TEXTAREA, this.TEXTAREA.value.length);
};
TextEditor.prototype.createElements = function() {
this.TEXTAREA = document.createElement('TEXTAREA');
this.TEXTAREA.tabIndex = -1;
addClass(this.TEXTAREA, 'handsontableInput');
this.textareaStyle = this.TEXTAREA.style;
this.textareaStyle.width = 0;
this.textareaStyle.height = 0;
this.TEXTAREA_PARENT = document.createElement('DIV');
addClass(this.TEXTAREA_PARENT, 'handsontableInputHolder');
this.textareaParentStyle = this.TEXTAREA_PARENT.style;
this.textareaParentStyle.zIndex = '-1';
this.TEXTAREA_PARENT.appendChild(this.TEXTAREA);
this.instance.rootElement.appendChild(this.TEXTAREA_PARENT);
};
TextEditor.prototype.getEditedCell = function() {
let editorSection = this.checkEditorSection();
let editedCell;
switch (editorSection) {
case 'top':
editedCell = this.instance.view.wt.wtOverlays.topOverlay.clone.wtTable.getCell({
row: this.row,
col: this.col
});
this.holderZIndex = 101;
break;
case 'top-left-corner':
editedCell = this.instance.view.wt.wtOverlays.topLeftCornerOverlay.clone.wtTable.getCell({
row: this.row,
col: this.col
});
this.holderZIndex = 103;
break;
case 'bottom-left-corner':
editedCell = this.instance.view.wt.wtOverlays.bottomLeftCornerOverlay.clone.wtTable.getCell({
row: this.row,
col: this.col
});
this.holderZIndex = 103;
break;
case 'left':
editedCell = this.instance.view.wt.wtOverlays.leftOverlay.clone.wtTable.getCell({
row: this.row,
col: this.col
});
this.holderZIndex = 102;
break;
case 'bottom':
editedCell = this.instance.view.wt.wtOverlays.bottomOverlay.clone.wtTable.getCell({
row: this.row,
col: this.col
});
this.holderZIndex = 102;
break;
default:
editedCell = this.instance.getCell(this.row, this.col);
this.holderZIndex = -1;
break;
}
return editedCell != -1 && editedCell != -2 ? editedCell : void 0;
};
TextEditor.prototype.refreshValue = function() {
let sourceData = this.instance.getSourceDataAtCell(this.row, this.prop);
this.originalValue = sourceData;
this.setValue(sourceData);
this.refreshDimensions();
};
TextEditor.prototype.refreshDimensions = function(force = false) {
if (this.state !== EditorState.EDITING && !force) {
return;
}
this.TD = this.getEditedCell();
// TD is outside of the viewport.
if (!this.TD) {
if (!force) {
this.close(true);
}
return;
}
const currentOffset = offset(this.TD);
const containerOffset = offset(this.instance.rootElement);
const scrollableContainer = this.instance.view.wt.wtOverlays.topOverlay.mainTableScrollableElement;
const totalRowsCount = this.instance.countRows();
const containerScrollTop = scrollableContainer !== window ?
scrollableContainer.scrollTop : 0;
const containerScrollLeft = scrollableContainer !== window ?
scrollableContainer.scrollLeft : 0;
const editorSection = this.checkEditorSection();
const scrollTop = ['', 'left'].includes(editorSection) ? containerScrollTop : 0;
const scrollLeft = ['', 'top', 'bottom'].includes(editorSection) ? containerScrollLeft : 0;
// If colHeaders is disabled, cells in the first row have border-top
const editTopModifier = currentOffset.top === containerOffset.top ? 0 : 1;
const settings = this.instance.getSettings();
const rowHeadersCount = this.instance.hasRowHeaders();
const colHeadersCount = this.instance.hasColHeaders();
const backgroundColor = this.TD.style.backgroundColor;
let editTop = currentOffset.top - containerOffset.top - editTopModifier - scrollTop;
let editLeft = currentOffset.left - containerOffset.left - 1 - scrollLeft;
let cssTransformOffset;
// TODO: Refactor this to the new instance.getCell method (from #ply-59), after 0.12.1 is released
switch (editorSection) {
case 'top':
cssTransformOffset = getCssTransform(this.instance.view.wt.wtOverlays.topOverlay.clone.wtTable.holder.parentNode);
break;
case 'left':
cssTransformOffset = getCssTransform(this.instance.view.wt.wtOverlays.leftOverlay.clone.wtTable.holder.parentNode);
break;
case 'top-left-corner':
cssTransformOffset = getCssTransform(this.instance.view.wt.wtOverlays.topLeftCornerOverlay.clone.wtTable.holder.parentNode);
break;
case 'bottom-left-corner':
cssTransformOffset = getCssTransform(this.instance.view.wt.wtOverlays.bottomLeftCornerOverlay.clone.wtTable.holder.parentNode);
break;
case 'bottom':
cssTransformOffset = getCssTransform(this.instance.view.wt.wtOverlays.bottomOverlay.clone.wtTable.holder.parentNode);
break;
default:
break;
}
if (colHeadersCount && this.instance.getSelectedLast()[0] === 0 ||
(settings.fixedRowsBottom && this.instance.getSelectedLast()[0] === totalRowsCount - settings.fixedRowsBottom)) {
editTop += 1;
}
if (this.instance.getSelectedLast()[1] === 0) {
editLeft += 1;
}
if (cssTransformOffset && cssTransformOffset != -1) {
this.textareaParentStyle[cssTransformOffset[0]] = cssTransformOffset[1];
} else {
resetCssTransform(this.TEXTAREA_PARENT);
}
this.textareaParentStyle.top = `${editTop}px`;
this.textareaParentStyle.left = `${editLeft}px`;
let firstRowOffset = this.instance.view.wt.wtViewport.rowsRenderCalculator.startPosition;
let firstColumnOffset = this.instance.view.wt.wtViewport.columnsRenderCalculator.startPosition;
let horizontalScrollPosition = this.instance.view.wt.wtOverlays.leftOverlay.getScrollPosition();
let verticalScrollPosition = this.instance.view.wt.wtOverlays.topOverlay.getScrollPosition();
let scrollbarWidth = getScrollbarWidth();
let cellTopOffset = this.TD.offsetTop + firstRowOffset - verticalScrollPosition;
let cellLeftOffset = this.TD.offsetLeft + firstColumnOffset - horizontalScrollPosition;
let width = innerWidth(this.TD) - 8;
let actualVerticalScrollbarWidth = hasVerticalScrollbar(scrollableContainer) ? scrollbarWidth : 0;
let actualHorizontalScrollbarWidth = hasHorizontalScrollbar(scrollableContainer) ? scrollbarWidth : 0;
let maxWidth = this.instance.view.maximumVisibleElementWidth(cellLeftOffset) - 9 - actualVerticalScrollbarWidth;
let height = this.TD.scrollHeight + 1;
let maxHeight = Math.max(this.instance.view.maximumVisibleElementHeight(cellTopOffset) - actualHorizontalScrollbarWidth, 23);
const cellComputedStyle = getComputedStyle(this.TD);
this.TEXTAREA.style.fontSize = cellComputedStyle.fontSize;
this.TEXTAREA.style.fontFamily = cellComputedStyle.fontFamily;
this.TEXTAREA.style.backgroundColor = backgroundColor ? backgroundColor : getComputedStyle(this.TEXTAREA).backgroundColor;
this.autoResize.init(this.TEXTAREA, {
minHeight: Math.min(height, maxHeight),
maxHeight, // TEXTAREA should never be wider than visible part of the viewport (should not cover the scrollbar)
minWidth: Math.min(width, maxWidth),
maxWidth // TEXTAREA should never be wider than visible part of the viewport (should not cover the scrollbar)
}, true);
};
TextEditor.prototype.bindEvents = function() {
var editor = this;
this.eventManager.addEventListener(this.TEXTAREA, 'cut', (event) => {
stopPropagation(event);
});
this.eventManager.addEventListener(this.TEXTAREA, 'paste', (event) => {
stopPropagation(event);
});
this.instance.addHook('afterScrollHorizontally', () => {
editor.refreshDimensions();
});
this.instance.addHook('afterScrollVertically', () => {
editor.refreshDimensions();
});
this.instance.addHook('afterColumnResize', () => {
editor.refreshDimensions();
editor.focus();
});
this.instance.addHook('afterRowResize', () => {
editor.refreshDimensions();
editor.focus();
});
this.instance.addHook('afterDestroy', () => {
editor.eventManager.destroy();
});
};
TextEditor.prototype.destroy = function() {
this.eventManager.destroy();
};
export default TextEditor;