@yqg/aminofx-css-kit 1.0.2 → 1.0.4
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/README.md +12 -10
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +237 -78
- package/dist/esm/type.d.ts +10 -0
- package/dist/umd/aminofx-css-kit.min.js +1 -1
- package/package.json +1 -1
- package/src/index.ts +187 -18
- package/src/type.ts +14 -0
package/README.md
CHANGED
|
@@ -269,9 +269,17 @@ export const AnimationComponent: React.FC = () => {
|
|
|
269
269
|
playerRef.current = player;
|
|
270
270
|
|
|
271
271
|
// 等待资源加载
|
|
272
|
-
player
|
|
273
|
-
|
|
274
|
-
|
|
272
|
+
player
|
|
273
|
+
.ready()
|
|
274
|
+
.then(() => {
|
|
275
|
+
// sdk初始化成功
|
|
276
|
+
setIsReady(true);
|
|
277
|
+
// 开始播放
|
|
278
|
+
player.play();
|
|
279
|
+
})
|
|
280
|
+
.catch((error) => {
|
|
281
|
+
console.error('sdk初始化失败', error);
|
|
282
|
+
});
|
|
275
283
|
|
|
276
284
|
// 清理
|
|
277
285
|
return () => {
|
|
@@ -280,16 +288,10 @@ export const AnimationComponent: React.FC = () => {
|
|
|
280
288
|
};
|
|
281
289
|
}, []);
|
|
282
290
|
|
|
283
|
-
const handlePlay = () => {
|
|
284
|
-
playerRef.current?.play();
|
|
285
|
-
};
|
|
286
|
-
|
|
287
291
|
return (
|
|
288
292
|
<div>
|
|
289
293
|
<div id='animation-container' ref={containerRef} />
|
|
290
|
-
<button
|
|
291
|
-
{isReady ? '播放动画' : '加载中...'}
|
|
292
|
-
</button>
|
|
294
|
+
<button disabled={!isReady}>{isReady ? '播放动画' : '加载中...'}</button>
|
|
293
295
|
</div>
|
|
294
296
|
);
|
|
295
297
|
};
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -10,9 +10,15 @@ declare class AnimationPlayer {
|
|
|
10
10
|
private isReady;
|
|
11
11
|
private readyPromise;
|
|
12
12
|
private loopTimerId;
|
|
13
|
+
private slotObservers;
|
|
13
14
|
constructor(container: HTMLElement, options: IPlayerOptions);
|
|
14
15
|
private init;
|
|
15
16
|
private replacePlaceholders;
|
|
17
|
+
private handleSlotList;
|
|
18
|
+
private moveElementToSlot;
|
|
19
|
+
private performMove;
|
|
20
|
+
private observeElementMount;
|
|
21
|
+
private cleanupSlotObservers;
|
|
16
22
|
private handleLoop;
|
|
17
23
|
ready(): Promise<void>;
|
|
18
24
|
play(): Promise<void>;
|
package/dist/esm/index.js
CHANGED
|
@@ -98,7 +98,7 @@ var ResourceLoader = /*#__PURE__*/function () {
|
|
|
98
98
|
_defineProperty(ResourceLoader, "imageCache", new Map());
|
|
99
99
|
_defineProperty(ResourceLoader, "loadingQueue", new Map());
|
|
100
100
|
var AnimationPlayer = /*#__PURE__*/function () {
|
|
101
|
-
//
|
|
101
|
+
// 保存 MutationObserver 实例,用于清理
|
|
102
102
|
|
|
103
103
|
function AnimationPlayer(container, options) {
|
|
104
104
|
_classCallCheck(this, AnimationPlayer);
|
|
@@ -107,6 +107,8 @@ var AnimationPlayer = /*#__PURE__*/function () {
|
|
|
107
107
|
_defineProperty(this, "isReady", false);
|
|
108
108
|
_defineProperty(this, "readyPromise", null);
|
|
109
109
|
_defineProperty(this, "loopTimerId", null);
|
|
110
|
+
// 保存定时器ID,用于清理
|
|
111
|
+
_defineProperty(this, "slotObservers", []);
|
|
110
112
|
this.container = container;
|
|
111
113
|
this.options = options;
|
|
112
114
|
this.init();
|
|
@@ -115,8 +117,6 @@ var AnimationPlayer = /*#__PURE__*/function () {
|
|
|
115
117
|
key: "init",
|
|
116
118
|
value: function () {
|
|
117
119
|
var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
118
|
-
var _this$options;
|
|
119
|
-
var _this$options2, _this$options$jsonSch, response, data;
|
|
120
120
|
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
121
121
|
while (1) switch (_context2.prev = _context2.next) {
|
|
122
122
|
case 0:
|
|
@@ -130,36 +130,9 @@ var AnimationPlayer = /*#__PURE__*/function () {
|
|
|
130
130
|
backgroundColor: 'rgba(0, 0, 0, 0.85)',
|
|
131
131
|
backdropFilter: 'blur(10px)'
|
|
132
132
|
});
|
|
133
|
-
|
|
134
|
-
_context2.next = 5;
|
|
135
|
-
break;
|
|
136
|
-
}
|
|
137
|
-
this.container.innerHTML = this.replacePlaceholders(this.options.jsonSchema.content);
|
|
138
|
-
_context2.next = 17;
|
|
139
|
-
break;
|
|
140
|
-
case 5:
|
|
141
|
-
if (!((_this$options = this.options) !== null && _this$options !== void 0 && _this$options.jsonUrl)) {
|
|
142
|
-
_context2.next = 16;
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
|
-
_context2.next = 8;
|
|
146
|
-
return fetch((_this$options2 = this.options) === null || _this$options2 === void 0 ? void 0 : _this$options2.jsonUrl);
|
|
147
|
-
case 8:
|
|
148
|
-
response = _context2.sent;
|
|
149
|
-
_context2.next = 11;
|
|
150
|
-
return response.json();
|
|
151
|
-
case 11:
|
|
152
|
-
data = _context2.sent;
|
|
153
|
-
this.options.jsonSchema = data;
|
|
154
|
-
this.container.innerHTML = this.replacePlaceholders(((_this$options$jsonSch = this.options.jsonSchema) === null || _this$options$jsonSch === void 0 ? void 0 : _this$options$jsonSch.content) || '');
|
|
155
|
-
_context2.next = 17;
|
|
156
|
-
break;
|
|
157
|
-
case 16:
|
|
158
|
-
throw new Error('没有提供jsonSchema或jsonUrl');
|
|
159
|
-
case 17:
|
|
160
|
-
_context2.next = 19;
|
|
133
|
+
_context2.next = 3;
|
|
161
134
|
return this.ready();
|
|
162
|
-
case
|
|
135
|
+
case 3:
|
|
163
136
|
case "end":
|
|
164
137
|
return _context2.stop();
|
|
165
138
|
}
|
|
@@ -192,14 +165,139 @@ var AnimationPlayer = /*#__PURE__*/function () {
|
|
|
192
165
|
return ((_this2$options$dynami = _this2.options.dynamicData) === null || _this2$options$dynami === void 0 ? void 0 : _this2$options$dynami[key]) !== undefined ? String(_this2.options.dynamicData[key]) : match;
|
|
193
166
|
});
|
|
194
167
|
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 处理插槽列表,将 elementId 对应的元素移动到 slotId 对应的插槽中
|
|
171
|
+
* 需要处理时序问题:slotId 需要弹窗渲染后才存在,elementId 可能尚未挂载
|
|
172
|
+
*/
|
|
173
|
+
}, {
|
|
174
|
+
key: "handleSlotList",
|
|
175
|
+
value: function handleSlotList() {
|
|
176
|
+
var _this3 = this;
|
|
177
|
+
if (!this.options.extensions || this.options.extensions.length === 0 || !this.options.extensions.some(function (item) {
|
|
178
|
+
return item.slotId;
|
|
179
|
+
}) || !this.options.extensions.some(function (item) {
|
|
180
|
+
return item.elementId;
|
|
181
|
+
})) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
console.log('aminofx: 🎰 开始处理插槽列表');
|
|
185
|
+
this.options.extensions.forEach(function (slotItem, index) {
|
|
186
|
+
var slotId = slotItem.slotId,
|
|
187
|
+
elementId = slotItem.elementId;
|
|
188
|
+
if (!slotId || !elementId) {
|
|
189
|
+
console.warn("aminofx: \u26A0\uFE0F \u63D2\u69FD\u914D\u7F6E ".concat(index + 1, " \u7F3A\u5C11 slotId \u6216 elementId"));
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
_this3.moveElementToSlot(slotId, elementId, index);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 将元素移动到插槽中
|
|
198
|
+
* @param slotId - 插槽 DOM id
|
|
199
|
+
* @param elementId - 元素 DOM id
|
|
200
|
+
* @param index - 插槽索引,用于日志
|
|
201
|
+
*/
|
|
202
|
+
}, {
|
|
203
|
+
key: "moveElementToSlot",
|
|
204
|
+
value: function moveElementToSlot(slotId, elementId, index) {
|
|
205
|
+
var slotElement = this.container.querySelector("#".concat(slotId));
|
|
206
|
+
if (!slotElement) {
|
|
207
|
+
console.warn("aminofx: \u26A0\uFE0F \u63D2\u69FD ".concat(index + 1, " \u7684 slotId \"").concat(slotId, "\" \u5728\u5F39\u7A97\u4E2D\u672A\u627E\u5230"));
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
var targetElement = document.getElementById(elementId);
|
|
211
|
+
if (targetElement) {
|
|
212
|
+
// elementId 已存在,直接移动
|
|
213
|
+
this.performMove(slotElement, targetElement, index);
|
|
214
|
+
} else {
|
|
215
|
+
// elementId 尚未挂载,添加 MutationObserver 监听
|
|
216
|
+
console.log("aminofx: \u23F3 \u63D2\u69FD ".concat(index + 1, " \u7684 elementId \"").concat(elementId, "\" \u5C1A\u672A\u6302\u8F7D\uFF0C\u7B49\u5F85\u4E2D..."));
|
|
217
|
+
this.observeElementMount(slotId, elementId, slotElement, index);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 执行元素移动操作
|
|
223
|
+
* @param slotElement - 插槽元素
|
|
224
|
+
* @param targetElement - 目标元素
|
|
225
|
+
* @param index - 插槽索引,用于日志
|
|
226
|
+
*/
|
|
227
|
+
}, {
|
|
228
|
+
key: "performMove",
|
|
229
|
+
value: function performMove(slotElement, targetElement, index) {
|
|
230
|
+
try {
|
|
231
|
+
slotElement.appendChild(targetElement);
|
|
232
|
+
console.log("aminofx: \u2705 \u63D2\u69FD ".concat(index + 1, " \u5143\u7D20\u79FB\u52A8\u6210\u529F: ").concat(targetElement.id, " -> ").concat(slotElement.id));
|
|
233
|
+
} catch (error) {
|
|
234
|
+
console.error("aminofx: \u274C \u63D2\u69FD ".concat(index + 1, " \u5143\u7D20\u79FB\u52A8\u5931\u8D25:"), error);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 使用 MutationObserver 监听 elementId 元素的挂载
|
|
240
|
+
* @param slotId - 插槽 DOM id
|
|
241
|
+
* @param elementId - 元素 DOM id
|
|
242
|
+
* @param slotElement - 插槽元素
|
|
243
|
+
* @param index - 插槽索引,用于日志
|
|
244
|
+
*/
|
|
245
|
+
}, {
|
|
246
|
+
key: "observeElementMount",
|
|
247
|
+
value: function observeElementMount(slotId, elementId, slotElement, index) {
|
|
248
|
+
var _this4 = this;
|
|
249
|
+
var observer = new MutationObserver(function (mutations, obs) {
|
|
250
|
+
var targetElement = document.getElementById(elementId);
|
|
251
|
+
if (targetElement) {
|
|
252
|
+
console.log("aminofx: \uD83C\uDFAF \u63D2\u69FD ".concat(index + 1, " \u68C0\u6D4B\u5230 elementId \"").concat(elementId, "\" \u5DF2\u6302\u8F7D"));
|
|
253
|
+
|
|
254
|
+
// 再次检查 slotElement 是否仍然有效(防止弹窗已销毁)
|
|
255
|
+
if (_this4.container.contains(slotElement)) {
|
|
256
|
+
_this4.performMove(slotElement, targetElement, index);
|
|
257
|
+
} else {
|
|
258
|
+
console.warn("aminofx: \u26A0\uFE0F \u63D2\u69FD ".concat(index + 1, " \u7684 slotId \"").concat(slotId, "\" \u5DF2\u4E0D\u5728 DOM \u4E2D"));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// 停止观察
|
|
262
|
+
obs.disconnect();
|
|
263
|
+
// 从数组中移除
|
|
264
|
+
var observerIndex = _this4.slotObservers.indexOf(obs);
|
|
265
|
+
if (observerIndex > -1) {
|
|
266
|
+
_this4.slotObservers.splice(observerIndex, 1);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// 保存 observer 实例,用于销毁时清理
|
|
272
|
+
this.slotObservers.push(observer);
|
|
273
|
+
|
|
274
|
+
// 观察整个 document.body 的子树变化
|
|
275
|
+
observer.observe(document.body, {
|
|
276
|
+
childList: true,
|
|
277
|
+
subtree: true
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* 清理所有 MutationObserver
|
|
283
|
+
*/
|
|
284
|
+
}, {
|
|
285
|
+
key: "cleanupSlotObservers",
|
|
286
|
+
value: function cleanupSlotObservers() {
|
|
287
|
+
this.slotObservers.forEach(function (observer) {
|
|
288
|
+
observer.disconnect();
|
|
289
|
+
});
|
|
290
|
+
this.slotObservers = [];
|
|
291
|
+
console.log('aminofx: 🧹 清理所有插槽观察器');
|
|
292
|
+
}
|
|
195
293
|
}, {
|
|
196
294
|
key: "handleLoop",
|
|
197
295
|
value: function handleLoop() {
|
|
198
|
-
var _this$options$
|
|
199
|
-
|
|
200
|
-
_this$options$
|
|
201
|
-
_this$options$
|
|
202
|
-
if (!this.options.autoLoop || !Number((_this$options$
|
|
296
|
+
var _this$options$jsonSch,
|
|
297
|
+
_this5 = this,
|
|
298
|
+
_this$options$jsonSch2,
|
|
299
|
+
_this$options$jsonSch3;
|
|
300
|
+
if (!this.options.autoLoop || !Number((_this$options$jsonSch = this.options.jsonSchema) === null || _this$options$jsonSch === void 0 ? void 0 : _this$options$jsonSch.duration)) {
|
|
203
301
|
return;
|
|
204
302
|
}
|
|
205
303
|
|
|
@@ -212,71 +310,129 @@ var AnimationPlayer = /*#__PURE__*/function () {
|
|
|
212
310
|
|
|
213
311
|
// 后续根据使用情况看是否调整为监听css动画结束事件
|
|
214
312
|
this.loopTimerId = window.setTimeout(function () {
|
|
215
|
-
|
|
313
|
+
_this5.container.style.display = 'none';
|
|
216
314
|
window.setTimeout(function () {
|
|
217
|
-
|
|
218
|
-
|
|
315
|
+
_this5.loopTimerId = null;
|
|
316
|
+
_this5.play();
|
|
219
317
|
}, 100);
|
|
220
|
-
}, Number((_this$options$
|
|
221
|
-
console.log("aminofx: \uD83D\uDD04 \u542F\u52A8\u5FAA\u73AF\u5B9A\u65F6\u5668\uFF0C\u65F6\u957F: ".concat((_this$options$
|
|
318
|
+
}, Number((_this$options$jsonSch2 = this.options.jsonSchema) === null || _this$options$jsonSch2 === void 0 ? void 0 : _this$options$jsonSch2.duration));
|
|
319
|
+
console.log("aminofx: \uD83D\uDD04 \u542F\u52A8\u5FAA\u73AF\u5B9A\u65F6\u5668\uFF0C\u65F6\u957F: ".concat((_this$options$jsonSch3 = this.options.jsonSchema) === null || _this$options$jsonSch3 === void 0 ? void 0 : _this$options$jsonSch3.duration, "ms"));
|
|
222
320
|
}
|
|
223
321
|
}, {
|
|
224
322
|
key: "ready",
|
|
225
323
|
value: function () {
|
|
226
|
-
var _ready = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
227
|
-
var
|
|
228
|
-
return _regeneratorRuntime().wrap(function
|
|
229
|
-
while (1) switch (
|
|
324
|
+
var _ready = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
|
|
325
|
+
var _this6 = this;
|
|
326
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
327
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
230
328
|
case 0:
|
|
231
329
|
if (!this.isReady) {
|
|
232
|
-
|
|
330
|
+
_context5.next = 3;
|
|
233
331
|
break;
|
|
234
332
|
}
|
|
235
333
|
console.log('aminofx: ✅ 动画已准备就绪,直接返回');
|
|
236
|
-
return
|
|
334
|
+
return _context5.abrupt("return", Promise.resolve());
|
|
237
335
|
case 3:
|
|
238
336
|
if (!this.readyPromise) {
|
|
239
|
-
|
|
337
|
+
_context5.next = 6;
|
|
240
338
|
break;
|
|
241
339
|
}
|
|
242
340
|
console.log('aminofx: ⏳ 正在准备中,等待现有Promise...');
|
|
243
|
-
return
|
|
341
|
+
return _context5.abrupt("return", this.readyPromise);
|
|
244
342
|
case 6:
|
|
245
343
|
this.readyPromise = new Promise( /*#__PURE__*/function () {
|
|
246
|
-
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
247
|
-
|
|
248
|
-
|
|
344
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(resolve, reject) {
|
|
345
|
+
var jsonPromise, imgPromise;
|
|
346
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
347
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
249
348
|
case 0:
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
349
|
+
_context4.prev = 0;
|
|
350
|
+
jsonPromise = new Promise( /*#__PURE__*/function () {
|
|
351
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(resolve, reject) {
|
|
352
|
+
var _this6$options;
|
|
353
|
+
var _this6$options2, _this6$options$jsonSc, response, data;
|
|
354
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
355
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
356
|
+
case 0:
|
|
357
|
+
if (!_this6.options.jsonSchema) {
|
|
358
|
+
_context3.next = 5;
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
_this6.container.innerHTML = _this6.replacePlaceholders(_this6.options.jsonSchema.content);
|
|
362
|
+
resolve();
|
|
363
|
+
_context3.next = 24;
|
|
364
|
+
break;
|
|
365
|
+
case 5:
|
|
366
|
+
if (!((_this6$options = _this6.options) !== null && _this6$options !== void 0 && _this6$options.jsonUrl)) {
|
|
367
|
+
_context3.next = 23;
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
_context3.prev = 6;
|
|
371
|
+
_context3.next = 9;
|
|
372
|
+
return fetch((_this6$options2 = _this6.options) === null || _this6$options2 === void 0 ? void 0 : _this6$options2.jsonUrl);
|
|
373
|
+
case 9:
|
|
374
|
+
response = _context3.sent;
|
|
375
|
+
_context3.next = 12;
|
|
376
|
+
return response.json();
|
|
377
|
+
case 12:
|
|
378
|
+
data = _context3.sent;
|
|
379
|
+
_this6.options.jsonSchema = data;
|
|
380
|
+
_this6.container.innerHTML = _this6.replacePlaceholders(((_this6$options$jsonSc = _this6.options.jsonSchema) === null || _this6$options$jsonSc === void 0 ? void 0 : _this6$options$jsonSc.content) || '');
|
|
381
|
+
resolve();
|
|
382
|
+
_context3.next = 21;
|
|
383
|
+
break;
|
|
384
|
+
case 18:
|
|
385
|
+
_context3.prev = 18;
|
|
386
|
+
_context3.t0 = _context3["catch"](6);
|
|
387
|
+
reject(_context3.t0);
|
|
388
|
+
case 21:
|
|
389
|
+
_context3.next = 24;
|
|
390
|
+
break;
|
|
391
|
+
case 23:
|
|
392
|
+
throw new Error('没有提供jsonSchema或jsonUrl');
|
|
393
|
+
case 24:
|
|
394
|
+
case "end":
|
|
395
|
+
return _context3.stop();
|
|
396
|
+
}
|
|
397
|
+
}, _callee3, null, [[6, 18]]);
|
|
398
|
+
}));
|
|
399
|
+
return function (_x4, _x5) {
|
|
400
|
+
return _ref2.apply(this, arguments);
|
|
401
|
+
};
|
|
402
|
+
}());
|
|
403
|
+
imgPromise = ResourceLoader.loadImages(_this6.options.preloadImgList || []);
|
|
404
|
+
_context4.next = 5;
|
|
405
|
+
return Promise.all([jsonPromise, imgPromise]);
|
|
406
|
+
case 5:
|
|
407
|
+
_this6.isReady = true; // 设置准备完成状态
|
|
408
|
+
|
|
409
|
+
// 处理插槽列表(此时弹窗内容已渲染,slotId 存在)
|
|
410
|
+
_this6.handleSlotList();
|
|
411
|
+
resolve();
|
|
255
412
|
console.log('aminofx: 🎉 动画准备完成,可以开始播放');
|
|
256
|
-
|
|
413
|
+
_context4.next = 15;
|
|
257
414
|
break;
|
|
258
|
-
case 7:
|
|
259
|
-
_context3.prev = 7;
|
|
260
|
-
_context3.t0 = _context3["catch"](0);
|
|
261
|
-
console.error('aminofx: ❌ 动画准备失败:', _context3.t0);
|
|
262
|
-
case 10:
|
|
263
|
-
resolve();
|
|
264
415
|
case 11:
|
|
416
|
+
_context4.prev = 11;
|
|
417
|
+
_context4.t0 = _context4["catch"](0);
|
|
418
|
+
reject(_context4.t0);
|
|
419
|
+
console.error('aminofx: ❌ 动画准备失败:', _context4.t0);
|
|
420
|
+
case 15:
|
|
265
421
|
case "end":
|
|
266
|
-
return
|
|
422
|
+
return _context4.stop();
|
|
267
423
|
}
|
|
268
|
-
},
|
|
424
|
+
}, _callee4, null, [[0, 11]]);
|
|
269
425
|
}));
|
|
270
|
-
return function (_x2) {
|
|
426
|
+
return function (_x2, _x3) {
|
|
271
427
|
return _ref.apply(this, arguments);
|
|
272
428
|
};
|
|
273
429
|
}());
|
|
274
|
-
return
|
|
430
|
+
return _context5.abrupt("return", this.readyPromise);
|
|
275
431
|
case 8:
|
|
276
432
|
case "end":
|
|
277
|
-
return
|
|
433
|
+
return _context5.stop();
|
|
278
434
|
}
|
|
279
|
-
},
|
|
435
|
+
}, _callee5, this);
|
|
280
436
|
}));
|
|
281
437
|
function ready() {
|
|
282
438
|
return _ready.apply(this, arguments);
|
|
@@ -286,16 +442,16 @@ var AnimationPlayer = /*#__PURE__*/function () {
|
|
|
286
442
|
}, {
|
|
287
443
|
key: "play",
|
|
288
444
|
value: function () {
|
|
289
|
-
var _play = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
290
|
-
return _regeneratorRuntime().wrap(function
|
|
291
|
-
while (1) switch (
|
|
445
|
+
var _play = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
446
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
447
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
292
448
|
case 0:
|
|
293
449
|
if (this.isReady) {
|
|
294
|
-
|
|
450
|
+
_context6.next = 4;
|
|
295
451
|
break;
|
|
296
452
|
}
|
|
297
453
|
console.warn('aminofx: ⚠️ 动画尚未准备就绪,自动调用ready()...');
|
|
298
|
-
|
|
454
|
+
_context6.next = 4;
|
|
299
455
|
return this.ready();
|
|
300
456
|
case 4:
|
|
301
457
|
this.container.style.display = 'flex';
|
|
@@ -306,9 +462,9 @@ var AnimationPlayer = /*#__PURE__*/function () {
|
|
|
306
462
|
}
|
|
307
463
|
case 6:
|
|
308
464
|
case "end":
|
|
309
|
-
return
|
|
465
|
+
return _context6.stop();
|
|
310
466
|
}
|
|
311
|
-
},
|
|
467
|
+
}, _callee6, this);
|
|
312
468
|
}));
|
|
313
469
|
function play() {
|
|
314
470
|
return _play.apply(this, arguments);
|
|
@@ -325,6 +481,9 @@ var AnimationPlayer = /*#__PURE__*/function () {
|
|
|
325
481
|
console.log('aminofx: 🧹 销毁时清理循环定时器');
|
|
326
482
|
}
|
|
327
483
|
|
|
484
|
+
// 清理插槽观察器
|
|
485
|
+
this.cleanupSlotObservers();
|
|
486
|
+
|
|
328
487
|
// 重置状态
|
|
329
488
|
this.container.innerHTML = '';
|
|
330
489
|
this.isReady = false;
|
package/dist/esm/type.d.ts
CHANGED
|
@@ -9,5 +9,15 @@ interface IPlayerOptions {
|
|
|
9
9
|
preloadImgList?: string[];
|
|
10
10
|
autoLoop?: boolean;
|
|
11
11
|
dynamicData?: Record<string, string | number | boolean>;
|
|
12
|
+
extensions?: {
|
|
13
|
+
slotId?: string;
|
|
14
|
+
elementId?: string;
|
|
15
|
+
clickAreas?: {
|
|
16
|
+
x?: number;
|
|
17
|
+
y?: number;
|
|
18
|
+
width?: number;
|
|
19
|
+
height?: number;
|
|
20
|
+
};
|
|
21
|
+
}[];
|
|
12
22
|
}
|
|
13
23
|
export type { IPlayerOptions, IJsonSchema };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["@yqg/aminofx-css-kit"]=e():t["@yqg/aminofx-css-kit"]=e()}(self,(function(){return function(){var t={506:function(){},365:function(t){function e(t,e,r,n,o,i,a){try{var s=t[i](a),c=s.value}catch(t){return void r(t)}s.done?e(c):Promise.resolve(c).then(n,o)}t.exports=function(t){return function(){var r=this,n=arguments;return new Promise((function(o,i){var a=t.apply(r,n);function s(t){e(a,o,i,s,c,"next",t)}function c(t){e(a,o,i,s,c,"throw",t)}s(void 0)}))}},t.exports.__esModule=!0,t.exports.default=t.exports},156:function(t){t.exports=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},t.exports.__esModule=!0,t.exports.default=t.exports},499:function(t,e,r){var n=r(714);function o(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,n(o.key),o)}}t.exports=function(t,e,r){return e&&o(t.prototype,e),r&&o(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t},t.exports.__esModule=!0,t.exports.default=t.exports},173:function(t,e,r){var n=r(714);t.exports=function(t,e,r){return(e=n(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t},t.exports.__esModule=!0,t.exports.default=t.exports},298:function(t,e,r){var n=r(241).default;function o(){"use strict";t.exports=o=function(){return r},t.exports.__esModule=!0,t.exports.default=t.exports;var e,r={},i=Object.prototype,a=i.hasOwnProperty,s=Object.defineProperty||function(t,e,r){t[e]=r.value},c="function"==typeof Symbol?Symbol:{},u=c.iterator||"@@iterator",l=c.asyncIterator||"@@asyncIterator",f=c.toStringTag||"@@toStringTag";function p(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{p({},"")}catch(e){p=function(t,e,r){return t[e]=r}}function h(t,e,r,n){var o=e&&e.prototype instanceof g?e:g,i=Object.create(o.prototype),a=new M(n||[]);return s(i,"_invoke",{value:S(t,r,a)}),i}function d(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}r.wrap=h;var y="suspendedStart",v="executing",m="completed",x={};function g(){}function w(){}function b(){}var j={};p(j,u,(function(){return this}));var P=Object.getPrototypeOf,_=P&&P(P(N([])));_&&_!==i&&a.call(_,u)&&(j=_);var k=b.prototype=g.prototype=Object.create(j);function L(t){["next","throw","return"].forEach((function(e){p(t,e,(function(t){return this._invoke(e,t)}))}))}function E(t,e){function r(o,i,s,c){var u=d(t[o],t,i);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==n(f)&&a.call(f,"__await")?e.resolve(f.__await).then((function(t){r("next",t,s,c)}),(function(t){r("throw",t,s,c)})):e.resolve(f).then((function(t){l.value=t,s(l)}),(function(t){return r("throw",t,s,c)}))}c(u.arg)}var o;s(this,"_invoke",{value:function(t,n){function i(){return new e((function(e,o){r(t,n,e,o)}))}return o=o?o.then(i,i):i()}})}function S(t,r,n){var o=y;return function(i,a){if(o===v)throw new Error("Generator is already running");if(o===m){if("throw"===i)throw a;return{value:e,done:!0}}for(n.method=i,n.arg=a;;){var s=n.delegate;if(s){var c=T(s,n);if(c){if(c===x)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===y)throw o=m,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=v;var u=d(t,r,n);if("normal"===u.type){if(o=n.done?m:"suspendedYield",u.arg===x)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(o=m,n.method="throw",n.arg=u.arg)}}}function T(t,r){var n=r.method,o=t.iterator[n];if(o===e)return r.delegate=null,"throw"===n&&t.iterator.return&&(r.method="return",r.arg=e,T(t,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),x;var i=d(o,t.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,x;var a=i.arg;return a?a.done?(r[t.resultName]=a.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,x):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,x)}function O(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function I(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function M(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(O,this),this.reset(!0)}function N(t){if(t||""===t){var r=t[u];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,i=function r(){for(;++o<t.length;)if(a.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return i.next=i}}throw new TypeError(n(t)+" is not iterable")}return w.prototype=b,s(k,"constructor",{value:b,configurable:!0}),s(b,"constructor",{value:w,configurable:!0}),w.displayName=p(b,f,"GeneratorFunction"),r.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===w||"GeneratorFunction"===(e.displayName||e.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,b):(t.__proto__=b,p(t,f,"GeneratorFunction")),t.prototype=Object.create(k),t},r.awrap=function(t){return{__await:t}},L(E.prototype),p(E.prototype,l,(function(){return this})),r.AsyncIterator=E,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new E(h(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},L(k),p(k,f,"Generator"),p(k,u,(function(){return this})),p(k,"toString",(function(){return"[object Generator]"})),r.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=N,M.prototype={constructor:M,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(I),!t)for(var r in this)"t"===r.charAt(0)&&a.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(n,o){return s.type="throw",s.arg=t,r.next=n,o&&(r.method="next",r.arg=e),!!o}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],s=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=a.call(i,"catchLoc"),u=a.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&a.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var i=o?o.completion:{};return i.type=t,i.arg=e,o?(this.method="next",this.next=o.finallyLoc,x):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),x},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),I(r),x}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;I(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:N(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),x}},r}t.exports=o,t.exports.__esModule=!0,t.exports.default=t.exports},569:function(t,e,r){var n=r(241).default;t.exports=function(t,e){if("object"!=n(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var o=r.call(t,e||"default");if("object"!=n(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)},t.exports.__esModule=!0,t.exports.default=t.exports},714:function(t,e,r){var n=r(241).default,o=r(569);t.exports=function(t){var e=o(t,"string");return"symbol"==n(e)?e:String(e)},t.exports.__esModule=!0,t.exports.default=t.exports},241:function(t){function e(r){return t.exports=e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t.exports.__esModule=!0,t.exports.default=t.exports,e(r)}t.exports=e,t.exports.__esModule=!0,t.exports.default=t.exports}},e={};function r(n){var o=e[n];if(void 0!==o)return o.exports;var i=e[n]={exports:{}};return t[n](i,i.exports,r),i.exports}r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,{a:e}),e},r.d=function(t,e){for(var n in e)r.o(e,n)&&!r.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var n={};return function(){"use strict";r.r(n),r.d(n,{AnimationPlayer:function(){return v}});var t=r(298),e=r.n(t),o=r(365),i=r.n(o),a=r(156),s=r.n(a),c=r(499),u=r.n(c),l=r(173),f=r.n(l),p=r(506),h={};for(var d in p)["default","AnimationPlayer"].indexOf(d)<0&&(h[d]=function(t){return p[t]}.bind(0,d));r.d(n,h);var y=function(){function t(){s()(this,t)}var r;return u()(t,null,[{key:"loadImages",value:(r=i()(e()().mark((function t(r){var n,o=this;return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(Array.isArray(r)&&0!==r.length){t.next=2;break}return t.abrupt("return",Promise.resolve());case 2:return n=r.map((function(t,e){if(!t.startsWith("http"))return Promise.resolve();if(o.imageCache.has(t))return console.log("aminofx: ✅ 图片 ".concat(e+1," 已缓存: ").concat(t)),Promise.resolve();if(o.loadingQueue.has(t))return o.loadingQueue.get(t);var r=new Promise((function(r){var n=new Image;n.onload=function(){o.imageCache.set(t,!0),o.loadingQueue.delete(t),console.log("aminofx: ✅ 图片 ".concat(e+1," 加载成功: ").concat(t)),"function"==typeof n.decode?n.decode().then((function(){console.log("aminofx: 🎨 图片 ".concat(e+1," 解码完成: ").concat(t)),r()})).catch((function(){console.log("aminofx: ⚠️ 图片 ".concat(e+1," decode失败,使用fallback: ").concat(t)),r()})):(console.log("aminofx: 📱 图片 ".concat(e+1," 使用兼容模式: ").concat(t)),r())},n.src=t,n.onerror=function(){o.loadingQueue.delete(t),console.error("aminofx: ❌ 图片 ".concat(e+1," 加载失败: ").concat(t)),r()}}));return o.loadingQueue.set(t,r),r})),t.prev=3,t.next=6,Promise.all(n);case 6:console.log("aminofx: 🎉 所有图片资源加载完成"),t.next=12;break;case 9:t.prev=9,t.t0=t.catch(3),console.error("aminofx: ❌ 图片加载过程中出现错误:",t.t0);case 12:case"end":return t.stop()}}),t,null,[[3,9]])}))),function(t){return r.apply(this,arguments)})}]),t}();f()(y,"imageCache",new Map),f()(y,"loadingQueue",new Map);var v=function(){function t(e,r){s()(this,t),f()(this,"container",void 0),f()(this,"options",void 0),f()(this,"isReady",!1),f()(this,"readyPromise",null),f()(this,"loopTimerId",null),this.container=e,this.options=r,this.init()}var r,n,o;return u()(t,[{key:"init",value:(o=i()(e()().mark((function t(){var r,n,o,i,a;return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(Object.assign(this.container.style,{display:"none",width:"100vw",height:"100vh",overflow:"hidden",alignItems:this.options.alignItems||"center",justifyContent:"center",backgroundColor:"rgba(0, 0, 0, 0.85)",backdropFilter:"blur(10px)"}),!this.options.jsonSchema){t.next=5;break}this.container.innerHTML=this.replacePlaceholders(this.options.jsonSchema.content),t.next=17;break;case 5:if(null===(r=this.options)||void 0===r||!r.jsonUrl){t.next=16;break}return t.next=8,fetch(null===(n=this.options)||void 0===n?void 0:n.jsonUrl);case 8:return i=t.sent,t.next=11,i.json();case 11:a=t.sent,this.options.jsonSchema=a,this.container.innerHTML=this.replacePlaceholders((null===(o=this.options.jsonSchema)||void 0===o?void 0:o.content)||""),t.next=17;break;case 16:throw new Error("没有提供jsonSchema或jsonUrl");case 17:return t.next=19,this.ready();case 19:case"end":return t.stop()}}),t,this)}))),function(){return o.apply(this,arguments)})},{key:"replacePlaceholders",value:function(t){var e=this;return this.options.dynamicData&&t?t.replace(/\{\{(\w+)\}\}|\$\{(\w+)\}/g,(function(t,r,n){var o,i=r||n;return void 0!==(null===(o=e.options.dynamicData)||void 0===o?void 0:o[i])?String(e.options.dynamicData[i]):t})):t}},{key:"handleLoop",value:function(){var t,e,r,n=this;this.options.autoLoop&&Number(null===(t=this.options.jsonSchema)||void 0===t?void 0:t.duration)&&(null!==this.loopTimerId&&(clearTimeout(this.loopTimerId),this.loopTimerId=null,console.log("aminofx: 🧹 清理旧的循环定时器")),this.loopTimerId=window.setTimeout((function(){n.container.style.display="none",window.setTimeout((function(){n.loopTimerId=null,n.play()}),100)}),Number(null===(e=this.options.jsonSchema)||void 0===e?void 0:e.duration)),console.log("aminofx: 🔄 启动循环定时器,时长: ".concat(null===(r=this.options.jsonSchema)||void 0===r?void 0:r.duration,"ms")))}},{key:"ready",value:(n=i()(e()().mark((function t(){var r=this;return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.isReady){t.next=3;break}return console.log("aminofx: ✅ 动画已准备就绪,直接返回"),t.abrupt("return",Promise.resolve());case 3:if(!this.readyPromise){t.next=6;break}return console.log("aminofx: ⏳ 正在准备中,等待现有Promise..."),t.abrupt("return",this.readyPromise);case 6:return this.readyPromise=new Promise(function(){var t=i()(e()().mark((function t(n){return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,y.loadImages(r.options.preloadImgList||[]);case 3:r.isReady=!0,console.log("aminofx: 🎉 动画准备完成,可以开始播放"),t.next=10;break;case 7:t.prev=7,t.t0=t.catch(0),console.error("aminofx: ❌ 动画准备失败:",t.t0);case 10:n();case 11:case"end":return t.stop()}}),t,null,[[0,7]])})));return function(e){return t.apply(this,arguments)}}()),t.abrupt("return",this.readyPromise);case 8:case"end":return t.stop()}}),t,this)}))),function(){return n.apply(this,arguments)})},{key:"play",value:(r=i()(e()().mark((function t(){return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(this.isReady){t.next=4;break}return console.warn("aminofx: ⚠️ 动画尚未准备就绪,自动调用ready()..."),t.next=4,this.ready();case 4:this.container.style.display="flex",this.options.autoLoop&&null===this.loopTimerId&&this.handleLoop();case 6:case"end":return t.stop()}}),t,this)}))),function(){return r.apply(this,arguments)})},{key:"destroy",value:function(){null!==this.loopTimerId&&(clearTimeout(this.loopTimerId),this.loopTimerId=null,console.log("aminofx: 🧹 销毁时清理循环定时器")),this.container.innerHTML="",this.isReady=!1,this.readyPromise=null,console.log("aminofx: 🗑️ AnimationPlayer 已销毁")}},{key:"updateSchema",value:function(t){this.destroy(),this.options=t,this.init()}}]),t}();function m(t,e){var r=document.getElementById(t);if(!r)throw new Error('Container with id "'.concat(t,'" not found'));var n=document.createElement("div"),o=new v(n,e);return r.appendChild(n),o}n.default=m,window.createCssAnimationPlayer=m}(),n}()}));
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["@yqg/aminofx-css-kit"]=e():t["@yqg/aminofx-css-kit"]=e()}(self,(function(){return function(){var t={506:function(){},365:function(t){function e(t,e,n,o,r,i,a){try{var s=t[i](a),c=s.value}catch(t){return void n(t)}s.done?e(c):Promise.resolve(c).then(o,r)}t.exports=function(t){return function(){var n=this,o=arguments;return new Promise((function(r,i){var a=t.apply(n,o);function s(t){e(a,r,i,s,c,"next",t)}function c(t){e(a,r,i,s,c,"throw",t)}s(void 0)}))}},t.exports.__esModule=!0,t.exports.default=t.exports},156:function(t){t.exports=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},t.exports.__esModule=!0,t.exports.default=t.exports},499:function(t,e,n){var o=n(714);function r(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,o(r.key),r)}}t.exports=function(t,e,n){return e&&r(t.prototype,e),n&&r(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t},t.exports.__esModule=!0,t.exports.default=t.exports},173:function(t,e,n){var o=n(714);t.exports=function(t,e,n){return(e=o(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t},t.exports.__esModule=!0,t.exports.default=t.exports},298:function(t,e,n){var o=n(241).default;function r(){"use strict";t.exports=r=function(){return n},t.exports.__esModule=!0,t.exports.default=t.exports;var e,n={},i=Object.prototype,a=i.hasOwnProperty,s=Object.defineProperty||function(t,e,n){t[e]=n.value},c="function"==typeof Symbol?Symbol:{},u=c.iterator||"@@iterator",l=c.asyncIterator||"@@asyncIterator",f=c.toStringTag||"@@toStringTag";function p(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{p({},"")}catch(e){p=function(t,e,n){return t[e]=n}}function h(t,e,n,o){var r=e&&e.prototype instanceof g?e:g,i=Object.create(r.prototype),a=new M(o||[]);return s(i,"_invoke",{value:S(t,n,a)}),i}function d(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}n.wrap=h;var v="suspendedStart",m="executing",y="completed",x={};function g(){}function b(){}function w(){}var k={};p(k,u,(function(){return this}));var P=Object.getPrototypeOf,j=P&&P(P(N([])));j&&j!==i&&a.call(j,u)&&(k=j);var L=w.prototype=g.prototype=Object.create(k);function _(t){["next","throw","return"].forEach((function(e){p(t,e,(function(t){return this._invoke(e,t)}))}))}function E(t,e){function n(r,i,s,c){var u=d(t[r],t,i);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==o(f)&&a.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,s,c)}),(function(t){n("throw",t,s,c)})):e.resolve(f).then((function(t){l.value=t,s(l)}),(function(t){return n("throw",t,s,c)}))}c(u.arg)}var r;s(this,"_invoke",{value:function(t,o){function i(){return new e((function(e,r){n(t,o,e,r)}))}return r=r?r.then(i,i):i()}})}function S(t,n,o){var r=v;return function(i,a){if(r===m)throw new Error("Generator is already running");if(r===y){if("throw"===i)throw a;return{value:e,done:!0}}for(o.method=i,o.arg=a;;){var s=o.delegate;if(s){var c=O(s,o);if(c){if(c===x)continue;return c}}if("next"===o.method)o.sent=o._sent=o.arg;else if("throw"===o.method){if(r===v)throw r=y,o.arg;o.dispatchException(o.arg)}else"return"===o.method&&o.abrupt("return",o.arg);r=m;var u=d(t,n,o);if("normal"===u.type){if(r=o.done?y:"suspendedYield",u.arg===x)continue;return{value:u.arg,done:o.done}}"throw"===u.type&&(r=y,o.method="throw",o.arg=u.arg)}}}function O(t,n){var o=n.method,r=t.iterator[o];if(r===e)return n.delegate=null,"throw"===o&&t.iterator.return&&(n.method="return",n.arg=e,O(t,n),"throw"===n.method)||"return"!==o&&(n.method="throw",n.arg=new TypeError("The iterator does not provide a '"+o+"' method")),x;var i=d(r,t.iterator,n.arg);if("throw"===i.type)return n.method="throw",n.arg=i.arg,n.delegate=null,x;var a=i.arg;return a?a.done?(n[t.resultName]=a.value,n.next=t.nextLoc,"return"!==n.method&&(n.method="next",n.arg=e),n.delegate=null,x):a:(n.method="throw",n.arg=new TypeError("iterator result is not an object"),n.delegate=null,x)}function I(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function T(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function M(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(I,this),this.reset(!0)}function N(t){if(t||""===t){var n=t[u];if(n)return n.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,i=function n(){for(;++r<t.length;)if(a.call(t,r))return n.value=t[r],n.done=!1,n;return n.value=e,n.done=!0,n};return i.next=i}}throw new TypeError(o(t)+" is not iterable")}return b.prototype=w,s(L,"constructor",{value:w,configurable:!0}),s(w,"constructor",{value:b,configurable:!0}),b.displayName=p(w,f,"GeneratorFunction"),n.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===b||"GeneratorFunction"===(e.displayName||e.name))},n.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,w):(t.__proto__=w,p(t,f,"GeneratorFunction")),t.prototype=Object.create(L),t},n.awrap=function(t){return{__await:t}},_(E.prototype),p(E.prototype,l,(function(){return this})),n.AsyncIterator=E,n.async=function(t,e,o,r,i){void 0===i&&(i=Promise);var a=new E(h(t,e,o,r),i);return n.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},_(L),p(L,f,"Generator"),p(L,u,(function(){return this})),p(L,"toString",(function(){return"[object Generator]"})),n.keys=function(t){var e=Object(t),n=[];for(var o in e)n.push(o);return n.reverse(),function t(){for(;n.length;){var o=n.pop();if(o in e)return t.value=o,t.done=!1,t}return t.done=!0,t}},n.values=N,M.prototype={constructor:M,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(T),!t)for(var n in this)"t"===n.charAt(0)&&a.call(this,n)&&!isNaN(+n.slice(1))&&(this[n]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var n=this;function o(o,r){return s.type="throw",s.arg=t,n.next=o,r&&(n.method="next",n.arg=e),!!r}for(var r=this.tryEntries.length-1;r>=0;--r){var i=this.tryEntries[r],s=i.completion;if("root"===i.tryLoc)return o("end");if(i.tryLoc<=this.prev){var c=a.call(i,"catchLoc"),u=a.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return o(i.catchLoc,!0);if(this.prev<i.finallyLoc)return o(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return o(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return o(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&a.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var r=o;break}}r&&("break"===t||"continue"===t)&&r.tryLoc<=e&&e<=r.finallyLoc&&(r=null);var i=r?r.completion:{};return i.type=t,i.arg=e,r?(this.method="next",this.next=r.finallyLoc,x):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),x},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),T(n),x}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var o=n.completion;if("throw"===o.type){var r=o.arg;T(n)}return r}}throw new Error("illegal catch attempt")},delegateYield:function(t,n,o){return this.delegate={iterator:N(t),resultName:n,nextLoc:o},"next"===this.method&&(this.arg=e),x}},n}t.exports=r,t.exports.__esModule=!0,t.exports.default=t.exports},569:function(t,e,n){var o=n(241).default;t.exports=function(t,e){if("object"!=o(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=o(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)},t.exports.__esModule=!0,t.exports.default=t.exports},714:function(t,e,n){var o=n(241).default,r=n(569);t.exports=function(t){var e=r(t,"string");return"symbol"==o(e)?e:String(e)},t.exports.__esModule=!0,t.exports.default=t.exports},241:function(t){function e(n){return t.exports=e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t.exports.__esModule=!0,t.exports.default=t.exports,e(n)}t.exports=e,t.exports.__esModule=!0,t.exports.default=t.exports}},e={};function n(o){var r=e[o];if(void 0!==r)return r.exports;var i=e[o]={exports:{}};return t[o](i,i.exports,n),i.exports}n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,{a:e}),e},n.d=function(t,e){for(var o in e)n.o(e,o)&&!n.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:e[o]})},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var o={};return function(){"use strict";n.r(o),n.d(o,{AnimationPlayer:function(){return m}});var t=n(298),e=n.n(t),r=n(365),i=n.n(r),a=n(156),s=n.n(a),c=n(499),u=n.n(c),l=n(173),f=n.n(l),p=n(506),h={};for(var d in p)["default","AnimationPlayer"].indexOf(d)<0&&(h[d]=function(t){return p[t]}.bind(0,d));n.d(o,h);var v=function(){function t(){s()(this,t)}var n;return u()(t,null,[{key:"loadImages",value:(n=i()(e()().mark((function t(n){var o,r=this;return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(Array.isArray(n)&&0!==n.length){t.next=2;break}return t.abrupt("return",Promise.resolve());case 2:return o=n.map((function(t,e){if(!t.startsWith("http"))return Promise.resolve();if(r.imageCache.has(t))return console.log("aminofx: ✅ 图片 ".concat(e+1," 已缓存: ").concat(t)),Promise.resolve();if(r.loadingQueue.has(t))return r.loadingQueue.get(t);var n=new Promise((function(n){var o=new Image;o.onload=function(){r.imageCache.set(t,!0),r.loadingQueue.delete(t),console.log("aminofx: ✅ 图片 ".concat(e+1," 加载成功: ").concat(t)),"function"==typeof o.decode?o.decode().then((function(){console.log("aminofx: 🎨 图片 ".concat(e+1," 解码完成: ").concat(t)),n()})).catch((function(){console.log("aminofx: ⚠️ 图片 ".concat(e+1," decode失败,使用fallback: ").concat(t)),n()})):(console.log("aminofx: 📱 图片 ".concat(e+1," 使用兼容模式: ").concat(t)),n())},o.src=t,o.onerror=function(){r.loadingQueue.delete(t),console.error("aminofx: ❌ 图片 ".concat(e+1," 加载失败: ").concat(t)),n()}}));return r.loadingQueue.set(t,n),n})),t.prev=3,t.next=6,Promise.all(o);case 6:console.log("aminofx: 🎉 所有图片资源加载完成"),t.next=12;break;case 9:t.prev=9,t.t0=t.catch(3),console.error("aminofx: ❌ 图片加载过程中出现错误:",t.t0);case 12:case"end":return t.stop()}}),t,null,[[3,9]])}))),function(t){return n.apply(this,arguments)})}]),t}();f()(v,"imageCache",new Map),f()(v,"loadingQueue",new Map);var m=function(){function t(e,n){s()(this,t),f()(this,"container",void 0),f()(this,"options",void 0),f()(this,"isReady",!1),f()(this,"readyPromise",null),f()(this,"loopTimerId",null),f()(this,"slotObservers",[]),this.container=e,this.options=n,this.init()}var n,o,r;return u()(t,[{key:"init",value:(r=i()(e()().mark((function t(){return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return Object.assign(this.container.style,{display:"none",width:"100vw",height:"100vh",overflow:"hidden",alignItems:this.options.alignItems||"center",justifyContent:"center",backgroundColor:"rgba(0, 0, 0, 0.85)",backdropFilter:"blur(10px)"}),t.next=3,this.ready();case 3:case"end":return t.stop()}}),t,this)}))),function(){return r.apply(this,arguments)})},{key:"replacePlaceholders",value:function(t){var e=this;return this.options.dynamicData&&t?t.replace(/\{\{(\w+)\}\}|\$\{(\w+)\}/g,(function(t,n,o){var r,i=n||o;return void 0!==(null===(r=e.options.dynamicData)||void 0===r?void 0:r[i])?String(e.options.dynamicData[i]):t})):t}},{key:"handleSlotList",value:function(){var t=this;this.options.extensions&&0!==this.options.extensions.length&&this.options.extensions.some((function(t){return t.slotId}))&&this.options.extensions.some((function(t){return t.elementId}))&&(console.log("aminofx: 🎰 开始处理插槽列表"),this.options.extensions.forEach((function(e,n){var o=e.slotId,r=e.elementId;o&&r?t.moveElementToSlot(o,r,n):console.warn("aminofx: ⚠️ 插槽配置 ".concat(n+1," 缺少 slotId 或 elementId"))})))}},{key:"moveElementToSlot",value:function(t,e,n){var o=this.container.querySelector("#".concat(t));if(o){var r=document.getElementById(e);r?this.performMove(o,r,n):(console.log("aminofx: ⏳ 插槽 ".concat(n+1,' 的 elementId "').concat(e,'" 尚未挂载,等待中...')),this.observeElementMount(t,e,o,n))}else console.warn("aminofx: ⚠️ 插槽 ".concat(n+1,' 的 slotId "').concat(t,'" 在弹窗中未找到'))}},{key:"performMove",value:function(t,e,n){try{t.appendChild(e),console.log("aminofx: ✅ 插槽 ".concat(n+1," 元素移动成功: ").concat(e.id," -> ").concat(t.id))}catch(t){console.error("aminofx: ❌ 插槽 ".concat(n+1," 元素移动失败:"),t)}}},{key:"observeElementMount",value:function(t,e,n,o){var r=this,i=new MutationObserver((function(i,a){var s=document.getElementById(e);if(s){console.log("aminofx: 🎯 插槽 ".concat(o+1,' 检测到 elementId "').concat(e,'" 已挂载')),r.container.contains(n)?r.performMove(n,s,o):console.warn("aminofx: ⚠️ 插槽 ".concat(o+1,' 的 slotId "').concat(t,'" 已不在 DOM 中')),a.disconnect();var c=r.slotObservers.indexOf(a);c>-1&&r.slotObservers.splice(c,1)}}));this.slotObservers.push(i),i.observe(document.body,{childList:!0,subtree:!0})}},{key:"cleanupSlotObservers",value:function(){this.slotObservers.forEach((function(t){t.disconnect()})),this.slotObservers=[],console.log("aminofx: 🧹 清理所有插槽观察器")}},{key:"handleLoop",value:function(){var t,e,n,o=this;this.options.autoLoop&&Number(null===(t=this.options.jsonSchema)||void 0===t?void 0:t.duration)&&(null!==this.loopTimerId&&(clearTimeout(this.loopTimerId),this.loopTimerId=null,console.log("aminofx: 🧹 清理旧的循环定时器")),this.loopTimerId=window.setTimeout((function(){o.container.style.display="none",window.setTimeout((function(){o.loopTimerId=null,o.play()}),100)}),Number(null===(e=this.options.jsonSchema)||void 0===e?void 0:e.duration)),console.log("aminofx: 🔄 启动循环定时器,时长: ".concat(null===(n=this.options.jsonSchema)||void 0===n?void 0:n.duration,"ms")))}},{key:"ready",value:(o=i()(e()().mark((function t(){var n=this;return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.isReady){t.next=3;break}return console.log("aminofx: ✅ 动画已准备就绪,直接返回"),t.abrupt("return",Promise.resolve());case 3:if(!this.readyPromise){t.next=6;break}return console.log("aminofx: ⏳ 正在准备中,等待现有Promise..."),t.abrupt("return",this.readyPromise);case 6:return this.readyPromise=new Promise(function(){var t=i()(e()().mark((function t(o,r){var a,s;return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,a=new Promise(function(){var t=i()(e()().mark((function t(o,r){var i,a,s,c,u;return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!n.options.jsonSchema){t.next=5;break}n.container.innerHTML=n.replacePlaceholders(n.options.jsonSchema.content),o(),t.next=24;break;case 5:if(null===(i=n.options)||void 0===i||!i.jsonUrl){t.next=23;break}return t.prev=6,t.next=9,fetch(null===(a=n.options)||void 0===a?void 0:a.jsonUrl);case 9:return c=t.sent,t.next=12,c.json();case 12:u=t.sent,n.options.jsonSchema=u,n.container.innerHTML=n.replacePlaceholders((null===(s=n.options.jsonSchema)||void 0===s?void 0:s.content)||""),o(),t.next=21;break;case 18:t.prev=18,t.t0=t.catch(6),r(t.t0);case 21:t.next=24;break;case 23:throw new Error("没有提供jsonSchema或jsonUrl");case 24:case"end":return t.stop()}}),t,null,[[6,18]])})));return function(e,n){return t.apply(this,arguments)}}()),s=v.loadImages(n.options.preloadImgList||[]),t.next=5,Promise.all([a,s]);case 5:n.isReady=!0,n.handleSlotList(),o(),console.log("aminofx: 🎉 动画准备完成,可以开始播放"),t.next=15;break;case 11:t.prev=11,t.t0=t.catch(0),r(t.t0),console.error("aminofx: ❌ 动画准备失败:",t.t0);case 15:case"end":return t.stop()}}),t,null,[[0,11]])})));return function(e,n){return t.apply(this,arguments)}}()),t.abrupt("return",this.readyPromise);case 8:case"end":return t.stop()}}),t,this)}))),function(){return o.apply(this,arguments)})},{key:"play",value:(n=i()(e()().mark((function t(){return e()().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(this.isReady){t.next=4;break}return console.warn("aminofx: ⚠️ 动画尚未准备就绪,自动调用ready()..."),t.next=4,this.ready();case 4:this.container.style.display="flex",this.options.autoLoop&&null===this.loopTimerId&&this.handleLoop();case 6:case"end":return t.stop()}}),t,this)}))),function(){return n.apply(this,arguments)})},{key:"destroy",value:function(){null!==this.loopTimerId&&(clearTimeout(this.loopTimerId),this.loopTimerId=null,console.log("aminofx: 🧹 销毁时清理循环定时器")),this.cleanupSlotObservers(),this.container.innerHTML="",this.isReady=!1,this.readyPromise=null,console.log("aminofx: 🗑️ AnimationPlayer 已销毁")}},{key:"updateSchema",value:function(t){this.destroy(),this.options=t,this.init()}}]),t}();function y(t,e){var n=document.getElementById(t);if(!n)throw new Error('Container with id "'.concat(t,'" not found'));var o=document.createElement("div"),r=new m(o,e);return n.appendChild(o),r}o.default=y,window.createCssAnimationPlayer=y}(),o}()}));
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -91,6 +91,7 @@ class AnimationPlayer {
|
|
|
91
91
|
private isReady = false;
|
|
92
92
|
private readyPromise: Promise<void> | null = null;
|
|
93
93
|
private loopTimerId: number | null = null; // 保存定时器ID,用于清理
|
|
94
|
+
private slotObservers: MutationObserver[] = []; // 保存 MutationObserver 实例,用于清理
|
|
94
95
|
|
|
95
96
|
constructor(container: HTMLElement, options: IPlayerOptions) {
|
|
96
97
|
this.container = container;
|
|
@@ -110,21 +111,6 @@ class AnimationPlayer {
|
|
|
110
111
|
backdropFilter: 'blur(10px)',
|
|
111
112
|
});
|
|
112
113
|
|
|
113
|
-
if (this.options.jsonSchema) {
|
|
114
|
-
this.container.innerHTML = this.replacePlaceholders(
|
|
115
|
-
this.options.jsonSchema.content,
|
|
116
|
-
);
|
|
117
|
-
} else if (this.options?.jsonUrl) {
|
|
118
|
-
// 通过url拉取资源
|
|
119
|
-
const response = await fetch(this.options?.jsonUrl);
|
|
120
|
-
const data = await response.json();
|
|
121
|
-
this.options.jsonSchema = data;
|
|
122
|
-
this.container.innerHTML = this.replacePlaceholders(
|
|
123
|
-
this.options.jsonSchema?.content || '',
|
|
124
|
-
);
|
|
125
|
-
} else {
|
|
126
|
-
throw new Error('没有提供jsonSchema或jsonUrl');
|
|
127
|
-
}
|
|
128
114
|
await this.ready();
|
|
129
115
|
}
|
|
130
116
|
|
|
@@ -152,6 +138,152 @@ class AnimationPlayer {
|
|
|
152
138
|
);
|
|
153
139
|
}
|
|
154
140
|
|
|
141
|
+
/**
|
|
142
|
+
* 处理插槽列表,将 elementId 对应的元素移动到 slotId 对应的插槽中
|
|
143
|
+
* 需要处理时序问题:slotId 需要弹窗渲染后才存在,elementId 可能尚未挂载
|
|
144
|
+
*/
|
|
145
|
+
private handleSlotList(): void {
|
|
146
|
+
if (!this.options.extensions || this.options.extensions.length === 0 || !this.options.extensions.some(item => item.slotId) || !this.options.extensions.some(item => item.elementId)) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
console.log('aminofx: 🎰 开始处理插槽列表');
|
|
151
|
+
|
|
152
|
+
this.options.extensions.forEach((slotItem, index) => {
|
|
153
|
+
const { slotId, elementId } = slotItem;
|
|
154
|
+
|
|
155
|
+
if (!slotId || !elementId) {
|
|
156
|
+
console.warn(
|
|
157
|
+
`aminofx: ⚠️ 插槽配置 ${index + 1} 缺少 slotId 或 elementId`,
|
|
158
|
+
);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
this.moveElementToSlot(slotId, elementId, index);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 将元素移动到插槽中
|
|
168
|
+
* @param slotId - 插槽 DOM id
|
|
169
|
+
* @param elementId - 元素 DOM id
|
|
170
|
+
* @param index - 插槽索引,用于日志
|
|
171
|
+
*/
|
|
172
|
+
private moveElementToSlot(
|
|
173
|
+
slotId: string,
|
|
174
|
+
elementId: string,
|
|
175
|
+
index: number,
|
|
176
|
+
): void {
|
|
177
|
+
const slotElement = this.container.querySelector(`#${slotId}`);
|
|
178
|
+
|
|
179
|
+
if (!slotElement) {
|
|
180
|
+
console.warn(
|
|
181
|
+
`aminofx: ⚠️ 插槽 ${index + 1} 的 slotId "${slotId}" 在弹窗中未找到`,
|
|
182
|
+
);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const targetElement = document.getElementById(elementId);
|
|
187
|
+
|
|
188
|
+
if (targetElement) {
|
|
189
|
+
// elementId 已存在,直接移动
|
|
190
|
+
this.performMove(slotElement as HTMLElement, targetElement, index);
|
|
191
|
+
} else {
|
|
192
|
+
// elementId 尚未挂载,添加 MutationObserver 监听
|
|
193
|
+
console.log(
|
|
194
|
+
`aminofx: ⏳ 插槽 ${index + 1} 的 elementId "${elementId}" 尚未挂载,等待中...`,
|
|
195
|
+
);
|
|
196
|
+
this.observeElementMount(
|
|
197
|
+
slotId,
|
|
198
|
+
elementId,
|
|
199
|
+
slotElement as HTMLElement,
|
|
200
|
+
index,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 执行元素移动操作
|
|
207
|
+
* @param slotElement - 插槽元素
|
|
208
|
+
* @param targetElement - 目标元素
|
|
209
|
+
* @param index - 插槽索引,用于日志
|
|
210
|
+
*/
|
|
211
|
+
private performMove(
|
|
212
|
+
slotElement: HTMLElement,
|
|
213
|
+
targetElement: HTMLElement,
|
|
214
|
+
index: number,
|
|
215
|
+
): void {
|
|
216
|
+
try {
|
|
217
|
+
slotElement.appendChild(targetElement);
|
|
218
|
+
console.log(
|
|
219
|
+
`aminofx: ✅ 插槽 ${index + 1} 元素移动成功: ${targetElement.id} -> ${slotElement.id}`,
|
|
220
|
+
);
|
|
221
|
+
} catch (error) {
|
|
222
|
+
console.error(`aminofx: ❌ 插槽 ${index + 1} 元素移动失败:`, error);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* 使用 MutationObserver 监听 elementId 元素的挂载
|
|
228
|
+
* @param slotId - 插槽 DOM id
|
|
229
|
+
* @param elementId - 元素 DOM id
|
|
230
|
+
* @param slotElement - 插槽元素
|
|
231
|
+
* @param index - 插槽索引,用于日志
|
|
232
|
+
*/
|
|
233
|
+
private observeElementMount(
|
|
234
|
+
slotId: string,
|
|
235
|
+
elementId: string,
|
|
236
|
+
slotElement: HTMLElement,
|
|
237
|
+
index: number,
|
|
238
|
+
): void {
|
|
239
|
+
const observer = new MutationObserver((mutations, obs) => {
|
|
240
|
+
const targetElement = document.getElementById(elementId);
|
|
241
|
+
|
|
242
|
+
if (targetElement) {
|
|
243
|
+
console.log(
|
|
244
|
+
`aminofx: 🎯 插槽 ${index + 1} 检测到 elementId "${elementId}" 已挂载`,
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
// 再次检查 slotElement 是否仍然有效(防止弹窗已销毁)
|
|
248
|
+
if (this.container.contains(slotElement)) {
|
|
249
|
+
this.performMove(slotElement, targetElement, index);
|
|
250
|
+
} else {
|
|
251
|
+
console.warn(
|
|
252
|
+
`aminofx: ⚠️ 插槽 ${index + 1} 的 slotId "${slotId}" 已不在 DOM 中`,
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// 停止观察
|
|
257
|
+
obs.disconnect();
|
|
258
|
+
// 从数组中移除
|
|
259
|
+
const observerIndex = this.slotObservers.indexOf(obs);
|
|
260
|
+
if (observerIndex > -1) {
|
|
261
|
+
this.slotObservers.splice(observerIndex, 1);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
// 保存 observer 实例,用于销毁时清理
|
|
267
|
+
this.slotObservers.push(observer);
|
|
268
|
+
|
|
269
|
+
// 观察整个 document.body 的子树变化
|
|
270
|
+
observer.observe(document.body, {
|
|
271
|
+
childList: true,
|
|
272
|
+
subtree: true,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* 清理所有 MutationObserver
|
|
278
|
+
*/
|
|
279
|
+
private cleanupSlotObservers(): void {
|
|
280
|
+
this.slotObservers.forEach((observer) => {
|
|
281
|
+
observer.disconnect();
|
|
282
|
+
});
|
|
283
|
+
this.slotObservers = [];
|
|
284
|
+
console.log('aminofx: 🧹 清理所有插槽观察器');
|
|
285
|
+
}
|
|
286
|
+
|
|
155
287
|
private handleLoop() {
|
|
156
288
|
if (!this.options.autoLoop || !Number(this.options.jsonSchema?.duration)) {
|
|
157
289
|
return;
|
|
@@ -189,15 +321,49 @@ class AnimationPlayer {
|
|
|
189
321
|
return this.readyPromise;
|
|
190
322
|
}
|
|
191
323
|
|
|
192
|
-
this.readyPromise = new Promise<void>(async (resolve) => {
|
|
324
|
+
this.readyPromise = new Promise<void>(async (resolve, reject) => {
|
|
193
325
|
try {
|
|
194
|
-
|
|
326
|
+
const jsonPromise = new Promise<void>(async (resolve, reject) => {
|
|
327
|
+
// 优先使用jsonSchema
|
|
328
|
+
if (this.options.jsonSchema) {
|
|
329
|
+
this.container.innerHTML = this.replacePlaceholders(
|
|
330
|
+
this.options.jsonSchema.content,
|
|
331
|
+
);
|
|
332
|
+
resolve();
|
|
333
|
+
} else if (this.options?.jsonUrl) {
|
|
334
|
+
try {
|
|
335
|
+
// 通过url拉取资源
|
|
336
|
+
const response = await fetch(this.options?.jsonUrl);
|
|
337
|
+
const data = await response.json();
|
|
338
|
+
this.options.jsonSchema = data;
|
|
339
|
+
this.container.innerHTML = this.replacePlaceholders(
|
|
340
|
+
this.options.jsonSchema?.content || '',
|
|
341
|
+
);
|
|
342
|
+
resolve();
|
|
343
|
+
} catch (error) {
|
|
344
|
+
reject(error);
|
|
345
|
+
}
|
|
346
|
+
} else {
|
|
347
|
+
throw new Error('没有提供jsonSchema或jsonUrl');
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
const imgPromise = ResourceLoader.loadImages(
|
|
352
|
+
this.options.preloadImgList || [],
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
await Promise.all([jsonPromise, imgPromise]);
|
|
195
356
|
this.isReady = true; // 设置准备完成状态
|
|
357
|
+
|
|
358
|
+
// 处理插槽列表(此时弹窗内容已渲染,slotId 存在)
|
|
359
|
+
this.handleSlotList();
|
|
360
|
+
|
|
361
|
+
resolve();
|
|
196
362
|
console.log('aminofx: 🎉 动画准备完成,可以开始播放');
|
|
197
363
|
} catch (error) {
|
|
364
|
+
reject(error);
|
|
198
365
|
console.error('aminofx: ❌ 动画准备失败:', error);
|
|
199
366
|
}
|
|
200
|
-
resolve();
|
|
201
367
|
});
|
|
202
368
|
|
|
203
369
|
return this.readyPromise;
|
|
@@ -225,6 +391,9 @@ class AnimationPlayer {
|
|
|
225
391
|
console.log('aminofx: 🧹 销毁时清理循环定时器');
|
|
226
392
|
}
|
|
227
393
|
|
|
394
|
+
// 清理插槽观察器
|
|
395
|
+
this.cleanupSlotObservers();
|
|
396
|
+
|
|
228
397
|
// 重置状态
|
|
229
398
|
this.container.innerHTML = '';
|
|
230
399
|
this.isReady = false;
|
package/src/type.ts
CHANGED
|
@@ -17,6 +17,20 @@ interface IPlayerOptions {
|
|
|
17
17
|
autoLoop?: boolean;
|
|
18
18
|
// 动态变量
|
|
19
19
|
dynamicData?: Record<string, string | number | boolean>;
|
|
20
|
+
// 扩展列表
|
|
21
|
+
extensions?: {
|
|
22
|
+
// 插槽 dom id
|
|
23
|
+
slotId?: string;
|
|
24
|
+
// 元素 dom id
|
|
25
|
+
elementId?: string;
|
|
26
|
+
// 点击区域
|
|
27
|
+
clickAreas?: {
|
|
28
|
+
x?: number;
|
|
29
|
+
y?: number;
|
|
30
|
+
width?: number;
|
|
31
|
+
height?: number;
|
|
32
|
+
};
|
|
33
|
+
}[];
|
|
20
34
|
}
|
|
21
35
|
|
|
22
36
|
export type { IPlayerOptions, IJsonSchema };
|