react-text-to-speech
Advanced tools
Comparing version 0.16.2 to 0.16.3
import React, { cloneElement, isValidElement, useEffect, useMemo, useRef, useState } from "react"; | ||
import { addToQueue, clearQueue, clearQueueHook, clearQueueUnload, dequeue, removeFromQueue, speakFromQueue, subscribe } from "./queue.js"; | ||
import { ArrayToText, cancel, findCharIndex, getIndex, isParent, JSXToArray, sanitize } from "./utils.js"; | ||
import { ArrayToText, cancel, findCharIndex, getIndex, isMobile, isParent, JSXToArray, sanitize, TextToChunks } from "./utils.js"; | ||
export function useQueue() { | ||
@@ -26,3 +26,6 @@ const [queue, setQueue] = useState([]); | ||
return; | ||
const utterance = new SpeechSynthesisUtterance(sanitize(ArrayToText(words))); | ||
const chunks = TextToChunks(sanitize(ArrayToText(words))); | ||
const numChunks = chunks.length; | ||
let currentChunk = 0, offset = 0; | ||
const utterance = new SpeechSynthesisUtterance(chunks[currentChunk]); | ||
utterance.pitch = pitch; | ||
@@ -46,2 +49,7 @@ utterance.rate = rate; | ||
const stopEventHandler = (event) => { | ||
if (event.type === "end" && currentChunk < numChunks - 1) { | ||
offset += chunks[currentChunk].length; | ||
utterance.text = chunks[++currentChunk]; | ||
return speakFromQueue(); | ||
} | ||
if (synth.paused) | ||
@@ -78,3 +86,3 @@ cancel(); | ||
utterance.onboundary = (event) => { | ||
setSpeakingWord({ index: findCharIndex(words, event.charIndex), length: event.charLength }); | ||
setSpeakingWord({ index: findCharIndex(words, offset + event.charIndex), length: event.charLength }); | ||
onBoundary === null || onBoundary === void 0 ? void 0 : onBoundary(event); | ||
@@ -99,6 +107,6 @@ }; | ||
var _a; | ||
if (isMobile(false) || speechStatus === "queued") | ||
return stop(); | ||
if (speechStatus === "started") | ||
return (_a = window.speechSynthesis) === null || _a === void 0 ? void 0 : _a.pause(); | ||
if (speechStatus === "queued") | ||
stop(); | ||
(_a = window.speechSynthesis) === null || _a === void 0 ? void 0 : _a.pause(); | ||
} | ||
@@ -105,0 +113,0 @@ function stop(status = speechStatus) { |
@@ -6,5 +6,7 @@ import { ReactNode } from "react"; | ||
export declare function cancel(): void; | ||
export declare function TextToChunks(text: string, desktopSize?: number, mobileSize?: number): string[]; | ||
export declare function findCharIndex(words: StringArray, index: number): string; | ||
export declare const getIndex: (parentIndex: Index, index: Index) => string; | ||
export declare function isMobile(iOS?: boolean): boolean; | ||
export declare function isParent(parentIndex: string, index?: string): boolean; | ||
export declare const sanitize: (text: string) => string; |
@@ -21,2 +21,19 @@ import { isValidElement } from "react"; | ||
} | ||
export function TextToChunks(text, desktopSize = 1000, mobileSize = 250) { | ||
const length = text.length; | ||
const size = isMobile() ? mobileSize : desktopSize; | ||
const result = []; | ||
let startIndex = 0; | ||
while (startIndex < length) { | ||
let endIndex = Math.min(startIndex + size, length); | ||
if (endIndex < length && text[endIndex] !== " ") { | ||
const spaceIndex = text.lastIndexOf(" ", endIndex); | ||
if (spaceIndex > startIndex) | ||
endIndex = spaceIndex; | ||
} | ||
result.push(text.slice(startIndex, endIndex)); | ||
startIndex = endIndex; | ||
} | ||
return result; | ||
} | ||
export function findCharIndex(words, index) { | ||
@@ -41,2 +58,8 @@ let currentIndex = 0; | ||
export const getIndex = (parentIndex, index) => `${parentIndex === "" ? "" : parentIndex + "-"}${index}`; | ||
export function isMobile(iOS = true) { | ||
var _a; | ||
let result = (_a = navigator.userAgentData) === null || _a === void 0 ? void 0 : _a.mobile; | ||
result !== null && result !== void 0 ? result : (result = /Android|webOS|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || (iOS && /iPhone|iPad|iPod/i.test(navigator.userAgent))); | ||
return result; | ||
} | ||
export function isParent(parentIndex, index) { | ||
@@ -43,0 +66,0 @@ if (!(index === null || index === void 0 ? void 0 : index.startsWith(parentIndex))) |
{ | ||
"name": "react-text-to-speech", | ||
"version": "0.16.2", | ||
"version": "0.16.3", | ||
"description": "An easy-to-use React.js component that leverages the Web Speech API to convert text to speech.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
27845
472