@ktt45678/vidstack
Version:
UI component library for building high-quality, accessible video and audio experiences on the web.
2 lines (1 loc) • 4.6 kB
JavaScript
import{a5 as w,l,G as E,i as f,E as C,D as d,a as p,k as m}from"./vidstack-B1PqmCkk.js";import{parseText as S}from"https://cdn.jsdelivr.net/npm/media-captions@next/dist/prod.js";import{g as B}from"./vidstack-Ds9fx5AB.js";const k=Symbol(0),x=Symbol(0),O=Symbol(0),$=Symbol(0),j=Symbol(0),F=Symbol(0),_=Symbol(0),n={Db:k,ma:x,Eb:O,Z:$,hb:j,_:F,Mf:_};function g(r,t){return t>=r.startTime&&t<r.endTime}function b(r,t,e){let s=null,a=w();function i(){const h=f(t)?[t]:t,o=r.toArray().find(c=>h.includes(c.kind)&&c.mode==="showing");if(o!==s){if(!o){e(null),s=null;return}o.readyState==2?e(o):(e(null),E(()=>{const c=l(o,"load",()=>{e(o),c()},{once:!0})},a)),s=o}}return i(),l(r,"mode-change",i)}function N(r,t,e){b(r,t,s=>{if(!s){e("");return}const a=()=>{const i=s?.activeCues[0];e(i?.text||"")};a(),l(s,"cue-change",a)})}var y,T,v;class R extends C{constructor(t){super(),this.id="",this.label="",this.language="",this.default=!1,this.contentLoaded=!1,this.Z=!1,this.ua=0,this.U="disabled",this.Nf={},this.$c=[],this.B=[],this.Fb=[],this[v]=0,this[T]=null,this[y]=null;for(const e of Object.keys(t))this[e]=t[e];this.type||(this.type="vtt"),t.content?this._h(t):t.src||(this[n.ma]=2)}static createId(t){return`vds-${t.type}-${t.kind}-${t.src??t.label??"?"}`}get metadata(){return this.Nf}get regions(){return this.$c}get cues(){return this.B}get activeCues(){return this.Fb}get readyState(){return this[n.ma]}get mode(){return this.U}set mode(t){this.setMode(t)}addCue(t,e){let s=0,a=this.B.length;for(s=0;s<a&&!(t.endTime<=this.B[s].startTime);s++);s===a?this.B.push(t):this.B.splice(s,0,t),t instanceof TextTrackCue||this[n._]?.track.addCue(t),this.dispatchEvent(new d("add-cue",{detail:t,trigger:e})),g(t,this.ua)&&this[n.Eb](this.ua,e)}removeCue(t,e){const s=this.B.indexOf(t);if(s>=0){const a=this.Fb.includes(t);this.B.splice(s,1),this[n._]?.track.removeCue(t),this.dispatchEvent(new d("remove-cue",{detail:t,trigger:e})),a&&this[n.Eb](this.ua,e)}}setMode(t,e){this.U!==t&&(this.U=t,t==="disabled"?(this.Fb=[],this.Of()):this.readyState===2?this[n.Eb](this.ua,e):this.Pf(),this.dispatchEvent(new d("mode-change",{detail:this,trigger:e})),this[n.hb]?.())}[(v=n.ma,T=n.hb,y=n._,n.Eb)](t,e){if(this.ua=t,this.mode==="disabled"||!this.B.length)return;const s=[];for(let i=0,h=this.B.length;i<h;i++){const o=this.B[i];g(o,t)&&s.push(o)}let a=s.length!==this.Fb.length;if(!a){for(let i=0;i<s.length;i++)if(!this.Fb.includes(s[i])){a=!0;break}}this.Fb=s,a&&this.Of(e)}[n.Z](){this.Z=!0,this.U!=="disabled"&&this.Pf()}_h(t){import("https://cdn.jsdelivr.net/npm/media-captions@next/dist/prod.js").then(({parseText:e,VTTCue:s,VTTRegion:a})=>{!f(t.content)||t.type==="json"?(this.Qf(t.content,s,a),this.readyState!==3&&this.Ga()):e(t.content,{type:t.type}).then(({cues:i,regions:h})=>{this.B=i,this.$c=h,this.Ga()})})}async Pf(){if(!(!this.Z||this[n.ma]>0)){if(this[n.ma]=1,this.dispatchEvent(new d("load-start")),!this.src){this.Ga();return}try{const{parseResponse:t,VTTCue:e,VTTRegion:s}=await import("https://cdn.jsdelivr.net/npm/media-captions@next/dist/prod.js"),a=this[n.Db]?.();if(!this.subtitleLoader||typeof this.subtitleLoader!="function"){const i=fetch(this.src,{headers:this.type==="json"?{"Content-Type":"application/json"}:void 0,credentials:B(a)});if(this.type==="json")this.Qf(await(await i).text(),e,s);else{const{errors:h,metadata:o,regions:c,cues:u}=await t(i,{type:this.type,encoding:this.encoding});if(h[0]?.code===0)throw h[0];this.Nf=o,this.$c=c,this.B=u}}else{const i=await Promise.resolve(this.subtitleLoader(this));if(!i)throw new Error("Subtitle loaded error");if(this.content=i,this.contentLoaded=!0,this.type==="json")this.Qf(i,e,s);else{const{errors:h,metadata:o,regions:c,cues:u}=await S(i,{type:this.type});if(h[0]?.code===0)throw h[0];this.Nf=o,this.$c=c,this.B=u}}this.Ga()}catch(t){this.Rf(t)}}}Ga(){if(this[n.ma]=2,!this.src||this.type!=="vtt"){const e=this[n._];if(e&&!e.managed)for(const s of this.B)e.track.addCue(s)}const t=new d("load");this[n.Eb](this.ua,t),this.dispatchEvent(t)}Rf(t){this[n.ma]=3,this.dispatchEvent(new d("error",{detail:t}))}Qf(t,e,s){try{const{regions:a,cues:i}=L(t,e,s);this.$c=a,this.B=i}catch(a){this.Rf(a)}}Of(t){this.dispatchEvent(new d("cue-change",{trigger:t}))}}const G=/captions|subtitles/;function A(r){return G.test(r.kind)}function L(r,t,e){const s=f(r)?JSON.parse(r):r;let a=[],i=[];return s.regions&&e&&(a=s.regions.map(h=>Object.assign(new e,h))),(s.cues||p(s))&&(i=(p(s)?s:s.cues).filter(h=>m(h.startTime)&&m(h.endTime)).map(h=>Object.assign(new t(0,0,""),h))),{regions:a,cues:i}}export{n as T,R as a,b,g as c,A as i,N as w};