mirror of https://github.com/msgbyte/tailchat
feat: 输入框粘贴上传图片
parent
1174a56de4
commit
5ffb5c1aee
@ -0,0 +1,34 @@
|
||||
export class ClipboardHelper {
|
||||
data: DataTransfer;
|
||||
|
||||
constructor(e: { clipboardData: DataTransfer }) {
|
||||
this.data = e.clipboardData;
|
||||
}
|
||||
|
||||
hasImage(): File | false {
|
||||
const image = this.isPasteImage(this.data.items);
|
||||
if (image === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const file = image.getAsFile();
|
||||
if (file === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
private isPasteImage(items: DataTransferItemList): DataTransferItem | false {
|
||||
let i = 0;
|
||||
let item: DataTransferItem;
|
||||
while (i < items.length) {
|
||||
item = items[i];
|
||||
if (item.type.indexOf('image') !== -1) {
|
||||
return item;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
import { ModalWrapper } from '@/plugin/common';
|
||||
import { Button } from '@/plugin/component';
|
||||
import React from 'react';
|
||||
import { t, useAsyncFn } from 'tailchat-shared';
|
||||
|
||||
interface ImageUploadPreviewerProps {
|
||||
imageUrl: string;
|
||||
onConfirm: () => void;
|
||||
}
|
||||
export const ImageUploadPreviewer: React.FC<ImageUploadPreviewerProps> =
|
||||
React.memo((props) => {
|
||||
const { imageUrl } = props;
|
||||
|
||||
const [{ loading }, handleConfirm] = useAsyncFn(async () => {
|
||||
if (typeof props.onConfirm === 'function') {
|
||||
await Promise.resolve(props.onConfirm());
|
||||
}
|
||||
}, [props.onConfirm]);
|
||||
|
||||
return (
|
||||
<ModalWrapper style={{ maxHeight: '60vh', maxWidth: '60vw' }}>
|
||||
<div className="flex">
|
||||
<div className="w-2/3 p-2.5 bg-black bg-opacity-20 rounded">
|
||||
<img className="max-h-160" src={imageUrl} />
|
||||
</div>
|
||||
|
||||
<div className="w-1/3 p-2 flex flex-col items-end justify-between">
|
||||
<div className="text-right">
|
||||
<div className="text-lg">{t('上传图片到会话')}</div>
|
||||
<div className="text-sm text-gray-400">
|
||||
{t('请勿上传违反当地法律法规的图片')}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
className="mt-4"
|
||||
type="primary"
|
||||
loading={loading}
|
||||
onClick={handleConfirm}
|
||||
>
|
||||
{t('确认')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</ModalWrapper>
|
||||
);
|
||||
});
|
||||
ImageUploadPreviewer.displayName = 'ImageUploadPreviewer';
|
Loading…
Reference in New Issue