useHtmlInlineImages.ts 1003 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { ImageViewerFile } from '#shared/composables/useImageViewer.ts'
  3. import type { Ref } from 'vue'
  4. const useHtmlInlineImages = (
  5. inlineImages: Ref<ImageViewerFile[]>,
  6. onClick: (int: number) => void,
  7. ) => {
  8. const populateInlineImages = (element: HTMLDivElement) => {
  9. inlineImages.value.splice(0)
  10. element.querySelectorAll('img').forEach((image) => {
  11. const mime = (image.alt || image.src)?.match(/\.(jpe?g)$/i)
  12. ? 'image/jpeg'
  13. : 'image/png'
  14. const preview: ImageViewerFile = {
  15. name: image.alt,
  16. inline: image.src,
  17. type: mime,
  18. }
  19. image.classList.add('cursor-pointer')
  20. const index = inlineImages.value.push(preview) - 1
  21. image.onclick = (event) => {
  22. event.preventDefault()
  23. event.stopPropagation()
  24. onClick(index)
  25. }
  26. })
  27. }
  28. return {
  29. populateInlineImages,
  30. }
  31. }
  32. export { useHtmlInlineImages }