feat: open file selection in BannerEditor and CreatorLogoEditor when no image to edit
This commit is contained in:
@@ -103,7 +103,10 @@ const uploadProgress = ref(0)
|
||||
const { t } = useI18n()
|
||||
|
||||
const triggerFileInput = () => {
|
||||
fileInput.value.click()
|
||||
if (fileInput.value) {
|
||||
fileInput.value.value = '' // Reset the input value to ensure the change event fires
|
||||
fileInput.value.click()
|
||||
}
|
||||
}
|
||||
|
||||
const onFileSelected = (event) => {
|
||||
@@ -124,23 +127,30 @@ const onFileSelected = (event) => {
|
||||
}
|
||||
|
||||
const startEditing = () => {
|
||||
if (fileUrl.value && fileUrl.value !== fallbackUrl) {
|
||||
// Use our API client to get the image
|
||||
client.get(fileUrl.value, { responseType: 'blob' })
|
||||
.then(response => {
|
||||
const blob = response.data
|
||||
selectedFile.value = new File([blob], 'current-image.jpg', { type: 'image/jpeg' })
|
||||
// Create a local URL for the cropper
|
||||
fileUrl.value = URL.createObjectURL(blob)
|
||||
showCropper.value = true
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error loading image for editing:', error)
|
||||
errorMessage.value = t('errors.imageLoad')
|
||||
})
|
||||
if (fileUrl.value && fileUrl.value.startsWith('data:')) {
|
||||
// Only try to load the image if it's a data URL (newly selected image)
|
||||
const blob = dataURLtoBlob(fileUrl.value)
|
||||
selectedFile.value = new File([blob], 'current-image.jpg', { type: 'image/jpeg' })
|
||||
showCropper.value = true
|
||||
} else {
|
||||
// If no image is selected, using fallback, or have an existing uploaded image, trigger the file input
|
||||
triggerFileInput()
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to convert data URL to blob
|
||||
const dataURLtoBlob = (dataURL) => {
|
||||
const arr = dataURL.split(',')
|
||||
const mime = arr[0].match(/:(.*?);/)[1]
|
||||
const bstr = atob(arr[1])
|
||||
let n = bstr.length
|
||||
const u8arr = new Uint8Array(n)
|
||||
while (n--) {
|
||||
u8arr[n] = bstr.charCodeAt(n)
|
||||
}
|
||||
return new Blob([u8arr], { type: mime })
|
||||
}
|
||||
|
||||
const applyCrop = () => {
|
||||
if (!cropper.value) return
|
||||
|
||||
|
||||
@@ -108,7 +108,10 @@ const TARGET_HEIGHT = 200
|
||||
const { t } = useI18n();
|
||||
|
||||
const triggerFileInput = () => {
|
||||
fileInput.value.click()
|
||||
if (fileInput.value) {
|
||||
fileInput.value.value = '' // Reset the input value to ensure the change event fires
|
||||
fileInput.value.click()
|
||||
}
|
||||
}
|
||||
|
||||
const onFileSelected = (event) => {
|
||||
@@ -129,23 +132,30 @@ const onFileSelected = (event) => {
|
||||
}
|
||||
|
||||
const startEditing = () => {
|
||||
if (fileUrl.value && fileUrl.value !== fallbackUrl) {
|
||||
// Use our API client to get the image
|
||||
client.get(fileUrl.value, { responseType: 'blob' })
|
||||
.then(response => {
|
||||
const blob = response.data
|
||||
selectedFile.value = new File([blob], 'current-image.jpg', { type: 'image/jpeg' })
|
||||
// Create a local URL for the cropper
|
||||
fileUrl.value = URL.createObjectURL(blob)
|
||||
showCropper.value = true
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error loading image for editing:', error)
|
||||
errorMessage.value = t('errors.imageLoad')
|
||||
})
|
||||
if (fileUrl.value && fileUrl.value.startsWith('data:')) {
|
||||
// Only try to load the image if it's a data URL (newly selected image)
|
||||
const blob = dataURLtoBlob(fileUrl.value)
|
||||
selectedFile.value = new File([blob], 'current-image.jpg', { type: 'image/jpeg' })
|
||||
showCropper.value = true
|
||||
} else {
|
||||
// If no image is selected, using fallback, or have an existing uploaded image, trigger the file input
|
||||
triggerFileInput()
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to convert data URL to blob
|
||||
const dataURLtoBlob = (dataURL) => {
|
||||
const arr = dataURL.split(',')
|
||||
const mime = arr[0].match(/:(.*?);/)[1]
|
||||
const bstr = atob(arr[1])
|
||||
let n = bstr.length
|
||||
const u8arr = new Uint8Array(n)
|
||||
while (n--) {
|
||||
u8arr[n] = bstr.charCodeAt(n)
|
||||
}
|
||||
return new Blob([u8arr], { type: mime })
|
||||
}
|
||||
|
||||
const applyCrop = () => {
|
||||
if (!cropper.value) return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user