|
|
@ -15,6 +15,8 @@ interface Props { |
|
|
|
uploadConfig?: { |
|
|
|
uploadConfig?: { |
|
|
|
path?: string |
|
|
|
path?: string |
|
|
|
scope?: PublicAttachmentScope |
|
|
|
scope?: PublicAttachmentScope |
|
|
|
|
|
|
|
// filesize in bytes |
|
|
|
|
|
|
|
maxFileSize?: number |
|
|
|
} |
|
|
|
} |
|
|
|
showCropper: boolean |
|
|
|
showCropper: boolean |
|
|
|
} |
|
|
|
} |
|
|
@ -38,12 +40,21 @@ const previewImage = ref({ |
|
|
|
src: '', |
|
|
|
src: '', |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fileSize = ref<number>(0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isValidFileSize = computed(() => { |
|
|
|
|
|
|
|
return !!(fileSize.value && fileSize.value * 1.33 <= (uploadConfig?.maxFileSize || 3 * 1024 * 1024)) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const handleCropImage = () => { |
|
|
|
const handleCropImage = () => { |
|
|
|
const { canvas } = cropperRef.value.getResult() |
|
|
|
const { canvas } = cropperRef.value.getResult() |
|
|
|
previewImage.value = { |
|
|
|
previewImage.value = { |
|
|
|
canvas, |
|
|
|
canvas, |
|
|
|
src: canvas.toDataURL(imageConfig.type), |
|
|
|
src: canvas.toDataURL(imageConfig.type), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
;(canvas as any).toBlob((blob: Blob) => { |
|
|
|
|
|
|
|
fileSize.value = blob.size |
|
|
|
|
|
|
|
}, imageConfig.type) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const handleUploadImage = async (fileToUpload: AttachmentReqType[]) => { |
|
|
|
const handleUploadImage = async (fileToUpload: AttachmentReqType[]) => { |
|
|
@ -77,18 +88,16 @@ const handleUploadImage = async (fileToUpload: AttachmentReqType[]) => { |
|
|
|
|
|
|
|
|
|
|
|
const handleSaveImage = async () => { |
|
|
|
const handleSaveImage = async () => { |
|
|
|
if (previewImage.value.canvas) { |
|
|
|
if (previewImage.value.canvas) { |
|
|
|
;(previewImage.value.canvas as any).toBlob(async (blob: Blob) => { |
|
|
|
await handleUploadImage([ |
|
|
|
await handleUploadImage([ |
|
|
|
{ |
|
|
|
{ |
|
|
|
title: imageConfig.name, |
|
|
|
title: imageConfig.name, |
|
|
|
fileName: imageConfig.name, |
|
|
|
fileName: imageConfig.name, |
|
|
|
mimetype: imageConfig.type, |
|
|
|
mimetype: imageConfig.type, |
|
|
|
size: fileSize.value, |
|
|
|
size: blob.size, |
|
|
|
url: previewImage.value.src, |
|
|
|
url: previewImage.value.src, |
|
|
|
data: previewImage.value.src, |
|
|
|
data: previewImage.value.src, |
|
|
|
}, |
|
|
|
}, |
|
|
|
]) |
|
|
|
]) |
|
|
|
|
|
|
|
}, imageConfig.type) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -139,7 +148,13 @@ watch(showCropper, () => { |
|
|
|
<span class="ml-2">Crop</span> |
|
|
|
<span class="ml-2">Crop</span> |
|
|
|
</NcButton> |
|
|
|
</NcButton> |
|
|
|
|
|
|
|
|
|
|
|
<NcButton size="small" :loading="isLoading" :disabled="!previewImage.src" @click="handleSaveImage"> Save </NcButton> |
|
|
|
<NcTooltip :disabled="isValidFileSize"> |
|
|
|
|
|
|
|
<template #title> Cropped file size is greter than max file size </template> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<NcButton size="small" :loading="isLoading" :disabled="!previewImage.src || !isValidFileSize" @click="handleSaveImage"> |
|
|
|
|
|
|
|
Save |
|
|
|
|
|
|
|
</NcButton> |
|
|
|
|
|
|
|
</NcTooltip> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</NcModal> |
|
|
|
</NcModal> |
|
|
|