/
DragUpload.vue
133 lines (115 loc) · 3.36 KB
/
DragUpload.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!--
- GNU GENERAL PUBLIC LICENSE
- Version 3, 29 June 2007
-
- Copyright (C) 2024 Terwer, Inc. <https://terwer.space/>
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
-->
<script setup lang="ts">
import { UploadFilled } from "@element-plus/icons-vue"
import { onBeforeMount, onBeforeUnmount, reactive, ref } from "vue"
import { ElMessage, UploadRequestOptions } from "element-plus"
import { generateUniqueName, ImageItem, retrieveImageFromClipboardAsBlob } from "zhi-siyuan-picgo"
import { useVueI18n } from "$composables/useVueI18n.ts"
import { createAppLogger } from "@/utils/appLogger.ts"
import { usePicgoUpload } from "$composables/usePicgoUpload.ts"
const logger = createAppLogger("drag-upload")
// props
const props = defineProps({
pageId: {
type: String,
default: "",
},
picgoCommonData: {
type: Object,
default: null,
},
picgoCommonMethods: {
type: Object,
default: null,
},
})
// refs
const formData = reactive({
picgoCommonData: props.picgoCommonData,
picgoCommonMethods: props.picgoCommonMethods,
})
// uses
const { t } = useVueI18n()
const { picgoUploadMethods } = usePicgoUpload(props, { picgoCommonMethods: formData.picgoCommonMethods }, {})
const limit = ref(5)
const handleDragAction = async (file: Blob) => {
let res: any
try {
formData.picgoCommonData.isUploadLoading = true
const imageItem = new ImageItem(generateUniqueName(), file as any, true, "", "")
await picgoUploadMethods.doUploadImageToBed(imageItem)
res = {
success: true,
message: "",
}
} catch (e: any) {
const errMsg = t("main.opt.failure") + "=>" + e
logger.error(errMsg)
ElMessage.error(errMsg)
res = {
success: false,
message: errMsg,
}
} finally {
formData.picgoCommonData.isUploadLoading = false
}
return res
}
const customRequestHandler = (options: UploadRequestOptions): Promise<unknown> => {
return handleDragAction(options.file)
}
const handleExceed = (_e: any) => {
ElMessage.error("selected files exceed to upload limit " + limit.value)
}
const handlePasteAction = (e: any) => {
e.preventDefault()
retrieveImageFromClipboardAsBlob(e, function (imageBlob: any) {
if (imageBlob && imageBlob instanceof Blob) {
const file = new File([imageBlob], "image.png", { type: "image/png" })
handleDragAction(file)
} else {
ElMessage.error("image not found in browser clipboard")
}
})
}
onBeforeMount(() => {
window.addEventListener("paste", handlePasteAction)
})
onBeforeUnmount(() => {
window.removeEventListener("paste", handlePasteAction)
})
</script>
<template>
<div>
<el-upload
class="drag-upload"
drag
:http-request="customRequestHandler"
multiple
:limit="limit"
accept="image/png,image/jpg,image/jpeg,image/gif,image/webp,image/svg"
:on-exceed="handleExceed"
>
<el-icon class="el-icon--upload">
<upload-filled />
</el-icon>
<div class="el-upload__text">
{{ t("upload.select.tip1") }} <em>{{ t("upload.select.tip2") }}</em>
</div>
<template #tip>
<div class="el-upload__tip">{{ t("upload.select.limit") }} 5</div>
</template>
</el-upload>
</div>
</template>
<style scoped lang="stylus">
.drag-upload
margin-bottom 10px
</style>