nzk-react-components 3.0.7 → 3.1.0
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("
|
|
1
|
+
"use strict";var e=require("../../_virtual/_tslib.js"),r=require("react"),t=require("./hooks/useTimeout.js"),n=require("../Button/Button.js"),u=require("../../icons/Megaphone.js"),l=require("../../icons/PurpleMegaphone.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=o(r);exports.SyncedText=function(o){var i=o.value,c=r.useRef(null),s=r.useRef(),d=r.useRef(null),f=r.useRef(!1),v=r.useRef([]),p=r.useRef([]),m=r.useState(!1),y=m[0],h=m[1],g=t.useAudioTimeout({timeoutMs:o.timeoutMs}),E=g[0],R=g[1],q=function(e){return R(),h(!1),o.onError?o.onError({type:"PLAY_ERROR",error:e}):null},A=function(e){return R(),h(!1),o.onError?o.onError({type:"LOAD_ERROR",error:e}):null},_=function(){var e;return null===(e=c.current)||void 0===e||e.stop(),h(!1),o.onError?o.onError({type:"TIMEOUT"}):null},w=function(){var e,r;(null===(e=c.current)||void 0===e?void 0:e.playing())||(E(_),null===(r=c.current)||void 0===r||r.play())};r.useEffect((function(){return i.audio&&o.Howl&&(c.current=new o.Howl({src:i.audio,autoplay:o.autoPlay}),o.autoPlay&&E(_),o.setRef&&o.setRef({play:w,getHowl:function(){return c.current},isPlaying:function(){var e;return null===(e=c.current)||void 0===e?void 0:e.playing()},pause:function(){var e;return null===(e=c.current)||void 0===e?void 0:e.pause()},stop:function(){var e;return null===(e=c.current)||void 0===e?void 0:e.stop()}})),function(){var e;R(),null===(e=c.current)||void 0===e||e.stop()}}),[]);var x=function(e){c.current&&d.current&&v.current[0]&&e>parseFloat(v.current[0].getAttribute("data-start")||"0")&&(p.current.forEach((function(e){e.classList.contains("highlighted")&&(e.classList.remove("highlighted"),e.classList.add("has-highlighted"))})),v.current[0].classList.add("highlighted"),v.current=v.current.slice(1,v.current.length))},L=function(){f.current=!0,v.current=p.current,h(!0);var e=function(){var r;if(null===(r=c.current)||void 0===r?void 0:r.playing()){R();var t=c.current.seek();x(t),s.current=requestAnimationFrame(e)}else s&&cancelAnimationFrame(s.current)};s.current=requestAnimationFrame(e)},M=function(){cancelAnimationFrame(s.current),h(!1),f.current&&(p.current.forEach((function(e){e.classList.remove("highlighted"),e.classList.remove("has-highlighted")})),o.onComplete&&o.onComplete())};r.useEffect((function(){var e,r,t,n,u,l;if(null===(e=c.current)||void 0===e||e.on("play",L),null===(r=c.current)||void 0===r||r.on("end",M),null===(t=c.current)||void 0===t||t.on("stop",M),null===(n=c.current)||void 0===n||n.on("playerror",q),null===(u=c.current)||void 0===u||u.on("loaderror",A),p.current){var o=null===(l=d.current)||void 0===l?void 0:l.querySelectorAll("span.synced-test--part");o&&(p.current=Array.from(o),v.current=p.current)}return function(){var e,r,t,n,u;M(),null===(e=c.current)||void 0===e||e.off("play",L),null===(r=c.current)||void 0===r||r.off("end",M),null===(t=c.current)||void 0===t||t.off("stop",M),null===(n=c.current)||void 0===n||n.off("playerror",q),null===(u=c.current)||void 0===u||u.off("loaderror",A)}}),[o.value]);var P=r.useMemo((function(){if(!o.showPlayButton)return null;var r={className:"synced-text--play",onClick:w,style:{marginRight:"10px"}};return o.playButton?a.default.createElement("div",e.__assign({},r,{"data-playing":y}),o.playButton):a.default.createElement(n,e.__assign({theme:y?"purple":"primary"},r,{size:"x-small",round:!0}),y?a.default.createElement(u,null):a.default.createElement(l,null))}),[o.showPlayButton,y]);return i?a.default.createElement("div",{ref:d,className:"synced-text"},P,i.sequences.map((function(e,r){return a.default.createElement("span",{className:"synced-test--part",style:{display:"inline-block"},"data-start":e.time[0],"data-end":e.time[1],key:e.time[0]},a.default.createElement("span",{className:"synced-text--word",dangerouslySetInnerHTML:{__html:"".concat(e.text)}}),r<i.sequences.length&&a.default.createElement("span",{className:"synced-text--space",style:{whiteSpace:"pre-wrap"}}," "))}))):null};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Howl } from 'howler';
|
|
2
|
+
import type { ReactElement } from 'react';
|
|
2
3
|
export interface ISequence {
|
|
3
4
|
text: string;
|
|
4
5
|
time: [number, number];
|
|
@@ -18,6 +19,7 @@ export interface ISyncedTextProps {
|
|
|
18
19
|
value: IAudioSync;
|
|
19
20
|
Howl: typeof Howl;
|
|
20
21
|
showPlayButton?: boolean;
|
|
22
|
+
playButton?: ReactElement;
|
|
21
23
|
setRef?: (ref: ISyncedTextRef) => void;
|
|
22
24
|
autoPlay?: boolean;
|
|
23
25
|
onComplete?: () => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import r,{useRef as
|
|
1
|
+
import{__assign as r}from"../../_virtual/_tslib.js";import e,{useRef as n,useState as t,useEffect as o,useMemo as u}from"react";import{useAudioTimeout as l}from"./hooks/useTimeout.js";import a from"../Button/Button.js";import i from"../../icons/Megaphone.js";import c from"../../icons/PurpleMegaphone.js";var s=function(s){var d=s.value,p=n(null),m=n(),v=n(null),f=n(!1),y=n([]),h=n([]),g=t(!1),E=g[0],w=g[1],A=l({timeoutMs:s.timeoutMs}),L=A[0],R=A[1],x=function(r){return R(),w(!1),s.onError?s.onError({type:"PLAY_ERROR",error:r}):null},P=function(r){return R(),w(!1),s.onError?s.onError({type:"LOAD_ERROR",error:r}):null},B=function(){var r;return null===(r=p.current)||void 0===r||r.stop(),w(!1),s.onError?s.onError({type:"TIMEOUT"}):null},M=function(){var r,e;(null===(r=p.current)||void 0===r?void 0:r.playing())||(L(B),null===(e=p.current)||void 0===e||e.play())};o((function(){return d.audio&&s.Howl&&(p.current=new s.Howl({src:d.audio,autoplay:s.autoPlay}),s.autoPlay&&L(B),s.setRef&&s.setRef({play:M,getHowl:function(){return p.current},isPlaying:function(){var r;return null===(r=p.current)||void 0===r?void 0:r.playing()},pause:function(){var r;return null===(r=p.current)||void 0===r?void 0:r.pause()},stop:function(){var r;return null===(r=p.current)||void 0===r?void 0:r.stop()}})),function(){var r;R(),null===(r=p.current)||void 0===r||r.stop()}}),[]);var _=function(r){p.current&&v.current&&y.current[0]&&r>parseFloat(y.current[0].getAttribute("data-start")||"0")&&(h.current.forEach((function(r){r.classList.contains("highlighted")&&(r.classList.remove("highlighted"),r.classList.add("has-highlighted"))})),y.current[0].classList.add("highlighted"),y.current=y.current.slice(1,y.current.length))},j=function(){f.current=!0,y.current=h.current,w(!0);var r=function(){var e;if(null===(e=p.current)||void 0===e?void 0:e.playing()){R();var n=p.current.seek();_(n),m.current=requestAnimationFrame(r)}else m&&cancelAnimationFrame(m.current)};m.current=requestAnimationFrame(r)},k=function(){cancelAnimationFrame(m.current),w(!1),f.current&&(h.current.forEach((function(r){r.classList.remove("highlighted"),r.classList.remove("has-highlighted")})),s.onComplete&&s.onComplete())};o((function(){var r,e,n,t,o,u;if(null===(r=p.current)||void 0===r||r.on("play",j),null===(e=p.current)||void 0===e||e.on("end",k),null===(n=p.current)||void 0===n||n.on("stop",k),null===(t=p.current)||void 0===t||t.on("playerror",x),null===(o=p.current)||void 0===o||o.on("loaderror",P),h.current){var l=null===(u=v.current)||void 0===u?void 0:u.querySelectorAll("span.synced-test--part");l&&(h.current=Array.from(l),y.current=h.current)}return function(){var r,e,n,t,o;k(),null===(r=p.current)||void 0===r||r.off("play",j),null===(e=p.current)||void 0===e||e.off("end",k),null===(n=p.current)||void 0===n||n.off("stop",k),null===(t=p.current)||void 0===t||t.off("playerror",x),null===(o=p.current)||void 0===o||o.off("loaderror",P)}}),[s.value]);var q=u((function(){if(!s.showPlayButton)return null;var n={className:"synced-text--play",onClick:M,style:{marginRight:"10px"}};return s.playButton?e.createElement("div",r({},n,{"data-playing":E}),s.playButton):e.createElement(a,r({theme:E?"purple":"primary"},n,{size:"x-small",round:!0}),E?e.createElement(i,null):e.createElement(c,null))}),[s.showPlayButton,E]);return d?e.createElement("div",{ref:v,className:"synced-text"},q,d.sequences.map((function(r,n){return e.createElement("span",{className:"synced-test--part",style:{display:"inline-block"},"data-start":r.time[0],"data-end":r.time[1],key:r.time[0]},e.createElement("span",{className:"synced-text--word",dangerouslySetInnerHTML:{__html:"".concat(r.text)}}),n<d.sequences.length&&e.createElement("span",{className:"synced-text--space",style:{whiteSpace:"pre-wrap"}}," "))}))):null};export{s as SyncedText};
|