From 6db44f5ea0db86d0eecac0f82b492119c7b71caa Mon Sep 17 00:00:00 2001 From: JayBridge <12310903@mail.sustech.edu.cn> Date: Thu, 24 Apr 2025 23:18:15 +0800 Subject: [PATCH] feat: add image-select modal (#49) --- .../chat-input/ImageUploadButton.tsx | 36 ++++-- src/components/modals/ImageSelectorModal.tsx | 114 ++++++++++++++++++ styles.css | 76 +++++++++++- 3 files changed, 212 insertions(+), 14 deletions(-) create mode 100644 src/components/modals/ImageSelectorModal.tsx diff --git a/src/components/chat-view/chat-input/ImageUploadButton.tsx b/src/components/chat-view/chat-input/ImageUploadButton.tsx index f8bbcd5..e1626be 100644 --- a/src/components/chat-view/chat-input/ImageUploadButton.tsx +++ b/src/components/chat-view/chat-input/ImageUploadButton.tsx @@ -1,30 +1,40 @@ import { ImageIcon } from 'lucide-react' +import { TFile } from 'obsidian' + +import { useApp } from '../../../contexts/AppContext' +import { ImageSelectorModal } from '../../modals/ImageSelectorModal' export function ImageUploadButton({ onUpload, }: { onUpload: (files: File[]) => void }) { - const handleFileChange = (event: React.ChangeEvent) => { - const files = Array.from(event.target.files ?? []) - if (files.length > 0) { - onUpload(files) + const app = useApp() + + const handleClick = () => { + const handleVaultImages = async (files: TFile[]) => { + const imageFiles = await Promise.all( + files.map(async (file) => { + const arrayBuffer = await app.vault.readBinary(file) + const blob = new Blob([arrayBuffer], { type: `image/${file.extension}` }) + return new File([blob], file.name, { type: `image/${file.extension}` }) + }) + ) + onUpload(imageFiles) } + + new ImageSelectorModal(app, onUpload, handleVaultImages).open() } return ( -