1 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
2 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
3 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
4 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
5 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
6 | };
|
7 | var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
8 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
9 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
10 | return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11 | };
|
12 | var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
13 | if (kind === "m") throw new TypeError("Private method is not writable");
|
14 | if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
15 | if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
16 | return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
17 | };
|
18 | var _DuoyunTextMaskElement_instances, _DuoyunTextMaskElement_placeholder_get, _DuoyunTextMaskElement_replacer_get, _DuoyunTextMaskElement_masks;
|
19 | import { adoptedStyle, customElement, property, attribute } from '@mantou/gem/lib/decorators';
|
20 | import { GemElement, html } from '@mantou/gem/lib/element';
|
21 | import { createCSSSheet, css } from '@mantou/gem/lib/utils';
|
22 | const style = createCSSSheet(css `
|
23 | span {
|
24 | user-select: none;
|
25 | }
|
26 | `);
|
27 |
|
28 |
|
29 |
|
30 | let DuoyunTextMaskElement = class DuoyunTextMaskElement extends GemElement {
|
31 | constructor() {
|
32 | super();
|
33 | _DuoyunTextMaskElement_instances.add(this);
|
34 | _DuoyunTextMaskElement_masks.set(this, void 0);
|
35 | this.willMount = () => {
|
36 | this.memo(() => {
|
37 | var _a;
|
38 | const clearRegRxp = new RegExp(`[^${__classPrivateFieldGet(this, _DuoyunTextMaskElement_instances, "a", _DuoyunTextMaskElement_placeholder_get)}${__classPrivateFieldGet(this, _DuoyunTextMaskElement_instances, "a", _DuoyunTextMaskElement_replacer_get)}]`, 'g');
|
39 | const matchRegRxp = new RegExp(`([${__classPrivateFieldGet(this, _DuoyunTextMaskElement_instances, "a", _DuoyunTextMaskElement_placeholder_get)}]+|[${__classPrivateFieldGet(this, _DuoyunTextMaskElement_instances, "a", _DuoyunTextMaskElement_replacer_get)}]+)`);
|
40 | __classPrivateFieldSet(this, _DuoyunTextMaskElement_masks, new Map(), "f");
|
41 | (_a = this.masks) === null || _a === void 0 ? void 0 : _a.forEach((mask) => {
|
42 | const { length } = mask.replaceAll(clearRegRxp, '');
|
43 | const templateArr = [];
|
44 | const values = [];
|
45 | mask.split(matchRegRxp).forEach((e) => {
|
46 | templateArr.push('');
|
47 | if (!e) {
|
48 | values.push('');
|
49 | }
|
50 | else if (e === __classPrivateFieldGet(this, _DuoyunTextMaskElement_instances, "a", _DuoyunTextMaskElement_placeholder_get).repeat(e.length)) {
|
51 | values.push(e);
|
52 | }
|
53 | else if (e === __classPrivateFieldGet(this, _DuoyunTextMaskElement_instances, "a", _DuoyunTextMaskElement_replacer_get).repeat(e.length)) {
|
54 | values.push(e.length);
|
55 | }
|
56 | else {
|
57 | values.push(html `<span>${e}</span>`);
|
58 | }
|
59 | });
|
60 | templateArr.push('');
|
61 | __classPrivateFieldGet(this, _DuoyunTextMaskElement_masks, "f").set(length, [templateArr, values]);
|
62 | });
|
63 | }, () => this.masks || []);
|
64 | };
|
65 | this.render = () => {
|
66 | var _a;
|
67 | const origin = this.origin || ((_a = this.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '';
|
68 | const arg = __classPrivateFieldGet(this, _DuoyunTextMaskElement_masks, "f").get(origin.length);
|
69 | if (!arg)
|
70 | return html `${origin}`;
|
71 | let index = 0;
|
72 | const values = arg[1].map((e) => {
|
73 | switch (typeof e) {
|
74 | case 'string': {
|
75 | const s = origin.slice(index, index + e.length);
|
76 | index += e.length;
|
77 | return s;
|
78 | }
|
79 | case 'number': {
|
80 | index += e;
|
81 | return __classPrivateFieldGet(this, _DuoyunTextMaskElement_instances, "a", _DuoyunTextMaskElement_replacer_get).repeat(e);
|
82 | }
|
83 | default:
|
84 | return e;
|
85 | }
|
86 | });
|
87 | return html(arg[0], ...values);
|
88 | };
|
89 | new MutationObserver(() => this.update()).observe(this, { childList: true, characterData: true, subtree: true });
|
90 | }
|
91 | };
|
92 | _DuoyunTextMaskElement_masks = new WeakMap(), _DuoyunTextMaskElement_instances = new WeakSet(), _DuoyunTextMaskElement_placeholder_get = function _DuoyunTextMaskElement_placeholder_get() {
|
93 | return this.placeholder || 'x';
|
94 | }, _DuoyunTextMaskElement_replacer_get = function _DuoyunTextMaskElement_replacer_get() {
|
95 | return this.replacer || '*';
|
96 | };
|
97 | __decorate([
|
98 | attribute
|
99 | ], DuoyunTextMaskElement.prototype, "origin", void 0);
|
100 | __decorate([
|
101 | attribute
|
102 | ], DuoyunTextMaskElement.prototype, "placeholder", void 0);
|
103 | __decorate([
|
104 | attribute
|
105 | ], DuoyunTextMaskElement.prototype, "replacer", void 0);
|
106 | __decorate([
|
107 | property
|
108 | ], DuoyunTextMaskElement.prototype, "masks", void 0);
|
109 | DuoyunTextMaskElement = __decorate([
|
110 | customElement('dy-text-mask'),
|
111 | adoptedStyle(style)
|
112 | ], DuoyunTextMaskElement);
|
113 | export { DuoyunTextMaskElement };
|
114 |
|
\ | No newline at end of file |