Skip to content

Commit 7c1554f

Browse files
committed
fix: Left click to copy incomplete content
1 parent 62ab02e commit 7c1554f

File tree

1 file changed

+23
-9
lines changed
  • ui/src/components/ai-chat/component/control

1 file changed

+23
-9
lines changed

ui/src/components/ai-chat/component/control/index.vue

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,41 @@ import { ref, nextTick, onMounted } from 'vue'
1717
import { t } from '@/locales'
1818
const isOpen = ref<boolean>(false)
1919
const eventVal = ref<any>({})
20+
2021
function getSelection() {
2122
const selection = window.getSelection()
22-
if (selection && selection.anchorNode == null) {
23-
return null
23+
if (selection) {
24+
if (selection.rangeCount === 0) return undefined
25+
const range = selection.getRangeAt(0)
26+
const fragment = range.cloneContents() // 克隆选区内容
27+
const div = document.createElement('div')
28+
div.appendChild(fragment)
29+
if (div.textContent) {
30+
return div.textContent.trim()
31+
}
2432
}
25-
const text = selection?.anchorNode?.textContent
26-
return text && text.substring(selection.anchorOffset, selection.focusOffset)
33+
return undefined
2734
}
35+
2836
/**
2937
* 打开控制台
3038
* @param event
3139
*/
3240
const openControl = (event: any) => {
3341
const c = getSelection()
34-
isOpen.value = false
3542
if (c) {
36-
nextTick(() => {
37-
eventVal.value = event
38-
isOpen.value = true
39-
})
43+
if (!isOpen.value) {
44+
nextTick(() => {
45+
eventVal.value = event
46+
isOpen.value = true
47+
})
48+
} else {
49+
clearSelectedText()
50+
isOpen.value = false
51+
}
4052
event.preventDefault()
53+
} else {
54+
isOpen.value = false
4155
}
4256
}
4357

0 commit comments

Comments
 (0)