Files
docuseal/app/javascript/template_builder/dropzone.vue
T

160 lines
3.9 KiB
Vue
Raw Normal View History

2023-07-01 00:15:15 +03:00
<template>
<div
class="flex h-60 w-full"
@dragover.prevent
2025-04-25 21:19:28 +03:00
@dragenter="isDragEntering = true"
@dragleave="isDragEntering = false"
2023-07-01 00:15:15 +03:00
@drop.prevent="onDropFiles"
>
<label
2024-07-09 23:47:46 +03:00
id="document_dropzone"
2025-04-25 21:19:28 +03:00
class="w-full relative rounded-md border-2 border-base-content/10 border-dashed"
2023-07-01 00:15:15 +03:00
:for="inputId"
2025-05-15 13:59:06 +03:00
:class="{ 'opacity-50': isLoading, 'hover:bg-base-200/50': withHoverClass && !isDragEntering, 'bg-base-200/50 border-base-content/30': isDragEntering }"
2023-07-01 00:15:15 +03:00
>
2025-04-25 21:19:28 +03:00
<div class="absolute top-0 right-0 left-0 bottom-0 flex items-center justify-center pointer-events-none">
2023-07-01 00:15:15 +03:00
<div class="flex flex-col items-center">
<IconInnerShadowTop
2025-05-09 00:01:50 +03:00
v-if="isLoading"
2023-07-01 00:15:15 +03:00
class="animate-spin"
:width="40"
:height="40"
/>
2025-04-25 21:19:28 +03:00
<component
:is="icon"
2023-07-01 00:15:15 +03:00
v-else
2025-05-10 13:03:52 +03:00
class="stroke-[1.5px]"
2023-07-01 00:15:15 +03:00
:width="40"
:height="40"
/>
<div
v-if="message"
2025-05-10 13:03:52 +03:00
class="text-lg mb-1"
:class="{ 'mt-1': !withDescription, 'font-medium': withDescription }"
2023-07-01 00:15:15 +03:00
>
{{ message }}
</div>
2025-04-25 21:19:28 +03:00
<div
v-if="withDescription"
class="text-sm"
>
2024-01-04 01:05:36 +02:00
<span class="font-medium">{{ t('click_to_upload') }}</span> {{ t('or_drag_and_drop_files') }}
2023-07-01 00:15:15 +03:00
</div>
</div>
</div>
<form
ref="form"
2023-07-01 00:15:15 +03:00
class="hidden"
>
<input
:id="inputId"
ref="input"
type="file"
name="files[]"
2023-09-05 20:33:40 +03:00
:accept="acceptFileTypes"
multiple
@change="upload"
>
</form>
2023-07-01 00:15:15 +03:00
</label>
</div>
</template>
<script>
import Upload from './upload'
2025-04-25 21:19:28 +03:00
import { IconCloudUpload, IconFilePlus, IconFileSymlink, IconFiles, IconInnerShadowTop } from '@tabler/icons-vue'
2023-07-01 00:15:15 +03:00
export default {
name: 'FileDropzone',
components: {
2025-04-25 21:19:28 +03:00
IconFilePlus,
2023-07-01 00:15:15 +03:00
IconCloudUpload,
2025-04-25 21:19:28 +03:00
IconInnerShadowTop,
IconFileSymlink,
IconFiles
2023-07-01 00:15:15 +03:00
},
2024-01-04 01:05:36 +02:00
inject: ['baseFetch', 't'],
2023-07-01 00:15:15 +03:00
props: {
templateId: {
type: [Number, String],
required: true
},
2025-05-15 13:59:06 +03:00
withHoverClass: {
type: Boolean,
required: false,
default: true
},
2025-04-25 21:19:28 +03:00
icon: {
type: String,
required: false,
default: 'IconCloudUpload'
},
cloneTemplateOnUpload: {
type: Boolean,
required: false,
default: false
},
withDescription: {
type: Boolean,
required: false,
default: true
},
2025-05-09 00:01:50 +03:00
title: {
type: String,
2025-04-25 21:19:28 +03:00
required: false,
2025-05-09 00:01:50 +03:00
default: ''
2025-04-25 21:19:28 +03:00
},
2023-09-05 20:33:40 +03:00
acceptFileTypes: {
type: String,
required: false,
default: 'image/*, application/pdf'
2023-07-01 00:15:15 +03:00
}
},
2025-05-09 00:01:50 +03:00
emits: ['success', 'error', 'loading'],
2023-07-01 00:15:15 +03:00
data () {
return {
isLoading: false,
2025-04-25 21:19:28 +03:00
isDragEntering: false
2023-07-01 00:15:15 +03:00
}
},
computed: {
inputId () {
return 'el' + Math.random().toString(32).split('.')[1]
},
2025-04-25 21:19:28 +03:00
uploadUrl () {
if (this.cloneTemplateOnUpload) {
2025-05-09 00:01:50 +03:00
return `/templates/${this.templateId}/clone_and_replace`
2025-04-25 21:19:28 +03:00
} else {
return `/templates/${this.templateId}/documents`
}
},
2023-07-01 00:15:15 +03:00
message () {
if (this.isLoading) {
2024-01-04 01:05:36 +02:00
return this.t('uploading')
2023-09-05 20:33:40 +03:00
} else if (this.acceptFileTypes === 'image/*, application/pdf') {
2025-05-09 00:01:50 +03:00
return this.title || this.t('add_pdf_documents_or_images')
2023-09-05 20:33:40 +03:00
} else {
2025-05-09 00:01:50 +03:00
return this.title || this.t('add_documents_or_images')
2023-07-01 00:15:15 +03:00
}
}
},
2025-04-25 21:19:28 +03:00
watch: {
isLoading (value) {
this.$emit('loading', value)
}
},
2023-07-01 00:15:15 +03:00
methods: {
upload: Upload.methods.upload,
onDropFiles (e) {
2023-09-05 20:33:40 +03:00
if (this.acceptFileTypes !== 'image/*, application/pdf' || [...e.dataTransfer.files].every((f) => f.type.match(/(?:image\/)|(?:application\/pdf)/))) {
2023-08-04 22:10:49 +03:00
this.$refs.input.files = e.dataTransfer.files
2023-07-01 00:15:15 +03:00
2023-08-04 22:10:49 +03:00
this.upload()
} else {
2024-09-23 15:22:07 +03:00
alert(this.t('only_pdf_and_images_are_supported'))
2023-08-04 22:10:49 +03:00
}
2023-07-01 00:15:15 +03:00
}
}
}
</script>