feat(chat): 修复输入法回车发送消息的问题
- 添加 isComposing 状态跟踪输入法组合事件 - 实现 handleCompositionStart 和 handleCompositionEnd 方法 - 新增 isImeEnter 函数判断是否为输入法回车 - 修改 handleKeydown 方法,在输入法状态下阻止回车发送 - 在 textarea 上添加 compositionstart 和 compositionend 事件监听
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed } from 'vue'
|
|
||||||
import { useI18n } from 'vue-i18n'
|
|
||||||
import { NButton, NTooltip } from 'naive-ui'
|
|
||||||
import { useChatStore } from '@/stores/chat'
|
|
||||||
import type { Attachment } from '@/stores/chat'
|
import type { Attachment } from '@/stores/chat'
|
||||||
|
import { useChatStore } from '@/stores/chat'
|
||||||
|
import { NButton, NTooltip } from 'naive-ui'
|
||||||
|
import { computed, ref } from 'vue'
|
||||||
|
import { useI18n } from 'vue-i18n'
|
||||||
|
|
||||||
const chatStore = useChatStore()
|
const chatStore = useChatStore()
|
||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
@@ -13,6 +13,7 @@ const fileInputRef = ref<HTMLInputElement>()
|
|||||||
const attachments = ref<Attachment[]>([])
|
const attachments = ref<Attachment[]>([])
|
||||||
const isDragging = ref(false)
|
const isDragging = ref(false)
|
||||||
const dragCounter = ref(0)
|
const dragCounter = ref(0)
|
||||||
|
const isComposing = ref(false)
|
||||||
|
|
||||||
const canSend = computed(() => inputText.value.trim() || attachments.value.length > 0)
|
const canSend = computed(() => inputText.value.trim() || attachments.value.length > 0)
|
||||||
|
|
||||||
@@ -126,11 +127,26 @@ function handleSend() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleCompositionStart() {
|
||||||
|
isComposing.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleCompositionEnd() {
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
isComposing.value = false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function isImeEnter(e: KeyboardEvent): boolean {
|
||||||
|
return isComposing.value || e.isComposing || e.keyCode === 229
|
||||||
|
}
|
||||||
|
|
||||||
function handleKeydown(e: KeyboardEvent) {
|
function handleKeydown(e: KeyboardEvent) {
|
||||||
if (e.key === 'Enter' && !e.shiftKey) {
|
if (e.key !== 'Enter' || e.shiftKey) return
|
||||||
e.preventDefault()
|
if (isImeEnter(e)) return
|
||||||
handleSend()
|
|
||||||
}
|
e.preventDefault()
|
||||||
|
handleSend()
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleInput(e: Event) {
|
function handleInput(e: Event) {
|
||||||
@@ -206,6 +222,8 @@ function isImage(type: string): boolean {
|
|||||||
:placeholder="t('chat.inputPlaceholder')"
|
:placeholder="t('chat.inputPlaceholder')"
|
||||||
rows="1"
|
rows="1"
|
||||||
@keydown="handleKeydown"
|
@keydown="handleKeydown"
|
||||||
|
@compositionstart="handleCompositionStart"
|
||||||
|
@compositionend="handleCompositionEnd"
|
||||||
@input="handleInput"
|
@input="handleInput"
|
||||||
@paste="handlePaste"
|
@paste="handlePaste"
|
||||||
></textarea>
|
></textarea>
|
||||||
|
|||||||
Reference in New Issue
Block a user