/
fixed-definition.ts
155 lines (122 loc) · 3.99 KB
/
fixed-definition.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
import ChangeDetails from '../../core/change-details';
import { DIRECTION, type Direction, isString } from '../../core/utils';
import { type TailDetails } from '../../core/tail-details';
import ContinuousTailDetails from '../../core/continuous-tail-details';
import { type ExtractFlags, type AppendFlags, type MaskedState } from '../base';
import type PatternBlock from './block';
export
type PatternFixedDefinitionOptions = Pick<PatternFixedDefinition, 'char' | 'isUnmasking' | 'eager'>;
export default
class PatternFixedDefinition implements PatternBlock {
/** */
declare _value: string;
/** */
declare char: string;
/** */
declare isUnmasking?: boolean;
/** */
declare eager: boolean | 'remove' | 'append' | undefined;
/** */
declare _isRawInput?: boolean;
/** */
declare isFixed: boolean;
constructor(opts: PatternFixedDefinitionOptions) {
Object.assign(this, opts);
this._value = '';
this.isFixed = true;
}
get value (): string {
return this._value;
}
get unmaskedValue (): string {
return this.isUnmasking ? this.value : '';
}
get rawInputValue (): string {
return this._isRawInput ? this.value : '';
}
get displayValue (): string {
return this.value;
}
reset () {
this._isRawInput = false;
this._value = '';
}
remove (fromPos: number=0, toPos: number=this._value.length): ChangeDetails {
this._value = this._value.slice(0, fromPos) + this._value.slice(toPos);
if (!this._value) this._isRawInput = false;
return new ChangeDetails();
}
nearestInputPos (cursorPos: number, direction: Direction=DIRECTION.NONE): number {
const minPos = 0;
const maxPos = this._value.length;
switch (direction) {
case DIRECTION.LEFT:
case DIRECTION.FORCE_LEFT:
return minPos;
case DIRECTION.NONE:
case DIRECTION.RIGHT:
case DIRECTION.FORCE_RIGHT:
default:
return maxPos;
}
}
totalInputPositions (fromPos: number=0, toPos: number=this._value.length): number {
return this._isRawInput ? (toPos - fromPos) : 0;
}
extractInput (fromPos: number=0, toPos: number=this._value.length, flags: ExtractFlags={}): string {
return flags.raw && this._isRawInput && this._value.slice(fromPos, toPos) || '';
}
get isComplete (): boolean {
return true;
}
get isFilled (): boolean {
return Boolean(this._value);
}
_appendChar (ch: string, flags: AppendFlags={}): ChangeDetails {
if (this.isFilled) return new ChangeDetails();
const appendEager = this.eager === true || this.eager === 'append';
const appended = this.char === ch;
const isResolved = appended && (this.isUnmasking || flags.input || flags.raw) && (!flags.raw || !appendEager) && !flags.tail;
const details = new ChangeDetails({
inserted: this.char,
rawInserted: isResolved ? this.char: '',
})
this._value = this.char;
this._isRawInput = isResolved && (flags.raw || flags.input);
return details;
}
_appendEager (): ChangeDetails {
return this._appendChar(this.char, { tail: true });
}
_appendPlaceholder (): ChangeDetails {
const details = new ChangeDetails();
if (this.isFilled) return details;
this._value = details.inserted = this.char;
return details;
}
extractTail (): TailDetails {
return new ContinuousTailDetails('');
}
appendTail (tail: string | String | TailDetails): ChangeDetails {
if (isString(tail)) tail = new ContinuousTailDetails(String(tail));
return (tail as TailDetails).appendTo(this);
}
append (str: string, flags?: AppendFlags, tail?: TailDetails): ChangeDetails {
const details = this._appendChar(str[0], flags);
if (tail != null) {
details.tailShift += this.appendTail(tail).tailShift;
}
return details;
}
doCommit () {}
get state (): MaskedState {
return {
_value: this._value,
_rawInputValue: this.rawInputValue,
};
}
set state (state: MaskedState) {
this._value = state._value;
this._isRawInput = Boolean(state._rawInputValue);
}
}