wujingjing
2025-04-02 5fb58c10b2bb44b3f2d3bdab4d7a6619271e2bbf
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,
   };
};