source: imaps-frontend/node_modules/react-use/esm/useSpeech.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[d565449]1import { __assign } from "tslib";
2import { useCallback, useEffect, useRef, useState } from 'react';
3var Status;
4(function (Status) {
5 Status[Status["init"] = 0] = "init";
6 Status[Status["play"] = 1] = "play";
7 Status[Status["pause"] = 2] = "pause";
8 Status[Status["end"] = 3] = "end";
9})(Status || (Status = {}));
10var useSpeech = function (text, options) {
11 var mounted = useRef(false);
12 var _a = useState(function () {
13 var _a = options.voice || {}, _b = _a.lang, lang = _b === void 0 ? 'default' : _b, _c = _a.name, name = _c === void 0 ? '' : _c;
14 return {
15 isPlaying: false,
16 status: Status[Status.init],
17 lang: options.lang || 'default',
18 voiceInfo: { lang: lang, name: name },
19 rate: options.rate || 1,
20 pitch: options.pitch || 1,
21 volume: options.volume || 1,
22 };
23 }), state = _a[0], setState = _a[1];
24 var handlePlay = useCallback(function () {
25 if (!mounted.current) {
26 return;
27 }
28 setState(function (preState) {
29 return __assign(__assign({}, preState), { isPlaying: true, status: Status[Status.play] });
30 });
31 }, []);
32 var handlePause = useCallback(function () {
33 if (!mounted.current) {
34 return;
35 }
36 setState(function (preState) {
37 return __assign(__assign({}, preState), { isPlaying: false, status: Status[Status.pause] });
38 });
39 }, []);
40 var handleEnd = useCallback(function () {
41 if (!mounted.current) {
42 return;
43 }
44 setState(function (preState) {
45 return __assign(__assign({}, preState), { isPlaying: false, status: Status[Status.end] });
46 });
47 }, []);
48 useEffect(function () {
49 mounted.current = true;
50 var utterance = new SpeechSynthesisUtterance(text);
51 options.lang && (utterance.lang = options.lang);
52 options.voice && (utterance.voice = options.voice);
53 utterance.rate = options.rate || 1;
54 utterance.pitch = options.pitch || 1;
55 utterance.volume = options.volume || 1;
56 utterance.onstart = handlePlay;
57 utterance.onpause = handlePause;
58 utterance.onresume = handlePlay;
59 utterance.onend = handleEnd;
60 window.speechSynthesis.speak(utterance);
61 return function () {
62 mounted.current = false;
63 };
64 }, []);
65 return state;
66};
67export default useSpeech;
Note: See TracBrowser for help on using the repository browser.