akkoma-fe/src/components/post_status_form/post_status_form.js

150 lines
4.0 KiB
JavaScript
Raw Normal View History

2016-10-30 15:53:58 +00:00
import statusPoster from '../../services/status_poster/status_poster.service.js'
2016-11-06 18:30:35 +00:00
import MediaUpload from '../media_upload/media_upload.vue'
2016-11-25 17:21:25 +00:00
import fileTypeService from '../../services/file_type/file_type.service.js'
2017-03-15 16:06:48 +00:00
import Completion from '../../services/completion/completion.js'
import { take, filter, reject, map, uniqBy } from 'lodash'
2016-11-03 16:17:32 +00:00
const buildMentionsString = ({user, attentions}, currentUser) => {
let allAttentions = [...attentions]
allAttentions.unshift(user)
allAttentions = uniqBy(allAttentions, 'id')
allAttentions = reject(allAttentions, {id: currentUser.id})
let mentions = map(allAttentions, (attention) => {
return `@${attention.screen_name}`
})
return mentions.join(' ') + ' '
}
2016-10-30 15:53:58 +00:00
const PostStatusForm = {
2016-11-03 15:59:27 +00:00
props: [
2016-11-03 16:17:32 +00:00
'replyTo',
'repliedUser',
'attentions'
2016-11-03 15:59:27 +00:00
],
2016-11-06 18:30:35 +00:00
components: {
MediaUpload
},
2016-11-03 15:59:27 +00:00
data () {
2016-11-03 16:17:32 +00:00
let statusText = ''
if (this.replyTo) {
const currentUser = this.$store.state.users.currentUser
statusText = buildMentionsString({ user: this.repliedUser, attentions: this.attentions }, currentUser)
}
2016-10-30 15:53:58 +00:00
return {
dropFiles: [],
submitDisabled: false,
error: null,
posting: false,
2016-11-03 16:17:32 +00:00
newStatus: {
2016-11-06 18:30:35 +00:00
status: statusText,
files: []
2017-03-15 16:06:48 +00:00
},
caret: 0
2016-10-30 15:53:58 +00:00
}
},
computed: {
2017-03-15 16:06:48 +00:00
candidates () {
if (this.textAtCaret.charAt(0) === '@') {
const matchedUsers = filter(this.users, (user) => (String(user.name + user.screen_name)).match(this.textAtCaret.slice(1)))
if (matchedUsers.length <= 0) {
return false
}
2017-03-15 16:14:51 +00:00
// eslint-disable-next-line camelcase
return map(take(matchedUsers, 5), ({screen_name, name, profile_image_url_original}) => ({
screen_name: screen_name,
name: name,
img: profile_image_url_original
}))
2017-03-15 16:06:48 +00:00
} else {
return false
2017-03-15 16:06:48 +00:00
}
},
textAtCaret () {
return (this.wordAtCaret || {}).word || ''
},
wordAtCaret () {
const word = Completion.wordAtPosition(this.newStatus.status, this.caret - 1) || {}
return word
},
users () {
return this.$store.state.users.users
}
},
2016-10-30 15:53:58 +00:00
methods: {
2017-03-15 16:06:48 +00:00
replace (replacement) {
this.newStatus.status = Completion.replaceWord(this.newStatus.status, this.wordAtCaret, replacement)
const el = this.$el.querySelector('textarea')
el.focus()
this.caret = 0
2017-03-15 16:06:48 +00:00
},
setCaret ({target: {selectionStart}}) {
this.caret = selectionStart
},
2016-11-03 15:59:27 +00:00
postStatus (newStatus) {
if (this.posting) { return }
this.posting = true
2016-10-30 15:53:58 +00:00
statusPoster.postStatus({
status: newStatus.status,
2016-11-06 18:30:35 +00:00
media: newStatus.files,
2016-11-03 15:59:27 +00:00
store: this.$store,
inReplyToStatusId: this.replyTo
}).then((data) => {
if (!data.error) {
this.newStatus = {
status: '',
files: []
}
this.$emit('posted')
let el = this.$el.querySelector('textarea')
el.style.height = '16px'
this.error = null
} else {
this.error = data.error
}
this.posting = false
2016-10-30 15:53:58 +00:00
})
2016-11-06 18:30:35 +00:00
},
addMediaFile (fileInfo) {
this.newStatus.files.push(fileInfo)
this.enableSubmit()
},
2016-11-26 02:00:06 +00:00
removeMediaFile (fileInfo) {
let index = this.newStatus.files.indexOf(fileInfo)
this.newStatus.files.splice(index, 1)
},
disableSubmit () {
this.submitDisabled = true
},
enableSubmit () {
this.submitDisabled = false
2016-11-25 17:21:25 +00:00
},
type (fileInfo) {
return fileTypeService.fileType(fileInfo.mimetype)
},
fileDrop (e) {
if (e.dataTransfer.files.length > 0) {
e.preventDefault() // allow dropping text like before
this.dropFiles = e.dataTransfer.files
}
},
fileDrag (e) {
2017-02-22 21:33:28 +00:00
e.dataTransfer.dropEffect = 'copy'
},
resize (e) {
e.target.style.height = 'auto'
2017-05-31 11:11:07 +00:00
e.target.style.height = `${e.target.scrollHeight - 10}px`
2017-05-31 14:43:43 +00:00
if (e.target.value === '') {
e.target.style.height = '16px'
}
2016-10-30 15:53:58 +00:00
}
}
}
export default PostStatusForm