PicList/src/renderer/pages/Upload.vue

357 lines
8.8 KiB
Vue
Raw Normal View History

2017-11-27 19:21:12 -05:00
<template>
<div id="upload-view">
2017-11-28 10:56:15 -05:00
<el-row :gutter="16">
<el-col
:span="20"
:offset="2"
>
2017-11-28 10:56:15 -05:00
<div class="view-title">
{{ $T('PICTURE_UPLOAD') }} - {{ picBedName }}
<el-icon
style="cursor: pointer; margin-left: 4px;"
@click="handleChangePicBed"
>
<CaretBottom />
</el-icon>
2017-11-28 10:56:15 -05:00
</div>
<div
id="upload-area"
2018-12-10 04:13:48 -05:00
:class="{ 'is-dragover': dragover }"
@drop.prevent="onDrop"
@dragover.prevent="dragover = true"
@dragleave.prevent="dragover = false"
2017-11-28 10:56:15 -05:00
>
<div
id="upload-dragger"
@click="openUplodWindow"
>
<el-icon>
<UploadFilled />
</el-icon>
2017-11-28 10:56:15 -05:00
<div class="upload-dragger__text">
{{ $T('DRAG_FILE_TO_HERE') }} <span>{{ $T('CLICK_TO_UPLOAD') }}</span>
2017-11-28 10:56:15 -05:00
</div>
<input
id="file-uploader"
type="file"
multiple
@change="onChange"
>
2017-11-28 10:56:15 -05:00
</div>
</div>
2019-12-19 06:17:21 -05:00
<el-progress
:percentage="progress"
:show-text="false"
2017-11-28 10:56:15 -05:00
class="upload-progress"
:class="{ 'show': showProgress }"
2019-12-19 06:17:21 -05:00
:status="showError ? 'exception' : undefined"
/>
2017-11-28 10:56:15 -05:00
<div class="paste-style">
<div class="el-col-16">
<div class="paste-style__text">
{{ $T('LINK_FORMAT') }}
</div>
<el-radio-group
v-model="pasteStyle"
size="small"
@change="handlePasteStyleChange"
>
<el-radio-button label="markdown">
Markdown
</el-radio-button>
<el-radio-button label="HTML" />
<el-radio-button label="URL" />
<el-radio-button label="UBB" />
<el-radio-button
label="Custom"
:title="$T('CUSTOM')"
/>
</el-radio-group>
</div>
<div class="el-col-8">
<div class="paste-style__text">
{{ $T('QUICK_UPLOAD') }}
</div>
<el-button
type="primary"
round
size="small"
class="quick-upload"
style="width: 50%"
@click="uploadClipboardFiles"
>
{{ $T('CLIPBOARD_PICTURE') }}
</el-button>
<el-button
type="primary"
round
size="small"
class="quick-upload"
style="width: 46%; margin-left: 6px"
@click="uploadURLFiles"
>
URL
</el-button>
2017-11-28 10:56:15 -05:00
</div>
</div>
</el-col>
</el-row>
2017-11-27 19:21:12 -05:00
</div>
</template>
<script lang="ts" setup>
// import { Component, Vue, Watch } from 'vue-property-decorator'
import { UploadFilled, CaretBottom } from '@element-plus/icons-vue'
2019-12-19 06:17:21 -05:00
import {
ipcRenderer,
IpcRendererEvent
2019-12-19 06:17:21 -05:00
} from 'electron'
import { ref, onBeforeMount, onBeforeUnmount, watch } from 'vue'
import { T as $T } from '@/i18n'
import $bus from '@/utils/bus'
import {
SHOW_INPUT_BOX,
SHOW_INPUT_BOX_RESPONSE,
SHOW_UPLOAD_PAGE_MENU,
GET_PICBEDS
} from '~/universal/events/constants'
2020-02-23 04:48:12 -05:00
import {
isUrl
} from '~/universal/utils/common'
import { ElMessage as $message } from 'element-plus'
import { getConfig, saveConfig, sendToMain } from '@/utils/dataSender'
const dragover = ref(false)
const progress = ref(0)
const showProgress = ref(false)
const showError = ref(false)
const pasteStyle = ref('')
const picBed = ref<IPicBedType[]>([])
const picBedName = ref('')
onBeforeMount(() => {
ipcRenderer.on('uploadProgress', (event: IpcRendererEvent, _progress: number) => {
if (_progress !== -1) {
showProgress.value = true
progress.value = _progress
} else {
progress.value = 100
showError.value = true
}
})
getPasteStyle()
getDefaultPicBed()
ipcRenderer.on('syncPicBed', () => {
getDefaultPicBed()
})
sendToMain(GET_PICBEDS)
ipcRenderer.on(GET_PICBEDS, getPicBeds)
$bus.on(SHOW_INPUT_BOX_RESPONSE, handleInputBoxValue)
2019-12-19 06:17:21 -05:00
})
watch(progress, onProgressChange)
function onProgressChange (val: number) {
if (val === 100) {
setTimeout(() => {
showProgress.value = false
showError.value = false
}, 1000)
setTimeout(() => {
progress.value = 0
}, 1200)
2019-12-19 06:17:21 -05:00
}
}
onBeforeUnmount(() => {
$bus.off(SHOW_INPUT_BOX_RESPONSE)
ipcRenderer.removeAllListeners('uploadProgress')
ipcRenderer.removeAllListeners('syncPicBed')
ipcRenderer.removeListener(GET_PICBEDS, getPicBeds)
})
function onDrop (e: DragEvent) {
dragover.value = false
const items = e.dataTransfer?.items!
const files = e.dataTransfer?.files!
// send files first
if (files?.length) {
ipcSendFiles(e.dataTransfer?.files!)
} else {
if (items.length === 2 && items[0].type === 'text/uri-list') {
handleURLDrag(items, e.dataTransfer!)
} else if (items[0].type === 'text/plain') {
const str = e.dataTransfer!.getData(items[0].type)
if (isUrl(str)) {
sendToMain('uploadChoosedFiles', [{ path: str }])
} else {
$message.error($T('TIPS_DRAG_VALID_PICTURE_OR_URL'))
}
}
2019-12-19 06:17:21 -05:00
}
}
function handleURLDrag (items: DataTransferItemList, dataTransfer: DataTransfer) {
// text/html
// Use this data to get a more precise URL
const urlString = dataTransfer.getData(items[1].type)
const urlMatch = urlString.match(/<img.*src="(.*?)"/)
if (urlMatch) {
sendToMain('uploadChoosedFiles', [
{
path: urlMatch[1]
}
])
} else {
$message.error($T('TIPS_DRAG_VALID_PICTURE_OR_URL'))
2019-12-19 06:17:21 -05:00
}
}
function openUplodWindow () {
document.getElementById('file-uploader')!.click()
}
function onChange (e: any) {
ipcSendFiles(e.target.files);
(document.getElementById('file-uploader') as HTMLInputElement).value = ''
}
function ipcSendFiles (files: FileList) {
const sendFiles: IFileWithPath[] = []
Array.from(files).forEach((item) => {
const obj = {
name: item.name,
path: item.path
}
sendFiles.push(obj)
})
sendToMain('uploadChoosedFiles', sendFiles)
}
async function getPasteStyle () {
pasteStyle.value = await getConfig('settings.pasteStyle') || 'markdown'
}
function handlePasteStyleChange (val: string | number | boolean) {
saveConfig({
'settings.pasteStyle': val
})
}
function uploadClipboardFiles () {
sendToMain('uploadClipboardFilesFromUploadPage')
}
async function uploadURLFiles () {
const str = await navigator.clipboard.readText()
$bus.emit(SHOW_INPUT_BOX, {
value: isUrl(str) ? str : '',
title: $T('TIPS_INPUT_URL'),
placeholder: $T('TIPS_HTTP_PREFIX')
})
}
function handleInputBoxValue (val: string) {
if (val === '') return
if (isUrl(val)) {
sendToMain('uploadChoosedFiles', [{
path: val
}])
} else {
$message.error($T('TIPS_INPUT_VALID_URL'))
2019-12-19 06:17:21 -05:00
}
}
async function getDefaultPicBed () {
const currentPicBed = await getConfig<string>('picBed.current')
picBed.value.forEach(item => {
if (item.type === currentPicBed) {
picBedName.value = item.name
}
})
}
function getPicBeds (event: Event, picBeds: IPicBedType[]) {
picBed.value = picBeds
getDefaultPicBed()
}
async function handleChangePicBed () {
sendToMain(SHOW_UPLOAD_PAGE_MENU)
}
</script>
<script lang="ts">
export default {
name: 'UploadPage'
2017-11-27 19:21:12 -05:00
}
</script>
<style lang='stylus'>
2017-11-28 10:56:15 -05:00
.view-title
display flex
2017-11-28 10:56:15 -05:00
color #eee
font-size 20px
text-align center
margin 10px auto
align-items center
justify-content center
2017-11-28 10:56:15 -05:00
#upload-view
height 100%
2018-12-27 22:10:28 -05:00
.view-title
margin 20px auto
2017-11-28 10:56:15 -05:00
#upload-area
height 400px
2017-11-28 10:56:15 -05:00
border 2px dashed #dddddd
border-radius 8px
text-align center
width 800px
2018-05-02 02:24:59 -04:00
margin 0 auto
2017-11-28 10:56:15 -05:00
color #dddddd
cursor pointer
transition all .2s ease-in-out
align-items center
2017-11-28 10:56:15 -05:00
#upload-dragger
height 100%
&.is-dragover,
&:hover
border 2px dashed #A4D8FA
background-color rgba(164, 216, 250, 0.3)
color #fff
i
font-size 66px
margin 50px 0 16px
line-height 66px
span
color #409EFF
#file-uploader
display none
.upload-progress
opacity 0
transition all .2s ease-in-out
2018-03-09 10:24:48 -05:00
width 450px
2018-05-08 02:04:43 -04:00
margin 20px auto 0
2017-11-28 10:56:15 -05:00
&.show
opacity 1
.el-progress-bar__inner
transition all .2s ease-in-out
.paste-style
text-align center
margin-top 16px
display flex
align-items flex-end
2017-11-28 10:56:15 -05:00
&__text
font-size 12px
color #eeeeee
margin-bottom 4px
.el-radio-button:first-child
.el-radio-button__inner
border-left none
2017-11-29 03:23:05 -05:00
.el-radio-button:first-child
.el-radio-button__inner
border-left none
border-radius 14px 0 0 14px
.el-radio-button:last-child
.el-radio-button__inner
border-left none
border-radius 0 14px 14px 0
2019-12-19 06:17:21 -05:00
.el-icon-caret-bottom
cursor pointer
</style>