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

218 lines
5.4 KiB
JavaScript
Raw Normal View History

2026-01-08 17:26:52 +02:00
import { mapGetters } from 'vuex'
import { useInstanceStore } from 'src/stores/instance.js'
2026-01-08 17:26:52 +02:00
import { useMediaViewerStore } from 'src/stores/media_viewer'
import nsfwImage from '../../assets/nsfw.png'
import fileTypeService from '../../services/file_type/file_type.service.js'
import Flash from '../flash/flash.vue'
import StillImage from '../still-image/still-image.vue'
import VideoAttachment from '../video_attachment/video_attachment.vue'
2020-10-21 00:01:28 +03:00
import { library } from '@fortawesome/fontawesome-svg-core'
import {
2026-01-06 16:23:17 +02:00
faAlignRight,
2020-10-21 00:01:28 +03:00
faFile,
faImage,
2026-01-06 16:23:17 +02:00
faMusic,
faPencilAlt,
faPlayCircle,
2021-06-18 02:04:01 +03:00
faSearchPlus,
2026-01-06 16:23:17 +02:00
faStop,
faTimes,
2021-06-18 02:04:01 +03:00
faTrashAlt,
2026-01-06 16:23:17 +02:00
faVideo,
2020-10-21 00:01:28 +03:00
} from '@fortawesome/free-solid-svg-icons'
library.add(
faFile,
faMusic,
faImage,
2020-11-19 14:19:03 +02:00
faVideo,
faPlayCircle,
faTimes,
faStop,
2021-06-18 02:04:01 +03:00
faSearchPlus,
faTrashAlt,
2021-08-15 21:04:28 +03:00
faPencilAlt,
2026-01-06 16:22:52 +02:00
faAlignRight,
2020-10-21 00:01:28 +03:00
)
2016-10-28 18:08:03 +02:00
const Attachment = {
props: [
'attachment',
'compact',
2021-06-18 02:04:01 +03:00
'description',
'hideDescription',
2016-10-29 01:38:41 +02:00
'nsfw',
'size',
'setMedia',
2021-06-18 02:04:01 +03:00
'remove',
'shiftUp',
'shiftDn',
2026-01-06 16:22:52 +02:00
'edit',
2016-10-28 18:08:03 +02:00
],
2026-01-06 16:22:52 +02:00
data() {
return {
2021-06-18 02:04:01 +03:00
localDescription: this.description || this.attachment.description,
nsfwImage: useInstanceStore().nsfwCensorImage || nsfwImage,
hideNsfwLocal: this.$store.getters.mergedConfig.hideNsfw,
preloadImage: this.$store.getters.mergedConfig.preloadImage,
loading: false,
2026-01-06 16:22:52 +02:00
img:
fileTypeService.fileType(this.attachment.mimetype) === 'image' &&
document.createElement('img'),
2019-01-26 17:45:03 +02:00
modalOpen: false,
showHidden: false,
2021-08-15 21:04:28 +03:00
flashLoaded: false,
2026-01-06 16:22:52 +02:00
showDescription: false,
}
},
2018-01-29 10:47:26 +03:00
components: {
Flash,
2019-01-26 17:45:03 +02:00
StillImage,
2026-01-06 16:22:52 +02:00
VideoAttachment,
2018-01-29 10:47:26 +03:00
},
2016-10-28 18:08:03 +02:00
computed: {
2026-01-06 16:22:52 +02:00
classNames() {
return [
{
'-loading': this.loading,
'-nsfw-placeholder': this.hidden,
'-editable': this.edit !== undefined,
2026-01-06 16:22:52 +02:00
'-compact': this.compact,
},
'-type-' + this.type,
this.size && '-size-' + this.size,
2026-01-06 16:22:52 +02:00
`-${this.useContainFit ? 'contain' : 'cover'}-fit`,
]
},
2026-01-06 16:22:52 +02:00
usePlaceholder() {
2022-02-10 15:42:28 +02:00
return this.size === 'hide'
},
2026-01-06 16:22:52 +02:00
useContainFit() {
return this.$store.getters.mergedConfig.useContainFit
},
2026-01-06 16:22:52 +02:00
placeholderName() {
if (this.attachment.description === '' || !this.attachment.description) {
return this.type.toUpperCase()
}
return this.attachment.description
},
2026-01-06 16:22:52 +02:00
placeholderIconClass() {
2020-10-21 00:01:28 +03:00
if (this.type === 'image') return 'image'
if (this.type === 'video') return 'video'
if (this.type === 'audio') return 'music'
return 'file'
},
2026-01-06 16:22:52 +02:00
referrerpolicy() {
return useInstanceStore().mediaProxyAvailable ? '' : 'no-referrer'
},
2026-01-06 16:22:52 +02:00
type() {
2016-11-25 20:21:25 +03:00
return fileTypeService.fileType(this.attachment.mimetype)
2016-12-02 14:22:42 +01:00
},
2026-01-06 16:22:52 +02:00
hidden() {
2019-01-26 17:45:03 +02:00
return this.nsfw && this.hideNsfwLocal && !this.showHidden
},
2026-01-06 16:22:52 +02:00
isEmpty() {
return this.type === 'html' && !this.attachment.oembed
2018-04-09 19:43:31 +03:00
},
2026-01-06 16:22:52 +02:00
useModal() {
2021-06-18 14:12:50 +03: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 13:42:33 +03:00
return modalTypes.includes(this.type)
},
2026-01-06 16:22:52 +02:00
videoTag() {
return this.useModal ? 'button' : 'span'
},
2026-01-06 16:22:52 +02:00
...mapGetters(['mergedConfig']),
2016-10-28 18:08:03 +02:00
},
2021-06-18 02:04:01 +03:00
watch: {
2026-01-06 16:22:52 +02:00
'attachment.description'(newVal) {
2022-06-26 13:25:36 -06:00
this.localDescription = newVal
},
2026-01-06 16:22:52 +02:00
localDescription(newVal) {
2021-06-18 02:04:01 +03:00
this.onEdit(newVal)
2026-01-06 16:22:52 +02:00
},
2021-06-18 02:04:01 +03:00
},
2016-10-28 18:08:03 +02:00
methods: {
2026-01-06 16:22:52 +02:00
linkClicked({ target }) {
if (target.tagName === 'A') {
window.open(target.href, '_blank')
}
},
2026-01-06 16:22:52 +02:00
openModal() {
if (this.useModal) {
this.$emit('setMedia')
2023-04-05 13:55:38 -06:00
useMediaViewerStore().setCurrentMedia(this.attachment)
2022-02-10 15:42:28 +02:00
} else if (this.type === 'unknown') {
window.open(this.attachment.url)
2019-01-26 17:45:03 +02:00
}
},
2026-01-06 16:22:52 +02:00
openModalForce() {
this.$emit('setMedia')
2023-04-05 13:55:38 -06:00
useMediaViewerStore().setCurrentMedia(this.attachment)
},
2026-01-06 16:22:52 +02:00
onEdit(event) {
2021-06-18 02:04:01 +03:00
this.edit && this.edit(this.attachment, event)
},
2026-01-06 16:22:52 +02:00
onRemove() {
2021-06-18 02:04:01 +03:00
this.remove && this.remove(this.attachment)
},
2026-01-06 16:22:52 +02:00
onShiftUp() {
this.shiftUp && this.shiftUp(this.attachment)
},
2026-01-06 16:22:52 +02:00
onShiftDn() {
this.shiftDn && this.shiftDn(this.attachment)
},
2026-01-06 16:22:52 +02:00
stopFlash() {
this.$refs.flash.closePlayer()
},
2026-01-06 16:22:52 +02:00
setFlashLoaded(event) {
this.flashLoaded = event
},
2026-01-06 16:22:52 +02:00
toggleDescription() {
2021-08-15 21:04:28 +03:00
this.showDescription = !this.showDescription
},
2026-01-06 16:22:52 +02:00
toggleHidden(event) {
2020-01-31 00:24:54 +00:00
if (
2026-01-06 16:22:52 +02:00
this.mergedConfig.useOneClickNsfw &&
!this.showHidden &&
2020-01-31 00:24:54 +00:00
(this.type !== 'video' || this.mergedConfig.playVideosInModal)
) {
2019-01-26 17:45:03 +02:00
this.openModal(event)
return
}
2019-01-26 17:45:03 +02: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
}
},
2026-01-06 16:22:52 +02:00
onImageLoad(image) {
const width = image.naturalWidth
const height = image.naturalHeight
2021-06-18 02:04:01 +03:00
this.$emit('naturalSizeLoad', { id: this.attachment.id, width, height })
2026-01-06 16:22:52 +02:00
},
},
2016-10-28 18:08:03 +02:00
}
2016-12-02 14:22:42 +01:00
export default Attachment