dom.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { type MaybeRef, toValue } from 'vue'
  3. import type { FormFieldValue } from '#shared/components/Form/types.ts'
  4. export const domFrom = (html: string, document_ = document) => {
  5. const dom = document_.createElement('div')
  6. dom.innerHTML = html
  7. return dom
  8. }
  9. export const removeSignatureFromBody = (input: FormFieldValue) => {
  10. if (!input || typeof input !== 'string') {
  11. return input
  12. }
  13. const dom = domFrom(input)
  14. dom
  15. .querySelectorAll('div[data-signature="true"]')
  16. .forEach((elem) => elem.remove())
  17. return dom.innerHTML
  18. }
  19. /**
  20. * Queries all images in the container and waits for them to load.
  21. * */
  22. export const waitForImagesToLoad = async (container: MaybeRef) => {
  23. const inlineImages: HTMLImageElement[] =
  24. toValue(container).querySelectorAll('img')
  25. if (inlineImages.length > 0) {
  26. return Promise.allSettled<null>(
  27. Array.from(inlineImages).map((image) => {
  28. return new Promise((resolve, reject) => {
  29. const cleanup = () => {
  30. image.onload = null
  31. image.onerror = null
  32. }
  33. const handleLoad = () => {
  34. cleanup()
  35. resolve(null)
  36. }
  37. const handleError = () => {
  38. cleanup()
  39. reject()
  40. }
  41. image.onload = handleLoad
  42. image.onerror = handleError
  43. })
  44. }),
  45. )
  46. }
  47. return Promise.allSettled<null>([])
  48. }