logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

media_modal.js (4521B)


  1. import StillImage from '../still-image/still-image.vue'
  2. import VideoAttachment from '../video_attachment/video_attachment.vue'
  3. import Modal from '../modal/modal.vue'
  4. import PinchZoom from '../pinch_zoom/pinch_zoom.vue'
  5. import SwipeClick from '../swipe_click/swipe_click.vue'
  6. import GestureService from '../../services/gesture_service/gesture_service'
  7. import Flash from 'src/components/flash/flash.vue'
  8. import fileTypeService from '../../services/file_type/file_type.service.js'
  9. import { library } from '@fortawesome/fontawesome-svg-core'
  10. import {
  11. faChevronLeft,
  12. faChevronRight,
  13. faCircleNotch,
  14. faTimes
  15. } from '@fortawesome/free-solid-svg-icons'
  16. import { useMediaViewerStore } from 'src/stores/media_viewer'
  17. library.add(
  18. faChevronLeft,
  19. faChevronRight,
  20. faCircleNotch,
  21. faTimes
  22. )
  23. const MediaModal = {
  24. components: {
  25. StillImage,
  26. VideoAttachment,
  27. PinchZoom,
  28. SwipeClick,
  29. Modal,
  30. Flash
  31. },
  32. data () {
  33. return {
  34. loading: false,
  35. swipeDirection: GestureService.DIRECTION_LEFT,
  36. swipeThreshold: () => {
  37. const considerableMoveRatio = 1 / 4
  38. return window.innerWidth * considerableMoveRatio
  39. },
  40. pinchZoomMinScale: 1,
  41. pinchZoomScaleResetLimit: 1.2
  42. }
  43. },
  44. computed: {
  45. showing () {
  46. return useMediaViewerStore().activated
  47. },
  48. media () {
  49. return useMediaViewerStore().media
  50. },
  51. description () {
  52. return this.currentMedia.description
  53. },
  54. currentIndex () {
  55. return useMediaViewerStore().currentIndex
  56. },
  57. currentMedia () {
  58. return this.media[this.currentIndex]
  59. },
  60. canNavigate () {
  61. return this.media.length > 1
  62. },
  63. type () {
  64. return this.currentMedia ? this.getType(this.currentMedia) : null
  65. },
  66. swipeDisableClickThreshold () {
  67. // If there is only one media, allow more mouse movements to close the modal
  68. // because there is less chance that the user wants to switch to another image
  69. return () => this.canNavigate ? 1 : 30
  70. }
  71. },
  72. methods: {
  73. getType (media) {
  74. return fileTypeService.fileType(media.mimetype)
  75. },
  76. hide () {
  77. // HACK: Closing immediately via a touch will cause the click
  78. // to be processed on the content below the overlay
  79. const transitionTime = 100 // ms
  80. setTimeout(() => {
  81. useMediaViewerStore().closeMediaViewer()
  82. }, transitionTime)
  83. },
  84. hideIfNotSwiped (event) {
  85. // If we have swiped over SwipeClick, do not trigger hide
  86. const comp = this.$refs.swipeClick
  87. if (!comp) {
  88. this.hide()
  89. } else {
  90. comp.$gesture.click(event)
  91. }
  92. },
  93. goPrev () {
  94. if (this.canNavigate) {
  95. const prevIndex = this.currentIndex === 0 ? this.media.length - 1 : (this.currentIndex - 1)
  96. const newMedia = this.media[prevIndex]
  97. if (this.getType(newMedia) === 'image') {
  98. this.loading = true
  99. }
  100. useMediaViewerStore().setCurrentMedia(newMedia)
  101. }
  102. },
  103. goNext () {
  104. if (this.canNavigate) {
  105. const nextIndex = this.currentIndex === this.media.length - 1 ? 0 : (this.currentIndex + 1)
  106. const newMedia = this.media[nextIndex]
  107. if (this.getType(newMedia) === 'image') {
  108. this.loading = true
  109. }
  110. useMediaViewerStore().setCurrentMedia(newMedia)
  111. }
  112. },
  113. onImageLoaded () {
  114. this.loading = false
  115. },
  116. handleSwipePreview (offsets) {
  117. this.$refs.pinchZoom.setTransform({ scale: 1, x: offsets[0], y: 0 })
  118. },
  119. handleSwipeEnd (sign) {
  120. this.$refs.pinchZoom.setTransform({ scale: 1, x: 0, y: 0 })
  121. if (sign > 0) {
  122. this.goNext()
  123. } else if (sign < 0) {
  124. this.goPrev()
  125. }
  126. },
  127. handleKeyupEvent (e) {
  128. if (this.showing && e.keyCode === 27) { // escape
  129. this.hide()
  130. }
  131. },
  132. handleKeydownEvent (e) {
  133. if (!this.showing) {
  134. return
  135. }
  136. if (e.keyCode === 39) { // arrow right
  137. this.goNext()
  138. } else if (e.keyCode === 37) { // arrow left
  139. this.goPrev()
  140. }
  141. }
  142. },
  143. mounted () {
  144. window.addEventListener('popstate', this.hide)
  145. document.addEventListener('keyup', this.handleKeyupEvent)
  146. document.addEventListener('keydown', this.handleKeydownEvent)
  147. },
  148. unmounted () {
  149. window.removeEventListener('popstate', this.hide)
  150. document.removeEventListener('keyup', this.handleKeyupEvent)
  151. document.removeEventListener('keydown', this.handleKeydownEvent)
  152. }
  153. }
  154. export default MediaModal