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

453 lines
13 KiB
Vue
Raw Normal View History

2018-03-12 17:42:47 +01:00
<template>
<el-dialog
:title="$t('modals.new_toot.title')"
2018-03-12 17:42:47 +01:00
:visible.sync="newTootModal"
:before-close="closeConfirm"
2018-03-12 17:42:47 +01:00
width="400px"
class="new-toot-modal">
2018-10-21 04:17:59 +02:00
<el-form v-on:submit.prevent="toot" role="form">
<div class="spoiler" v-show="showContentWarning">
<el-input :placeholder="$t('modals.new_toot.cw')" v-model="spoiler"></el-input>
</div>
<Status
v-model="status"
:opened="newTootModal"
:fixCursorPos="hashtagInserting"
2018-08-20 05:22:35 +02:00
@paste="onPaste"
@toot="toot"
/>
2018-03-12 17:42:47 +01:00
</el-form>
<div class="preview">
2018-04-09 14:10:25 +02:00
<div class="image-wrapper" v-for="media in attachedMedias" v-bind:key="media.id">
<img :src="media.preview_url" class="preview-image" />
2018-11-19 15:56:49 +01:00
<el-button type="text" @click="removeAttachment(media)" class="remove-image"><icon name="times-circle"></icon></el-button>
<textarea
maxlength="420"
class="image-description"
:placeholder="$t('modals.new_toot.description')"
role="textbox"
contenteditable="true"
aria-multiline="true">
</textarea>
</div>
</div>
<div slot="footer" class="dialog-footer">
<div class="upload-image">
2018-08-21 02:18:11 +02:00
<el-button size="small" type="text" @click="selectImage" :title="$t('modals.new_toot.add_image')">
<icon name="camera"></icon>
</el-button>
<input name="image" type="file" class="image-input" ref="image" @change="onChangeImage" :key="attachedMediaId"/>
</div>
<div class="privacy">
<el-dropdown trigger="click" @command="changeVisibility">
2018-08-21 02:18:11 +02:00
<el-button size="small" type="text" :title="$t('modals.new_toot.change_visibility')">
<icon :name="visibilityIcon"></icon>
</el-button>
<el-dropdown-menu slot="dropdown">
2018-08-10 17:59:48 +02:00
<el-dropdown-item :command="visibilityList.Public.value">
<icon name="globe" class="privacy-icon"></icon>
{{ $t(visibilityList.Public.name) }}
2018-08-10 17:59:48 +02:00
</el-dropdown-item>
<el-dropdown-item :command="visibilityList.Unlisted.value">
<icon name="unlock" class="privacy-icon"></icon>
{{ $t(visibilityList.Unlisted.name) }}
2018-08-10 17:59:48 +02:00
</el-dropdown-item>
<el-dropdown-item :command="visibilityList.Private.value">
<icon name="lock" class="privacy-icon"></icon>
{{ $t(visibilityList.Private.name) }}
2018-08-10 17:59:48 +02:00
</el-dropdown-item>
<el-dropdown-item :command="visibilityList.Direct.value">
<icon name="envelope" class="privacy-icon" scale="0.8"></icon>
{{ $t(visibilityList.Direct.name) }}
2018-08-10 17:59:48 +02:00
</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</div>
<div class="sensitive" v-show="attachedMedias.length > 0">
2018-08-21 02:18:11 +02:00
<el-button size="small" type="text" @click="changeSensitive" :title="$t('modals.new_toot.change_sensitive')">
<icon name="eye-slash" v-show="sensitive"></icon>
<icon name="eye" v-show="!sensitive"></icon>
</el-button>
</div>
<div class="content-warning">
<el-button size="small" type="text" @click="showContentWarning = !showContentWarning" :title="$t('modals.new_toot.add_cw')" :class="showContentWarning? '' : 'clickable'">
2018-09-18 15:19:11 +02:00
<span class="cw-text">CW</span>
</el-button>
</div>
<div class="pined-hashtag">
<el-button size="small" type="text" @click="pinedHashtag = !pinedHashtag" :title="$t('modals.new_toot.pined_hashtag')" :class="pinedHashtag? '' : 'clickable'">
<icon name="hashtag"></icon>
</el-button>
</div>
<span class="text-count">{{ tootMax - status.length }}</span>
2018-11-17 19:27:58 +01:00
<el-button class="toot-action" size="small" @click="closeConfirm(close)">{{ $t('modals.new_toot.cancel') }}</el-button>
<el-button class="toot-action" size="small" type="primary" @click="toot" v-loading="blockSubmit">{{ $t('modals.new_toot.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, mapGetters } from 'vuex'
2018-08-20 05:22:35 +02:00
import { clipboard } from 'electron'
import Visibility from '~/src/constants/visibility'
import Status from './NewToot/Status'
2018-03-12 17:42:47 +01:00
export default {
name: 'new-toot',
components: {
Status
},
data () {
return {
status: '',
2018-11-19 15:56:49 +01:00
mediaDescriptions: {},
spoiler: '',
2018-08-10 17:59:48 +02:00
showContentWarning: false,
visibilityList: Visibility
}
},
2018-03-12 17:42:47 +01:00
computed: {
...mapState('TimelineSpace/Modals/NewToot', {
replyToId: (state) => {
if (state.replyToMessage !== null) {
return state.replyToMessage.id
} else {
return null
}
},
attachedMedias: state => state.attachedMedias,
attachedMediaId: state => state.attachedMediaId,
blockSubmit: state => state.blockSubmit,
visibility: state => state.visibility,
sensitive: state => state.sensitive,
initialStatus: state => state.initialStatus,
2018-11-17 19:27:58 +01:00
initialSpoiler: state => state.initialSpoiler,
visibilityIcon: (state) => {
switch (state.visibility) {
2018-08-01 16:41:05 +02:00
case Visibility.Public.value:
return 'globe'
2018-08-01 16:41:05 +02:00
case Visibility.Unlisted.value:
return 'unlock'
2018-08-01 16:41:05 +02:00
case Visibility.Private.value:
return 'lock'
2018-08-01 16:41:05 +02:00
case Visibility.Direct.value:
return 'envelope'
default:
return 'globe'
}
}
}),
...mapState('TimelineSpace', {
tootMax: state => state.tootMax
}),
...mapGetters('TimelineSpace/Modals/NewToot', [
'hashtagInserting'
]),
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) {
if (value) {
this.$store.dispatch('TimelineSpace/Modals/NewToot/openModal')
} else {
this.$store.dispatch('TimelineSpace/Modals/NewToot/closeModal')
}
2018-03-12 17:42:47 +01:00
}
},
pinedHashtag: {
get () {
return this.$store.state.TimelineSpace.Modals.NewToot.pinedHashtag
},
set (value) {
this.$store.commit('TimelineSpace/Modals/NewToot/changePinedHashtag', value)
}
2018-03-12 17:42:47 +01:00
}
2018-03-13 00:41:03 +01:00
},
watch: {
newTootModal: function (newState, oldState) {
if (!oldState && newState) {
2018-11-17 19:27:58 +01:00
this.showContentWarning = this.initialSpoiler
this.status = this.initialStatus
2018-11-17 19:27:58 +01:00
this.spoiler = this.initialSpoiler
}
}
},
2018-03-13 00:41:03 +01:00
methods: {
close () {
this.filteredAccount = []
this.$store.dispatch('TimelineSpace/Modals/NewToot/resetMediaId')
this.$store.dispatch('TimelineSpace/Modals/NewToot/closeModal')
},
async toot () {
if (!this.newTootModal) {
return
}
if (this.status.length <= 0 || this.status.length >= 500) {
2018-03-13 02:52:28 +01:00
return this.$message({
2018-08-13 08:27:53 +02:00
message: this.$t('validation.new_toot.toot_length', {min: 1, max: 500}),
2018-03-13 02:52:28 +01:00
type: 'error'
})
}
const visibilityKey = Object.keys(Visibility).find((key) => {
return Visibility[key].value === this.visibility
})
let form = {
status: this.status,
visibility: Visibility[visibilityKey].key,
sensitive: this.sensitive,
spoiler_text: this.spoiler
}
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({
2018-08-13 08:27:53 +02:00
message: this.$t('validation.new_toot.attach_length', {max: 4}),
2018-03-29 06:16:15 +02:00
type: 'error'
})
}
form = Object.assign(form, {
media_ids: this.attachedMedias.map((m) => { return m.id })
})
}
2018-04-09 09:49:41 +02:00
2018-11-19 15:56:49 +01:00
const status = await this.$store.dispatch('TimelineSpace/Modals/NewToot/updateMedia', this.mediaDescriptions)
.then(() => {
return this.$store.dispatch('TimelineSpace/Modals/NewToot/postToot', form)
})
.catch((e) => {
console.error(e)
2018-03-13 00:41:03 +01:00
this.$message({
2018-08-13 08:27:53 +02:00
message: this.$t('message.toot_error'),
2018-03-13 00:41:03 +01:00
type: 'error'
})
})
this.$store.dispatch('TimelineSpace/Modals/NewToot/updateHashtags', status.tags)
2018-08-01 14:32:21 +02:00
this.close()
},
selectImage () {
this.$refs.image.click()
},
onChangeImage (e) {
if (e.target.files.item(0) === null || e.target.files.item(0) === undefined) {
return
}
const file = e.target.files.item(0)
if (!file.type.includes('image') && !file.type.includes('video')) {
this.$message({
2018-08-13 08:27:53 +02:00
message: this.$t('validation.new_toot.attach_image'),
type: 'error'
})
return
}
this.updateImage(file)
},
2018-08-20 05:22:35 +02:00
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)
.catch(() => {
this.$message({
2018-08-13 08:27:53 +02:00
message: this.$t('message.attach_error'),
type: 'error'
})
})
},
removeAttachment (media) {
2018-03-29 08:49:39 +02:00
this.$store.commit('TimelineSpace/Modals/NewToot/removeMedia', media)
2018-11-19 15:56:49 +01:00
delete this.mediaDescriptions[media.id]
2018-03-29 08:49:39 +02:00
},
changeVisibility (level) {
2018-08-10 17:59:48 +02:00
this.$store.commit('TimelineSpace/Modals/NewToot/changeVisibilityValue', level)
},
changeSensitive () {
this.$store.commit('TimelineSpace/Modals/NewToot/changeSensitive', !this.sensitive)
},
closeConfirm (done) {
if (this.status.length === 0) {
done()
} else {
this.$confirm(
this.$t('modals.new_toot.close_confirm'),
{
confirmButtonText: this.$t('modals.new_toot.close_confirm_ok'),
cancelButtonText: this.$t('modals.new_toot.close_confirm_cancel')
})
.then(_ => {
done()
})
.catch(_ => {})
}
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;
.spoiler {
box-sizing: border-box;
padding: 4px 0;
background-color: #4a5664;
input {
border-radius: 0;
&::placeholder {
color: #c0c4cc;
}
}
}
.preview {
box-sizing: border-box;
2018-11-19 15:56:49 +01:00
display: flex;
flex-direction: row;
flex-wrap: wrap;
.image-wrapper {
position: relative;
2018-11-19 15:56:49 +01:00
flex: 1 1 0;
min-width: 40%;
height: 150px;
margin: 4px;
.preview-image {
2018-11-19 15:56:49 +01:00
width: 100%;
height: 100%;
object-fit: cover;
border: 0;
border-radius: 8px;
}
.image-description {
position: absolute;
2018-11-19 19:17:38 +01:00
left: 0;
bottom: 0;
2018-11-19 15:56:49 +01:00
width: 100%;
box-sizing: border-box;
border: 0;
border-bottom-left-radius: 8px;
border-bottom-right-radius: 8px;
background: linear-gradient(0deg, rgba(0, 0, 0, 0.8) 0, rgba(0, 0, 0, 0.35) 80%, transparent);
font-size: var(--font-base-size);
color: #fff;
opacity: 1;
resize: none;
overflow: scroll;
2018-11-19 15:56:49 +01:00
&::placeholder {
color: #c0c4cc;
}
}
.remove-image {
position: absolute;
2018-11-19 15:56:49 +01:00
top: 2px;
left: 2px;
2018-03-29 08:49:39 +02:00
padding: 0;
cursor: pointer;
2018-11-19 15:56:49 +01:00
font-size: 1.5rem;
.fa-icon {
font-size: 0.9em;
width: auto;
height: 1em;
max-width: 100%;
max-height: 100%;
}
}
}
}
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;
}
}
.privacy {
float: left;
margin-left: 8px;
}
.sensitive {
float: left;
margin-left: 8px;
}
.content-warning {
float: left;
margin-left: 8px;
2018-09-18 15:19:11 +02:00
.cw-text {
font-weight: 800;
2018-09-18 15:19:11 +02:00
line-height: 18px;
}
}
.pined-hashtag {
float: left;
margin-left: 8px;
}
.clickable {
color: #909399;
}
2018-03-13 02:52:28 +01:00
.text-count {
2018-11-17 19:27:58 +01:00
padding-right: 10px;
2018-03-13 02:52:28 +01:00
color: #909399;
}
2018-11-17 19:27:58 +01:00
.toot-action {
font-size: var(--base-font-size);
margin-top: 2px;
margin-bottom: 2px;
}
2018-03-12 17:42:47 +01:00
}
}
.privacy-icon {
margin-right: 4px;
}
2018-03-12 17:42:47 +01:00
</style>