UNPKG

@7sage/vidstack

Version:

UI component library for building high-quality, accessible video and audio experiences on the web.

2 lines (1 loc) 4.32 kB
import{l as u,n as C,q as m,i as l,d as S,D as c,a as p,F as g}from"./vidstack-CYF5HBsg.js";import{g as T}from"./vidstack-BH4h6ty-.js";const b=Symbol(0),E=Symbol(0),A=Symbol(0),w=Symbol(0),O=Symbol(0),x=Symbol(0),N=Symbol(0),a={crossOrigin:b,readyState:E,updateActiveCues:A,canLoad:w,onModeChange:O,native:x,nativeHLS:N};function f(h,t){return t>=h.startTime&&t<h.endTime}function v(h,t,s){let e=null,i=m();function n(){const r=l(t)?[t]:t,o=h.toArray().find(d=>r.includes(d.kind)&&d.mode==="showing");if(o!==e){if(!o){s(null),e=null;return}o.readyState==2?s(o):(s(null),C(()=>{const d=u(o,"load",()=>{s(o),d()},{once:!0})},i)),e=o}}return n(),u(h,"mode-change",n)}function k(h,t,s){v(h,t,e=>{if(!e){s("");return}const i=()=>{const n=e?.activeCues[0];s(n?.text||"")};i(),u(e,"cue-change",i)})}class M extends S{static createId(t){return`vds-${t.type}-${t.kind}-${t.src??t.label??"?"}`}src;content;type;encoding;id="";label="";language="";kind;default=!1;#r=!1;#e=0;#i="disabled";#h={};#n=[];#t=[];#s=[];[a.readyState]=0;[a.crossOrigin];[a.onModeChange]=null;[a.native]=null;get metadata(){return this.#h}get regions(){return this.#n}get cues(){return this.#t}get activeCues(){return this.#s}get readyState(){return this[a.readyState]}get mode(){return this.#i}set mode(t){this.setMode(t)}constructor(t){super();for(const s of Object.keys(t))this[s]=t[s];this.type||(this.type="vtt"),t.content?this.#l(t):t.src||(this[a.readyState]=2)}addCue(t,s){let e=0,i=this.#t.length;for(e=0;e<i&&!(t.endTime<=this.#t[e].startTime);e++);e===i?this.#t.push(t):this.#t.splice(e,0,t),t instanceof TextTrackCue||this[a.native]?.track.addCue(t),this.dispatchEvent(new c("add-cue",{detail:t,trigger:s})),f(t,this.#e)&&this[a.updateActiveCues](this.#e,s)}removeCue(t,s){const e=this.#t.indexOf(t);if(e>=0){const i=this.#s.includes(t);this.#t.splice(e,1),this[a.native]?.track.removeCue(t),this.dispatchEvent(new c("remove-cue",{detail:t,trigger:s})),i&&this[a.updateActiveCues](this.#e,s)}}setMode(t,s){this.#i!==t&&(this.#i=t,t==="disabled"?(this.#s=[],this.#u()):this.readyState===2?this[a.updateActiveCues](this.#e,s):this.#o(),this.dispatchEvent(new c("mode-change",{detail:this,trigger:s})),this[a.onModeChange]?.())}[a.updateActiveCues](t,s){if(this.#e=t,this.mode==="disabled"||!this.#t.length)return;const e=[];for(let n=0,r=this.#t.length;n<r;n++){const o=this.#t[n];f(o,t)&&e.push(o)}let i=e.length!==this.#s.length;if(!i){for(let n=0;n<e.length;n++)if(!this.#s.includes(e[n])){i=!0;break}}this.#s=e,i&&this.#u(s)}[a.canLoad](){this.#r=!0,this.#i!=="disabled"&&this.#o()}#l(t){import("https://cdn.jsdelivr.net/npm/media-captions@next/dist/prod.js").then(({parseText:s,VTTCue:e,VTTRegion:i})=>{!l(t.content)||t.type==="json"?(this.#d(t.content,e,i),this.readyState!==3&&this.#a()):s(t.content,{type:t.type}).then(({cues:n,regions:r})=>{this.#t=n,this.#n=r,this.#a()})})}async#o(){if(!(!this.#r||this[a.readyState]>0)){if(this[a.readyState]=1,this.dispatchEvent(new c("load-start")),!this.src){this.#a();return}try{const{parseResponse:t,VTTCue:s,VTTRegion:e}=await import("https://cdn.jsdelivr.net/npm/media-captions@next/dist/prod.js"),i=this[a.crossOrigin]?.(),n=fetch(this.src,{headers:this.type==="json"?{"Content-Type":"application/json"}:void 0,credentials:T(i)});if(this.type==="json")this.#d(await(await n).text(),s,e);else{const{errors:r,metadata:o,regions:d,cues:y}=await t(n,{type:this.type,encoding:this.encoding});if(r[0]?.code===0)throw r[0];this.#h=o,this.#n=d,this.#t=y}this.#a()}catch(t){this.#c(t)}}}#a(){if(this[a.readyState]=2,!this.src||this.type!=="vtt"){const s=this[a.native];if(s&&!s.managed)for(const e of this.#t)s.track.addCue(e)}const t=new c("load");this[a.updateActiveCues](this.#e,t),this.dispatchEvent(t)}#c(t){this[a.readyState]=3,this.dispatchEvent(new c("error",{detail:t}))}#d(t,s,e){try{const{regions:i,cues:n}=I(t,s,e);this.#n=i,this.#t=n}catch(i){this.#c(i)}}#u(t){this.dispatchEvent(new c("cue-change",{trigger:t}))}}const _=/captions|subtitles/;function D(h){return _.test(h.kind)}function I(h,t,s){const e=l(h)?JSON.parse(h):h;let i=[],n=[];return e.regions&&s&&(i=e.regions.map(r=>Object.assign(new s,r))),(e.cues||p(e))&&(n=(p(e)?e:e.cues).filter(r=>g(r.startTime)&&g(r.endTime)).map(r=>Object.assign(new t(0,0,""),r))),{regions:i,cues:n}}export{a as T,M as a,v as b,f as c,D as i,k as w};