Whalebird-desktop-client-ma.../src/renderer/components/TimelineSpace/Modals/NewToot.vue

232 lines
5.7 KiB
Vue
Raw Normal View History

2018-03-12 17:42:47 +01:00
<template>
<el-dialog
title="New Toot"
:visible.sync="newTootModal"
width="400px"
class="new-toot-modal">
<el-form v-on:submit.prevent="toot">
<div class="status">
<textarea v-model="status" ref="status" v-shortkey="{linux: ['ctrl', 'enter'], mac: ['meta', 'enter']}" @shortkey="toot()"></textarea>
</div>
2018-03-12 17:42:47 +01:00
</el-form>
<div class="preview">
<div class="image-wrapper" v-for="media in attachedMedias" v-on:key="media.id">
<img :src="media.preview_url" class="preview-image" />
2018-03-29 08:49:39 +02:00
<el-button size="small" type="text" @click="removeAttachment(media)" class="remove-image"><icon name="times-circle"></icon></el-button>
</div>
</div>
<div slot="footer" class="dialog-footer">
<div class="upload-image">
<el-button size="small" type="text" @click="selectImage"><icon name="camera"></icon></el-button>
2018-03-29 08:49:39 +02:00
<input name="image" type="file" class="image-input" ref="image" @change="updateImage" :key="attachedImageId"/>
</div>
<span class="text-count">{{ 500 - status.length }}</span>
2018-03-13 00:41:03 +01:00
<el-button @click="close">Cancel</el-button>
<el-button type="primary" @click="toot" v-loading="blockSubmit">Toot</el-button>
2018-03-29 08:49:39 +02:00
<div class="clearfix"></div>
</div>
2018-03-12 17:42:47 +01:00
</el-dialog>
</template>
<script>
import { mapState } from 'vuex'
2018-03-12 17:42:47 +01:00
export default {
name: 'new-toot',
data () {
return {
2018-03-29 08:49:39 +02:00
attachedImageId: 0
}
},
2018-03-12 17:42:47 +01:00
computed: {
...mapState({
replyToId: (state) => {
if (state.TimelineSpace.Modals.NewToot.replyToMessage !== null) {
return state.TimelineSpace.Modals.NewToot.replyToMessage.id
} else {
return null
}
},
attachedMedias: state => state.TimelineSpace.Modals.NewToot.attachedMedias,
blockSubmit: state => state.TimelineSpace.Modals.NewToot.blockSubmit
}),
2018-03-12 17:42:47 +01:00
newTootModal: {
get () {
return this.$store.state.TimelineSpace.Modals.NewToot.modalOpen
2018-03-12 17:42:47 +01:00
},
set (value) {
this.$store.dispatch('TimelineSpace/Modals/NewToot/changeModal', value)
2018-03-12 17:42:47 +01:00
}
},
status: {
get () {
return this.$store.state.TimelineSpace.Modals.NewToot.status
},
set (value) {
this.$store.commit('TimelineSpace/Modals/NewToot/updateStatus', value)
}
2018-03-12 17:42:47 +01:00
}
2018-03-13 00:41:03 +01:00
},
updated () {
if (this.newTootModal) {
this.$refs.status.focus()
}
},
2018-03-13 00:41:03 +01:00
methods: {
close () {
2018-03-29 08:49:39 +02:00
this.resetImage()
this.$store.dispatch('TimelineSpace/Modals/NewToot/changeModal', false)
},
2018-03-13 00:41:03 +01:00
toot () {
if (this.status.length <= 0 || this.status.length >= 500) {
2018-03-13 02:52:28 +01:00
return this.$message({
message: 'Toot length should be 1 to 500',
type: 'error'
})
}
let form = {
status: this.status
}
if (this.replyToId !== null) {
form = Object.assign(form, {
in_reply_to_id: this.replyToId
})
}
2018-03-29 06:16:15 +02:00
if (this.attachedMedias.length > 0) {
if (this.attachedMedias.length > 4) {
return this.$message({
message: 'You can only attach up to 4 images',
type: 'error'
})
}
form = Object.assign(form, {
media_ids: this.attachedMedias.map((m) => { return m.id })
})
}
this.$store.dispatch('TimelineSpace/Modals/NewToot/postToot', form)
2018-03-13 00:41:03 +01:00
.then(() => {
2018-03-26 13:04:29 +02:00
this.close()
2018-03-13 00:41:03 +01:00
this.$message({
message: 'Toot',
type: 'success'
})
})
.catch(() => {
this.$message({
message: 'Could not toot',
type: 'error'
})
})
},
selectImage () {
this.$refs.image.click()
},
updateImage (e) {
2018-03-29 08:49:39 +02:00
this.resetImage()
if (e.target.files.item(0) === null || e.target.files.item(0) === undefined) {
return
}
if (!e.target.files.item(0).type.includes('image')) {
this.$message({
message: 'You can only attach images',
type: 'error'
})
return
}
this.$store.dispatch('TimelineSpace/Modals/NewToot/uploadImage', e.target.files.item(0))
.catch(() => {
this.$message({
message: 'Could not attach the file',
type: 'error'
})
})
},
removeAttachment (media) {
2018-03-29 08:49:39 +02:00
this.$store.commit('TimelineSpace/Modals/NewToot/removeMedia', media)
},
resetImage () {
++this.attachedImageId
2018-03-13 00:41:03 +01:00
}
2018-03-12 17:42:47 +01:00
}
}
</script>
<style lang="scss" scoped>
.new-toot-modal /deep/ {
2018-03-12 17:42:47 +01:00
.el-dialog__header {
background-color: #4a5664;
.el-dialog__title {
color: #ebeef5;
}
}
.el-dialog__body {
padding: 0;
.status {
2018-03-12 17:42:47 +01:00
textarea {
display: block;
padding: 5px 15px;
line-height: 1.5;
box-sizing: border-box;
width: 100%;
font-size: inherit;
color: #606266;
background-image: none;
2018-03-12 17:42:47 +01:00
border: 0;
border-radius: 4px;
2018-03-12 17:42:47 +01:00
resize: none;
height: 120px;
transition: border-color .2s cubic-bezier(.645,.045,.355,1);
}
textarea:focus {
outline: 0;
2018-03-12 17:42:47 +01:00
}
}
.preview {
box-sizing: border-box;
padding: 0 12px;
.image-wrapper {
position: relative;
display: inline-block;
.preview-image {
width: 60px;
2018-03-29 08:49:39 +02:00
margin-right: 8px;
}
.remove-image {
position: absolute;
top: 0;
left: 0;
2018-03-29 08:49:39 +02:00
padding: 0;
cursor: pointer;
}
}
}
2018-03-12 17:42:47 +01:00
}
.el-dialog__footer {
background-color: #f2f6fc;
2018-03-13 02:52:28 +01:00
.upload-image {
text-align: left;
2018-03-29 08:49:39 +02:00
float: left;
.image-input {
display: none;
}
}
2018-03-13 02:52:28 +01:00
.text-count {
padding-right: 24px;
color: #909399;
}
2018-03-12 17:42:47 +01:00
}
}
</style>