From 5fb58c10b2bb44b3f2d3bdab4d7a6619271e2bbf Mon Sep 17 00:00:00 2001
From: wujingjing <gersonwu@qq.com>
Date: 星期三, 02 四月 2025 17:51:39 +0800
Subject: [PATCH] smallScreenClick

---
 src/components/chat/hooks/useAssistantContentOpt.ts |   34 ++++++++++++++++++++++++++++++----
 1 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/components/chat/hooks/useAssistantContentOpt.ts b/src/components/chat/hooks/useAssistantContentOpt.ts
index 2cd2ef5..e95a358 100644
--- a/src/components/chat/hooks/useAssistantContentOpt.ts
+++ b/src/components/chat/hooks/useAssistantContentOpt.ts
@@ -1,17 +1,16 @@
 import { ElMessage } from 'element-plus';
-import type { ComputedRef } from 'vue';
-import { computed, nextTick, ref } from 'vue';
+import { computed, nextTick, onDeactivated, ref } from 'vue';
 // import useClipboard from 'vue-clipboard3';
 import { onClickOutside, useClipboard } from '@vueuse/core';
-import type { ChatMessage } from '../model/types';
+import markdownToTxt from 'markdown-to-txt';
 import { AnswerState, AnswerType, RoleEnum } from '../model/types';
 import { SetHistoryAnswerState } from '/@/api/ai/chat';
 import { isSharePage } from '/@/stores/chatRoom';
 import BrowserSpeechSynthesis from '/@/utils/speech/synthesis';
-import markdownToTxt from 'markdown-to-txt';
 export type AssistantContentOptOption = {
 	sendChatMessage: any;
 };
+const activeSpeakItem = ref(null);
 
 export const useAssistantContentOpt = (option: AssistantContentOptOption) => {
 	const isSpeaking = ref(false);
@@ -94,6 +93,11 @@
 		});
 	};
 
+	const isItemSpeaking = (item) => {
+		const checkSpeak = activeSpeakItem.value === item && isSpeaking.value;
+		return checkSpeak;
+	};
+
 	onClickOutside(
 		computed(() => feedbackPanelRef.value?.[curFeedbackIndex.value]),
 		(e) => {
@@ -134,8 +138,19 @@
 
 	let isEnterStop = false;
 
+	const resetSpeak = () => {
+		isSpeaking.value = false;
+		isEnterStop = false;
+		const instance = BrowserSpeechSynthesis.getInstance();
+		instance.cancel();
+		activeSpeakItem.value = null;
+	};
+
 	const speechClick = (item) => {
 		if (!checkIsText(item)) return;
+		if (activeSpeakItem.value !== item) {
+			resetSpeak();
+		}
 		isSpeaking.value = !isSpeaking.value;
 		if (isSpeaking.value) {
 			startSpeechClick(item);
@@ -145,7 +160,12 @@
 	};
 
 	const startSpeechClick = (item) => {
+		activeSpeakItem.value = item;
+
 		const instance = BrowserSpeechSynthesis.getInstance();
+		instance.onEnd(() => {
+			resetSpeak();
+		});
 		if (isEnterStop) {
 			instance.resume();
 		} else {
@@ -163,6 +183,11 @@
 		instance.pause();
 	};
 
+	onDeactivated(() => {
+		const instance = BrowserSpeechSynthesis.getInstance();
+		instance.cancel();
+	});
+
 	return {
 		copyClick,
 		likeClick,
@@ -179,6 +204,7 @@
 		showFixQuestion,
 		speechClick,
 		isSpeaking,
+		isItemSpeaking,
 		checkIsText,
 	};
 };

--
Gitblit v1.9.3