101 lines
2.0 KiB
Vue
101 lines
2.0 KiB
Vue
<script setup lang="ts">
|
||
import { ref } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
|
||
interface Props {
|
||
hint?: string
|
||
disabled?: boolean
|
||
}
|
||
|
||
withDefaults(defineProps<Props>(), {
|
||
hint: '支持图片/文档/压缩包,大小不超过1G',
|
||
disabled: false,
|
||
})
|
||
|
||
const emit = defineEmits<{ upload: [file: File] }>()
|
||
|
||
const inputRef = ref<HTMLInputElement | null>(null)
|
||
const dragging = ref(false)
|
||
|
||
const MAX_SIZE = 1024 * 1024 * 1024
|
||
|
||
function pick(): void {
|
||
inputRef.value?.click()
|
||
}
|
||
|
||
function handleFiles(list: FileList | null): void {
|
||
if (!list) return
|
||
for (const file of Array.from(list)) {
|
||
if (file.size > MAX_SIZE) {
|
||
ElMessage.warning(`「${file.name}」超过1G,未添加`)
|
||
continue
|
||
}
|
||
emit('upload', file)
|
||
}
|
||
}
|
||
|
||
function onDrop(event: DragEvent): void {
|
||
dragging.value = false
|
||
handleFiles(event.dataTransfer?.files ?? null)
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div
|
||
class="upload-area"
|
||
:class="{ 'is-dragging': dragging, 'is-disabled': disabled }"
|
||
@click="pick"
|
||
@dragover.prevent="dragging = true"
|
||
@dragleave.prevent="dragging = false"
|
||
@drop.prevent="onDrop"
|
||
>
|
||
<span class="upload-area__icon">⬆</span>
|
||
<span class="upload-area__hint text-secondary">{{ hint }}</span>
|
||
<input
|
||
ref="inputRef"
|
||
type="file"
|
||
multiple
|
||
class="upload-area__input"
|
||
:disabled="disabled"
|
||
@change="handleFiles(($event.target as HTMLInputElement).files)"
|
||
>
|
||
</div>
|
||
</template>
|
||
|
||
<style lang="less" scoped>
|
||
.upload-area {
|
||
.flex(column, center);
|
||
gap: @spacing-xs;
|
||
min-height: 69px;
|
||
padding: @spacing-base;
|
||
border: none;
|
||
border-radius: @radius-sm;
|
||
background: @demand-upload-bg;
|
||
cursor: pointer;
|
||
transition: background 0.2s;
|
||
|
||
&:hover,
|
||
&.is-dragging {
|
||
background: darken(@demand-upload-bg, 5%);
|
||
}
|
||
|
||
&.is-disabled {
|
||
opacity: 0.5;
|
||
cursor: not-allowed;
|
||
}
|
||
|
||
&__icon {
|
||
font-size: @font-size-lg;
|
||
color: @text-color-disabled;
|
||
}
|
||
|
||
&__hint {
|
||
font-size: @font-size-sm;
|
||
}
|
||
|
||
&__input {
|
||
display: none;
|
||
}
|
||
}
|
||
</style>
|