/
input.ts
413 lines (335 loc) · 11.6 KB
/
input.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
import { DIRECTION, type Selection } from '../core/utils';
import ActionDetails from '../core/action-details';
import createMask, { type UpdateOpts, maskedClass, type FactoryArg, type FactoryReturnMasked } from '../masked/factory';
import Masked from '../masked/base';
import MaskElement from './mask-element';
import HTMLInputMaskElement, { type InputElement } from './html-input-mask-element';
import HTMLContenteditableMaskElement from './html-contenteditable-mask-element';
import IMask from '../core/holder';
import InputHistory, { type InputHistoryState } from './input-history';
export
type InputMaskElement = MaskElement | InputElement | HTMLElement;
export
type InputMaskEventListener = (e?: InputEvent) => void;
/** Listens to element events and controls changes between element and {@link Masked} */
export default
class InputMask<Opts extends FactoryArg=Record<string, unknown>> {
/**
View element
*/
declare el: MaskElement;
/** Internal {@link Masked} model */
declare masked: FactoryReturnMasked<Opts>;
declare _listeners: Record<string, Array<InputMaskEventListener>>;
declare _value: string;
declare _changingCursorPos: number;
declare _unmaskedValue: string;
declare _rawInputValue: string;
declare _selection: Selection;
declare _cursorChanging?: ReturnType<typeof setTimeout>;
declare _historyChanging?: boolean;
declare _inputEvent?: InputEvent;
declare history: InputHistory;
constructor (el: InputMaskElement, opts: Opts) {
this.el =
(el instanceof MaskElement) ? el :
(el.isContentEditable && el.tagName !== 'INPUT' && el.tagName !== 'TEXTAREA') ? new HTMLContenteditableMaskElement(el) :
new HTMLInputMaskElement(el as InputElement);
this.masked = createMask(opts);
this._listeners = {};
this._value = '';
this._unmaskedValue = '';
this._rawInputValue = '';
this.history = new InputHistory();
this._saveSelection = this._saveSelection.bind(this);
this._onInput = this._onInput.bind(this);
this._onChange = this._onChange.bind(this);
this._onDrop = this._onDrop.bind(this);
this._onFocus = this._onFocus.bind(this);
this._onClick = this._onClick.bind(this);
this._onUndo = this._onUndo.bind(this);
this._onRedo = this._onRedo.bind(this);
this.alignCursor = this.alignCursor.bind(this);
this.alignCursorFriendly = this.alignCursorFriendly.bind(this);
this._bindEvents();
// refresh
this._onChange();
}
maskEquals (mask: any): boolean {
return mask == null || this.masked?.maskEquals(mask);
}
/** Masked */
get mask (): FactoryReturnMasked<Opts>['mask'] {
return this.masked.mask;
}
set mask (mask: any) {
if (this.maskEquals(mask)) return;
if (!((mask as Masked) instanceof IMask.Masked) && this.masked.constructor === maskedClass(mask as Masked)) {
// TODO "any" no idea
this.masked.updateOptions({ mask } as any);
return;
}
const masked = (mask instanceof IMask.Masked ? mask : createMask({ mask } as Opts)) as FactoryReturnMasked<Opts>;
masked.unmaskedValue = this.masked.unmaskedValue;
this.masked = masked;
}
/** Raw value */
get value (): string {
return this._value;
}
set value (str: string) {
if (this.value === str) return;
this.masked.value = str;
this.updateControl('auto');
}
/** Unmasked value */
get unmaskedValue (): string {
return this._unmaskedValue;
}
set unmaskedValue (str: string) {
if (this.unmaskedValue === str) return;
this.masked.unmaskedValue = str;
this.updateControl('auto');
}
/** Raw input value */
get rawInputValue (): string {
return this._rawInputValue;
}
set rawInputValue (str: string) {
if (this.rawInputValue === str) return;
this.masked.rawInputValue = str;
this.updateControl();
this.alignCursor();
}
/** Typed unmasked value */
get typedValue (): FactoryReturnMasked<Opts>['typedValue'] {
return this.masked.typedValue;
}
set typedValue (val: FactoryReturnMasked<Opts>['typedValue']) {
if (this.masked.typedValueEquals(val)) return;
this.masked.typedValue = val;
this.updateControl('auto');
}
/** Display value */
get displayValue (): string {
return this.masked.displayValue;
}
/** Starts listening to element events */
_bindEvents () {
this.el.bindEvents({
selectionChange: this._saveSelection,
input: this._onInput,
drop: this._onDrop,
click: this._onClick,
focus: this._onFocus,
commit: this._onChange,
undo: this._onUndo,
redo: this._onRedo,
});
}
/** Stops listening to element events */
_unbindEvents () {
if (this.el) this.el.unbindEvents();
}
/** Fires custom event */
_fireEvent (ev: string, e?: InputEvent) {
const listeners = this._listeners[ev];
if (!listeners) return;
listeners.forEach(l => l(e));
}
/** Current selection start */
get selectionStart (): number {
return this._cursorChanging ?
this._changingCursorPos :
this.el.selectionStart;
}
/** Current cursor position */
get cursorPos (): number {
return this._cursorChanging ?
this._changingCursorPos :
this.el.selectionEnd;
}
set cursorPos (pos: number) {
if (!this.el || !this.el.isActive) return;
this.el.select(pos, pos);
this._saveSelection();
}
/** Stores current selection */
_saveSelection (/* ev */) {
if (this.displayValue !== this.el.value) {
console.warn('Element value was changed outside of mask. Syncronize mask using `mask.updateValue()` to work properly.'); // eslint-disable-line no-console
}
this._selection = {
start: this.selectionStart,
end: this.cursorPos,
};
}
/** Syncronizes model value from view */
updateValue () {
this.masked.value = this.el.value;
this._value = this.masked.value;
this._unmaskedValue = this.masked.unmaskedValue;
this._rawInputValue = this.masked.rawInputValue;
}
/** Syncronizes view from model value, fires change events */
updateControl (cursorPos?: number | 'auto') {
const newUnmaskedValue = this.masked.unmaskedValue;
const newValue = this.masked.value;
const newRawInputValue = this.masked.rawInputValue;
const newDisplayValue = this.displayValue;
const isChanged =
this.unmaskedValue !== newUnmaskedValue ||
this.value !== newValue ||
this._rawInputValue !== newRawInputValue
;
this._unmaskedValue = newUnmaskedValue;
this._value = newValue;
this._rawInputValue = newRawInputValue;
if (this.el.value !== newDisplayValue) this.el.value = newDisplayValue;
if (cursorPos === 'auto') this.alignCursor();
else if (cursorPos != null) this.cursorPos = cursorPos;
if (isChanged) this._fireChangeEvents();
if (!this._historyChanging && (isChanged || this.history.isEmpty)) this.history.push({
unmaskedValue: newUnmaskedValue,
selection: { start: this.selectionStart, end: this.cursorPos },
});
}
/** Updates options with deep equal check, recreates {@link Masked} model if mask type changes */
updateOptions(opts: UpdateOpts<Opts>) {
const { mask, ...restOpts } = opts as any; // TODO types, yes, mask is optional
const updateMask = !this.maskEquals(mask);
const updateOpts = this.masked.optionsIsChanged(restOpts);
if (updateMask) this.mask = mask;
if (updateOpts) this.masked.updateOptions(restOpts); // TODO
if (updateMask || updateOpts) this.updateControl();
}
/** Updates cursor */
updateCursor (cursorPos: number) {
if (cursorPos == null) return;
this.cursorPos = cursorPos;
// also queue change cursor for mobile browsers
this._delayUpdateCursor(cursorPos);
}
/** Delays cursor update to support mobile browsers */
_delayUpdateCursor (cursorPos: number) {
this._abortUpdateCursor();
this._changingCursorPos = cursorPos;
this._cursorChanging = setTimeout(() => {
if (!this.el) return; // if was destroyed
this.cursorPos = this._changingCursorPos;
this._abortUpdateCursor();
}, 10);
}
/** Fires custom events */
_fireChangeEvents () {
this._fireEvent('accept', this._inputEvent);
if (this.masked.isComplete) this._fireEvent('complete', this._inputEvent);
}
/** Aborts delayed cursor update */
_abortUpdateCursor () {
if (this._cursorChanging) {
clearTimeout(this._cursorChanging);
delete this._cursorChanging;
}
}
/** Aligns cursor to nearest available position */
alignCursor () {
this.cursorPos = this.masked.nearestInputPos(this.masked.nearestInputPos(this.cursorPos, DIRECTION.LEFT));
}
/** Aligns cursor only if selection is empty */
alignCursorFriendly () {
if (this.selectionStart !== this.cursorPos) return; // skip if range is selected
this.alignCursor();
}
/** Adds listener on custom event */
on (ev: string, handler: InputMaskEventListener): this {
if (!this._listeners[ev]) this._listeners[ev] = [];
this._listeners[ev].push(handler);
return this;
}
/** Removes custom event listener */
off (ev: string, handler: InputMaskEventListener): this {
if (!this._listeners[ev]) return this;
if (!handler) {
delete this._listeners[ev];
return this;
}
const hIndex = this._listeners[ev].indexOf(handler);
if (hIndex >= 0) this._listeners[ev].splice(hIndex, 1);
return this;
}
/** Handles view input event */
_onInput (e: InputEvent): void {
this._inputEvent = e;
this._abortUpdateCursor();
const details = new ActionDetails({
// new state
value: this.el.value,
cursorPos: this.cursorPos,
// old state
oldValue: this.displayValue,
oldSelection: this._selection,
});
const oldRawValue = this.masked.rawInputValue;
const offset = this.masked.splice(
details.startChangePos,
details.removed.length,
details.inserted,
details.removeDirection,
{ input: true, raw: true },
).offset;
// force align in remove direction only if no input chars were removed
// otherwise we still need to align with NONE (to get out from fixed symbols for instance)
const removeDirection = oldRawValue === this.masked.rawInputValue ?
details.removeDirection :
DIRECTION.NONE;
let cursorPos = this.masked.nearestInputPos(
details.startChangePos + offset,
removeDirection,
);
if (removeDirection !== DIRECTION.NONE) cursorPos = this.masked.nearestInputPos(cursorPos, DIRECTION.NONE);
this.updateControl(cursorPos);
delete this._inputEvent;
}
/** Handles view change event and commits model value */
_onChange () {
if (this.displayValue !== this.el.value) this.updateValue();
this.masked.doCommit();
this.updateControl();
this._saveSelection();
}
/** Handles view drop event, prevents by default */
_onDrop (ev: Event) {
ev.preventDefault();
ev.stopPropagation();
}
/** Restore last selection on focus */
_onFocus (ev: Event) {
this.alignCursorFriendly();
}
/** Restore last selection on focus */
_onClick (ev: Event) {
this.alignCursorFriendly();
}
_onUndo () {
this._applyHistoryState(this.history.undo());
}
_onRedo () {
this._applyHistoryState(this.history.redo());
}
_applyHistoryState (state: InputHistoryState | undefined) {
if (!state) return;
this._historyChanging = true;
this.unmaskedValue = state.unmaskedValue;
this.el.select(state.selection.start, state.selection.end);
this._saveSelection();
this._historyChanging = false;
}
/** Unbind view events and removes element reference */
destroy () {
this._unbindEvents();
(this._listeners as any).length = 0;
delete (this as any).el;
}
}
IMask.InputMask = InputMask;