Move media_viewer module to store

This commit is contained in:
Sean King 2023-04-05 13:55:38 -06:00
parent 3430604dda
commit c25cfe540b
No known key found for this signature in database
GPG Key ID: 510C52BACD6E7257
8 changed files with 49 additions and 56 deletions

View File

@ -18,6 +18,7 @@ import {
faPencilAlt, faPencilAlt,
faAlignRight faAlignRight
} from '@fortawesome/free-solid-svg-icons' } from '@fortawesome/free-solid-svg-icons'
import { useMediaViewerStore } from '../../stores/media_viewer'
library.add( library.add(
faFile, faFile,
@ -147,14 +148,14 @@ const Attachment = {
openModal (event) { openModal (event) {
if (this.useModal) { if (this.useModal) {
this.$emit('setMedia') this.$emit('setMedia')
this.$store.dispatch('setCurrentMedia', this.attachment) useMediaViewerStore().setCurrentMedia(this.attachment)
} else if (this.type === 'unknown') { } else if (this.type === 'unknown') {
window.open(this.attachment.url) window.open(this.attachment.url)
} }
}, },
openModalForce (event) { openModalForce (event) {
this.$emit('setMedia') this.$emit('setMedia')
this.$store.dispatch('setCurrentMedia', this.attachment) useMediaViewerStore().setCurrentMedia(this.attachment)
}, },
onEdit (event) { onEdit (event) {
this.edit && this.edit(this.attachment, event) this.edit && this.edit(this.attachment, event)

View File

@ -1,3 +1,4 @@
import { useMediaViewerStore } from '../../stores/media_viewer'
import Attachment from '../attachment/attachment.vue' import Attachment from '../attachment/attachment.vue'
import { sumBy, set } from 'lodash' import { sumBy, set } from 'lodash'
@ -107,11 +108,11 @@ const Gallery = {
this.hidingLong = event this.hidingLong = event
}, },
openGallery () { openGallery () {
this.$store.dispatch('setMedia', this.attachments) useMediaViewerStore().setMedia(this.attachments)
this.$store.dispatch('setCurrentMedia', this.attachments[0]) useMediaViewerStore().setCurrentMedia(this.attachments[0])
}, },
onMedia () { onMedia () {
this.$store.dispatch('setMedia', this.attachments) useMediaViewerStore().setMedia(this.attachments)
} }
} }
} }

View File

@ -13,6 +13,7 @@ import {
faCircleNotch, faCircleNotch,
faTimes faTimes
} from '@fortawesome/free-solid-svg-icons' } from '@fortawesome/free-solid-svg-icons'
import { useMediaViewerStore } from '../../stores/media_viewer'
library.add( library.add(
faChevronLeft, faChevronLeft,
@ -44,16 +45,16 @@ const MediaModal = {
}, },
computed: { computed: {
showing () { showing () {
return this.$store.state.mediaViewer.activated return useMediaViewerStore().activated
}, },
media () { media () {
return this.$store.state.mediaViewer.media return useMediaViewerStore().media
}, },
description () { description () {
return this.currentMedia.description return this.currentMedia.description
}, },
currentIndex () { currentIndex () {
return this.$store.state.mediaViewer.currentIndex return useMediaViewerStore().currentIndex
}, },
currentMedia () { currentMedia () {
return this.media[this.currentIndex] return this.media[this.currentIndex]
@ -79,7 +80,7 @@ const MediaModal = {
// to be processed on the content below the overlay // to be processed on the content below the overlay
const transitionTime = 100 // ms const transitionTime = 100 // ms
setTimeout(() => { setTimeout(() => {
this.$store.dispatch('closeMediaViewer') useMediaViewerStore().closeMediaViewer()
}, transitionTime) }, transitionTime)
}, },
hideIfNotSwiped (event) { hideIfNotSwiped (event) {
@ -98,7 +99,7 @@ const MediaModal = {
if (this.getType(newMedia) === 'image') { if (this.getType(newMedia) === 'image') {
this.loading = true this.loading = true
} }
this.$store.dispatch('setCurrentMedia', newMedia) useMediaViewerStore().setCurrentMedia(newMedia)
} }
}, },
goNext () { goNext () {
@ -108,7 +109,7 @@ const MediaModal = {
if (this.getType(newMedia) === 'image') { if (this.getType(newMedia) === 'image') {
this.loading = true this.loading = true
} }
this.$store.dispatch('setCurrentMedia', newMedia) useMediaViewerStore().setCurrentMedia(newMedia)
} }
}, },
onImageLoaded () { onImageLoaded () {

View File

@ -13,6 +13,7 @@ import {
faLink, faLink,
faPollH faPollH
} from '@fortawesome/free-solid-svg-icons' } from '@fortawesome/free-solid-svg-icons'
import { useMediaViewerStore } from '../../stores/media_viewer'
library.add( library.add(
faCircleNotch, faCircleNotch,
@ -123,7 +124,7 @@ const StatusContent = {
}, },
setMedia () { setMedia () {
const attachments = this.attachmentSize === 'hide' ? this.status.attachments : this.galleryAttachments const attachments = this.attachmentSize === 'hide' ? this.status.attachments : this.galleryAttachments
return () => this.$store.dispatch('setMedia', attachments) return () => useMediaViewerStore().setMedia(attachments)
} }
} }
} }

View File

@ -23,6 +23,7 @@ import {
faTimes, faTimes,
faExpandAlt faExpandAlt
} from '@fortawesome/free-solid-svg-icons' } from '@fortawesome/free-solid-svg-icons'
import { useMediaViewerStore } from '../../stores/media_viewer'
library.add( library.add(
faRss, faRss,
@ -222,8 +223,8 @@ export default {
url: this.user.profile_image_url_original, url: this.user.profile_image_url_original,
mimetype: 'image' mimetype: 'image'
} }
this.$store.dispatch('setMedia', [attachment]) useMediaViewerStore().setMedia([attachment])
this.$store.dispatch('setCurrentMedia', attachment) useMediaViewerStore().setCurrentMedia(attachment)
}, },
mentionUser () { mentionUser () {
usePostStatusStore().openPostStatusModal({ replyTo: true, repliedUser: this.user }) usePostStatusStore().openPostStatusModal({ replyTo: true, repliedUser: this.user })

View File

@ -15,7 +15,6 @@ import serverSideConfigModule from './modules/serverSideConfig.js'
import serverSideStorageModule from './modules/serverSideStorage.js' import serverSideStorageModule from './modules/serverSideStorage.js'
import oauthModule from './modules/oauth.js' import oauthModule from './modules/oauth.js'
import authFlowModule from './modules/auth_flow.js' import authFlowModule from './modules/auth_flow.js'
import mediaViewerModule from './modules/media_viewer.js'
import oauthTokensModule from './modules/oauth_tokens.js' import oauthTokensModule from './modules/oauth_tokens.js'
import reportsModule from './modules/reports.js' import reportsModule from './modules/reports.js'
import pollsModule from './modules/polls.js' import pollsModule from './modules/polls.js'
@ -78,7 +77,6 @@ const persistedStateOptions = {
serverSideStorage: serverSideStorageModule, serverSideStorage: serverSideStorageModule,
oauth: oauthModule, oauth: oauthModule,
authFlow: authFlowModule, authFlow: authFlowModule,
mediaViewer: mediaViewerModule,
oauthTokens: oauthTokensModule, oauthTokens: oauthTokensModule,
reports: reportsModule, reports: reportsModule,
polls: pollsModule, polls: pollsModule,

View File

@ -1,40 +0,0 @@
import fileTypeService from '../services/file_type/file_type.service.js'
const supportedTypes = new Set(['image', 'video', 'audio', 'flash'])
const mediaViewer = {
state: {
media: [],
currentIndex: 0,
activated: false
},
mutations: {
setMedia (state, media) {
state.media = media
},
setCurrentMedia (state, index) {
state.activated = true
state.currentIndex = index
},
close (state) {
state.activated = false
}
},
actions: {
setMedia ({ commit }, attachments) {
const media = attachments.filter(attachment => {
const type = fileTypeService.fileType(attachment.mimetype)
return supportedTypes.has(type)
})
commit('setMedia', media)
},
setCurrentMedia ({ commit, state }, current) {
const index = state.media.indexOf(current)
commit('setCurrentMedia', index || 0)
},
closeMediaViewer ({ commit }) {
commit('close')
}
}
}
export default mediaViewer

View File

@ -0,0 +1,30 @@
import { defineStore } from 'pinia'
import fileTypeService from '../services/file_type/file_type.service.js'
const supportedTypes = new Set(['image', 'video', 'audio', 'flash'])
export const useMediaViewerStore = defineStore('mediaViewer', {
state: () => ({
media: [],
currentIndex: 0,
activated: false
}),
actions: {
setMedia (attachments) {
const media = attachments.filter(attachment => {
const type = fileTypeService.fileType(attachment.mimetype)
return supportedTypes.has(type)
})
this.media = media
},
setCurrentMedia (current) {
const index = this.media.indexOf(current)
this.activated = true
this.currentIndex = index
},
closeMediaViewer () {
this.activated = false
}
}
})