|
1 | 1 | <script lang="ts"> |
2 | | - import { Send, Loader2 } from "lucide-svelte"; |
| 2 | + import { Send, Loader2, Slash } from "lucide-svelte"; |
| 3 | +
|
| 4 | + interface SlashCommand { |
| 5 | + name: string; |
| 6 | + description: string; |
| 7 | + prefix: string; |
| 8 | + } |
| 9 | +
|
| 10 | + const slashCommands: SlashCommand[] = [ |
| 11 | + { name: "learn", description: "Start a learning session on a topic", prefix: "/learn " }, |
| 12 | + { name: "quiz", description: "Generate a quiz on the current topic", prefix: "/quiz " }, |
| 13 | + { name: "explain", description: "Explain a concept or code block", prefix: "/explain " }, |
| 14 | + { name: "debug", description: "Debug the current code or error", prefix: "/debug " }, |
| 15 | + { name: "review", description: "Review code for improvements", prefix: "/review " }, |
| 16 | + { name: "report", description: "Generate a report from results", prefix: "/report " }, |
| 17 | + ]; |
3 | 18 |
|
4 | 19 | interface Props { |
5 | 20 | ready?: boolean; |
|
19 | 34 |
|
20 | 35 | let inputValue = $state(""); |
21 | 36 | let textareaEl: HTMLTextAreaElement | undefined = $state(); |
| 37 | + let showSlashMenu = $state(false); |
| 38 | + let filteredCommands = $state<SlashCommand[]>([]); |
| 39 | + let selectedCommandIdx = $state(0); |
22 | 40 |
|
23 | 41 | let maxHeight = $derived(compact ? 120 : 200); |
24 | 42 |
|
|
29 | 47 | } |
30 | 48 | } |
31 | 49 |
|
| 50 | + function handleInput() { |
| 51 | + adjustTextarea(); |
| 52 | + if (inputValue.startsWith("/") && !inputValue.includes(" ")) { |
| 53 | + const query = inputValue.slice(1).toLowerCase(); |
| 54 | + filteredCommands = slashCommands.filter(c => c.name.startsWith(query)); |
| 55 | + showSlashMenu = filteredCommands.length > 0; |
| 56 | + selectedCommandIdx = 0; |
| 57 | + } else { |
| 58 | + showSlashMenu = false; |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + function selectCommand(cmd: SlashCommand) { |
| 63 | + inputValue = cmd.prefix; |
| 64 | + showSlashMenu = false; |
| 65 | + textareaEl?.focus(); |
| 66 | + } |
| 67 | +
|
32 | 68 | function handleSend() { |
33 | 69 | const text = inputValue.trim(); |
34 | 70 | if (!text || loading) return; |
35 | 71 | inputValue = ""; |
| 72 | + showSlashMenu = false; |
36 | 73 | if (textareaEl) textareaEl.style.height = "auto"; |
37 | 74 | onSend(text); |
38 | 75 | } |
39 | 76 |
|
40 | 77 | function handleKeydown(e: KeyboardEvent) { |
| 78 | + if (showSlashMenu) { |
| 79 | + if (e.key === "ArrowDown") { |
| 80 | + e.preventDefault(); |
| 81 | + selectedCommandIdx = Math.min(selectedCommandIdx + 1, filteredCommands.length - 1); |
| 82 | + return; |
| 83 | + } |
| 84 | + if (e.key === "ArrowUp") { |
| 85 | + e.preventDefault(); |
| 86 | + selectedCommandIdx = Math.max(selectedCommandIdx - 1, 0); |
| 87 | + return; |
| 88 | + } |
| 89 | + if (e.key === "Tab" || (e.key === "Enter" && !e.shiftKey)) { |
| 90 | + e.preventDefault(); |
| 91 | + if (filteredCommands[selectedCommandIdx]) { |
| 92 | + selectCommand(filteredCommands[selectedCommandIdx]); |
| 93 | + } |
| 94 | + return; |
| 95 | + } |
| 96 | + if (e.key === "Escape") { |
| 97 | + showSlashMenu = false; |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
41 | 101 | if (e.key === "Enter" && !e.shiftKey) { |
42 | 102 | e.preventDefault(); |
43 | 103 | handleSend(); |
|
50 | 110 | </script> |
51 | 111 |
|
52 | 112 | <div class="chat-input-wrap" class:compact> |
| 113 | + {#if showSlashMenu} |
| 114 | + <div class="slash-menu"> |
| 115 | + {#each filteredCommands as cmd, idx} |
| 116 | + <button |
| 117 | + class="slash-item" |
| 118 | + class:active={idx === selectedCommandIdx} |
| 119 | + onclick={() => selectCommand(cmd)} |
| 120 | + > |
| 121 | + <Slash size={12} /> |
| 122 | + <span class="slash-name">{cmd.name}</span> |
| 123 | + <span class="slash-desc">{cmd.description}</span> |
| 124 | + </button> |
| 125 | + {/each} |
| 126 | + </div> |
| 127 | + {/if} |
| 128 | + |
53 | 129 | <div class="input-container"> |
54 | 130 | <textarea |
55 | 131 | bind:this={textareaEl} |
|
58 | 134 | {placeholder} |
59 | 135 | disabled={!ready || loading} |
60 | 136 | rows="1" |
61 | | - oninput={adjustTextarea} |
| 137 | + oninput={handleInput} |
62 | 138 | onkeydown={handleKeydown} |
63 | 139 | ></textarea> |
64 | 140 | <button |
|
68 | 144 | aria-label="Send message" |
69 | 145 | > |
70 | 146 | {#if loading} |
71 | | - <Loader2 class="h-{compact ? '4' : '5'} w-{compact ? '4' : '5'} animate-spin" /> |
| 147 | + <Loader2 size={compact ? 16 : 20} class="animate-spin" /> |
72 | 148 | {:else} |
73 | | - <Send class="h-{compact ? '4' : '5'} w-{compact ? '4' : '5'}" /> |
| 149 | + <Send size={compact ? 16 : 20} /> |
74 | 150 | {/if} |
75 | 151 | </button> |
76 | 152 | </div> |
77 | 153 | {#if !compact} |
78 | 154 | <p class="input-hint"> |
79 | | - Codaro may produce inaccurate responses. Verify important information. |
| 155 | + Type <kbd>/</kbd> for commands. Shift+Enter for new line. |
80 | 156 | </p> |
81 | 157 | {/if} |
82 | 158 | </div> |
|
85 | 161 | .chat-input-wrap { |
86 | 162 | flex-shrink: 0; |
87 | 163 | padding: 12px 24px 16px; |
| 164 | + position: relative; |
88 | 165 | } |
89 | 166 |
|
90 | 167 | .chat-input-wrap.compact { |
91 | 168 | padding: 8px; |
92 | 169 | } |
93 | 170 |
|
| 171 | + .slash-menu { |
| 172 | + position: absolute; |
| 173 | + bottom: 100%; |
| 174 | + left: 24px; |
| 175 | + right: 24px; |
| 176 | + max-width: 768px; |
| 177 | + margin: 0 auto 4px; |
| 178 | + background: var(--background); |
| 179 | + border: 1px solid var(--border); |
| 180 | + border-radius: 10px; |
| 181 | + padding: 4px; |
| 182 | + box-shadow: 0 4px 12px color-mix(in srgb, var(--foreground) 10%, transparent); |
| 183 | + z-index: 10; |
| 184 | + } |
| 185 | +
|
| 186 | + .compact .slash-menu { |
| 187 | + left: 8px; |
| 188 | + right: 8px; |
| 189 | + border-radius: 8px; |
| 190 | + } |
| 191 | +
|
| 192 | + .slash-item { |
| 193 | + display: flex; |
| 194 | + align-items: center; |
| 195 | + gap: 8px; |
| 196 | + width: 100%; |
| 197 | + padding: 8px 10px; |
| 198 | + border: none; |
| 199 | + border-radius: 6px; |
| 200 | + background: transparent; |
| 201 | + color: var(--foreground); |
| 202 | + font-size: 0.85rem; |
| 203 | + cursor: pointer; |
| 204 | + text-align: left; |
| 205 | + transition: background 0.1s; |
| 206 | + } |
| 207 | +
|
| 208 | + .slash-item:hover, |
| 209 | + .slash-item.active { |
| 210 | + background: color-mix(in srgb, var(--accent) 10%, transparent); |
| 211 | + } |
| 212 | +
|
| 213 | + .slash-name { |
| 214 | + font-weight: 600; |
| 215 | + color: var(--accent); |
| 216 | + } |
| 217 | +
|
| 218 | + .slash-desc { |
| 219 | + color: color-mix(in srgb, var(--foreground) 50%, transparent); |
| 220 | + } |
| 221 | +
|
94 | 222 | .input-container { |
95 | 223 | max-width: 768px; |
96 | 224 | margin: 0 auto; |
|
181 | 309 | font-size: 0.72rem; |
182 | 310 | color: color-mix(in srgb, var(--foreground) 30%, transparent); |
183 | 311 | } |
| 312 | +
|
| 313 | + .input-hint kbd { |
| 314 | + display: inline-block; |
| 315 | + padding: 1px 4px; |
| 316 | + font-size: 0.7rem; |
| 317 | + font-family: inherit; |
| 318 | + border: 1px solid color-mix(in srgb, var(--border) 60%, transparent); |
| 319 | + border-radius: 3px; |
| 320 | + background: color-mix(in srgb, var(--foreground) 5%, transparent); |
| 321 | + } |
184 | 322 | </style> |
0 commit comments