From dbb57d8894cc473d25348b000cf132037bdcdf26 Mon Sep 17 00:00:00 2001
From: ChenZhaoYu <790348264@qq.com>
Date: Wed, 26 Apr 2023 08:27:46 +0800
Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=A4=8D=E5=88=B6?=
=?UTF-8?q?=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/utils/copy.ts | 18 +++++++++
src/views/chat/components/Message/Text.vue | 45 +++++++++++++++++++--
src/views/chat/components/Message/index.vue | 18 +++++++--
src/views/chat/hooks/useCopyCode.ts | 24 -----------
src/views/chat/index.vue | 3 --
5 files changed, 75 insertions(+), 33 deletions(-)
create mode 100644 src/utils/copy.ts
delete mode 100644 src/views/chat/hooks/useCopyCode.ts
diff --git a/src/utils/copy.ts b/src/utils/copy.ts
new file mode 100644
index 0000000..6fd32d8
--- /dev/null
+++ b/src/utils/copy.ts
@@ -0,0 +1,18 @@
+export function copyToClip(text: string) {
+ return new Promise((resolve, reject) => {
+ try {
+ const input: HTMLTextAreaElement = document.createElement('textarea')
+ input.setAttribute('readonly', 'readonly')
+ input.value = text
+ document.body.appendChild(input)
+ input.select()
+ if (document.execCommand('copy'))
+ document.execCommand('copy')
+ document.body.removeChild(input)
+ resolve(text)
+ }
+ catch (error) {
+ reject(error)
+ }
+ })
+}
diff --git a/src/views/chat/components/Message/Text.vue b/src/views/chat/components/Message/Text.vue
index c9f969b..4f2bb55 100644
--- a/src/views/chat/components/Message/Text.vue
+++ b/src/views/chat/components/Message/Text.vue
@@ -1,11 +1,12 @@
diff --git a/src/views/chat/components/Message/index.vue b/src/views/chat/components/Message/index.vue
index 541dde1..d08cb04 100644
--- a/src/views/chat/components/Message/index.vue
+++ b/src/views/chat/components/Message/index.vue
@@ -1,13 +1,13 @@
diff --git a/src/views/chat/hooks/useCopyCode.ts b/src/views/chat/hooks/useCopyCode.ts
deleted file mode 100644
index 8816b5d..0000000
--- a/src/views/chat/hooks/useCopyCode.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import { onMounted, onUpdated } from 'vue'
-import { copyText } from '@/utils/format'
-
-export function useCopyCode() {
- function copyCodeBlock() {
- const codeBlockWrapper = document.querySelectorAll('.code-block-wrapper')
- codeBlockWrapper.forEach((wrapper) => {
- const copyBtn = wrapper.querySelector('.code-block-header__copy')
- const codeBlock = wrapper.querySelector('.code-block-body')
- if (copyBtn && codeBlock) {
- copyBtn.addEventListener('click', () => {
- if (navigator.clipboard?.writeText)
- navigator.clipboard.writeText(codeBlock.textContent ?? '')
- else
- copyText({ text: codeBlock.textContent ?? '', origin: true })
- })
- }
- })
- }
-
- onMounted(() => copyCodeBlock())
-
- onUpdated(() => copyCodeBlock())
-}
diff --git a/src/views/chat/index.vue b/src/views/chat/index.vue
index c66ddae..27f220c 100644
--- a/src/views/chat/index.vue
+++ b/src/views/chat/index.vue
@@ -8,7 +8,6 @@ import html2canvas from 'html2canvas'
import { Message } from './components'
import { useScroll } from './hooks/useScroll'
import { useChat } from './hooks/useChat'
-import { useCopyCode } from './hooks/useCopyCode'
import { useUsingContext } from './hooks/useUsingContext'
import HeaderComponent from './components/Header/index.vue'
import { HoverButton, SvgIcon } from '@/components/common'
@@ -27,8 +26,6 @@ const ms = useMessage()
const chatStore = useChatStore()
-useCopyCode()
-
const { isMobile } = useBasicLayout()
const { addChat, updateChat, updateChatSome, getChatByUuidAndIndex } = useChat()
const { scrollRef, scrollToBottom, scrollToBottomIfAtBottom } = useScroll()