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

236 lines
6.1 KiB
JavaScript
Raw Normal View History

2018-01-29 07:47:26 +00:00
import StillImage from '../still-image/still-image.vue'
import Flash from '../flash/flash.vue'
2019-01-26 15:45:03 +00:00
import VideoAttachment from '../video_attachment/video_attachment.vue'
2016-12-02 13:42:01 +00:00
import nsfwImage from '../../assets/nsfw.png'
2016-11-25 17:21:25 +00:00
import fileTypeService from '../../services/file_type/file_type.service.js'
2020-01-31 00:24:54 +00:00
import { mapGetters } from 'vuex'
2020-10-20 21:01:28 +00:00
import { library } from '@fortawesome/fontawesome-svg-core'
import {
faFile,
faMusic,
faImage,
2020-11-19 12:19:03 +00:00
faVideo,
faPlayCircle,
faTimes,
faStop,
2021-06-17 23:04:01 +00:00
faSearchPlus,
faTrashAlt,
2021-08-15 18:04:28 +00:00
faPencilAlt,
faAlignRight
2020-10-20 21:01:28 +00:00
} from '@fortawesome/free-solid-svg-icons'
library.add(
faFile,
faMusic,
faImage,
2020-11-19 12:19:03 +00:00
faVideo,
faPlayCircle,
faTimes,
faStop,
2021-06-17 23:04:01 +00:00
faSearchPlus,
faTrashAlt,
2021-08-15 18:04:28 +00:00
faPencilAlt,
faAlignRight
2020-10-20 21:01:28 +00:00
)
2016-10-28 16:08:03 +00:00
const Attachment = {
props: [
'attachment',
2021-06-17 23:04:01 +00:00
'description',
'hideDescription',
2016-10-28 23:38:41 +00:00
'nsfw',
'size',
'setMedia',
2021-06-17 23:04:01 +00:00
'remove',
'shiftUp',
'shiftDn',
2021-06-17 23:04:01 +00:00
'edit'
2016-10-28 16:08:03 +00:00
],
data () {
return {
2021-06-17 23:04:01 +00:00
localDescription: this.description || this.attachment.description,
2019-01-22 21:10:59 +00:00
nsfwImage: this.$store.state.instance.nsfwCensorImage || nsfwImage,
hideNsfwLocal: this.$store.getters.mergedConfig.hideNsfw,
preloadImage: this.$store.getters.mergedConfig.preloadImage,
loading: false,
2019-01-26 15:45:03 +00:00
img: fileTypeService.fileType(this.attachment.mimetype) === 'image' && document.createElement('img'),
modalOpen: false,
showHidden: false,
2021-08-15 18:04:28 +00:00
flashLoaded: false,
showDescription: false
}
},
2018-01-29 07:47:26 +00:00
components: {
Flash,
2019-01-26 15:45:03 +00:00
StillImage,
VideoAttachment
2018-01-29 07:47:26 +00:00
},
2016-10-28 16:08:03 +00:00
computed: {
classNames () {
return [
{
'-loading': this.loading,
'-nsfw-placeholder': this.hidden,
'-editable': this.edit !== undefined
},
'-type-' + this.type,
this.size && '-size-' + this.size,
`-${this.useContainFit ? 'contain' : 'cover'}-fit`
]
},
usePlaceholder () {
2022-02-10 13:42:28 +00:00
return this.size === 'hide'
},
useContainFit () {
return this.$store.getters.mergedConfig.useContainFit
},
placeholderName () {
if (this.attachment.description === '' || !this.attachment.description) {
return this.type.toUpperCase()
}
return this.attachment.description
},
placeholderIconClass () {
2020-10-20 21:01:28 +00:00
if (this.type === 'image') return 'image'
if (this.type === 'video') return 'video'
if (this.type === 'audio') return 'music'
return 'file'
},
referrerpolicy () {
2019-01-19 13:56:18 +00:00
return this.$store.state.instance.mediaProxyAvailable ? '' : 'no-referrer'
},
2016-10-28 16:08:03 +00:00
type () {
2016-11-25 17:21:25 +00:00
return fileTypeService.fileType(this.attachment.mimetype)
2016-12-02 13:22:42 +00:00
},
hidden () {
2019-01-26 15:45:03 +00:00
return this.nsfw && this.hideNsfwLocal && !this.showHidden
},
isEmpty () {
2022-02-10 13:42:28 +00:00
return (this.type === 'html' && !this.attachment.oembed)
2018-04-09 16:43:31 +00:00
},
useModal () {
2021-06-18 11:12:50 +00:00
let modalTypes = []
switch (this.size) {
case 'hide':
case 'small':
modalTypes = ['image', 'video', 'audio', 'flash']
break
default:
modalTypes = this.mergedConfig.playVideosInModal
? ['image', 'video', 'flash']
: ['image']
break
}
2020-07-06 10:42:33 +00:00
return modalTypes.includes(this.type)
},
videoTag () {
return this.useModal ? 'button' : 'span'
},
statusForm () {
return this.$parent.$parent
},
2020-01-31 00:24:54 +00:00
...mapGetters(['mergedConfig'])
2016-10-28 16:08:03 +00:00
},
2021-06-17 23:04:01 +00:00
watch: {
'attachment.description' (newVal) {
this.localDescription = newVal
},
2021-06-17 23:04:01 +00:00
localDescription (newVal) {
this.onEdit(newVal)
}
},
2016-10-28 16:08:03 +00:00
methods: {
2019-07-05 07:02:14 +00:00
linkClicked ({ target }) {
if (target.tagName === 'A') {
window.open(target.href, '_blank')
}
},
2019-01-26 15:45:03 +00:00
openModal (event) {
if (this.useModal) {
this.$emit('setMedia')
this.$store.dispatch('setCurrentMedia', this.attachment)
2022-02-10 13:42:28 +00:00
} else if (this.type === 'unknown') {
window.open(this.attachment.url)
2019-01-26 15:45:03 +00:00
}
},
openModalForce (event) {
this.$emit('setMedia')
this.$store.dispatch('setCurrentMedia', this.attachment)
},
2021-06-17 23:04:01 +00:00
onEdit (event) {
this.edit && this.edit(this.attachment, event)
},
onRemove () {
this.remove && this.remove(this.attachment)
},
onShiftUp () {
this.shiftUp && this.shiftUp(this.attachment)
},
onShiftDn () {
this.shiftDn && this.shiftDn(this.attachment)
},
stopFlash () {
this.$refs.flash.closePlayer()
},
setFlashLoaded (event) {
this.flashLoaded = event
},
2021-08-15 18:04:28 +00:00
toggleDescription () {
this.showDescription = !this.showDescription
},
2019-01-26 15:45:03 +00:00
toggleHidden (event) {
2020-01-31 00:24:54 +00:00
if (
(this.mergedConfig.useOneClickNsfw && !this.showHidden) &&
(this.type !== 'video' || this.mergedConfig.playVideosInModal)
) {
2019-01-26 15:45:03 +00:00
this.openModal(event)
return
}
2019-01-26 15:45:03 +00:00
if (this.img && !this.preloadImage) {
if (this.img.onload) {
this.img.onload()
} else {
this.loading = true
this.img.src = this.attachment.url
this.img.onload = () => {
this.loading = false
this.showHidden = !this.showHidden
}
}
} else {
this.showHidden = !this.showHidden
}
},
onImageLoad (image) {
const width = image.naturalWidth
const height = image.naturalHeight
2021-06-17 23:04:01 +00:00
this.$emit('naturalSizeLoad', { id: this.attachment.id, width, height })
},
resize (e) {
const target = e.target || e
if (!(target instanceof window.Element)) { return }
// Reset to default height for empty form, nothing else to do here.
if (target.value === '') {
target.style.height = null
this.$emit('resize')
return
}
const paddingString = getComputedStyle(target)['padding']
// remove -px suffix
const padding = Number(paddingString.substring(0, paddingString.length - 2))
target.style.height = 'auto'
const newHeight = Math.floor(target.scrollHeight - padding * 2)
target.style.height = `${newHeight}px`
this.$emit('resize', newHeight)
},
postStatus (event) {
console.log(this.statusForm.postStatus(event, this.statusForm.newStatus))
2016-10-28 16:08:03 +00:00
}
}
}
2016-12-02 13:22:42 +00:00
export default Attachment