index.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <div ref="htmlRef" v-html="context"></div>
  3. </template>
  4. <script lang="ts" setup>
  5. import { shallowRef, onMounted, onUnmounted } from 'vue'
  6. import plus from '@/utils/h5plus'
  7. import PhotoSwipeLightbox, { SlideData } from 'photoswipe/lightbox'
  8. import 'photoswipe/style.css'
  9. defineProps({
  10. context: {
  11. type: String,
  12. default: ''
  13. }
  14. })
  15. let lightbox: PhotoSwipeLightbox = null
  16. const htmlRef = shallowRef<HTMLElement>()
  17. onMounted(() => {
  18. const el = htmlRef.value
  19. if (el) {
  20. el.querySelectorAll('a').forEach((e) => {
  21. const href = e.href
  22. e.onclick = () => plus.openURL(href)
  23. e.removeAttribute('href')
  24. })
  25. if (!lightbox) {
  26. lightbox = new PhotoSwipeLightbox({
  27. gallery: el,
  28. children: 'img',
  29. pswpModule: () => import('photoswipe'),
  30. })
  31. // https://photoswipe.com/data-sources/#custom-html-markup
  32. lightbox.addFilter('domItemData', (itemData: SlideData, el: HTMLImageElement) => {
  33. if (el) {
  34. itemData.src = el.src
  35. itemData.w = el.naturalWidth
  36. itemData.h = el.naturalHeight
  37. itemData.msrc = el.src
  38. }
  39. return itemData
  40. })
  41. lightbox.on('beforeOpen', () => plus.hideStatusBar())
  42. lightbox.on('close', () => plus.showStatusBar())
  43. lightbox.init()
  44. }
  45. }
  46. })
  47. onUnmounted(() => {
  48. if (lightbox) {
  49. lightbox.destroy()
  50. lightbox = null
  51. }
  52. })
  53. </script>