Add paste event listener

This commit is contained in:
kPherox 2018-08-20 12:22:35 +09:00
parent 9ffbc10288
commit e8789f2a95
No known key found for this signature in database
GPG Key ID: C04751C2BFA2F62D
2 changed files with 22 additions and 0 deletions

View File

@ -12,6 +12,7 @@
<Status
v-model="status"
:opened="newTootModal"
@paste="onPaste"
@toot="toot"
/>
</el-form>
@ -70,6 +71,7 @@
<script>
import { mapState } from 'vuex'
import { clipboard } from 'electron'
import Visibility from '~/src/constants/visibility'
import Status from './NewToot/Status'
@ -217,6 +219,22 @@ export default {
}
this.updateImage(file)
},
onPaste (e) {
const mimeTypes = clipboard.availableFormats().filter(type => type.startsWith('image'))
if (mimeTypes.length === 0) {
return
}
e.preventDefault()
const image = clipboard.readImage()
let data
if (/^image\/jpe?g$/.test(mimeTypes[0])) {
data = image.toJPEG(100)
} else {
data = image.toPNG()
}
const file = new File([data], 'clipboard.picture', { type: mimeTypes[0] })
this.updateImage(file)
},
updateImage (file) {
this.$store.dispatch('TimelineSpace/Modals/NewToot/incrementMediaId')
this.$store.dispatch('TimelineSpace/Modals/NewToot/uploadImage', file)

View File

@ -5,6 +5,7 @@
ref="status"
v-shortkey="openSuggest ? {up: ['arrowup'], down: ['arrowdown'], enter: ['enter'], esc: ['esc']} : {linux: ['ctrl', 'enter'], mac: ['meta', 'enter'], left: ['arrowleft'], right: ['arrowright']}"
@shortkey="handleKey"
@paste="onPaste"
v-on:input="startSuggest"
:placeholder="$t('modals.new_toot.status')"
autofocus>
@ -162,6 +163,9 @@ export default {
const item = this.filteredSuggestion[this.highlightedIndex]
this.insertItem(item)
},
onPaste (e) {
this.$emit('paste', e)
},
handleKey (event) {
const current = event.target.selectionStart
switch (event.srcKey) {