feat: support paste file in file input

This commit is contained in:
Zhang Minghan 2024-02-04 11:47:21 +08:00
parent ce5a165c1f
commit 4fa85b1f12
2 changed files with 15 additions and 6 deletions

View File

@ -1,4 +1,4 @@
import {getUniqueList} from "@/utils/base.ts";
import { getUniqueList } from "@/utils/base.ts";
export type Channel = {
id: number;
@ -208,9 +208,9 @@ export const ChannelInfos: Record<string, ChannelInfo> = {
},
};
export const channelModels: string[] = getUniqueList(Object.values(ChannelInfos).flatMap(
(info) => info.models,
));
export const channelModels: string[] = getUniqueList(
Object.values(ChannelInfos).flatMap((info) => info.models),
);
export const channelGroups: string[] = [
"anonymous",

View File

@ -54,9 +54,10 @@ function FileProvider({ value, onChange }: FileProviderProps) {
});
}, []);
const triggerFile = async (files: File[]) => {
const triggerFile = async (files: (File | null)[]) => {
setLoading(true);
for (const file of files) {
if (!file) continue;
if (file.size > MaxFileSize) {
toast({
title: t("file.over-size"),
@ -243,7 +244,7 @@ type FileInputProps = {
id: string;
loading: boolean;
className?: string;
handleEvent: (files: File[]) => void;
handleEvent: (files: (File | null)[]) => void;
};
function FileInput({ id, loading, className, handleEvent }: FileInputProps) {
@ -273,6 +274,14 @@ function FileInput({ id, loading, className, handleEvent }: FileInputProps) {
onChange={(e) => handleEvent(Array.from(e.target?.files || []))}
accept="*"
style={{ display: "none" }}
// on transfer file
onPaste={(e) => {
const items = e.clipboardData.items;
const files = Array.from(items).filter(
(item) => item.kind === "file",
);
handleEvent(files.map((file) => file.getAsFile()));
}}
/>
</>
);