@cat-of-summer/st-script 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mask.esm.min.js +1 -1
- package/dist/mask.min.js +1 -1
- package/dist/modal.esm.min.js +1 -1
- package/dist/modal.min.js +1 -1
- package/dist/toggle.esm.min.js +1 -1
- package/dist/toggle.min.js +1 -1
- package/package.json +1 -2
- package/src/mask/_engine.js +425 -0
- package/src/mask/index.js +86 -384
- package/src/modal/index.js +3 -2
- package/src/toggle/index.js +12 -1
package/src/mask/index.js
CHANGED
|
@@ -1,364 +1,20 @@
|
|
|
1
1
|
import { element, elements, expose, find, own } from '../_traits/hasInstanceSymbol.js';
|
|
2
|
+
import { build, run, run_all, render, caret_for, cap, prepare, filler } from './_engine.js';
|
|
3
|
+
|
|
4
|
+
const CALLBACKS = ['before_init', 'on_init', 'before_char', 'before_slot', 'before_input',
|
|
5
|
+
'on_input', 'before_paste', 'on_paste', 'on_accept', 'on_complete', 'on_incomplete', 'on_mask_change'];
|
|
2
6
|
|
|
3
7
|
export default class Mask {
|
|
4
8
|
|
|
5
|
-
static #SLOTS = { '0': /^[0-9]$/, 'a': /^\p{L}$/u, '*': /^[^\n]$/ };
|
|
6
9
|
static #CONFIG = ['target', 'mask'];
|
|
7
10
|
static #SELECTABLE = ['text', 'search', 'tel', 'url', 'password'];
|
|
8
11
|
|
|
9
|
-
static #unesc = str => str.replace(/\\(.)/g, '$1');
|
|
10
|
-
static #no_g = re => re.global ? new RegExp(re.source, re.flags.replace('g', '')) : re;
|
|
11
|
-
|
|
12
|
-
static #scan(str, from) {
|
|
13
|
-
let open = str[from], close = open == '{' ? '}' : ']', depth = 0;
|
|
14
|
-
for (let i = from; i < str.length; i++) {
|
|
15
|
-
let c = str[i];
|
|
16
|
-
if (c == '\\') { i++; continue; }
|
|
17
|
-
if (open == '[' && c == '{') {
|
|
18
|
-
if ((i = Mask.#scan(str, i)) < 0) return -1;
|
|
19
|
-
} else if (c == open) depth++;
|
|
20
|
-
else if (c == close && --depth == 0) return i;
|
|
21
|
-
}
|
|
22
|
-
return -1;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
static #last_top(body, needle) {
|
|
26
|
-
let depth = 0, found = -1;
|
|
27
|
-
for (let i = 0; i < body.length; i++) {
|
|
28
|
-
if (body[i] == '\\') { i++; continue; }
|
|
29
|
-
if (body[i] == '{' || body[i] == '[') depth++;
|
|
30
|
-
else if (body[i] == '}' || body[i] == ']') depth--;
|
|
31
|
-
else if (depth == 0 && body.startsWith(needle, i)) found = i;
|
|
32
|
-
}
|
|
33
|
-
return found;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
static #classify(body) {
|
|
37
|
-
let fail = () => {
|
|
38
|
-
console.warn('Некорректный блок маски', `{${body}}`);
|
|
39
|
-
return [...`{${body}}`].map(char => ({ type: 'literal', char }));
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
let semi = Mask.#last_top(body, ';');
|
|
43
|
-
if (semi > 0) {
|
|
44
|
-
let range = body.slice(semi + 1);
|
|
45
|
-
if (/^(\d+|\d*-\d*)$/.test(range) && range != '-') {
|
|
46
|
-
let [min, max] = /^\d+$/.test(range)
|
|
47
|
-
? [+range, +range]
|
|
48
|
-
: range.split('-').map((n, i) => n === '' ? (i ? Infinity : 0) : +n);
|
|
49
|
-
return max < min ? fail()
|
|
50
|
-
: [{ type: 'repeat', children: Mask.compile(body.slice(0, semi)), min, max }];
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (body[0] == '{' && body.at(-1) == '}')
|
|
55
|
-
try {
|
|
56
|
-
return [{ type: 'slot', test: new RegExp(`^(?:${body.slice(1, -1)})$`, 'u') }];
|
|
57
|
-
} catch { return fail(); }
|
|
58
|
-
|
|
59
|
-
let arrow = Mask.#last_top(body, '=>');
|
|
60
|
-
if (arrow > 0) {
|
|
61
|
-
let from = Mask.#unesc(body.slice(0, arrow)), to = Mask.#unesc(body.slice(arrow + 2));
|
|
62
|
-
return from.length != 1 || !to ? fail() : [{ type: 'transform', from, to }];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return fail();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
static compile(pattern) {
|
|
69
|
-
let nodes = [];
|
|
70
|
-
|
|
71
|
-
for (let i = 0; i < pattern.length; i++) {
|
|
72
|
-
let char = pattern[i], end;
|
|
73
|
-
|
|
74
|
-
if (char == '\\')
|
|
75
|
-
nodes.push({ type: 'literal', char: pattern[++i] ?? '\\' });
|
|
76
|
-
else if (char == '[' && (end = Mask.#scan(pattern, i)) >= 0) {
|
|
77
|
-
nodes.push({ type: 'optional', children: Mask.compile(pattern.slice(i + 1, end)) });
|
|
78
|
-
i = end;
|
|
79
|
-
} else if (char == '{' && (end = Mask.#scan(pattern, i)) >= 0) {
|
|
80
|
-
nodes.push(...Mask.#classify(pattern.slice(i + 1, end)));
|
|
81
|
-
i = end;
|
|
82
|
-
} else
|
|
83
|
-
nodes.push(Mask.#SLOTS[char] ? { type: 'slot', test: Mask.#SLOTS[char] } : { type: 'literal', char });
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return nodes;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
static #is_fixed(nodes) {
|
|
90
|
-
return nodes.every(node =>
|
|
91
|
-
node.type == 'literal' || node.type == 'slot'
|
|
92
|
-
|| node.type == 'repeat' && node.min == node.max && Mask.#is_fixed(node.children));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
static build(mask, base = {}) {
|
|
96
|
-
if (mask && typeof mask == 'object' && !Array.isArray(mask) && !(mask instanceof RegExp)
|
|
97
|
-
&& mask.pattern == null && mask.filter == null)
|
|
98
|
-
return Object.entries(mask).flatMap(([key, m]) =>
|
|
99
|
-
Mask.build(m, base).map(def => ({ ...def, key })));
|
|
100
|
-
|
|
101
|
-
let defs = (Array.isArray(mask) ? mask : [mask]).map(m => {
|
|
102
|
-
if (typeof m == 'function') return { fn: m, base };
|
|
103
|
-
if (m instanceof RegExp) m = { filter: m };
|
|
104
|
-
if (typeof m == 'string') m = { pattern: m };
|
|
105
|
-
|
|
106
|
-
let nodes = m.pattern != null
|
|
107
|
-
? Mask.compile(m.pattern)
|
|
108
|
-
: [{ type: 'repeat', min: 0, max: m.max_length ?? Infinity, greedy: true,
|
|
109
|
-
children: [{ type: 'slot', test: Mask.#no_g(m.filter) }] }];
|
|
110
|
-
|
|
111
|
-
return {
|
|
112
|
-
nodes,
|
|
113
|
-
fixed: m.pattern != null && Mask.#is_fixed(nodes),
|
|
114
|
-
valid: m.valid ? Mask.#no_g(m.valid) : (m.filter ? /./ : null),
|
|
115
|
-
filler: m.filler ?? base.filler,
|
|
116
|
-
before_char: m.before_char ?? base.before_char,
|
|
117
|
-
before_slot: m.before_slot ?? base.before_slot
|
|
118
|
-
};
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
defs.forEach((def, i) => def.key ??= i);
|
|
122
|
-
return defs;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
static #filler(def, ordinal) {
|
|
126
|
-
let filler = def.filler ?? '_';
|
|
127
|
-
return filler[ordinal] ?? filler[filler.length - 1] ?? '_';
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
static #prepare(def, input_string, ctx) {
|
|
131
|
-
let chars = [...String(input_string ?? '')];
|
|
132
|
-
if (!def.before_char) return chars;
|
|
133
|
-
|
|
134
|
-
return chars.flatMap(char => {
|
|
135
|
-
let out = def.before_char(ctx?.input, { char });
|
|
136
|
-
if (out === false || out === '' || out === null) return [];
|
|
137
|
-
return typeof out == 'string' ? [...out] : [char];
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
static #takes(node, c, s) {
|
|
142
|
-
if (node.type == 'slot') return node.test.test(c);
|
|
143
|
-
if (node.type == 'transform') return node.from === c || s.input.slice(s.ip, s.ip + node.to.length).join('') === node.to;
|
|
144
|
-
if (node.type == 'repeat') return Mask.#consumes(node.children, c, s);
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
static #consumes(nodes, c, s, entry) {
|
|
149
|
-
for (let node of nodes) {
|
|
150
|
-
if (node.type == 'literal') {
|
|
151
|
-
if (!entry && node.char === c) return true;
|
|
152
|
-
continue;
|
|
153
|
-
}
|
|
154
|
-
if (node.type == 'optional' || node.type == 'repeat' && node.min == 0) {
|
|
155
|
-
if (Mask.#consumes(node.children, c, s, entry)) return true;
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
return Mask.#takes(node, c, s);
|
|
159
|
-
}
|
|
160
|
-
return entry ? nodes.some(node => node.type == 'literal' && node.char === c) : false;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
static #accept(s, text, fmt_text, kind = 'char', ordinal = null) {
|
|
164
|
-
s.units.push({
|
|
165
|
-
kind, ordinal,
|
|
166
|
-
stream_start: s.stream.length, stream_end: s.stream.length + text.length,
|
|
167
|
-
fmt_start: s.formatted.length, fmt_end: s.formatted.length + fmt_text.length
|
|
168
|
-
});
|
|
169
|
-
s.stream += text;
|
|
170
|
-
if (kind == 'char') s.raw += text;
|
|
171
|
-
if (ordinal != null) s.cells[ordinal] = kind == 'char' ? text : null;
|
|
172
|
-
s.formatted += fmt_text;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
static #stop(s) {
|
|
176
|
-
if (s.stop_fmt < 0) s.stop_fmt = s.formatted.length + s.tail.length;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
static #walk(nodes, s, def) {
|
|
180
|
-
for (let i = 0; i < nodes.length; i++) {
|
|
181
|
-
let node = nodes[i];
|
|
182
|
-
|
|
183
|
-
if (node.type == 'literal') {
|
|
184
|
-
if (s.done) s.ph += node.char;
|
|
185
|
-
else if (s.ip < s.input.length) {
|
|
186
|
-
if (s.input[s.ip] === node.char) {
|
|
187
|
-
s.ip++;
|
|
188
|
-
s.consumed++;
|
|
189
|
-
Mask.#accept(s, node.char, node.char, 'literal');
|
|
190
|
-
} else
|
|
191
|
-
s.formatted += node.char;
|
|
192
|
-
} else s.tail += node.char;
|
|
193
|
-
|
|
194
|
-
} else if (node.type == 'slot') {
|
|
195
|
-
let ordinal = s.ordinal++, filler = Mask.#filler(def, ordinal);
|
|
196
|
-
if (s.done) {
|
|
197
|
-
s.ph_slots.push({ fmt: s.formatted.length + s.tail.length + s.ph.length, node });
|
|
198
|
-
s.ph += filler;
|
|
199
|
-
continue;
|
|
200
|
-
}
|
|
201
|
-
let no_fix = false;
|
|
202
|
-
for (;;) {
|
|
203
|
-
if (s.ip >= s.input.length) {
|
|
204
|
-
s.complete = false;
|
|
205
|
-
s.done = true;
|
|
206
|
-
Mask.#stop(s);
|
|
207
|
-
s.ph_slots.push({ fmt: s.formatted.length + s.tail.length, node });
|
|
208
|
-
s.ph += filler;
|
|
209
|
-
break;
|
|
210
|
-
}
|
|
211
|
-
let c = s.input[s.ip];
|
|
212
|
-
if (node.test.test(c)) {
|
|
213
|
-
if (def.before_slot && !no_fix) {
|
|
214
|
-
let out = def.before_slot(s.ctx?.input,
|
|
215
|
-
{ char: c, slot: ordinal, cells: s.cells, raw: s.raw, node });
|
|
216
|
-
if (out === false) { s.ip++; continue; }
|
|
217
|
-
if (typeof out == 'string' && out !== c) {
|
|
218
|
-
s.input.splice(s.ip, 1, ...out);
|
|
219
|
-
no_fix = true;
|
|
220
|
-
continue;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
Mask.#accept(s, c, c, 'char', ordinal);
|
|
224
|
-
} else if (c === filler) {
|
|
225
|
-
s.complete = false;
|
|
226
|
-
Mask.#accept(s, c, c, 'hole', ordinal);
|
|
227
|
-
} else { s.ip++; continue; }
|
|
228
|
-
s.ip++;
|
|
229
|
-
s.consumed++;
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
} else if (node.type == 'transform') {
|
|
234
|
-
if (s.done) { s.ph += node.to; continue; }
|
|
235
|
-
for (;;) {
|
|
236
|
-
if (s.ip >= s.input.length) {
|
|
237
|
-
s.complete = false;
|
|
238
|
-
s.done = true;
|
|
239
|
-
Mask.#stop(s);
|
|
240
|
-
s.ph += node.to;
|
|
241
|
-
break;
|
|
242
|
-
}
|
|
243
|
-
if (s.input.slice(s.ip, s.ip + node.to.length).join('') === node.to)
|
|
244
|
-
s.ip += node.to.length, s.consumed += node.to.length;
|
|
245
|
-
else if (s.input[s.ip] === node.from)
|
|
246
|
-
s.ip++, s.consumed++;
|
|
247
|
-
else { s.ip++; continue; }
|
|
248
|
-
Mask.#accept(s, node.to, node.to);
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
} else if (node.type == 'repeat') {
|
|
253
|
-
if (s.done) {
|
|
254
|
-
for (let k = 0; k < node.min; k++) Mask.#walk(node.children, s, def);
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
|
-
let count = 0;
|
|
258
|
-
while (count < node.max) {
|
|
259
|
-
if (s.ip >= s.input.length) {
|
|
260
|
-
if (count < node.min) {
|
|
261
|
-
s.complete = false;
|
|
262
|
-
s.done = true;
|
|
263
|
-
Mask.#stop(s);
|
|
264
|
-
for (let k = count; k < node.min; k++) Mask.#walk(node.children, s, def);
|
|
265
|
-
} else Mask.#stop(s);
|
|
266
|
-
break;
|
|
267
|
-
}
|
|
268
|
-
if (!Mask.#consumes(node.children, s.input[s.ip], s)) {
|
|
269
|
-
if (!node.greedy && count >= node.min) break;
|
|
270
|
-
s.ip++;
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
let before = s.ip;
|
|
274
|
-
Mask.#walk(node.children, s, def);
|
|
275
|
-
if (s.done) {
|
|
276
|
-
for (let k = count + 1; k < node.min; k++) Mask.#walk(node.children, s, def);
|
|
277
|
-
break;
|
|
278
|
-
}
|
|
279
|
-
if (s.ip === before) break;
|
|
280
|
-
count++;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
} else if (node.type == 'optional') {
|
|
284
|
-
if (s.done) continue;
|
|
285
|
-
let rest = nodes.slice(i + 1), entered = false;
|
|
286
|
-
while (s.ip < s.input.length) {
|
|
287
|
-
let c = s.input[s.ip];
|
|
288
|
-
if (Mask.#consumes(node.children, c, s, true)) {
|
|
289
|
-
entered = true;
|
|
290
|
-
Mask.#walk(node.children, s, def);
|
|
291
|
-
break;
|
|
292
|
-
}
|
|
293
|
-
if (Mask.#consumes(rest, c, s)) break;
|
|
294
|
-
s.ip++;
|
|
295
|
-
}
|
|
296
|
-
if (!entered && s.ip >= s.input.length) Mask.#stop(s);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
static run(def, input_string, ctx) {
|
|
302
|
-
let s = {
|
|
303
|
-
input: Mask.#prepare(def, input_string, ctx),
|
|
304
|
-
ctx,
|
|
305
|
-
ip: 0,
|
|
306
|
-
stream: '', raw: '', formatted: '', tail: '', ph: '',
|
|
307
|
-
ph_slots: [], units: [], cells: [],
|
|
308
|
-
consumed: 0, ordinal: 0,
|
|
309
|
-
complete: true, done: false, stop_fmt: -1
|
|
310
|
-
};
|
|
311
|
-
|
|
312
|
-
Mask.#walk(def.nodes, s, def);
|
|
313
|
-
if (s.stop_fmt < 0) s.stop_fmt = s.formatted.length;
|
|
314
|
-
if (def.valid) s.complete = s.complete && def.valid.test(s.raw);
|
|
315
|
-
|
|
316
|
-
return {
|
|
317
|
-
stream: s.stream, raw: s.raw, formatted: s.formatted, tail: s.tail,
|
|
318
|
-
ph: s.ph, ph_slots: s.ph_slots, complete: s.complete, consumed: s.consumed,
|
|
319
|
-
units: s.units, cells: s.cells, stop_fmt: s.stop_fmt
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
static run_all(defs, input_string, ctx = {}) {
|
|
324
|
-
let resolved = [];
|
|
325
|
-
ctx = { ...ctx, stream: String(input_string ?? '') };
|
|
326
|
-
for (let def of defs)
|
|
327
|
-
def.fn ? resolved.push(...Mask.build(def.fn(ctx), def.base).map(d => ({ ...d, key: def.key ?? d.key })))
|
|
328
|
-
: resolved.push(def);
|
|
329
|
-
|
|
330
|
-
let best = null;
|
|
331
|
-
for (let def of resolved) {
|
|
332
|
-
let result = Mask.run(def, input_string, ctx);
|
|
333
|
-
if (!best
|
|
334
|
-
|| result.consumed > best.result.consumed
|
|
335
|
-
|| result.consumed == best.result.consumed && result.complete && !best.result.complete)
|
|
336
|
-
best = { result, def, mask_id: def.key };
|
|
337
|
-
}
|
|
338
|
-
return best;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
static render(result, placeholder) {
|
|
342
|
-
if (placeholder == 'always') return result.formatted + result.tail + result.ph;
|
|
343
|
-
if (placeholder === false) return result.formatted;
|
|
344
|
-
return result.formatted + result.tail;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
static caret_for(result, stream_pos) {
|
|
348
|
-
for (let unit of result.units) {
|
|
349
|
-
if (unit.kind == 'literal') continue;
|
|
350
|
-
if (stream_pos <= unit.stream_start || stream_pos < unit.stream_end)
|
|
351
|
-
return unit.fmt_start;
|
|
352
|
-
}
|
|
353
|
-
return result.stop_fmt;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
12
|
static #layout(def) {
|
|
357
|
-
let layout =
|
|
13
|
+
let layout = run(def, ''), part = 0;
|
|
358
14
|
layout.slots = layout.ph_slots;
|
|
359
15
|
layout.parts = layout.slots.map((slot, i) =>
|
|
360
16
|
i && slot.fmt > layout.slots[i - 1].fmt + 1 ? ++part : part);
|
|
361
|
-
layout.template =
|
|
17
|
+
layout.template = render(layout, 'always');
|
|
362
18
|
return layout;
|
|
363
19
|
}
|
|
364
20
|
|
|
@@ -375,52 +31,40 @@ export default class Mask {
|
|
|
375
31
|
get value() { return this.#inputs.map(input => input.value); }
|
|
376
32
|
|
|
377
33
|
clone(params) {
|
|
378
|
-
return new Mask({
|
|
379
|
-
...this.#params,
|
|
380
|
-
...this.#methods,
|
|
381
|
-
...params,
|
|
382
|
-
parent: this
|
|
383
|
-
});
|
|
34
|
+
return new Mask({ ...this.#params, ...this.#methods, ...params, parent: this });
|
|
384
35
|
}
|
|
385
36
|
|
|
386
37
|
constructor(params) {
|
|
387
38
|
params = {
|
|
388
39
|
target: 'input[mask]',
|
|
389
40
|
mask: null,
|
|
41
|
+
numeral: null,
|
|
390
42
|
filler: '_',
|
|
391
43
|
placeholder: true,
|
|
392
44
|
flow: true,
|
|
393
45
|
rewrite: false,
|
|
394
46
|
caret: true,
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
before_input: () => {},
|
|
401
|
-
on_input: () => {},
|
|
402
|
-
before_paste: () => {},
|
|
403
|
-
on_paste: () => {},
|
|
404
|
-
on_accept: () => {},
|
|
405
|
-
on_complete: () => {},
|
|
406
|
-
on_incomplete: () => {},
|
|
407
|
-
on_mask_change: () => {},
|
|
408
|
-
|
|
47
|
+
coerce_type: true,
|
|
48
|
+
max_raw: null,
|
|
49
|
+
validate: null,
|
|
50
|
+
validation_message: null,
|
|
51
|
+
...Object.fromEntries(CALLBACKS.map(k => [k, () => {}])),
|
|
409
52
|
...params
|
|
410
53
|
};
|
|
411
54
|
|
|
412
55
|
for (let [key, value] of Object.entries(params))
|
|
413
56
|
if (typeof value == "function" && !Mask.#CONFIG.includes(key)) {
|
|
414
57
|
this[key] = value.bind(this);
|
|
415
|
-
|
|
416
58
|
this.#methods[key] = value;
|
|
417
59
|
} else
|
|
418
60
|
this.#params[key] = value;
|
|
419
61
|
|
|
420
62
|
this.before_init(this.#params);
|
|
421
63
|
|
|
422
|
-
if (this.#params.
|
|
423
|
-
this.#defs =
|
|
64
|
+
if (this.#params.numeral != null)
|
|
65
|
+
this.#defs = build({ numeral: this.#params.numeral }, this.#base());
|
|
66
|
+
else if (this.#params.mask != null)
|
|
67
|
+
this.#defs = build(this.#params.mask, this.#base());
|
|
424
68
|
|
|
425
69
|
for (let input of elements(this.#params.target))
|
|
426
70
|
this.#bind(input);
|
|
@@ -455,7 +99,7 @@ export default class Mask {
|
|
|
455
99
|
}
|
|
456
100
|
|
|
457
101
|
set_mask(mask, target = null) {
|
|
458
|
-
let defs =
|
|
102
|
+
let defs = build(mask, this.#base());
|
|
459
103
|
if (!target) this.#defs = defs;
|
|
460
104
|
|
|
461
105
|
for (let input of (target ? elements(target) : this.#inputs)) {
|
|
@@ -475,20 +119,41 @@ export default class Mask {
|
|
|
475
119
|
};
|
|
476
120
|
}
|
|
477
121
|
|
|
122
|
+
#default_for(coerced) {
|
|
123
|
+
if (coerced == 'email')
|
|
124
|
+
return build({ filter: /[a-z0-9@._%+-]/i, valid: /^[^@\s]+@[^@\s]+\.[^@\s]{2,}$/ }, this.#base());
|
|
125
|
+
if (coerced == 'number')
|
|
126
|
+
return build({ numeral: {} }, this.#base());
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
478
130
|
#bind(input) {
|
|
479
131
|
if (find(input)) throw new Error("Маска уже привязана к этому полю");
|
|
480
132
|
own(input, this);
|
|
481
133
|
|
|
134
|
+
let coerced = null;
|
|
135
|
+
if (this.#params.coerce_type !== false && (input.type == 'number' || input.type == 'email')) {
|
|
136
|
+
coerced = input.type;
|
|
137
|
+
input.inputMode = coerced == 'number' ? 'numeric' : 'email';
|
|
138
|
+
input.type = 'text';
|
|
139
|
+
}
|
|
140
|
+
|
|
482
141
|
if (!Mask.#SELECTABLE.includes(input.type))
|
|
483
142
|
console.warn(`Mask: поле type="${input.type}" не поддерживает управление кареткой — используйте type="text" или "tel"`, input);
|
|
484
143
|
|
|
485
|
-
let
|
|
144
|
+
let ml = input.getAttribute('maxlength');
|
|
145
|
+
if (ml != null) input.removeAttribute('maxlength');
|
|
146
|
+
let max_raw = this.#params.max_raw ?? (ml != null ? +ml : null);
|
|
147
|
+
|
|
148
|
+
let defs = this.#defs
|
|
149
|
+
?? (input.getAttribute('mask') ? build(input.getAttribute('mask'), this.#base()) : this.#default_for(coerced));
|
|
486
150
|
if (!defs) {
|
|
487
151
|
console.warn('Mask: для поля не задана маска (ни в params.mask, ни в атрибуте mask)', input);
|
|
488
152
|
return;
|
|
489
153
|
}
|
|
490
154
|
|
|
491
|
-
let
|
|
155
|
+
let numeric = coerced == 'number' || this.#params.numeral != null;
|
|
156
|
+
let st = { defs, def: null, stream: '', result: null, mask_id: null, rendered: '', run: null, composing: false, handled: false, coerced, max_raw, numeric };
|
|
492
157
|
this.#states.set(input, st);
|
|
493
158
|
this.#inputs.push(input);
|
|
494
159
|
|
|
@@ -565,15 +230,25 @@ export default class Mask {
|
|
|
565
230
|
if (out === false) return;
|
|
566
231
|
if (typeof out == 'string') insert = out;
|
|
567
232
|
}
|
|
233
|
+
|
|
234
|
+
if (st.numeric) {
|
|
235
|
+
let sign = st.defs.some(d => d.numeral?.sign);
|
|
236
|
+
insert = [...insert].filter(c => c >= '0' && c <= '9' || sign && c == '-').join('');
|
|
237
|
+
}
|
|
568
238
|
}
|
|
569
239
|
|
|
570
240
|
if (this.before_input(input, { type, insert, start, end }) === false) return;
|
|
571
241
|
|
|
242
|
+
let was = input.value;
|
|
243
|
+
|
|
572
244
|
this.#params.placeholder == 'always' && st.def?.fixed
|
|
573
245
|
? this.#grid(input, type, start, end, is_delete ? '' : insert)
|
|
574
246
|
: this.#edit(input, start, end, is_delete ? '' : insert, type);
|
|
575
247
|
|
|
576
248
|
if (type == 'insertFromPaste') this.on_paste(input, this.#state_of(input, st));
|
|
249
|
+
|
|
250
|
+
if (event.defaultPrevented && input.value !== was)
|
|
251
|
+
input.dispatchEvent(new InputEvent('input', { bubbles: true, inputType: type, data: insert || null }));
|
|
577
252
|
}
|
|
578
253
|
|
|
579
254
|
#edit(input, fmt_start, fmt_end, insert, type) {
|
|
@@ -651,7 +326,7 @@ export default class Mask {
|
|
|
651
326
|
anchor ??= k < n ? parts[k] : null;
|
|
652
327
|
let crossed = null;
|
|
653
328
|
|
|
654
|
-
for (let c of
|
|
329
|
+
for (let c of prepare(def, insert, { input })) {
|
|
655
330
|
if (k >= n) {
|
|
656
331
|
if (restart == null) break;
|
|
657
332
|
k = parts.indexOf(restart);
|
|
@@ -690,9 +365,9 @@ export default class Mask {
|
|
|
690
365
|
}
|
|
691
366
|
|
|
692
367
|
let last = cells.reduce((acc, cell, i) => cell != null ? i : acc, -1);
|
|
693
|
-
let stream = cells.slice(0, last + 1).map((cell, i) => cell ??
|
|
368
|
+
let stream = cells.slice(0, last + 1).map((cell, i) => cell ?? filler(def, i)).join('');
|
|
694
369
|
|
|
695
|
-
let pos = Math.min(
|
|
370
|
+
let pos = Math.min(run(def, stream.slice(0, k), { input }).stream.length, n);
|
|
696
371
|
let caret = pos < n ? slots[pos].fmt : slots[n - 1].fmt + 1;
|
|
697
372
|
if (!flow && insert && pos > 0 && pos < n && parts[pos] != parts[pos - 1])
|
|
698
373
|
caret = slots[pos - 1].fmt + 1;
|
|
@@ -767,17 +442,24 @@ export default class Mask {
|
|
|
767
442
|
|
|
768
443
|
#reconcile(input, stream_next, { prefix = null, caret_fmt = null, silent = false } = {}) {
|
|
769
444
|
let st = this.#states.get(input);
|
|
770
|
-
let
|
|
445
|
+
let ctx = { raw: st.result?.raw ?? '', value: input.value, input };
|
|
446
|
+
let best = run_all(st.defs, stream_next, ctx);
|
|
771
447
|
if (!best) return;
|
|
772
448
|
|
|
773
449
|
let { result, def, mask_id } = best;
|
|
450
|
+
|
|
451
|
+
if (st.max_raw != null && result.raw.length > st.max_raw) {
|
|
452
|
+
best = run_all(st.defs, result.stream.slice(0, cap(result, st.max_raw)), ctx);
|
|
453
|
+
({ result, def, mask_id } = best);
|
|
454
|
+
}
|
|
455
|
+
|
|
774
456
|
let text = this.#text_for(input, result);
|
|
775
457
|
|
|
776
458
|
if (input.value !== text) input.value = text;
|
|
777
459
|
st.rendered = text;
|
|
778
460
|
|
|
779
461
|
if (caret_fmt == null && prefix != null && document.activeElement === input)
|
|
780
|
-
caret_fmt =
|
|
462
|
+
caret_fmt = caret_for(result, run(def, prefix, { input }).stream.length);
|
|
781
463
|
if (caret_fmt != null && document.activeElement === input) {
|
|
782
464
|
caret_fmt = Math.min(caret_fmt, text.length);
|
|
783
465
|
input.setSelectionRange(caret_fmt, caret_fmt);
|
|
@@ -794,6 +476,9 @@ export default class Mask {
|
|
|
794
476
|
input.setAttribute('progress', result.raw.length);
|
|
795
477
|
input.setAttribute('is_complete', result.complete);
|
|
796
478
|
|
|
479
|
+
if (this.#validate_on(st) && input.setCustomValidity)
|
|
480
|
+
input.setCustomValidity(!result.stream || result.complete ? '' : this.#validation_message(input, st));
|
|
481
|
+
|
|
797
482
|
if (silent) return;
|
|
798
483
|
|
|
799
484
|
let state = this.#state_of(input, st);
|
|
@@ -807,10 +492,10 @@ export default class Mask {
|
|
|
807
492
|
#text_for(input, result) {
|
|
808
493
|
if (!result.stream) {
|
|
809
494
|
if (document.activeElement !== input) return '';
|
|
810
|
-
if (this.#params.placeholder == 'always') return
|
|
811
|
-
return this.#params.placeholder == false ? '' :
|
|
495
|
+
if (this.#params.placeholder == 'always') return render(result, 'always');
|
|
496
|
+
return this.#params.placeholder == false ? '' : render(result, true);
|
|
812
497
|
}
|
|
813
|
-
return
|
|
498
|
+
return render(result, this.#params.placeholder);
|
|
814
499
|
}
|
|
815
500
|
|
|
816
501
|
#state_of(input, st) {
|
|
@@ -822,4 +507,21 @@ export default class Mask {
|
|
|
822
507
|
progress: st.result?.raw.length ?? 0
|
|
823
508
|
};
|
|
824
509
|
}
|
|
510
|
+
|
|
511
|
+
#validate_on(st) {
|
|
512
|
+
let v = this.#params.validate;
|
|
513
|
+
return v === true || (v == null && st.coerced != null);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
#validation_message(input, st) {
|
|
517
|
+
let custom = typeof this.validation_message == 'function'
|
|
518
|
+
? this.validation_message
|
|
519
|
+
: (this.#params.validation_message ?? st.def?.message);
|
|
520
|
+
if (typeof custom == 'function') return custom(input, this.#state_of(input, st)) || '';
|
|
521
|
+
if (typeof custom == 'string') return custom;
|
|
522
|
+
|
|
523
|
+
if (st.coerced == 'email') return 'Введите корректный адрес электронной почты';
|
|
524
|
+
if (st.coerced == 'number' || st.def?.numeral) return 'Введите число в допустимом диапазоне';
|
|
525
|
+
return 'Заполните поле полностью';
|
|
526
|
+
}
|
|
825
527
|
}
|
package/src/modal/index.js
CHANGED
|
@@ -43,6 +43,7 @@ export default class Modal {
|
|
|
43
43
|
|
|
44
44
|
content: `<div></div>`,
|
|
45
45
|
location: 'center center',
|
|
46
|
+
fluid: false,
|
|
46
47
|
trigger: null,
|
|
47
48
|
|
|
48
49
|
close_by_overlay: true,
|
|
@@ -152,9 +153,9 @@ export default class Modal {
|
|
|
152
153
|
Object.assign(this.container.style, {
|
|
153
154
|
position: 'relative',
|
|
154
155
|
flexShrink: '0',
|
|
155
|
-
width: 'max-content',
|
|
156
|
+
width: this.#params.fluid ? '100%' : 'max-content',
|
|
156
157
|
height: 'max-content',
|
|
157
|
-
maxWidth: '100vw',
|
|
158
|
+
maxWidth: this.#params.fluid ? 'none' : '100vw',
|
|
158
159
|
zIndex: ++this.#params.zIndex,
|
|
159
160
|
transition: 'inherit',
|
|
160
161
|
pointerEvents: 'all',
|
package/src/toggle/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export default class Toggle {
|
|
|
7
7
|
#methods = {};
|
|
8
8
|
#timers = new WeakMap();
|
|
9
9
|
#anchors = new Map();
|
|
10
|
+
#triggers = [];
|
|
10
11
|
#last = null;
|
|
11
12
|
|
|
12
13
|
static find = find;
|
|
@@ -31,6 +32,7 @@ export default class Toggle {
|
|
|
31
32
|
action: 'click',
|
|
32
33
|
anchor: false,
|
|
33
34
|
keep_history: false,
|
|
35
|
+
group: false,
|
|
34
36
|
|
|
35
37
|
before_activate: () => {},
|
|
36
38
|
on_activate: () => {},
|
|
@@ -64,6 +66,7 @@ export default class Toggle {
|
|
|
64
66
|
|
|
65
67
|
for (let trigger of elements(this.#params.trigger)) {
|
|
66
68
|
own(trigger, this);
|
|
69
|
+
this.#triggers.push(trigger);
|
|
67
70
|
expose(trigger, {
|
|
68
71
|
toggle: () => this.toggle(trigger),
|
|
69
72
|
activate: () => this.activate(trigger),
|
|
@@ -164,6 +167,11 @@ export default class Toggle {
|
|
|
164
167
|
final = on ? 'active' : 'inactive',
|
|
165
168
|
targets = this.#targets(trigger);
|
|
166
169
|
|
|
170
|
+
if (on && this.#params.group)
|
|
171
|
+
this.#triggers
|
|
172
|
+
.filter(t => t !== trigger && ['active', 'activating'].includes(t.getAttribute('state')))
|
|
173
|
+
.forEach(t => this.#act(t, false));
|
|
174
|
+
|
|
167
175
|
let id = this.#anchorId(trigger);
|
|
168
176
|
if (id) {
|
|
169
177
|
if (on) { this.#writeHash(id); this.#last = trigger; }
|
|
@@ -178,7 +186,10 @@ export default class Toggle {
|
|
|
178
186
|
}
|
|
179
187
|
|
|
180
188
|
toggle(el) {
|
|
181
|
-
elements(el).forEach(t =>
|
|
189
|
+
elements(el).forEach(t => {
|
|
190
|
+
let on = !['active', 'activating'].includes(t.getAttribute('state'));
|
|
191
|
+
if (on || this.#params.group != 'radio') this.#act(t, on);
|
|
192
|
+
});
|
|
182
193
|
}
|
|
183
194
|
|
|
184
195
|
activate(el) { elements(el).forEach(t => this.#act(t, true)); }
|