-
Notifications
You must be signed in to change notification settings - Fork 80
Adds support for image pasting from clipboard, and multiple file attachments #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,15 @@ import { useAiState } from '@/context/ai-state-context'; | |
| import { useInterrupt } from '@/components/canvas/live2d'; | ||
| import { useChatHistory } from '@/context/chat-history-context'; | ||
| import { useVAD } from '@/context/vad-context'; | ||
| import { useMediaCapture } from '@/hooks/utils/use-media-capture'; | ||
| import { ImageData, useMediaCapture } from '@/hooks/utils/use-media-capture'; | ||
|
|
||
| export function useTextInput() { | ||
| const [inputText, setInputText] = useState(''); | ||
| const [isComposing, setIsComposing] = useState(false); | ||
| const [attachedImages, setAttachedImages] = useState<ImageData[]>([]); | ||
| const clearAttachments = () => setAttachedImages([]); | ||
|
|
||
|
|
||
| const wsContext = useWebSocket(); | ||
| const { aiState } = useAiState(); | ||
| const { interrupt } = useInterrupt(); | ||
|
|
@@ -20,13 +24,46 @@ export function useTextInput() { | |
| setInputText(e.target.value); | ||
| }; | ||
|
|
||
| const handlePaste = (e: React.ClipboardEvent<HTMLInputElement | HTMLTextAreaElement>) => { | ||
| const { items } = e.clipboardData; | ||
| let foundImage = false; | ||
|
|
||
| for (let i = 0; i < items.length; i += 1) { | ||
| const item = items[i]; | ||
| if (item.type.startsWith('image/')) { | ||
| const file = item.getAsFile(); | ||
| if (file) { | ||
| foundImage = true; | ||
| const reader = new FileReader(); | ||
| reader.onload = () => { | ||
| setAttachedImages(prev => [ | ||
| ...prev, | ||
| { | ||
| source: 'clipboard', | ||
| data: reader.result as string, | ||
| mime_type: file.type, | ||
| }, | ||
| ]); | ||
| }; | ||
| reader.readAsDataURL(file); | ||
| } | ||
| } | ||
| } | ||
| // Prevent the raw image from being inserted as text | ||
| if (foundImage) e.preventDefault(); | ||
| }; | ||
|
Comment on lines
+27
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| const handleSend = async () => { | ||
| if (!inputText.trim() || !wsContext) return; | ||
| if (!inputText.trim() && attachedImages.length === 0) return; | ||
| if (!wsContext) return; | ||
|
|
||
| if (aiState === 'thinking-speaking') { | ||
| interrupt(); | ||
| } | ||
|
|
||
| const images = await captureAllMedia(); | ||
| const captured = await captureAllMedia(); | ||
| const images = [...attachedImages, ...captured]; | ||
|
|
||
|
|
||
| appendHumanMessage(inputText.trim()); | ||
| wsContext.sendMessage({ | ||
|
|
@@ -37,6 +74,7 @@ export function useTextInput() { | |
|
|
||
| if (autoStopMic) stopMic(); | ||
| setInputText(''); | ||
| setAttachedImages([]); | ||
| }; | ||
|
|
||
| const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
|
|
@@ -58,5 +96,8 @@ export function useTextInput() { | |
| handleKeyPress, | ||
| handleCompositionStart, | ||
| handleCompositionEnd, | ||
| handlePaste, | ||
| attachmentsCount: attachedImages.length, | ||
| clearAttachments, | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the paperclip icon (
BsPaperclip) for an action that clears attachments is counter-intuitive, as this icon is universally understood to mean "attach". This can lead to a confusing user experience, even though thearia-labelis correct. To improve clarity, consider changing the icon when there are attachments to visually indicate the "clear" action. For example, you could show a close icon (IoClosefromreact-icons/io5, which would need to be imported) next to the attachment count.