mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
bugfix: add paste image functionality to main index page
This commit is contained in:
1 parent
2f50b2611c
commit
6e03767f62
3 files changed
+52
-29
No files matched your search
@@ -69,3 +69,40 @@ export function formatRelativeTime(timestamp: number | Date): string {
|
||||
return `${diffYears} years ago`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a paste event handler that extracts images from clipboard data
|
||||
* and adds them via the provided callback.
|
||||
* @param onFileAdd - Callback function to add the pasted file
|
||||
* @returns A paste event handler function
|
||||
*/
|
||||
export function createPasteHandler(onFileAdd: (file: File) => void) {
|
||||
return 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
|
||||
onFileAdd(namedFile);
|
||||
}
|
||||
break; // Only handle the first image found
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user