UNPKG

@amaui/ui-react

Version:
158 lines (157 loc) 8.93 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const utils_1 = require("@amaui/utils"); const style_react_1 = require("@amaui/style-react"); const IconMaterialPauseW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialPauseW100")); const IconMaterialTextToSpeechW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialTextToSpeechW100")); const Line_1 = __importDefault(require("../Line")); const Tooltip_1 = __importDefault(require("../Tooltip")); const IconButton_1 = __importDefault(require("../IconButton")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: {}, iconButton: { '&.amaui-IconButton-root': { transition: theme.methods.transitions.make('transform'), '&:active': { transform: 'scale(0.94)' } } } }), { name: 'amaui-TextToSpeech' }); const TextToSpeech = react_1.default.forwardRef((props_, ref) => { const theme = (0, style_react_1.useAmauiTheme)(); const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiTextToSpeech) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]); const Tooltip = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Tooltip) || Tooltip_1.default; }, [theme]); const IconButton = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.IconButton) || IconButton_1.default; }, [theme]); const { size = 'regular', read, speechSynthesisUtterance, language, pitch, rate, text, voice, volume, loading, disabled, Icon: Icon_ = IconMaterialTextToSpeechW100_1.default, IconPause = IconMaterialPauseW100_1.default, onStart: onStart_, onPause: onPause_, onResume: onResume_, onEnd: onEnd_, onError: onError_, TooltipProps, IconButtonProps, IconProps, className } = props, other = __rest(props, ["size", "read", "speechSynthesisUtterance", "language", "pitch", "rate", "text", "voice", "volume", "loading", "disabled", "Icon", "IconPause", "onStart", "onPause", "onResume", "onEnd", "onError", "TooltipProps", "IconButtonProps", "IconProps", "className"]); const [status, setStatus] = react_1.default.useState('initial'); const { classes } = useStyle(); const refs = { root: react_1.default.useRef(undefined), recognition: react_1.default.useRef(undefined), status: react_1.default.useRef(status) }; refs.status.current = status; const supported = (0, utils_1.isEnvironment)('browser') && ('speechSynthesis' in window && 'SpeechSynthesisUtterance' in window); const onPause = react_1.default.useCallback(async (event) => { setStatus('paused'); if ((0, utils_1.is)('function', onPause_)) onPause_(event); }, [onPause_]); const onResume = react_1.default.useCallback(async (event) => { setStatus('resumed'); if ((0, utils_1.is)('function', onResume_)) onResume_(event); }, [onResume_]); const onEnd = react_1.default.useCallback((event) => { setStatus('initial'); if ((0, utils_1.is)('function', onEnd_)) onEnd_(event); }, [onEnd_]); const onError = react_1.default.useCallback((event) => { setStatus('initial'); if ((0, utils_1.is)('function', onError_)) onError_(event); }, [onError_]); react_1.default.useEffect(() => { // Clean up previous utterance // if there was any if (supported) { window.speechSynthesis.cancel(); setStatus('initial'); } }, [read, speechSynthesisUtterance, supported, language, pitch, rate, text, voice, volume]); const onStart = react_1.default.useCallback(async (event) => { if (supported && (0, utils_1.is)('string', read)) { setStatus('started'); window.speechSynthesis.cancel(); await (0, utils_1.wait)(140); const utterance = speechSynthesisUtterance || new SpeechSynthesisUtterance(read); // properties if (language !== undefined) utterance.lang = language; if (pitch !== undefined) utterance.pitch = pitch; if (rate !== undefined) utterance.rate = rate; if (text !== undefined) utterance.text = text; if (voice !== undefined) utterance.voice = voice; if (volume !== undefined) utterance.volume = volume; // events utterance.addEventListener('pause', onPause); utterance.addEventListener('resume', onResume); utterance.addEventListener('end', onEnd); utterance.addEventListener('error', onError); window.speechSynthesis.speak(utterance); if ((0, utils_1.is)('function', onStart_)) onStart_(event); } }, [read, speechSynthesisUtterance, supported, onStart_, onPause, onResume, onEnd, language, pitch, rate, text, voice, volume]); const onClick = react_1.default.useCallback(async () => { const status_ = refs.status.current; if (['started', 'resumed'].includes(status_)) window.speechSynthesis.pause(); else if (status_ === 'paused') window.speechSynthesis.resume(); else if (status_ === 'initial') onStart(); }, [onStart]); const iconProps = Object.assign({ size }, IconProps); const iconButtonProps = Object.assign({ size, loading, disabled }, IconButtonProps); if (!supported) return null; let IconToUse = Icon_; let name = 'Text to speech'; if (['started', 'resumed'].includes(status)) { IconToUse = IconPause; name = 'Pause'; } if (status === 'paused') name = 'Resume'; return ((0, jsx_runtime_1.jsx)(Line, Object.assign({ ref: (item) => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }, gap: 1, direction: 'row', align: 'center', className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('TextToSpeech', theme) && [ `amaui-TextToSpeech-root`, `amaui-TextToSpeech-size-${size}` ], className, classes.root ]) }, other, { children: (0, jsx_runtime_1.jsx)(Tooltip, Object.assign({ name: name }, TooltipProps, { children: (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ onClick: onClick }, iconButtonProps, { selected: status === 'started', disabled: disabled !== undefined ? disabled : !supported, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('TextToSpeech', theme) && [ `amaui-TextToSpeech-iconButton` ], classes.iconButton ]) }, { children: (0, jsx_runtime_1.jsx)(IconToUse, Object.assign({}, iconProps)) })) })) }))); }); TextToSpeech.displayName = 'amaui-TextToSpeech'; exports.default = TextToSpeech;