UNPKG

vlibras-player-webjs

Version:

Biblioteca JavaScript moderna para integração do VLibras Player com React, Vue, Angular e vanilla JS

130 lines 3.94 kB
import { useState, useEffect } from 'react'; /** * Hook React para gerenciar o VLibras Player * Gerencia estado, lifecycle e fornece API simplificada */ export function useVLibras(options = {}) { const [player, setPlayer] = useState(null); const [isLoaded, setIsLoaded] = useState(false); const [isPlaying, setIsPlaying] = useState(false); const [error, setError] = useState(null); useEffect(() => { let mounted = true; async function initPlayer() { try { // Import usando caminho relativo correto const playerModule = await import('../../../../core/player/VLibrasPlayer'); const VLibrasPlayer = playerModule.VLibrasPlayer; if (!mounted) return; const playerInstance = new VLibrasPlayer({ targetPath: options.targetPath || '/assets/vlibras', theme: options.theme || 'auto', debug: options.debug || false, autoplay: options.autoplay || false }); setPlayer(playerInstance); setIsLoaded(true); } catch (err) { if (mounted) { setError(err instanceof Error ? err.message : 'Failed to initialize VLibras Player'); } } } if (options.autoInit !== false) { initPlayer(); } return () => { mounted = false; }; }, [options.targetPath, options.theme, options.debug, options.autoplay, options.autoInit]); // Cleanup quando componente desmonta useEffect(() => { return () => { if (player) { try { player.destroy(); } catch (error) { console.error('Error during player cleanup:', error); } } }; }, [player]); // Handlers com error handling const translate = async (text) => { if (!player) throw new Error('Player not initialized'); try { await player.translateAsync(text); setIsPlaying(true); } catch (err) { setError(err instanceof Error ? err.message : 'Translation failed'); throw err; } }; const play = async () => { if (!player) throw new Error('Player not initialized'); try { await player.playAsync(); setIsPlaying(true); } catch (err) { setError(err instanceof Error ? err.message : 'Play failed'); throw err; } }; const pause = async () => { if (!player) throw new Error('Player not initialized'); try { player.pause(); setIsPlaying(false); } catch (err) { setError(err instanceof Error ? err.message : 'Pause failed'); throw err; } }; const stop = async () => { if (!player) throw new Error('Player not initialized'); try { player.stop(); setIsPlaying(false); } catch (err) { setError(err instanceof Error ? err.message : 'Stop failed'); throw err; } }; const destroy = async () => { if (!player) return; try { player.destroy(); setPlayer(null); setIsLoaded(false); setIsPlaying(false); } catch (err) { setError(err instanceof Error ? err.message : 'Destroy failed'); throw err; } }; return { player, isLoaded, isPlaying, error, translate, play, pause, stop, destroy }; } //# sourceMappingURL=useVLibras.js.map