utils.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { convertFileList } from '#shared/utils/files.ts'
  3. export const populateEditorNewLines = (htmlContent: string): string => {
  4. const body = document.createElement('div')
  5. body.innerHTML = htmlContent
  6. // prosemirror always adds a visible linebreak inside an empty paragraph,
  7. // but it doesn't return it inside a schema, so we need to add it manually
  8. body.querySelectorAll('p').forEach((p) => {
  9. p.removeAttribute('data-marker')
  10. if (
  11. p.childNodes.length === 0 ||
  12. p.lastChild?.nodeType !== Node.TEXT_NODE ||
  13. p.textContent?.endsWith('\n')
  14. ) {
  15. p.appendChild(document.createElement('br'))
  16. }
  17. })
  18. return body.innerHTML
  19. }
  20. export const convertInlineImages = (
  21. inlineImages: FileList | File[],
  22. editorElement: HTMLElement,
  23. ) => {
  24. return convertFileList(inlineImages, {
  25. compress: true,
  26. onCompress: () => {
  27. const editorWidth = editorElement.clientWidth
  28. const maxWidth = editorWidth > 1000 ? editorWidth : 1000
  29. return {
  30. x: maxWidth,
  31. scale: 2,
  32. type: 'image/jpeg',
  33. }
  34. },
  35. })
  36. }