From a611b4f34635a556626eb58b7e506c1f0911c49f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=B4=94=E6=B0=B8=E4=BA=AE?= <702625325@qq.com>
Date: Fri, 28 Feb 2025 13:22:55 +0800
Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E7=BD=91=E9=A1=B5?=
=?UTF-8?q?=E5=BD=95=E9=9F=B3=20#283?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
1. 为防止输入一大堆 k,改 k 键为 Ctrl 键;
2. 改为长按录音,松手结束;
3. 为防止误触改为只有点击输入框之后才会生效
---
dashboard/src/views/ChatPage.vue | 62 ++++++++++++++++++++++++++------
1 file changed, 51 insertions(+), 11 deletions(-)
diff --git a/dashboard/src/views/ChatPage.vue b/dashboard/src/views/ChatPage.vue
index 4a7a07d9..8f412cec 100644
--- a/dashboard/src/views/ChatPage.vue
+++ b/dashboard/src/views/ChatPage.vue
@@ -60,10 +60,10 @@ marked.setOptions({
获取帮助 😊
- 按
+ 长按
K
- 开始语音 🎤
+ style="background-color: #eee; padding-left: 4px; padding-right: 4px; margin: 2px; border-radius: 4px;">Ctrl
+ 录制语音 🎤
按
@@ -112,7 +112,8 @@ marked.setOptions({
+ @click:clear="clearMessage" style="width: 100%; max-width: 850px;"
+ @keydown="handleInputKeyDown">
@@ -189,7 +190,12 @@ export default {
status: {},
statusText: '',
- eventSource: null
+ eventSource: null,
+
+ // 添加Ctrl键长按相关变量
+ ctrlKeyDown: false,
+ ctrlKeyTimer: null,
+ ctrlKeyLongPressThreshold: 300 // 长按阈值,单位毫秒
}
},
@@ -205,11 +211,9 @@ export default {
this.sendMessage();
}
}.bind(this));
- document.addEventListener('keydown', function (e) {
- if (e.keyCode == 75) {
- this.isRecording ? this.stopRecording() : this.startRecording();
- }
- }.bind(this));
+
+ // 添加keyup事件监听
+ document.addEventListener('keyup', this.handleInputKeyUp);
},
beforeUnmount() {
@@ -218,6 +222,9 @@ export default {
this.eventSource.cancel();
console.log('SSE连接已断开');
}
+
+ // 移除keyup事件监听
+ document.removeEventListener('keyup', this.handleInputKeyUp);
},
methods: {
@@ -531,7 +538,40 @@ export default {
const container = this.$refs.messageContainer;
container.scrollTop = container.scrollHeight;
});
- }
+ },
+
+ handleInputKeyDown(e) {
+ if (e.keyCode === 17) { // Ctrl键
+ // 防止重复触发
+ if (this.ctrlKeyDown) return;
+
+ this.ctrlKeyDown = true;
+
+ // 设置定时器识别长按
+ this.ctrlKeyTimer = setTimeout(() => {
+ if (this.ctrlKeyDown && !this.isRecording) {
+ this.startRecording();
+ }
+ }, this.ctrlKeyLongPressThreshold);
+ }
+ },
+
+ handleInputKeyUp(e) {
+ if (e.keyCode === 17) { // Ctrl键
+ this.ctrlKeyDown = false;
+
+ // 清除定时器
+ if (this.ctrlKeyTimer) {
+ clearTimeout(this.ctrlKeyTimer);
+ this.ctrlKeyTimer = null;
+ }
+
+ // 如果正在录音,停止录音
+ if (this.isRecording) {
+ this.stopRecording();
+ }
+ }
+ },
},
}