|
16 | 16 | --> |
17 | 17 |
|
18 | 18 | <script setup lang="ts"> |
19 | | - import { ref, nextTick } from 'vue'; |
| 19 | + import { ref, computed, nextTick, watch } from 'vue'; |
20 | 20 |
|
21 | 21 | import type { PendingQuestion } from '@/composables/useAgentChat'; |
22 | 22 |
|
23 | 23 | const props = defineProps<{ |
24 | 24 | isStreaming: boolean; |
25 | 25 | pendingQuestion: PendingQuestion | null; |
| 26 | + // Previous user messages from the chat, used as input history for Up/Down navigation. |
| 27 | + // Ordered oldest → newest. |
| 28 | + userMessages: string[]; |
26 | 29 | }>(); |
27 | 30 |
|
28 | 31 | const emit = defineEmits<{ |
|
31 | 34 | }>(); |
32 | 35 |
|
33 | 36 | const inputText = ref(''); |
34 | | - const textareaRef = ref<{ style: { height: string }; scrollHeight: number }>(); |
| 37 | + const textareaRef = ref<{ |
| 38 | + value: string; |
| 39 | + style: { height: string }; |
| 40 | + scrollHeight: number; |
| 41 | + selectionStart: number; |
| 42 | + selectionEnd: number; |
| 43 | + }>(); |
| 44 | +
|
| 45 | + // -1 = not browsing; 0 = newest user message, increasing = older |
| 46 | + const historyIndex = ref(-1); |
| 47 | +
|
| 48 | + // Full original message at the current history position (used by Tab confirmation) |
| 49 | + const historyFull = computed(() => { |
| 50 | + if (historyIndex.value < 0) return ''; |
| 51 | + const msgs = props.userMessages; |
| 52 | + const idx = msgs.length - 1 - historyIndex.value; |
| 53 | + return idx >= 0 && idx < msgs.length ? msgs[idx] : ''; |
| 54 | + }); |
| 55 | +
|
| 56 | + // Truncated version shown as placeholder: max 20 chars or cut at first newline, |
| 57 | + // whichever comes first. Appends "…" whenever anything was dropped. |
| 58 | + const historyPreview = computed(() => { |
| 59 | + const raw = historyFull.value; |
| 60 | + if (!raw) return null; |
| 61 | + const nl = raw.indexOf('\n'); |
| 62 | + const cut = nl === -1 ? raw.length : nl; |
| 63 | + const limit = Math.min(50, cut); |
| 64 | + const truncated = raw.slice(0, limit); |
| 65 | + return truncated.length < raw.length ? truncated + '…' : truncated; |
| 66 | + }); |
| 67 | +
|
| 68 | + watch(inputText, () => { |
| 69 | + nextTick(autoResize); |
| 70 | + }); |
35 | 71 |
|
36 | 72 | function handleSend() { |
37 | 73 | const text = inputText.value.trim(); |
38 | 74 | if (!text || props.isStreaming) return; |
| 75 | + historyIndex.value = -1; |
39 | 76 | emit('send', text); |
40 | 77 | inputText.value = ''; |
41 | | - nextTick(() => { |
42 | | - if (textareaRef.value) { |
43 | | - textareaRef.value.style.height = 'auto'; |
44 | | - } |
45 | | - }); |
46 | 78 | } |
47 | 79 |
|
48 | | - function handleKeydown(e: { key: string; shiftKey: boolean; preventDefault: () => void }) { |
| 80 | + function handleKeydown(e: { |
| 81 | + key: string; |
| 82 | + shiftKey: boolean; |
| 83 | + ctrlKey: boolean; |
| 84 | + altKey: boolean; |
| 85 | + metaKey: boolean; |
| 86 | + preventDefault: () => void; |
| 87 | + }) { |
49 | 88 | if (e.key === 'Enter' && !e.shiftKey) { |
50 | 89 | e.preventDefault(); |
51 | 90 | if (props.isStreaming) { |
52 | 91 | emit('stop'); |
53 | 92 | } else { |
54 | 93 | handleSend(); |
55 | 94 | } |
| 95 | + return; |
| 96 | + } |
| 97 | +
|
| 98 | + const hasModifier = e.shiftKey || e.ctrlKey || e.altKey || e.metaKey; |
| 99 | + if (hasModifier) return; |
| 100 | +
|
| 101 | + const el = textareaRef.value; |
| 102 | + if (!el) return; |
| 103 | +
|
| 104 | + // History preview via Up/Down: |
| 105 | + // - When input is empty → always works |
| 106 | + // - When input has text → only when cursor is at the boundary |
| 107 | + // (Up at start of text, Down at end of text) |
| 108 | + // The history item shows as placeholder (not real value); Tab to confirm. |
| 109 | +
|
| 110 | + if (e.key === 'ArrowUp') { |
| 111 | + if (props.userMessages.length === 0) return; |
| 112 | + const len = el.value.length; |
| 113 | + const cursorAtStart = el.selectionStart === 0 && el.selectionEnd === 0; |
| 114 | + if (len > 0 && !cursorAtStart) return; |
| 115 | + e.preventDefault(); |
| 116 | + const next = historyIndex.value === -1 ? 0 : historyIndex.value + 1; |
| 117 | + historyIndex.value = Math.min(next, props.userMessages.length - 1); |
| 118 | + return; |
| 119 | + } |
| 120 | +
|
| 121 | + if (e.key === 'ArrowDown') { |
| 122 | + if (historyIndex.value === -1) return; |
| 123 | + const len = el.value.length; |
| 124 | + const cursorAtEnd = el.selectionStart === len && el.selectionEnd === len; |
| 125 | + if (len > 0 && !cursorAtEnd) return; |
| 126 | + e.preventDefault(); |
| 127 | + historyIndex.value = Math.max(historyIndex.value - 1, -1); |
| 128 | + return; |
| 129 | + } |
| 130 | +
|
| 131 | + // Tab: confirm the current history preview, filling the full original text into the input |
| 132 | + if (e.key === 'Tab' && historyPreview.value !== null) { |
| 133 | + e.preventDefault(); |
| 134 | + inputText.value = historyFull.value; |
| 135 | + historyIndex.value = -1; |
| 136 | + nextTick(autoResize); |
| 137 | + return; |
| 138 | + } |
| 139 | +
|
| 140 | + // Escape: dismiss the history preview |
| 141 | + if (e.key === 'Escape' && historyPreview.value !== null) { |
| 142 | + e.preventDefault(); |
| 143 | + historyIndex.value = -1; |
56 | 144 | } |
57 | 145 | } |
58 | 146 |
|
|
75 | 163 | v-model="inputText" |
76 | 164 | class="chat-input__textarea" |
77 | 165 | :placeholder=" |
78 | | - props.pendingQuestion && !isStreaming |
79 | | - ? '输入你的回答,Enter 发送...' |
80 | | - : isStreaming |
81 | | - ? '正在回复中...' |
82 | | - : '输入消息,Enter 发送,Shift+Enter 换行' |
| 166 | + historyPreview !== null |
| 167 | + ? historyPreview + ' (Tab 确认)' |
| 168 | + : props.pendingQuestion && !isStreaming |
| 169 | + ? '输入你的回答,Enter 发送...' |
| 170 | + : isStreaming |
| 171 | + ? '正在回复中...' |
| 172 | + : '输入消息,Enter 发送,Shift+Enter 换行' |
83 | 173 | " |
84 | 174 | :disabled="isStreaming" |
85 | 175 | rows="1" |
86 | | - @input="autoResize" |
87 | 176 | @keydown="handleKeydown" |
88 | 177 | /> |
89 | 178 | <el-button |
|
0 commit comments