react-text-to-speech
Advanced tools
Comparing version 0.8.2 to 0.8.3
@@ -23,3 +23,3 @@ var __rest = (this && this.__rest) || function (s, e) { | ||
if (typeof element === "string") | ||
return element.split(" "); | ||
return element.split(/\s/); | ||
if (typeof element === "number") | ||
@@ -32,18 +32,21 @@ return [element.toString()]; | ||
function recursiveSearch(subArray, parentIndex) { | ||
for (let i = 0; i < subArray.length; i++) { | ||
const element = subArray[i]; | ||
if (Array.isArray(element)) { | ||
const result = recursiveSearch(element, i); | ||
if (result !== null) | ||
return `${parentIndex === null ? "" : parentIndex + "-"}${result}`; | ||
if (subArray.length) | ||
for (let i = 0; i < subArray.length; i++) { | ||
const element = subArray[i]; | ||
if (Array.isArray(element)) { | ||
const result = recursiveSearch(element, i); | ||
if (result !== null) | ||
return `${parentIndex === undefined ? "" : parentIndex + "-"}${result}`; | ||
} | ||
else { | ||
currentIndex += element.length + 1; | ||
if (currentIndex > index) | ||
return `${parentIndex === undefined ? "" : parentIndex + "-"}${i}`; | ||
} | ||
} | ||
else if (element) { | ||
currentIndex += element.length + 1; | ||
if (currentIndex > index) | ||
return `${parentIndex === null ? "" : parentIndex + "-"}${i}`; | ||
} | ||
} | ||
else | ||
currentIndex++; | ||
return null; | ||
} | ||
return recursiveSearch(words, null); | ||
return recursiveSearch(words); | ||
} | ||
@@ -67,3 +70,3 @@ export default function Speech({ text, id, pitch = 1, rate = 1, volume = 1, lang, voiceURI, startBtn = React.createElement(HiVolumeUp, null), pauseBtn = React.createElement(HiVolumeOff, null), stopBtn = React.createElement(HiMiniStop, null), useStopOverPause, highlightText = false, highlightProps = { style: { fontWeight: "bold" } }, onError = () => alert("Browser not supported! Try some other browser."), props = {}, children, }) { | ||
synth.cancel(); | ||
const utterance = new window.SpeechSynthesisUtterance(words.join(" ").replace(/\s|,/g, " ")); | ||
const utterance = new window.SpeechSynthesisUtterance(words.join(" ").replace(",", " ")); | ||
utterance.pitch = pitch; | ||
@@ -113,8 +116,11 @@ utterance.rate = rate; | ||
if (typeof element === "string" || typeof element === "number") { | ||
const words = String(element).split(" "); | ||
element = element.toString(); | ||
const words = element.split(/\s/); | ||
const index = +highlightedIndex.split("-").at(-1); | ||
const before = index ? words.slice(0, index).join(" ").length : -1; | ||
const highlightedWord = words[index]; | ||
return (React.createElement(Fragment, { key: highlightedIndex }, | ||
words.slice(0, index).join(" ") + " ", | ||
React.createElement("span", Object.assign({}, highlightProps), words[index]), | ||
" " + words.slice(index + 1).join(" "))); | ||
element.slice(0, before + 1), | ||
React.createElement("span", Object.assign({}, highlightProps), highlightedWord), | ||
element.slice(before + 1 + highlightedWord.length))); | ||
} | ||
@@ -121,0 +127,0 @@ return element; |
{ | ||
"name": "react-text-to-speech", | ||
"version": "0.8.2", | ||
"version": "0.8.3", | ||
"description": "An easy to use react component for the Web Speech API.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
22087
206
1