From 0e4673fcfc94a5c4b7d02699dc244a74fd1cc7d3 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Thu, 5 Jul 2018 22:15:31 +0900
Subject: [PATCH] refs #417 Allow drop file to upload the media to mastodon
---
src/renderer/components/TimelineSpace.vue | 59 ++++++++++++++++++-
.../TimelineSpace/Modals/NewToot.vue | 34 ++---------
.../components/TimelineSpace/ReceiveDrop.vue | 48 +++++++++++++++
.../store/TimelineSpace/Modals/NewToot.js | 12 +++-
4 files changed, 120 insertions(+), 33 deletions(-)
create mode 100644 src/renderer/components/TimelineSpace/ReceiveDrop.vue
diff --git a/src/renderer/components/TimelineSpace.vue b/src/renderer/components/TimelineSpace.vue
index 96ec6858ab..c3dadcef33 100644
--- a/src/renderer/components/TimelineSpace.vue
+++ b/src/renderer/components/TimelineSpace.vue
@@ -4,7 +4,8 @@
v-loading="loading"
element-loading-text="Loading..."
element-loading-spinner="el-icon-loading"
- element-loading-background="rgba(0, 0, 0, 0.8)">
+ element-loading-background="rgba(0, 0, 0, 0.8)"
+ >
+
@@ -23,10 +25,17 @@ import HeaderMenu from './TimelineSpace/HeaderMenu'
import Contents from './TimelineSpace/Contents'
import Modals from './TimelineSpace/Modals'
import Mousetrap from 'mousetrap'
+import ReceiveDrop from './TimelineSpace/ReceiveDrop'
export default {
name: 'timeline-space',
- components: { SideMenu, HeaderMenu, Modals, Contents },
+ components: { SideMenu, HeaderMenu, Modals, Contents, ReceiveDrop },
+ data () {
+ return {
+ dropTarget: null,
+ droppableVisible: false
+ }
+ },
computed: {
...mapState({
loading: state => state.TimelineSpace.loading,
@@ -42,11 +51,19 @@ export default {
})
},
mounted () {
+ window.addEventListener('dragenter', this.onDragEnter)
+ window.addEventListener('dragleave', this.onDragLeave)
+ window.addEventListener('dragover', this.onDragOver)
+ window.addEventListener('drop', this.handleDrop)
Mousetrap.bind(['command+t', 'ctrl+t'], () => {
this.$store.commit('TimelineSpace/Modals/Jump/changeModal', true)
})
},
beforeDestroy () {
+ window.removeEventListener('dragenter', this.onDragEnter)
+ window.removeEventListener('dragleave', this.onDragLeave)
+ window.removeEventListener('dragover', this.onDragOver)
+ window.removeEventListener('drop', this.handleDrop)
this.$store.dispatch('TimelineSpace/stopUserStreaming')
this.$store.dispatch('TimelineSpace/stopLocalStreaming')
},
@@ -103,6 +120,44 @@ export default {
})
})
this.$store.dispatch('TimelineSpace/startLocalStreaming', account)
+ },
+ handleDrop (e) {
+ e.preventDefault()
+ e.stopPropagation()
+ this.droppableVisible = false
+ if (e.dataTransfer.files.item(0) === null || e.dataTransfer.files.item(0) === undefined) {
+ return false
+ }
+ const file = e.dataTransfer.files.item(0)
+ if (!file.type.includes('image') && !file.type.includes('video')) {
+ this.$message({
+ message: 'You can only attach images or videos',
+ type: 'error'
+ })
+ return false
+ }
+ this.$store.dispatch('TimelineSpace/Modals/NewToot/openModal')
+ this.$store.dispatch('TimelineSpace/Modals/NewToot/incrementMediaId')
+ this.$store.dispatch('TimelineSpace/Modals/NewToot/uploadImage', file)
+ .catch(() => {
+ this.$message({
+ message: 'Could not attach the file',
+ type: 'error'
+ })
+ })
+ return false
+ },
+ onDragEnter (e) {
+ this.dropTarget = e.target
+ this.droppableVisible = true
+ },
+ onDragLeave (e) {
+ if (e.target === this.dropTarget) {
+ this.droppableVisible = false
+ }
+ },
+ onDragOver (e) {
+ e.preventDefault()
}
}
}
diff --git a/src/renderer/components/TimelineSpace/Modals/NewToot.vue b/src/renderer/components/TimelineSpace/Modals/NewToot.vue
index 7a4d8b74e7..246ee79f35 100644
--- a/src/renderer/components/TimelineSpace/Modals/NewToot.vue
+++ b/src/renderer/components/TimelineSpace/Modals/NewToot.vue
@@ -21,7 +21,7 @@