From 4ebda2564b377732cdbe5253a05decdf6dce28eb Mon Sep 17 00:00:00 2001 From: troyeguo <13820674+troyeguo@users.noreply.github.com> Date: Tue, 28 Apr 2026 20:24:53 +0800 Subject: [PATCH] refactor: simplify speech text normalization and improve getSpeechStartIndex logic --- src/components/textToSpeech/component.tsx | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/components/textToSpeech/component.tsx b/src/components/textToSpeech/component.tsx index 9ffcc1a3..108368ac 100644 --- a/src/components/textToSpeech/component.tsx +++ b/src/components/textToSpeech/component.tsx @@ -191,9 +191,6 @@ class TextToSpeech extends React.Component< this.handleSpeechAutoStartRequest(); } } - normalizeSpeechText = (text: string) => { - return text.replace(/\s+/g, " ").trim(); - }; clearSpeechStartState = () => { if (this.props.speechStartText) { this.props.handleSpeechStartText(""); @@ -203,26 +200,12 @@ class TextToSpeech extends React.Component< } }; getSpeechStartIndex = (nodeTextList: string[]) => { - const speechStartText = this.normalizeSpeechText( - this.props.speechStartText - ); + const speechStartText = this.props.speechStartText; + if (!speechStartText) return -1; - const candidates = [speechStartText]; - if (speechStartText.length > 40) { - candidates.push(speechStartText.slice(0, 40)); - candidates.push(speechStartText.slice(-40)); - } - console.log(nodeTextList, candidates, "candidates"); - return nodeTextList.findIndex((item) => { - const normalizedItem = this.normalizeSpeechText(item); - return candidates.some((candidate) => { - return ( - normalizedItem.includes(candidate) || - candidate.includes(normalizedItem) - ); - }); + return item.includes(speechStartText) || speechStartText.includes(item); }); }; handleSpeechAutoStartRequest = async () => {