@cos-design/slot-machine 4.0.0 → 4.1.0
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/core/packages/shared/src/element-options.d.ts +19 -0
- package/dist/core/packages/shared/src/index.d.ts +2 -0
- package/dist/element/index.js +56 -69
- package/dist/element/packages/shared/src/element-options.d.ts +19 -0
- package/dist/element/packages/shared/src/index.d.ts +2 -0
- package/dist/react/packages/shared/src/element-options.d.ts +19 -0
- package/dist/react/packages/shared/src/index.d.ts +2 -0
- package/dist/vue/index.js +43 -43
- package/dist/vue/packages/shared/src/element-options.d.ts +19 -0
- package/dist/vue/packages/shared/src/index.d.ts +2 -0
- package/package.json +3 -3
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Component attribute → Core options parsing (v4 Element adapters).
|
|
3
|
+
* Keeps behavior consistent across 91 components.
|
|
4
|
+
*/
|
|
5
|
+
export type BooleanAttrMode =
|
|
6
|
+
/** `options.key = el.hasAttribute(attr)` — always written */
|
|
7
|
+
'presence'
|
|
8
|
+
/** Only when attribute present: `raw !== 'false' && raw !== '0'` (omit when absent) */
|
|
9
|
+
| 'optional-true'
|
|
10
|
+
/** Only when attribute present; when absent leave `undefined` (controlled props like `flipped`) */
|
|
11
|
+
| 'optional-undefined';
|
|
12
|
+
export declare const camelToKebabAttr: (name: string) => string;
|
|
13
|
+
export declare function readStringAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
14
|
+
export declare function readNumberAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
15
|
+
export declare function readBooleanAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string, mode?: BooleanAttrMode): void;
|
|
16
|
+
export declare function readJsonAttr<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, attr?: string): void;
|
|
17
|
+
/** Property setters on the custom element class (`_prizes`, `_items`, …) override attributes. */
|
|
18
|
+
export declare function readPropertyOverride<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, backingKey: string): void;
|
|
19
|
+
export declare function optionCallbackDispatcher(el: HTMLElement, eventName: string): (...args: unknown[]) => void;
|
|
@@ -9,3 +9,5 @@ export type { PhotoItem, PhotoIndexChangeHandler, PhotoFaceChangeHandler } from
|
|
|
9
9
|
export { resolveCanvasBoxSize } from './canvas-box';
|
|
10
10
|
export type { CanvasBoxSizeInput, CanvasBoxSize } from './canvas-box';
|
|
11
11
|
export { applyCanvasHostBox, applyBlockHostBox, setHidden, optionsFingerprint, optionsVisualChanged, } from './host-layout';
|
|
12
|
+
export type { BooleanAttrMode } from './element-options';
|
|
13
|
+
export { camelToKebabAttr, optionCallbackDispatcher, readBooleanAttr, readJsonAttr, readNumberAttr, readPropertyOverride, readStringAttr, } from './element-options';
|
package/dist/element/index.js
CHANGED
|
@@ -1,151 +1,138 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { optionCallbackDispatcher as e, prefersReducedMotion as t, readJsonAttr as n, readNumberAttr as r, readPropertyOverride as i, readStringAttr as a } from "@cos-design/shared";
|
|
2
2
|
import './element.css';//#region src/components/slotMachine/core/engine.ts
|
|
3
|
-
var
|
|
3
|
+
var o = "cos-slot-machine", s = [
|
|
4
4
|
"🍒",
|
|
5
5
|
"🍋",
|
|
6
6
|
"🍊",
|
|
7
7
|
"🍇",
|
|
8
8
|
"⭐",
|
|
9
9
|
"7️⃣"
|
|
10
|
-
],
|
|
10
|
+
], c = 30, l = 8, u = [
|
|
11
11
|
0,
|
|
12
12
|
350,
|
|
13
13
|
700
|
|
14
|
-
],
|
|
14
|
+
], d = 80, f = 3, p = (e) => 1 - (1 - e) ** 3, m = (e, t) => {
|
|
15
15
|
if (t === void 0) return Math.floor(Math.random() * e.length);
|
|
16
16
|
let n = e.indexOf(t);
|
|
17
17
|
return n >= 0 ? n : Math.floor(Math.random() * e.length);
|
|
18
18
|
};
|
|
19
|
-
function
|
|
20
|
-
let
|
|
21
|
-
symbols:
|
|
19
|
+
function h(e, n = {}) {
|
|
20
|
+
let r = {
|
|
21
|
+
symbols: s,
|
|
22
22
|
spinDuration: 3e3,
|
|
23
23
|
spinningText: "旋转中...",
|
|
24
24
|
jackpotText: "🎰 大奖!",
|
|
25
25
|
resultPrefix: "结果:",
|
|
26
|
-
...
|
|
27
|
-
},
|
|
26
|
+
...n
|
|
27
|
+
}, i = !1, a = !1, h = [
|
|
28
28
|
0,
|
|
29
29
|
0,
|
|
30
30
|
0
|
|
31
31
|
], g = [], _ = 0, v = null, y = document.createElement("div"), b = document.createElement("div");
|
|
32
|
-
b.className = `${
|
|
32
|
+
b.className = `${o}__machine`;
|
|
33
33
|
let x = [], S = [];
|
|
34
34
|
for (let e = 0; e < 3; e++) {
|
|
35
35
|
let e = document.createElement("div");
|
|
36
|
-
e.className = `${
|
|
36
|
+
e.className = `${o}__reel`;
|
|
37
|
+
let t = document.createElement("div");
|
|
38
|
+
t.className = `${o}__strip`;
|
|
37
39
|
let n = document.createElement("div");
|
|
38
|
-
n.className = `${t
|
|
39
|
-
let r = document.createElement("div");
|
|
40
|
-
r.className = `${t}__mask`, e.append(n, r), b.appendChild(e), x.push(e), S.push(n);
|
|
40
|
+
n.className = `${o}__mask`, e.append(t, n), b.appendChild(e), x.push(e), S.push(t);
|
|
41
41
|
}
|
|
42
42
|
let C = document.createElement("button");
|
|
43
|
-
C.type = "button", C.className = `${
|
|
43
|
+
C.type = "button", C.className = `${o}__spin-btn`, C.dataset.testid = "slot-machine-spin";
|
|
44
44
|
let w = document.createElement("p");
|
|
45
|
-
w.className = `${
|
|
46
|
-
let T = () =>
|
|
47
|
-
let e = T(),
|
|
48
|
-
for (let
|
|
45
|
+
w.className = `${o}__result`, w.hidden = !0, y.append(b, C, w), e.appendChild(y);
|
|
46
|
+
let T = () => r.symbols?.length ? r.symbols : s, E = () => T().length * d, D = () => Math.floor(c / 2) * E(), O = (e, t) => -(e - 1 + t * T().length) * d, k = () => r.buttonText ?? r.startText ?? "开始", A = () => {
|
|
47
|
+
let e = T(), t = [];
|
|
48
|
+
for (let n = 0; n < c; n++) t.push(...e);
|
|
49
49
|
for (let e of S) {
|
|
50
50
|
e.replaceChildren();
|
|
51
|
-
for (let
|
|
52
|
-
let
|
|
53
|
-
|
|
51
|
+
for (let n of t) {
|
|
52
|
+
let t = document.createElement("div");
|
|
53
|
+
t.className = `${o}__item`, t.style.height = `${d}px`, t.textContent = n, e.appendChild(t);
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
|
-
let
|
|
57
|
-
for (let e of x) e.style.height = `${
|
|
56
|
+
let n = d * f;
|
|
57
|
+
for (let e of x) e.style.height = `${n}px`;
|
|
58
58
|
}, j = () => {
|
|
59
59
|
S.forEach((e, t) => {
|
|
60
60
|
e.style.transform = `translateY(${h[t]}px)`;
|
|
61
61
|
});
|
|
62
62
|
}, M = () => {
|
|
63
|
-
y.className =
|
|
64
|
-
}, N = (
|
|
63
|
+
y.className = o, C.disabled = a, C.setAttribute("aria-busy", String(a)), C.textContent = a ? r.spinningText ?? "旋转中..." : k(), g.length > 0 && !a ? (w.hidden = !1, w.textContent = g[0] === g[1] && g[1] === g[2] ? r.jackpotText ?? "🎰 大奖!" : `${r.resultPrefix ?? "结果:"} ${g.join(" ")}`) : (w.hidden = !0, w.textContent = "");
|
|
64
|
+
}, N = (e) => {
|
|
65
65
|
let n = T();
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
let
|
|
66
|
+
if (a || n.length === 0 || i) return;
|
|
67
|
+
a = !0, g = [], M();
|
|
68
|
+
let o = e ?? r.targetResults, s = [
|
|
69
69
|
0,
|
|
70
70
|
1,
|
|
71
71
|
2
|
|
72
|
-
].map((e) =>
|
|
73
|
-
let n =
|
|
74
|
-
for (; n -
|
|
75
|
-
return
|
|
76
|
-
}), b = Math.max(
|
|
72
|
+
].map((e) => m(n, o?.[e])), c = [...h], f = n.length * d, y = s.map((e, t) => {
|
|
73
|
+
let n = c[t], r = l * f, i = Math.ceil((-n + r) / f) + 1, a = O(e, i);
|
|
74
|
+
for (; n - a < r;) i += 1, a = O(e, i);
|
|
75
|
+
return a;
|
|
76
|
+
}), b = Math.max(r.spinDuration ?? 3e3, 16), x = performance.now(), S = { cancelled: !1 };
|
|
77
77
|
v = S;
|
|
78
78
|
let C = () => {
|
|
79
|
-
if (S.cancelled ||
|
|
79
|
+
if (S.cancelled || i) return;
|
|
80
80
|
let e = s.map((e) => n[e]);
|
|
81
|
-
h = s.map((e) => O(e, Math.round(D() /
|
|
81
|
+
h = s.map((e) => O(e, Math.round(D() / f))), g = e, a = !1, j(), M(), r.onSpinEnd?.(e);
|
|
82
82
|
};
|
|
83
|
-
if (
|
|
83
|
+
if (t()) {
|
|
84
84
|
C();
|
|
85
85
|
return;
|
|
86
86
|
}
|
|
87
87
|
let w = (e) => {
|
|
88
|
-
S.cancelled ||
|
|
89
|
-
let r = e - x -
|
|
88
|
+
S.cancelled || i || (h = c.map((t, n) => {
|
|
89
|
+
let r = e - x - u[n];
|
|
90
90
|
if (r < 0) return t;
|
|
91
91
|
let i = Math.min(r / b, 1);
|
|
92
|
-
return t + (y[n] - t) *
|
|
93
|
-
}), j(),
|
|
92
|
+
return t + (y[n] - t) * p(i);
|
|
93
|
+
}), j(), u.every((t) => e - x - t >= b) ? C() : _ = requestAnimationFrame(w));
|
|
94
94
|
};
|
|
95
95
|
_ = requestAnimationFrame(w);
|
|
96
96
|
};
|
|
97
97
|
return C.addEventListener("click", () => N()), A(), j(), M(), {
|
|
98
98
|
update(e) {
|
|
99
99
|
let t = T().join("\0");
|
|
100
|
-
|
|
101
|
-
...
|
|
100
|
+
r = {
|
|
101
|
+
...r,
|
|
102
102
|
...e
|
|
103
103
|
}, T().join("\0") !== t && A(), M();
|
|
104
104
|
},
|
|
105
105
|
spin: N,
|
|
106
106
|
reset: () => {
|
|
107
|
-
|
|
107
|
+
a || (g = [], M());
|
|
108
108
|
},
|
|
109
109
|
destroy() {
|
|
110
|
-
|
|
110
|
+
i || (i = !0, v && (v.cancelled = !0), cancelAnimationFrame(_), y.remove());
|
|
111
111
|
}
|
|
112
112
|
};
|
|
113
113
|
}
|
|
114
114
|
//#endregion
|
|
115
115
|
//#region src/components/slotMachine/element/index.ts
|
|
116
|
-
var
|
|
117
|
-
function
|
|
118
|
-
let
|
|
119
|
-
|
|
120
|
-
t.symbols = JSON.parse(e.getAttribute("symbols") ?? "null");
|
|
121
|
-
} catch {}
|
|
122
|
-
let n = e._symbols;
|
|
123
|
-
if (n !== void 0 && (t.symbols = n), e.hasAttribute("target-results")) try {
|
|
124
|
-
t.targetResults = JSON.parse(e.getAttribute("target-results") ?? "null");
|
|
125
|
-
} catch {}
|
|
126
|
-
let r = e._targetResults;
|
|
127
|
-
return r !== void 0 && (t.targetResults = r), t.onSpinEnd = (...t) => {
|
|
128
|
-
e.dispatchEvent(new CustomEvent("spin-end", {
|
|
129
|
-
detail: t.length <= 1 ? t[0] : t,
|
|
130
|
-
bubbles: !0
|
|
131
|
-
}));
|
|
132
|
-
}, t;
|
|
116
|
+
var g = "cos-slot-machine";
|
|
117
|
+
function _(t) {
|
|
118
|
+
let o = {};
|
|
119
|
+
return a(o, t, "startText", "start-text"), a(o, t, "buttonText", "button-text"), a(o, t, "spinningText", "spinning-text"), a(o, t, "jackpotText", "jackpot-text"), a(o, t, "resultPrefix", "result-prefix"), r(o, t, "spinDuration", "spin-duration"), n(o, t, "symbols", "symbols"), i(o, t, "symbols", "_symbols"), n(o, t, "targetResults", "target-results"), i(o, t, "targetResults", "_targetResults"), o.onSpinEnd = e(t, "spin-end"), o;
|
|
133
120
|
}
|
|
134
|
-
var
|
|
121
|
+
var v = class extends HTMLElement {
|
|
135
122
|
ctrl = null;
|
|
136
123
|
_symbols;
|
|
137
124
|
get symbols() {
|
|
138
125
|
return this._symbols;
|
|
139
126
|
}
|
|
140
127
|
set symbols(e) {
|
|
141
|
-
this._symbols = e, this.ctrl?.update(
|
|
128
|
+
this._symbols = e, this.ctrl?.update(_(this));
|
|
142
129
|
}
|
|
143
130
|
_targetResults;
|
|
144
131
|
get targetResults() {
|
|
145
132
|
return this._targetResults;
|
|
146
133
|
}
|
|
147
134
|
set targetResults(e) {
|
|
148
|
-
this._targetResults = e, this.ctrl?.update(
|
|
135
|
+
this._targetResults = e, this.ctrl?.update(_(this));
|
|
149
136
|
}
|
|
150
137
|
static get observedAttributes() {
|
|
151
138
|
return [
|
|
@@ -160,13 +147,13 @@ var p = class extends HTMLElement {
|
|
|
160
147
|
];
|
|
161
148
|
}
|
|
162
149
|
connectedCallback() {
|
|
163
|
-
this.ctrl =
|
|
150
|
+
this.ctrl = h(this, _(this));
|
|
164
151
|
}
|
|
165
152
|
disconnectedCallback() {
|
|
166
153
|
this.ctrl?.destroy(), this.ctrl = null;
|
|
167
154
|
}
|
|
168
155
|
attributeChangedCallback() {
|
|
169
|
-
this.ctrl?.update(
|
|
156
|
+
this.ctrl?.update(_(this));
|
|
170
157
|
}
|
|
171
158
|
spin(e) {
|
|
172
159
|
return this.ctrl?.spin(e);
|
|
@@ -175,6 +162,6 @@ var p = class extends HTMLElement {
|
|
|
175
162
|
return this.ctrl?.reset();
|
|
176
163
|
}
|
|
177
164
|
};
|
|
178
|
-
typeof customElements < "u" && !customElements.get("cos-slot-machine") && customElements.define(
|
|
165
|
+
typeof customElements < "u" && !customElements.get("cos-slot-machine") && customElements.define(g, v);
|
|
179
166
|
//#endregion
|
|
180
|
-
export {
|
|
167
|
+
export { v as CosSlotMachineElement, g as TAG };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Component attribute → Core options parsing (v4 Element adapters).
|
|
3
|
+
* Keeps behavior consistent across 91 components.
|
|
4
|
+
*/
|
|
5
|
+
export type BooleanAttrMode =
|
|
6
|
+
/** `options.key = el.hasAttribute(attr)` — always written */
|
|
7
|
+
'presence'
|
|
8
|
+
/** Only when attribute present: `raw !== 'false' && raw !== '0'` (omit when absent) */
|
|
9
|
+
| 'optional-true'
|
|
10
|
+
/** Only when attribute present; when absent leave `undefined` (controlled props like `flipped`) */
|
|
11
|
+
| 'optional-undefined';
|
|
12
|
+
export declare const camelToKebabAttr: (name: string) => string;
|
|
13
|
+
export declare function readStringAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
14
|
+
export declare function readNumberAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
15
|
+
export declare function readBooleanAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string, mode?: BooleanAttrMode): void;
|
|
16
|
+
export declare function readJsonAttr<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, attr?: string): void;
|
|
17
|
+
/** Property setters on the custom element class (`_prizes`, `_items`, …) override attributes. */
|
|
18
|
+
export declare function readPropertyOverride<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, backingKey: string): void;
|
|
19
|
+
export declare function optionCallbackDispatcher(el: HTMLElement, eventName: string): (...args: unknown[]) => void;
|
|
@@ -9,3 +9,5 @@ export type { PhotoItem, PhotoIndexChangeHandler, PhotoFaceChangeHandler } from
|
|
|
9
9
|
export { resolveCanvasBoxSize } from './canvas-box';
|
|
10
10
|
export type { CanvasBoxSizeInput, CanvasBoxSize } from './canvas-box';
|
|
11
11
|
export { applyCanvasHostBox, applyBlockHostBox, setHidden, optionsFingerprint, optionsVisualChanged, } from './host-layout';
|
|
12
|
+
export type { BooleanAttrMode } from './element-options';
|
|
13
|
+
export { camelToKebabAttr, optionCallbackDispatcher, readBooleanAttr, readJsonAttr, readNumberAttr, readPropertyOverride, readStringAttr, } from './element-options';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Component attribute → Core options parsing (v4 Element adapters).
|
|
3
|
+
* Keeps behavior consistent across 91 components.
|
|
4
|
+
*/
|
|
5
|
+
export type BooleanAttrMode =
|
|
6
|
+
/** `options.key = el.hasAttribute(attr)` — always written */
|
|
7
|
+
'presence'
|
|
8
|
+
/** Only when attribute present: `raw !== 'false' && raw !== '0'` (omit when absent) */
|
|
9
|
+
| 'optional-true'
|
|
10
|
+
/** Only when attribute present; when absent leave `undefined` (controlled props like `flipped`) */
|
|
11
|
+
| 'optional-undefined';
|
|
12
|
+
export declare const camelToKebabAttr: (name: string) => string;
|
|
13
|
+
export declare function readStringAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
14
|
+
export declare function readNumberAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
15
|
+
export declare function readBooleanAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string, mode?: BooleanAttrMode): void;
|
|
16
|
+
export declare function readJsonAttr<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, attr?: string): void;
|
|
17
|
+
/** Property setters on the custom element class (`_prizes`, `_items`, …) override attributes. */
|
|
18
|
+
export declare function readPropertyOverride<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, backingKey: string): void;
|
|
19
|
+
export declare function optionCallbackDispatcher(el: HTMLElement, eventName: string): (...args: unknown[]) => void;
|
|
@@ -9,3 +9,5 @@ export type { PhotoItem, PhotoIndexChangeHandler, PhotoFaceChangeHandler } from
|
|
|
9
9
|
export { resolveCanvasBoxSize } from './canvas-box';
|
|
10
10
|
export type { CanvasBoxSizeInput, CanvasBoxSize } from './canvas-box';
|
|
11
11
|
export { applyCanvasHostBox, applyBlockHostBox, setHidden, optionsFingerprint, optionsVisualChanged, } from './host-layout';
|
|
12
|
+
export type { BooleanAttrMode } from './element-options';
|
|
13
|
+
export { camelToKebabAttr, optionCallbackDispatcher, readBooleanAttr, readJsonAttr, readNumberAttr, readPropertyOverride, readStringAttr, } from './element-options';
|
package/dist/vue/index.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import { createElementBlock as e, defineComponent as t, onMounted as n, onUnmounted as r, openBlock as i, ref as a, watch as o } from "vue";
|
|
2
|
-
import {
|
|
2
|
+
import { prefersReducedMotion as s } from "@cos-design/shared";
|
|
3
3
|
import './vue.css';//#region src/components/slotMachine/core/engine.ts
|
|
4
|
-
var
|
|
4
|
+
var c = "cos-slot-machine", l = [
|
|
5
5
|
"🍒",
|
|
6
6
|
"🍋",
|
|
7
7
|
"🍊",
|
|
8
8
|
"🍇",
|
|
9
9
|
"⭐",
|
|
10
10
|
"7️⃣"
|
|
11
|
-
],
|
|
11
|
+
], u = 30, d = 8, f = [
|
|
12
12
|
0,
|
|
13
13
|
350,
|
|
14
14
|
700
|
|
15
|
-
],
|
|
15
|
+
], p = 80, m = 3, h = (e) => 1 - (1 - e) ** 3, g = (e, t) => {
|
|
16
16
|
if (t === void 0) return Math.floor(Math.random() * e.length);
|
|
17
17
|
let n = e.indexOf(t);
|
|
18
18
|
return n >= 0 ? n : Math.floor(Math.random() * e.length);
|
|
19
19
|
};
|
|
20
|
-
function
|
|
20
|
+
function _(e, t = {}) {
|
|
21
21
|
let n = {
|
|
22
|
-
symbols:
|
|
22
|
+
symbols: l,
|
|
23
23
|
spinDuration: 3e3,
|
|
24
24
|
spinningText: "旋转中...",
|
|
25
25
|
jackpotText: "🎰 大奖!",
|
|
@@ -29,71 +29,71 @@ function v(e, t = {}) {
|
|
|
29
29
|
0,
|
|
30
30
|
0,
|
|
31
31
|
0
|
|
32
|
-
], o = [],
|
|
33
|
-
b.className = `${
|
|
32
|
+
], o = [], _ = 0, v = null, y = document.createElement("div"), b = document.createElement("div");
|
|
33
|
+
b.className = `${c}__machine`;
|
|
34
34
|
let x = [], S = [];
|
|
35
35
|
for (let e = 0; e < 3; e++) {
|
|
36
36
|
let e = document.createElement("div");
|
|
37
|
-
e.className = `${
|
|
37
|
+
e.className = `${c}__reel`;
|
|
38
38
|
let t = document.createElement("div");
|
|
39
|
-
t.className = `${
|
|
39
|
+
t.className = `${c}__strip`;
|
|
40
40
|
let n = document.createElement("div");
|
|
41
|
-
n.className = `${
|
|
41
|
+
n.className = `${c}__mask`, e.append(t, n), b.appendChild(e), x.push(e), S.push(t);
|
|
42
42
|
}
|
|
43
43
|
let C = document.createElement("button");
|
|
44
|
-
C.type = "button", C.className = `${
|
|
44
|
+
C.type = "button", C.className = `${c}__spin-btn`, C.dataset.testid = "slot-machine-spin";
|
|
45
45
|
let w = document.createElement("p");
|
|
46
|
-
w.className = `${
|
|
47
|
-
let T = () => n.symbols?.length ? n.symbols :
|
|
46
|
+
w.className = `${c}__result`, w.hidden = !0, y.append(b, C, w), e.appendChild(y);
|
|
47
|
+
let T = () => n.symbols?.length ? n.symbols : l, E = () => T().length * p, D = () => Math.floor(u / 2) * E(), O = (e, t) => -(e - 1 + t * T().length) * p, k = () => n.buttonText ?? n.startText ?? "开始", A = () => {
|
|
48
48
|
let e = T(), t = [];
|
|
49
|
-
for (let n = 0; n <
|
|
49
|
+
for (let n = 0; n < u; n++) t.push(...e);
|
|
50
50
|
for (let e of S) {
|
|
51
51
|
e.replaceChildren();
|
|
52
52
|
for (let n of t) {
|
|
53
53
|
let t = document.createElement("div");
|
|
54
|
-
t.className = `${
|
|
54
|
+
t.className = `${c}__item`, t.style.height = `${p}px`, t.textContent = n, e.appendChild(t);
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
-
let n =
|
|
57
|
+
let n = p * m;
|
|
58
58
|
for (let e of x) e.style.height = `${n}px`;
|
|
59
59
|
}, j = () => {
|
|
60
60
|
S.forEach((e, t) => {
|
|
61
61
|
e.style.transform = `translateY(${a[t]}px)`;
|
|
62
62
|
});
|
|
63
63
|
}, M = () => {
|
|
64
|
-
y.className =
|
|
64
|
+
y.className = c, C.disabled = i, C.setAttribute("aria-busy", String(i)), C.textContent = i ? n.spinningText ?? "旋转中..." : k(), o.length > 0 && !i ? (w.hidden = !1, w.textContent = o[0] === o[1] && o[1] === o[2] ? n.jackpotText ?? "🎰 大奖!" : `${n.resultPrefix ?? "结果:"} ${o.join(" ")}`) : (w.hidden = !0, w.textContent = "");
|
|
65
65
|
}, N = (e) => {
|
|
66
66
|
let t = T();
|
|
67
67
|
if (i || t.length === 0 || r) return;
|
|
68
68
|
i = !0, o = [], M();
|
|
69
|
-
let
|
|
69
|
+
let c = e ?? n.targetResults, l = [
|
|
70
70
|
0,
|
|
71
71
|
1,
|
|
72
72
|
2
|
|
73
|
-
].map((e) =>
|
|
74
|
-
let n =
|
|
73
|
+
].map((e) => g(t, c?.[e])), u = [...a], m = t.length * p, y = l.map((e, t) => {
|
|
74
|
+
let n = u[t], r = d * m, i = Math.ceil((-n + r) / m) + 1, a = O(e, i);
|
|
75
75
|
for (; n - a < r;) i += 1, a = O(e, i);
|
|
76
76
|
return a;
|
|
77
77
|
}), b = Math.max(n.spinDuration ?? 3e3, 16), x = performance.now(), S = { cancelled: !1 };
|
|
78
78
|
v = S;
|
|
79
79
|
let C = () => {
|
|
80
80
|
if (S.cancelled || r) return;
|
|
81
|
-
let e =
|
|
82
|
-
a =
|
|
81
|
+
let e = l.map((e) => t[e]);
|
|
82
|
+
a = l.map((e) => O(e, Math.round(D() / m))), o = e, i = !1, j(), M(), n.onSpinEnd?.(e);
|
|
83
83
|
};
|
|
84
|
-
if (
|
|
84
|
+
if (s()) {
|
|
85
85
|
C();
|
|
86
86
|
return;
|
|
87
87
|
}
|
|
88
88
|
let w = (e) => {
|
|
89
|
-
S.cancelled || r || (a =
|
|
90
|
-
let r = e - x -
|
|
89
|
+
S.cancelled || r || (a = u.map((t, n) => {
|
|
90
|
+
let r = e - x - f[n];
|
|
91
91
|
if (r < 0) return t;
|
|
92
92
|
let i = Math.min(r / b, 1);
|
|
93
|
-
return t + (y[n] - t) *
|
|
94
|
-
}), j(),
|
|
93
|
+
return t + (y[n] - t) * h(i);
|
|
94
|
+
}), j(), f.every((t) => e - x - t >= b) ? C() : _ = requestAnimationFrame(w));
|
|
95
95
|
};
|
|
96
|
-
|
|
96
|
+
_ = requestAnimationFrame(w);
|
|
97
97
|
};
|
|
98
98
|
return C.addEventListener("click", () => N()), A(), j(), M(), {
|
|
99
99
|
update(e) {
|
|
@@ -108,13 +108,13 @@ function v(e, t = {}) {
|
|
|
108
108
|
i || (o = [], M());
|
|
109
109
|
},
|
|
110
110
|
destroy() {
|
|
111
|
-
r || (r = !0, v && (v.cancelled = !0), cancelAnimationFrame(
|
|
111
|
+
r || (r = !0, v && (v.cancelled = !0), cancelAnimationFrame(_), y.remove());
|
|
112
112
|
}
|
|
113
113
|
};
|
|
114
114
|
}
|
|
115
115
|
//#endregion
|
|
116
116
|
//#region src/components/slotMachine/vue/SlotMachine.vue
|
|
117
|
-
var
|
|
117
|
+
var v = /* @__PURE__ */ t({
|
|
118
118
|
__name: "SlotMachine",
|
|
119
119
|
props: {
|
|
120
120
|
symbols: {},
|
|
@@ -128,24 +128,24 @@ var y = /* @__PURE__ */ t({
|
|
|
128
128
|
resultPrefix: {}
|
|
129
129
|
},
|
|
130
130
|
emits: ["spin-end"],
|
|
131
|
-
setup(t, { expose:
|
|
132
|
-
let
|
|
133
|
-
...
|
|
134
|
-
onSpinEnd: (...e) =>
|
|
131
|
+
setup(t, { expose: s, emit: c }) {
|
|
132
|
+
let l = t, u = c, d = a(), f = null, p = () => ({
|
|
133
|
+
...l,
|
|
134
|
+
onSpinEnd: (...e) => u("spin-end", ...e)
|
|
135
135
|
});
|
|
136
136
|
return n(() => {
|
|
137
|
-
|
|
138
|
-
}), o(() =>
|
|
139
|
-
|
|
140
|
-
}),
|
|
141
|
-
spin: (e) =>
|
|
142
|
-
reset: () =>
|
|
137
|
+
d.value && (f = _(d.value, p()));
|
|
138
|
+
}), o(() => ({ ...l }), () => f?.update(p()), { deep: !0 }), r(() => {
|
|
139
|
+
f?.destroy(), f = null;
|
|
140
|
+
}), s({
|
|
141
|
+
spin: (e) => f?.spin(e),
|
|
142
|
+
reset: () => f?.reset()
|
|
143
143
|
}), (t, n) => (i(), e("div", {
|
|
144
144
|
ref_key: "hostRef",
|
|
145
|
-
ref:
|
|
145
|
+
ref: d,
|
|
146
146
|
class: "cos-slotMachine-host"
|
|
147
147
|
}, null, 512));
|
|
148
148
|
}
|
|
149
149
|
});
|
|
150
150
|
//#endregion
|
|
151
|
-
export {
|
|
151
|
+
export { v as SlotMachine };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Component attribute → Core options parsing (v4 Element adapters).
|
|
3
|
+
* Keeps behavior consistent across 91 components.
|
|
4
|
+
*/
|
|
5
|
+
export type BooleanAttrMode =
|
|
6
|
+
/** `options.key = el.hasAttribute(attr)` — always written */
|
|
7
|
+
'presence'
|
|
8
|
+
/** Only when attribute present: `raw !== 'false' && raw !== '0'` (omit when absent) */
|
|
9
|
+
| 'optional-true'
|
|
10
|
+
/** Only when attribute present; when absent leave `undefined` (controlled props like `flipped`) */
|
|
11
|
+
| 'optional-undefined';
|
|
12
|
+
export declare const camelToKebabAttr: (name: string) => string;
|
|
13
|
+
export declare function readStringAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
14
|
+
export declare function readNumberAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string): void;
|
|
15
|
+
export declare function readBooleanAttr<T extends object>(options: T, el: HTMLElement, key: keyof T & string, attr?: string, mode?: BooleanAttrMode): void;
|
|
16
|
+
export declare function readJsonAttr<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, attr?: string): void;
|
|
17
|
+
/** Property setters on the custom element class (`_prizes`, `_items`, …) override attributes. */
|
|
18
|
+
export declare function readPropertyOverride<T extends object, K extends keyof T & string>(options: T, el: HTMLElement, key: K, backingKey: string): void;
|
|
19
|
+
export declare function optionCallbackDispatcher(el: HTMLElement, eventName: string): (...args: unknown[]) => void;
|
|
@@ -9,3 +9,5 @@ export type { PhotoItem, PhotoIndexChangeHandler, PhotoFaceChangeHandler } from
|
|
|
9
9
|
export { resolveCanvasBoxSize } from './canvas-box';
|
|
10
10
|
export type { CanvasBoxSizeInput, CanvasBoxSize } from './canvas-box';
|
|
11
11
|
export { applyCanvasHostBox, applyBlockHostBox, setHidden, optionsFingerprint, optionsVisualChanged, } from './host-layout';
|
|
12
|
+
export type { BooleanAttrMode } from './element-options';
|
|
13
|
+
export { camelToKebabAttr, optionCallbackDispatcher, readBooleanAttr, readJsonAttr, readNumberAttr, readPropertyOverride, readStringAttr, } from './element-options';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cos-design/slot-machine",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "SlotMachine component from cos-design",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/react/index.cjs",
|
|
@@ -84,6 +84,6 @@
|
|
|
84
84
|
}
|
|
85
85
|
},
|
|
86
86
|
"dependencies": {
|
|
87
|
-
"@cos-design/shared": "4.
|
|
87
|
+
"@cos-design/shared": "4.1.0"
|
|
88
88
|
}
|
|
89
|
-
}
|
|
89
|
+
}
|