UNPKG

mstf-kit

Version:

一个现代化的 JavaScript/TypeScript 工具库,提供了丰富的常用工具函数

2 lines (1 loc) 4.93 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@lottiefiles/dotlottie-web");async function t(t){const o=await async function(t={}){const o=t.source||"built-in";if("undefined"!=typeof window&&"DotLottie"in window)return window.DotLottie;switch(o){case"built-in":return"undefined"!=typeof window&&(window.DotLottie=e.DotLottie),e.DotLottie;case"external":if(!t.externalLib)throw new Error('External library not provided. Please provide externalLib when using source="external"');return"undefined"!=typeof window&&(window.DotLottie=t.externalLib),t.externalLib;case"cdn":return new Promise(((e,o)=>{const n=document.createElement("script");n.src=t.cdnURL||"https://cdn.jsdelivr.net/npm/@lottiefiles/dotlottie-web/+esm",n.type="module",n.async=!0,n.onload=()=>{const t=window.DotLottie;t?e(t):o(new Error("Failed to load DotLottie from CDN"))},n.onerror=()=>o(new Error("Failed to load DotLottie library from CDN")),document.head.appendChild(n)}));default:throw new Error(`Invalid library source: ${o}`)}}(t.libOptions),n=function(e){return e?"string"==typeof e?document.querySelector(e):e instanceof HTMLElement?e:e&&"value"in e&&e.value instanceof HTMLElement?e.value:null:null}(t.container);if(!n)throw new Error("Lottie container not found or invalid");const r={renderer:"svg",loop:!1,autoplay:!1,speed:1,direction:1,quality:1,libOptions:{source:"built-in"},...t};let i;n instanceof HTMLCanvasElement?i=n:(n.innerHTML="",i=document.createElement("canvas"),i.style.width="100%",i.style.height="100%",n.appendChild(i));let a="loading",l=null,s=null,d=null;return{async load(e){try{a="loading",d&&(d.destroy(),d=null),s=e;const n={canvas:i,autoplay:r.autoplay,loop:!0===r.loop||!1!==r.loop&&r.loop,speed:r.speed};if("string"==typeof e)if(e.startsWith("http")||e.startsWith("./")||e.startsWith("/")||e.endsWith(".json")||e.endsWith(".lottie"))n.src=e;else{if(!e.startsWith("{"))throw new Error("Invalid Lottie source");try{n.data=JSON.parse(e)}catch(t){throw new Error("Invalid Lottie JSON string")}}else n.data=e;d=new o(n),l=d,r.onComplete&&d.addEventListener("complete",r.onComplete),r.onLoopComplete&&d.addEventListener("loopComplete",r.onLoopComplete),r.onFrame&&d.addEventListener("frame",(e=>{var t,o;null==(o=r.onFrame)||o.call(r,(null==(t=e.detail)?void 0:t.frame)||0)})),r.onError&&d.addEventListener("error",(e=>{var t,o;null==(o=r.onError)||o.call(r,(null==(t=e.detail)?void 0:t.error)||new Error("Unknown error"))})),r.onLoad&&d.addEventListener("ready",r.onLoad),r.onPlay&&d.addEventListener("play",r.onPlay),r.onPause&&d.addEventListener("pause",r.onPause),r.onStop&&d.addEventListener("stop",r.onStop),r.onDestroy&&d.addEventListener("destroy",r.onDestroy),-1===r.direction&&d.setDirection(-1),r.initialSegment&&d.setSegments(r.initialSegment[0],r.initialSegment[1]),await new Promise((e=>{d.addEventListener("ready",(()=>{a=r.autoplay?"playing":"paused",e()}),{once:!0}),d.addEventListener("error",(t=>{a="error",e()}),{once:!0})}))}catch(n){throw a="error",r.onError&&r.onError(n instanceof Error?n:new Error("Failed to load animation")),n}},play(){d&&(d.play(),a="playing")},pause(){d&&(d.pause(),a="paused")},stop(){d&&(d.stop(),a="stopped")},goToFrame(e){d&&d.seek(`${Math.round(e)}/${this.getTotalFrames()}`)},goToAndPlay(e){d&&(this.goToFrame(e),this.play())},goToAndStop(e){d&&(this.goToFrame(e),this.pause())},setSpeed(e){d&&d.setSpeed(e)},setDirection(e){d&&d.setDirection(e)},setLoop(e){if(d)if("boolean"==typeof e)d.setLooping(e);else{let t=0;const o=e;d.removeEventListener("loopComplete"),e>0?(d.setLooping(!0),d.addEventListener("loopComplete",(()=>{t++,t>=o&&(d.setLooping(!1),d.pause(),a="paused")}))):d.setLooping(!1)}},getCurrentFrame(){var e;return d&&null!=(e=d.currentFrame)?e:0},getTotalFrames(){var e;return d&&null!=(e=d.totalFrames)?e:0},getDuration(){var e;return d?1e3*(null!=(e=d.duration)?e:0):0},getState:()=>a,isPlaying:()=>!!d&&(d.isPlaying()||"playing"===a),setSegment(e,t){d&&d.setSegments(e,t)},setQuality(e){},getAnimationInstance:()=>d,resize(){d&&d.resize()},destroy(){d&&(d.removeEventListener("complete"),d.removeEventListener("loopComplete"),d.removeEventListener("frame"),d.removeEventListener("error"),d.removeEventListener("ready"),d.removeEventListener("play"),d.removeEventListener("pause"),d.removeEventListener("stop"),d.removeEventListener("destroy"),d.destroy(),d=null,l=null,a="destroyed")}}}exports.createLottiePlayer=t,exports.playLottie=async function(e,o,n={}){const r=await t({container:o,autoplay:void 0===n.autoplay||n.autoplay,loop:void 0===n.loop||n.loop,renderer:n.renderer||"svg",speed:n.speed||1,direction:n.direction||1,quality:n.quality||1,initialSegment:n.initialSegment,libOptions:n.libOptions,onLoad:n.onLoad,onComplete:n.onComplete,onLoopComplete:n.onLoopComplete,onFrame:n.onFrame,onPlay:n.onPlay,onPause:n.onPause,onStop:n.onStop,onError:n.onError,onDestroy:n.onDestroy});return await r.load(e),r};