mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Merge branch 'pasteImage' into 'main'
bugfix: add paste image functionality to main index page See merge request soapbox-pub/shakespeare!85
This commit is contained in:
commit
179a59c58c
4 files changed
+53
-32
No files matched your search
@@ -83,35 +83,6 @@ export const ChatInput = memo(function ChatInput({
|
||||
}
|
||||
}, [handleSend]);
|
||||
|
||||
const handlePaste = useCallback(async (e: React.ClipboardEvent) => {
|
||||
const items = e.clipboardData?.items;
|
||||
if (!items) return;
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const item = items[i];
|
||||
|
||||
// Check if the item is an image
|
||||
if (item.type.startsWith('image/')) {
|
||||
e.preventDefault(); // Prevent default paste behavior for images
|
||||
|
||||
const file = item.getAsFile();
|
||||
if (file) {
|
||||
// Generate a filename with timestamp
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
const extension = file.type.split('/')[1] || 'png';
|
||||
const filename = `pasted-image-${timestamp}.${extension}`;
|
||||
|
||||
// Create a new File object with the generated name
|
||||
const namedFile = new File([file], filename, { type: file.type });
|
||||
|
||||
// Add to attached files
|
||||
setAttachedFiles(prev => [...prev, namedFile]);
|
||||
}
|
||||
break; // Only handle the first image found
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleInputChange = useCallback((e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
setInput(e.target.value);
|
||||
}, []);
|
||||
@@ -132,7 +103,7 @@ export const ChatInput = memo(function ChatInput({
|
||||
value={input}
|
||||
onChange={handleInputChange}
|
||||
onKeyDown={handleKeyDown}
|
||||
onPaste={handlePaste}
|
||||
onPasteImage={(file) => setAttachedFiles(prev => [...prev, file])}
|
||||
onFocus={onFocus}
|
||||
placeholder={
|
||||
!isConfigured
|
||||
|
||||
@@ -2,10 +2,57 @@ import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
export type TextareaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement>
|
||||
export interface TextareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
|
||||
/**
|
||||
* Callback fired when an image is pasted into the textarea.
|
||||
* If both onPaste and onPasteImage are provided, onPaste takes precedence.
|
||||
*/
|
||||
onPasteImage?: (file: File) => void;
|
||||
}
|
||||
|
||||
const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
|
||||
({ className, ...props }, ref) => {
|
||||
({ className, onPaste, onPasteImage, ...props }, ref) => {
|
||||
const handlePaste = React.useCallback(
|
||||
async (e: React.ClipboardEvent<HTMLTextAreaElement>) => {
|
||||
// If onPaste is provided, use it and skip image handling
|
||||
if (onPaste) {
|
||||
onPaste(e);
|
||||
return;
|
||||
}
|
||||
|
||||
// If onPasteImage is provided, handle image pasting
|
||||
if (onPasteImage) {
|
||||
const items = e.clipboardData?.items;
|
||||
if (!items) return;
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const item = items[i];
|
||||
|
||||
// Check if the item is an image
|
||||
if (item.type.startsWith('image/')) {
|
||||
e.preventDefault(); // Prevent default paste behavior for images
|
||||
|
||||
const file = item.getAsFile();
|
||||
if (file) {
|
||||
// Generate a filename with timestamp
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
const extension = file.type.split('/')[1] || 'png';
|
||||
const filename = `pasted-image-${timestamp}.${extension}`;
|
||||
|
||||
// Create a new File object with the generated name
|
||||
const namedFile = new File([file], filename, { type: file.type });
|
||||
|
||||
// Call the onPasteImage callback
|
||||
onPasteImage(namedFile);
|
||||
}
|
||||
break; // Only handle the first image found
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
[onPaste, onPasteImage]
|
||||
);
|
||||
|
||||
return (
|
||||
<textarea
|
||||
className={cn(
|
||||
@@ -13,6 +60,7 @@ const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
|
||||
className
|
||||
)}
|
||||
ref={ref}
|
||||
onPaste={onPaste || onPasteImage ? handlePaste : undefined}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
|
||||
@@ -69,3 +69,4 @@ export function formatRelativeTime(timestamp: number | Date): string {
|
||||
return `${diffYears} years ago`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -333,6 +333,7 @@ export default function Index() {
|
||||
value={prompt}
|
||||
onChange={handlePromptChange}
|
||||
onKeyDown={handleKeyDown}
|
||||
onPasteImage={(file) => setAttachedFiles(prev => [...prev, file])}
|
||||
onClick={handleTextareaClick}
|
||||
className="resize-none border-0 bg-transparent px-4 py-3 text-base focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:text-muted-foreground"
|
||||
disabled={isCreating || isGeneratingInfo || (hasProvidersConfigured && !providerModel.trim())}
|
||||
|
||||
Reference in new issue
Block a user