a11y-player
Version:
An accessible DAISY format audiobook player for React applications
2 lines (1 loc) • 21.8 kB
JavaScript
(function(S,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("react/jsx-runtime"),require("react"),require("react-router-dom")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react","react-router-dom"],o):(S=typeof globalThis<"u"?globalThis:S||self,o(S.DaisyPlayerReact={},S.jsxRuntime,S.React,S.ReactRouterDOM))})(this,function(S,o,i,de){"use strict";const R=e=>{const r=(t,a)=>{const n={...a,parent:t.parentSmil||null};if(a.children){const l=a.children.reduce(r,{flattened:[...t.flattened,n],parentSmil:n.smilFile});return{...t,flattened:l.flattened}}return{...t,flattened:[...t.flattened,n]}};return e.reduce(r,{flattened:[],parentSmil:null}).flattened},J=e=>!e||e.flat.length<=0?null:e.flat[0],ee=e=>{const r=J(e);return r&&r.smilFile},fe=(e,r)=>{const t=(a,n)=>{if(a.found)return a;if(n.smilFile===r)return{...a,found:n};if(n.children){const l=n.children.reduce(t,{found:null,previous:n});if(l.found)return l}return{found:null,previous:n}};return e.reduce(t,{found:null,previous:null})},$=(e,r)=>{const t=e.findIndex(a=>a===r)+1;return t<=e.length-1?e[t]:null},W=(e,r)=>{const t=e.findIndex(a=>a===r)-1;return t>=0?e[t]:null},te=(e,r)=>e.find(t=>t.smilFile===r)||null,he=(e,r,t)=>{const a=i.useRef(new Audio),[n,l]=i.useState(null),[c,d]=i.useState(null),[u,P]=i.useState(!1),[y,f]=i.useState(null),[v,_]=i.useState(0),g=(n==null?void 0:n.smilFile)||ee(r),[E,m]=i.useState(!1),s=a.current,T=(h,k,C=0)=>{if(!h)return;const A=()=>{d(C),l(h),k&&!u&&P(!0)};if(h===n){A();return}m(!0),fetch(`${e}/${h.smilFile}`).then(N=>N.text()).then(N=>{var z;const j=(z=new DOMParser().parseFromString(N,"application/xml").querySelector("audio"))==null?void 0:z.getAttribute("src");if(!a.current||!j)return;const U=a.current;U.src=`${e}/${j}`,A()}).catch(N=>{console.error("Failed to fetch SMIL file:",N),m(!1)})};i.useEffect(()=>{const h=J(r);if(h){if(t){const[k,C]=t.split(":"),A=r.flat.find(N=>N.smilFile===k);if(A){T(A,!1,parseFloat(C));return}}T(h,!1,0)}},[r,t]);const x=i.useCallback((h,k=0)=>{if(n===null)return;const C=h==="prev"?W(r.flat,n):$(r.flat,n);C?T(C,u,k):h==="prev"?T(n,u,0):T(n,u,k)},[$,W,T,r,n,u]),w=i.useCallback(h=>{if(n===null||!s)return;const k=s.currentTime+h,C=k,A=k,N=k-s.duration,F=C>s.duration,B=C<0;if(F){x("next",N);return}else if(B){x("prev",A);return}d(C)},[n,s]),L=i.useCallback(()=>{if(s){if(y===null){f(s.playbackRate);return}s.playbackRate!==y&&(s.playbackRate=y)}},[s,y]),b=i.useCallback(()=>{if(c!==null&&!(n===null||!s)){if(E&&m(!1),c>s.duration){if($(r.flat,n)===null){s.currentTime=s.duration;return}x("next",c-s.duration);return}else if(c<0)if(s.duration+c<0){if(W(r.flat,n)===null){s.currentTime=0;return}x("prev",s.duration+c);return}else{s.currentTime=s.duration+c,d(null);return}s.currentTime=c,L(),d(null)}},[s,c,m]);return i.useEffect(()=>{c===null||E||b()},[c,E]),i.useEffect(()=>{L()},[s,y]),i.useEffect(()=>{n===null||!s||(u&&s.paused&&s.play(),!u&&!s.paused&&s.pause())},[s,n,u]),i.useEffect(()=>{if(n===null||!a.current)return;const h=a.current,k=()=>x("next",0),C=()=>_(h.currentTime||0);return h.addEventListener("loadedmetadata",b),h.addEventListener("ended",k),h.addEventListener("timeupdate",C),()=>{h.removeEventListener("loadedmetadata",b),h.removeEventListener("ended",k),h.removeEventListener("timeupdate",C)}},[n,r]),{audioRef:a,setAudioFor:T,currentSection:n,lastKnownBookmark:`${g}:${Math.floor(v)}`,isPlaying:u,togglePlayPause:()=>P(!u),currentTime:v,moveHeadAcrossBy:w,moveToPrevNextSection:x,playbackRate:y||1,setPlaybackRate:f}},q={en:{audioPlayer:"Audio Player",previousSection:"Previous Section",nextSection:"Next Section",play:"Play",pause:"Pause",backward30Seconds:"Backward 30 Seconds",forward30Seconds:"Forward 30 Seconds",speed:"Speed",decreasePlaybackRate:"Decrease playback rate",increasePlaybackRate:"Increase playback rate",toggleSectionsView:"Toggle sections view",shareLink:"Share link to current position",linkCopied:"Link copied!",failedToCopy:"Failed to copy",tableOfContents:"Table of Contents",noTitle:"No title",unknownAuthor:"Unknown author",defaultBookTitle:"Daisy player"},fr:{audioPlayer:"Lecteur Audio",previousSection:"Section Précédente",nextSection:"Section Suivante",play:"Lecture",pause:"Pause",backward30Seconds:"Reculer de 30 Secondes",forward30Seconds:"Avancer de 30 Secondes",speed:"Vitesse",decreasePlaybackRate:"Diminuer la vitesse de lecture",increasePlaybackRate:"Augmenter la vitesse de lecture",toggleSectionsView:"Afficher/Masquer les sections",shareLink:"Partager un lien vers la position actuelle",linkCopied:"Lien copié !",failedToCopy:"Échec de la copie",tableOfContents:"Table des matières",noTitle:"Sans titre",unknownAuthor:"Auteur inconnu",defaultBookTitle:"Lecteur Daisy"},de:{audioPlayer:"Audio-Player",previousSection:"Vorheriger Abschnitt",nextSection:"Nächster Abschnitt",play:"Abspielen",pause:"Pause",backward30Seconds:"30 Sekunden zurück",forward30Seconds:"30 Sekunden vorwärts",speed:"Geschwindigkeit",decreasePlaybackRate:"Wiedergabegeschwindigkeit verringern",increasePlaybackRate:"Wiedergabegeschwindigkeit erhöhen",toggleSectionsView:"Abschnittsansicht umschalten",shareLink:"Link zur aktuellen Position teilen",linkCopied:"Link kopiert!",failedToCopy:"Kopieren fehlgeschlagen",tableOfContents:"Inhaltsverzeichnis",noTitle:"Kein Titel",unknownAuthor:"Unbekannter Autor",defaultBookTitle:"Daisy-Player"},it:{audioPlayer:"Lettore Audio",previousSection:"Sezione Precedente",nextSection:"Sezione Successiva",play:"Riproduci",pause:"Pausa",backward30Seconds:"Indietro di 30 Secondi",forward30Seconds:"Avanti di 30 Secondi",speed:"Velocità",decreasePlaybackRate:"Diminuisci velocità di riproduzione",increasePlaybackRate:"Aumenta velocità di riproduzione",toggleSectionsView:"Attiva/disattiva vista sezioni",shareLink:"Condividi link alla posizione attuale",linkCopied:"Link copiato!",failedToCopy:"Copia fallita",tableOfContents:"Indice",noTitle:"Nessun titolo",unknownAuthor:"Autore sconosciuto",defaultBookTitle:"Lettore Daisy"},es:{audioPlayer:"Reproductor de Audio",previousSection:"Sección Anterior",nextSection:"Sección Siguiente",play:"Reproducir",pause:"Pausar",backward30Seconds:"Retroceder 30 Segundos",forward30Seconds:"Avanzar 30 Segundos",speed:"Velocidad",decreasePlaybackRate:"Disminuir velocidad de reproducción",increasePlaybackRate:"Aumentar velocidad de reproducción",toggleSectionsView:"Alternar vista de secciones",shareLink:"Compartir enlace a la posición actual",linkCopied:"¡Enlace copiado!",failedToCopy:"Error al copiar",tableOfContents:"Tabla de contenidos",noTitle:"Sin título",unknownAuthor:"Autor desconocido",defaultBookTitle:"Reproductor Daisy"}},re=e=>{const r=e.toLowerCase().substring(0,2);return q[r]?q[r]:q.en},V=e=>{const r=re(e);return t=>r[t]||q.en[t]},pe=({sectionsHolder:e,onSectionClick:r,currentSection:t,toggleDisplay:a,isDisplayed:n=!1,language:l="en"})=>{const c=V(l),[d,u]=i.useState(null),P=(t==null?void 0:t.smilFile)||null;i.useEffect(()=>{if(!n||d===t)return;u(t);const v=document.getElementsByClassName("Sections__Button--selected")[0];v&&(v.focus(),setTimeout(()=>v.scrollIntoView({behavior:"smooth",block:"center"}),100))},[t,n]);const y=(f,v,_=!1)=>o.jsx("ul",{className:`Sections__List Sections__List--level${v}`,"aria-hidden":!_,hidden:!_,children:f.map(g=>o.jsxs("li",{children:[o.jsx("button",{className:`Sections__Button${P===g.smilFile?" Sections__Button--selected":""}`,onClick:()=>r(te(e.flat,g.smilFile),!0,0),children:g.title}),g.children&&y(g.children,v+1,_)||null]},g.smilFile))});return o.jsxs("div",{className:`Sections__Container Sections__Container--${n?"visible":"hidden"}`,"aria-hidden":!n,hidden:!n,children:[o.jsx("div",{className:"Sections__BackArrow",onClick:a,role:"button","aria-label":"Close sections view",tabIndex:n?0:void 0,children:"←"}),o.jsx("h2",{children:c("tableOfContents")}),y(e.tree,0,n)]})},ye=e=>{const t=new DOMParser().parseFromString(e,"text/html"),a=Array.from(t.querySelectorAll("h1, h2, h3")),n=[],l=[];return a.forEach(c=>{var f,v;const d=parseInt(c.tagName[1]),u=((v=(f=c.querySelector("a"))==null?void 0:f.getAttribute("href"))==null?void 0:v.split("#")[0])??"",y={title:c.textContent??"Unknown Title",level:d,smilFile:u};for(;n.length&&n[n.length-1].level>=d;)n.pop();if(n.length){const _=n[n.length-1];_.children=_.children??[],_.children.push(y)}else l.push(y);n.push(y)}),{tree:l,flat:R(l)}},ve=e=>{var l,c;const t=new DOMParser().parseFromString(e,"text/html"),a=((l=t.querySelector('meta[name="dc:title"]'))==null?void 0:l.getAttribute("content"))??"Unknown Title",n=((c=t.querySelector('meta[name="dc:creator"]'))==null?void 0:c.getAttribute("content"))??"Unknown Author";return{title:a,author:n}},me=e=>{const[r,t]=i.useState({tree:[],flat:[]}),[a,n]=i.useState(null);return i.useEffect(()=>{fetch(`${e}/ncc.html`).then(l=>l.text()).then(l=>(n(ve(l)),l)).then(l=>{t(ye(l))}).catch(l=>console.error("Failed to fetch book sections:",l))},[e]),{sectionsHolder:r,bookInfo:a}};var ne={color:void 0,size:void 0,className:void 0,style:void 0,attr:void 0},ae=i.createContext&&i.createContext(ne),Se=["attr","size","title"];function be(e,r){if(e==null)return{};var t=Pe(e,r),a,n;if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(n=0;n<l.length;n++)a=l[n],!(r.indexOf(a)>=0)&&Object.prototype.propertyIsEnumerable.call(e,a)&&(t[a]=e[a])}return t}function Pe(e,r){if(e==null)return{};var t={};for(var a in e)if(Object.prototype.hasOwnProperty.call(e,a)){if(r.indexOf(a)>=0)continue;t[a]=e[a]}return t}function K(){return K=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var a in t)Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a])}return e},K.apply(this,arguments)}function oe(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);r&&(a=a.filter(function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable})),t.push.apply(t,a)}return t}function G(e){for(var r=1;r<arguments.length;r++){var t=arguments[r]!=null?arguments[r]:{};r%2?oe(Object(t),!0).forEach(function(a){ge(e,a,t[a])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):oe(Object(t)).forEach(function(a){Object.defineProperty(e,a,Object.getOwnPropertyDescriptor(t,a))})}return e}function ge(e,r,t){return r=we(r),r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function we(e){var r=ke(e,"string");return typeof r=="symbol"?r:r+""}function ke(e,r){if(typeof e!="object"||!e)return e;var t=e[Symbol.toPrimitive];if(t!==void 0){var a=t.call(e,r);if(typeof a!="object")return a;throw new TypeError("@@toPrimitive must return a primitive value.")}return(r==="string"?String:Number)(e)}function ie(e){return e&&e.map((r,t)=>i.createElement(r.tag,G({key:t},r.attr),ie(r.child)))}function O(e){return r=>i.createElement(Ce,K({attr:G({},e.attr)},r),ie(e.child))}function Ce(e){var r=t=>{var{attr:a,size:n,title:l}=e,c=be(e,Se),d=n||t.size||"1em",u;return t.className&&(u=t.className),e.className&&(u=(u?u+" ":"")+e.className),i.createElement("svg",K({stroke:"currentColor",fill:"currentColor",strokeWidth:"0"},t.attr,a,c,{className:u,style:G(G({color:e.color||t.color},t.style),e.style),height:d,width:d,xmlns:"http://www.w3.org/2000/svg"}),l&&i.createElement("title",null,l),e.children)};return ae!==void 0?i.createElement(ae.Consumer,null,t=>r(t)):r(ne)}function _e(e){return O({attr:{viewBox:"0 0 512 512"},child:[{tag:"path",attr:{d:"M80 368H16a16 16 0 0 0-16 16v64a16 16 0 0 0 16 16h64a16 16 0 0 0 16-16v-64a16 16 0 0 0-16-16zm0-320H16A16 16 0 0 0 0 64v64a16 16 0 0 0 16 16h64a16 16 0 0 0 16-16V64a16 16 0 0 0-16-16zm0 160H16a16 16 0 0 0-16 16v64a16 16 0 0 0 16 16h64a16 16 0 0 0 16-16v-64a16 16 0 0 0-16-16zm416 176H176a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h320a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16zm0-320H176a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h320a16 16 0 0 0 16-16V80a16 16 0 0 0-16-16zm0 160H176a16 16 0 0 0-16 16v32a16 16 0 0 0 16 16h320a16 16 0 0 0 16-16v-32a16 16 0 0 0-16-16z"},child:[]}]})(e)}function Ae(e){return O({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M416 208H32c-17.67 0-32 14.33-32 32v32c0 17.67 14.33 32 32 32h384c17.67 0 32-14.33 32-32v-32c0-17.67-14.33-32-32-32z"},child:[]}]})(e)}function Te(e){return O({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M144 479H48c-26.5 0-48-21.5-48-48V79c0-26.5 21.5-48 48-48h96c26.5 0 48 21.5 48 48v352c0 26.5-21.5 48-48 48zm304-48V79c0-26.5-21.5-48-48-48h-96c-26.5 0-48 21.5-48 48v352c0 26.5 21.5 48 48 48h96c26.5 0 48-21.5 48-48z"},child:[]}]})(e)}function Ne(e){return O({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M424.4 214.7L72.4 6.6C43.8-10.3 0 6.1 0 47.9V464c0 37.5 40.7 60.1 72.4 41.3l352-208c31.4-18.5 31.5-64.1 0-82.6z"},child:[]}]})(e)}function Fe(e){return O({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M416 208H272V64c0-17.67-14.33-32-32-32h-32c-17.67 0-32 14.33-32 32v144H32c-17.67 0-32 14.33-32 32v32c0 17.67 14.33 32 32 32h144v144c0 17.67 14.33 32 32 32h32c17.67 0 32-14.33 32-32V304h144c17.67 0 32-14.33 32-32v-32c0-17.67-14.33-32-32-32z"},child:[]}]})(e)}function xe(e){return O({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M352 320c-22.608 0-43.387 7.819-59.79 20.895l-102.486-64.054a96.551 96.551 0 0 0 0-41.683l102.486-64.054C308.613 184.181 329.392 192 352 192c53.019 0 96-42.981 96-96S405.019 0 352 0s-96 42.981-96 96c0 7.158.79 14.13 2.276 20.841L155.79 180.895C139.387 167.819 118.608 160 96 160c-53.019 0-96 42.981-96 96s42.981 96 96 96c22.608 0 43.387-7.819 59.79-20.895l102.486 64.054A96.301 96.301 0 0 0 256 416c0 53.019 42.981 96 96 96s96-42.981 96-96-42.981-96-96-96z"},child:[]}]})(e)}function Oe(e){return O({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M64 468V44c0-6.6 5.4-12 12-12h48c6.6 0 12 5.4 12 12v176.4l195.5-181C352.1 22.3 384 36.6 384 64v384c0 27.4-31.9 41.7-52.5 24.6L136 292.7V468c0 6.6-5.4 12-12 12H76c-6.6 0-12-5.4-12-12z"},child:[]}]})(e)}function Le(e){return O({attr:{viewBox:"0 0 448 512"},child:[{tag:"path",attr:{d:"M384 44v424c0 6.6-5.4 12-12 12h-48c-6.6 0-12-5.4-12-12V291.6l-195.5 181C95.9 489.7 64 475.4 64 448V64c0-27.4 31.9-41.7 52.5-24.6L312 219.3V44c0-6.6 5.4-12 12-12h48c6.6 0 12 5.4 12 12z"},child:[]}]})(e)}const Be=({appUrl:e,pathPrefix:r,path:t,language:a="en"})=>{const n=V(a),[l,c]=i.useState(null),[d,u]=i.useState(null),P=async()=>{try{const f=`${e}/${r}/${t}`;await navigator.clipboard.writeText(f),c(!0),u(t),setTimeout(()=>{c(null)},3e3)}catch(f){c(!1),console.error("Failed to copy text: ",f),setTimeout(()=>{c(null)},3e3)}};i.useEffect(()=>{d!==null&&d!==t&&(u(null),c(null))},[t]);const y=`ShareButton ${l===!0?"ShareButton--success":l===!1?"ShareButton--error":""}`;return o.jsx("button",{onClick:P,disabled:t===null,className:y,"aria-label":n("shareLink"),title:n("shareLink"),children:o.jsx("span",{children:l===!0?n("linkCopied"):l===!1?n("failedToCopy"):o.jsx(xe,{})})})};function le(e){return O({attr:{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"},child:[{tag:"path",attr:{d:"M19.933 13.041a8 8 0 1 1 -9.925 -8.788c3.899 -1 7.935 1.007 9.425 4.747"},child:[]},{tag:"path",attr:{d:"M20 4v5h-5"},child:[]}]})(e)}const Ee=({audioRef:e,title:r,playing:t,moveHeadAcrossBy:a,moveToPrevNextSection:n,currentTime:l,togglePlayPause:c,playbackRate:d,setPlaybackRate:u,isDisplayed:P=!0,language:y="en"})=>{const f=V(y),v=i.useRef(null),[_,g]=i.useState(0);i.useEffect(()=>{const m=e.current;m&&g(l/m.duration*100||0)},[l,e]);const E=i.useCallback(m=>{if(P)switch(m.key){case"k":case" ":m.preventDefault(),c();break;case"j":case"ArrowLeft":m.preventDefault(),a(-10);break;case"l":case"ArrowRight":m.preventDefault(),a(10);break;case"ArrowUp":m.preventDefault(),u(d+.1);break;case"ArrowDown":m.preventDefault(),u(d-.1);break}},[P,u,a,c,d]);return i.useEffect(()=>{if(!P)return;const s=document.getElementsByClassName("AudioPlayer__Control--play-pause")[0];s&&s.focus()},[P]),o.jsxs("div",{className:"AudioPlayer",role:"region","aria-label":f("audioPlayer"),ref:v,tabIndex:0,onKeyDown:E,children:[o.jsx("audio",{ref:e,"aria-label":r,preload:"metadata",className:"AudioPlayer__Audio"}),o.jsx("h2",{className:"AudioPlayer__Title",children:r}),o.jsxs("div",{className:"AudioPlayer__Controls",children:[o.jsxs("div",{className:"AudioPlayer__ControlColumns",children:[o.jsxs("div",{className:"AudioPlayer__ControlsColumn",children:[o.jsx("button",{className:"AudioPlayer__Control",onClick:()=>n("prev"),"aria-label":f("previousSection"),children:o.jsx(Oe,{})}),o.jsx("button",{className:"AudioPlayer__Control AudioPlayer__Control--mirrored",onClick:()=>a(-30),"aria-label":f("backward30Seconds"),children:o.jsx(le,{})})]}),o.jsx("div",{className:"AudioPlayer__ControlsColumn AudioPlayer__ControlsColumn--play",children:o.jsx("button",{className:`AudioPlayer__Control AudioPlayer__Control--play-pause AudioPlayer__Control--${t?"playing":"paused"}`,onClick:c,"aria-label":f(t?"pause":"play"),children:t?o.jsx(Te,{}):o.jsx(Ne,{})})}),o.jsxs("div",{className:"AudioPlayer__ControlsColumn AudioPlayer__ControlsColumn--reverse",children:[o.jsx("button",{className:"AudioPlayer__Control",onClick:()=>a(30),"aria-label":f("forward30Seconds"),children:o.jsx(le,{})}),o.jsx("button",{className:"AudioPlayer__Control",onClick:()=>n("next"),"aria-label":f("nextSection"),children:o.jsx(Le,{})})]})]}),o.jsxs("div",{className:"AudioPlayer__ControlsRow AudioPlayer__ControlsRow--speed",role:"group","aria-label":f("speed"),children:[o.jsx("button",{className:"AudioPlayer__Control",onClick:()=>u(d-.25),"aria-label":`${f("decreasePlaybackRate")} ${d-.25}`,children:o.jsx(Ae,{})}),o.jsxs("div",{className:"AudioPlayer__SpeedText","aria-live":"polite",children:[o.jsx("span",{children:f("speed")}),o.jsxs("strong",{children:[d," x"]})]}),o.jsx("button",{className:"AudioPlayer__Control",onClick:()=>u(d+.25),"aria-label":`${f("increasePlaybackRate")} ${d+.25}`,children:o.jsx(Fe,{})})]})]}),o.jsxs("div",{className:"AudioPlayer__ProgressContainer",children:[o.jsxs("progress",{className:"AudioPlayer__Progress",value:_,max:"100","aria-hidden":"true",children:[_,"%"]}),o.jsxs("span",{className:"AudioPlayer__Time","aria-live":"off",children:[Math.trunc(l)," / ",Math.round(e.current.duration)," s"]})]})]})},Q=i.forwardRef(({dirUrl:e,appUrl:r,pathPrefix:t,initialBookmark:a,className:n="",language:l="en",onTimeUpdate:c,timeUpdateInterval:d=1e3,onBookmarkChange:u,onPlaybackStateChange:P,onPlay:y,onPause:f,onSeek:v},_)=>{const g=V(l),E=i.useRef(null),{sectionsHolder:m,bookInfo:s}=me(e),{audioRef:T,setAudioFor:x,lastKnownBookmark:w,currentSection:L,isPlaying:b,togglePlayPause:h,moveHeadAcrossBy:k,moveToPrevNextSection:C,playbackRate:A,setPlaybackRate:N,currentTime:F}=he(e,m,a),[B,j]=i.useState("playerView"),[U,z]=i.useState(!1),[ce,De]=i.useState(""),I=i.useRef(null),D=i.useRef(c),ue=i.useRef(0);i.useEffect(()=>{D.current=c},[c]),i.useEffect(()=>{if(!D.current)return;const p=()=>{D.current&&D.current(F,w,b)};return I.current&&clearInterval(I.current),I.current=setInterval(p,d),()=>{I.current&&clearInterval(I.current)}},[d]),i.useEffect(()=>{if(!D.current)return;const p=Date.now();p-ue.current>=Math.min(d,1e3)&&(D.current(F,w,b),ue.current=p)},[F,w,b,d]),i.useEffect(()=>{w!==ce&&(De(w),u&&w&&u(w))},[w,ce,u]),i.useEffect(()=>{if(!P||!T.current||!L)return;const p=T.current;P({currentTime:F,duration:p.duration||0,isPlaying:b,currentBookmark:w,playbackRate:A})},[P,F,b,w,A,L]),i.useEffect(()=>{b&&y?y():!b&&f&&f()},[b,y,f]),i.useImperativeHandle(_,()=>({getCurrentTime:()=>F,getDuration:()=>{var p;return((p=T.current)==null?void 0:p.duration)||0},isPlaying:()=>b,getCurrentBookmark:()=>w,getPlaybackRate:()=>A,togglePlayPause:()=>h(),seek:p=>{const[Y,H]=p.split(":"),Z=m.flat.find(ze=>ze.smilFile===Y);Z&&(x(Z,b,parseFloat(H)||0),v&&v(p))}}),[F,b,w,A,h,m,x,v]),i.useEffect(()=>{B==="sectionsView"?b&&(z(!0),h()):B==="playerView"&&U&&!b&&(h(),z(!1))},[B,b,h,U]);const Ve=(p,Y,H)=>{if(x(p,Y,H),j("playerView"),v&&p){const Z=`${p.smilFile}:${Math.floor(H)}`;v(Z)}},M=()=>{j(p=>p==="sectionsView"?"playerView":"sectionsView")},X=B!=="playerView",je=i.useCallback(p=>{switch(p.key){case"i":p.preventDefault(),M();break}if(X)switch(p.key){case"Escape":p.preventDefault(),M();break}},[M,X]);return o.jsxs("div",{className:`DaisyPlayer__Container ${n}`,ref:E,tabIndex:0,onKeyDown:je,children:[o.jsxs("div",{className:"DaisyPlayer__OtherButtons",children:[o.jsx(Be,{appUrl:r,pathPrefix:t,path:w,language:l}),o.jsx("button",{className:"DaisyPlayer__ToggleSectionsViewButton",onClick:M,"aria-label":g("toggleSectionsView"),title:g("toggleSectionsView"),children:o.jsx(_e,{})})]}),o.jsx("h1",{children:(s==null?void 0:s.title)||g("defaultBookTitle")}),o.jsx("h3",{children:(s==null?void 0:s.author)||g("unknownAuthor")}),o.jsx("div",{className:"DaisyPlayer__PlayerContainer",children:o.jsx(Ee,{setPlaybackRate:N,playbackRate:A,moveToPrevNextSection:C,audioRef:T,title:(L==null?void 0:L.title)||g("noTitle"),playing:b,moveHeadAcrossBy:k,currentTime:F,togglePlayPause:h,isDisplayed:!X,language:l})}),o.jsx(pe,{isDisplayed:B==="sectionsView",toggleDisplay:M,sectionsHolder:m,onSectionClick:Ve,currentSection:L,language:l})]})});Q.displayName="DaisyPlayer";const se=i.forwardRef(({pathPrefix:e,bookmarkParam:r="bookmark",language:t="en",className:a="",...n},l)=>{const d=de.useParams()[r];return o.jsx(Q,{ref:l,initialBookmark:d,pathPrefix:e,language:t,className:a,...n})});se.displayName="DaisyPlayerWithRouter",S.DaisyPlayer=Q,S.DaisyPlayerWithRouter=se,S.createTranslator=V,S.flatFindBySmil=te,S.flatGetNext=$,S.flatGetPrev=W,S.flatten=R,S.getFirst=J,S.getFirstSmil=ee,S.getTranslations=re,S.treeFindBySmil=fe,Object.defineProperty(S,Symbol.toStringTag,{value:"Module"})});