-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
mask-element.ts
75 lines (62 loc) · 1.48 KB
/
mask-element.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
import IMask from '../core/holder';
export
type ElementEvent =
| 'selectionChange'
| 'input'
| 'drop'
| 'click'
| 'focus'
| 'commit'
;
export
type EventHandlers = { [key in ElementEvent]: (...args: any[]) => void } & {
undo?: (...args: any[]) => void;
redo?: (...args: any[]) => void;
}
/** Generic element API to use with mask */
export default
abstract class MaskElement {
/** */
abstract _unsafeSelectionStart: number | null;
/** */
abstract _unsafeSelectionEnd: number | null;
/** */
abstract value: string;
/** Safely returns selection start */
get selectionStart (): number {
let start;
try {
start = this._unsafeSelectionStart;
} catch {}
return start != null ?
start :
this.value.length;
}
/** Safely returns selection end */
get selectionEnd (): number {
let end;
try {
end = this._unsafeSelectionEnd;
} catch {}
return end != null ?
end :
this.value.length;
}
/** Safely sets element selection */
select (start: number, end: number) {
if (start == null || end == null ||
start === this.selectionStart && end === this.selectionEnd) return;
try {
this._unsafeSelect(start, end);
} catch {}
}
/** */
get isActive (): boolean { return false; }
/** */
abstract _unsafeSelect (start: number, end: number): void;
/** */
abstract bindEvents (handlers: EventHandlers): void;
/** */
abstract unbindEvents (): void
}
IMask.MaskElement = MaskElement;