UNPKG

@amaui/ui-react

Version:
178 lines (177 loc) 10.2 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 IconMaterialStopW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialStopW100")); const IconMaterialSpeechToTextW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialSpeechToTextW100")); 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-SpeechToText' }); const SpeechToText = 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.amauiSpeechToText) === 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', SpeechRecognition: SpeechRecognition_, continuous = true, interimResults = true, grammars, language = 'en-US', maxAlternatives = 1, join = '. ', loading, disabled, Icon: Icon_ = IconMaterialSpeechToTextW100_1.default, IconStop = IconMaterialStopW100_1.default, onChange, onData: onData_, onListen: onListen_, onListenStop: onListenStop_, onStart: onStart_, onStop: onStop_, onError: onError_, TooltipProps, IconButtonProps, IconProps, className } = props, other = __rest(props, ["size", "SpeechRecognition", "continuous", "interimResults", "grammars", "language", "maxAlternatives", "join", "loading", "disabled", "Icon", "IconStop", "onChange", "onData", "onListen", "onListenStop", "onStart", "onStop", "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), value: react_1.default.useRef(''), result: react_1.default.useRef([]), status: react_1.default.useRef(status) }; refs.status.current = status; const supported = (0, utils_1.isEnvironment)('browser') && ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window); const onData = react_1.default.useCallback((event) => { const value = Array.from(event.results || []).map((item) => { var _a; return ((_a = item[0]) === null || _a === void 0 ? void 0 : _a.transcript) || ''; }).join(''); refs.value.current = value; if (!value) { refs.result.current.push(value); } if ((0, utils_1.is)('function', onData_)) onData_(value); }, [onData_]); const onStart = react_1.default.useCallback(async (event) => { setStatus('started'); if ((0, utils_1.is)('function', onStart_)) onStart_(event); }, [onStart_]); const onEnd = react_1.default.useCallback((event) => { setStatus('initial'); // result refs.result.current.push(refs.value.current); const response = refs.result.current.join(join); if ((0, utils_1.is)('function', onChange)) onChange(response); }, [onChange, join]); const onError = react_1.default.useCallback((event) => { if ((0, utils_1.is)('function', onError_)) onError_(event); }, [onError_]); react_1.default.useEffect(() => { // previous clean up if (refs.recognition.current) { refs.recognition.current.abort(); } // make a new instance if (supported) { // eslint-disable-next-line @typescript-eslint/no-use-before-define const SpeechRecognitionClass = SpeechRecognition_ || window.SpeechRecognition || window.webkitSpeechRecognition; if (SpeechRecognitionClass) { refs.recognition.current = new SpeechRecognitionClass(); // options if (continuous !== undefined) refs.recognition.current.continuous = continuous; if (interimResults !== undefined) refs.recognition.current.interimResults = interimResults; if (grammars !== undefined) refs.recognition.current.grammars = grammars; if (language !== undefined) refs.recognition.current.lang = language; if (maxAlternatives !== undefined) refs.recognition.current.maxAlternatives = maxAlternatives; // events // result refs.recognition.current.addEventListener('start', onStart); refs.recognition.current.addEventListener('end', onEnd); refs.recognition.current.addEventListener('result', onData); refs.recognition.current.addEventListener('error', onError); } } return () => { // Clean up if (refs.recognition.current) { refs.recognition.current.removeEventListener('start', onStart); refs.recognition.current.removeEventListener('end', onEnd); refs.recognition.current.removeEventListener('result', onData); refs.recognition.current.removeEventListener('error', onError); } }; }, [SpeechRecognition_, supported, continuous, interimResults, grammars, language, maxAlternatives]); const onCleanUp = react_1.default.useCallback(() => { refs.result.current = []; refs.value.current = ''; }, []); const onListen = react_1.default.useCallback(async (event) => { if (refs.recognition.current) { // Clean up onCleanUp(); refs.recognition.current.start(); if ((0, utils_1.is)('function', onListen_)) onListen_(event); } }, [onListen_]); const onListenStop = react_1.default.useCallback(async (event) => { if (refs.recognition.current) { refs.recognition.current.stop(); if ((0, utils_1.is)('function', onListenStop_)) onListenStop_(event); } }, [onListenStop_]); const iconProps = Object.assign({ size }, IconProps); const iconButtonProps = Object.assign({ size, loading, disabled }, IconButtonProps); if (!supported) return null; let IconToUse = Icon_; let name = 'Speech to text'; if (status === 'started') { IconToUse = IconStop; name = 'Stop'; } 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)('SpeechToText', theme) && [ `amaui-SpeechToText-root`, `amaui-SpeechToText-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: status === 'started' ? onListenStop : onListen }, iconButtonProps, { selected: status === 'started', disabled: disabled !== undefined ? disabled : !supported, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('SpeechToText', theme) && [ `amaui-SpeechToText-iconButton` ], classes.iconButton ]) }, { children: (0, jsx_runtime_1.jsx)(IconToUse, Object.assign({}, iconProps)) })) })) }))); }); SpeechToText.displayName = 'amaui-SpeechToText'; exports.default = SpeechToText;