From f65bfbe83c94bf4ad0abb60fc415419fc47b88ea Mon Sep 17 00:00:00 2001 From: shaohuzhang1 <80892890+shaohuzhang1@users.noreply.github.com> Date: Wed, 26 Feb 2025 17:12:36 +0800 Subject: [PATCH] fix: Left click to copy incomplete content (#2419) --- .../ai-chat/component/control/index.vue | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/ui/src/components/ai-chat/component/control/index.vue b/ui/src/components/ai-chat/component/control/index.vue index daeeaeae0..6c7eefa76 100644 --- a/ui/src/components/ai-chat/component/control/index.vue +++ b/ui/src/components/ai-chat/component/control/index.vue @@ -17,27 +17,41 @@ import { ref, nextTick, onMounted } from 'vue' import { t } from '@/locales' const isOpen = ref(false) const eventVal = ref({}) + function getSelection() { const selection = window.getSelection() - if (selection && selection.anchorNode == null) { - return null + if (selection) { + if (selection.rangeCount === 0) return undefined + const range = selection.getRangeAt(0) + const fragment = range.cloneContents() // 克隆选区内容 + const div = document.createElement('div') + div.appendChild(fragment) + if (div.textContent) { + return div.textContent.trim() + } } - const text = selection?.anchorNode?.textContent - return text && text.substring(selection.anchorOffset, selection.focusOffset) + return undefined } + /** * 打开控制台 * @param event */ const openControl = (event: any) => { const c = getSelection() - isOpen.value = false if (c) { - nextTick(() => { - eventVal.value = event - isOpen.value = true - }) + if (!isOpen.value) { + nextTick(() => { + eventVal.value = event + isOpen.value = true + }) + } else { + clearSelectedText() + isOpen.value = false + } event.preventDefault() + } else { + isOpen.value = false } }