UNPKG

@mhpdev/react-native-speech

Version:

A high-performance React Native library for text-to-speech on iOS and Android

82 lines (81 loc) 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _jsxRuntime = require("react/jsx-runtime"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } const HighlightedText = ({ text, style, highlights = [], highlightedStyle, onHighlightedPress, ...rest }) => { const segments = (0, _react.useMemo)(() => { const sorted = [...highlights].sort((a, b) => a.start - b.start); const parts = []; let currentIndex = 0; for (let i = 0; i < sorted.length; ++i) { const currentSegment = sorted[i]; const { start, end } = currentSegment; if (start > currentIndex) { parts.push({ isHighlighted: false, text: text.slice(currentIndex, start) }); } parts.push({ ...currentSegment, isHighlighted: true, text: text.slice(start, end) }); currentIndex = end; } if (currentIndex < text.length) { parts.push({ isHighlighted: false, text: text.slice(currentIndex) }); } return parts; }, [highlights, text]); const getHighlightedSegmentStyle = (0, _react.useCallback)((isHighlighted = false, segmentStyle) => { return !isHighlighted ? style : _reactNative.StyleSheet.flatten([style, highlightedStyle ?? styles.isHighlighted, segmentStyle]); }, [highlightedStyle, style]); const renderText = (0, _react.useCallback)((segment, index) => { return !segment.isHighlighted ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, { style: getHighlightedSegmentStyle(segment.isHighlighted), children: segment.text }, index) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableWithoutFeedback, { onPress: () => onHighlightedPress?.({ end: segment.end, text: segment.text, start: segment.start }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, { style: getHighlightedSegmentStyle(segment.isHighlighted, segment.style), children: segment.text }) }, index); }, [getHighlightedSegmentStyle, onHighlightedPress]); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, { style: style, ...rest, children: segments.map(renderText) }); }; var _default = exports.default = HighlightedText; const styles = _reactNative.StyleSheet.create({ isHighlighted: { backgroundColor: '#FFFF00' } }); //# sourceMappingURL=index.js.map