selection.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { TicketArticle } from '#shared/entities/ticket/types.ts'
  3. import { i18n } from '#shared/i18n.ts'
  4. import type { ConfigList } from '#shared/types/store.ts'
  5. import { textToHtml, textCleanup } from '#shared/utils/helpers.ts'
  6. import type { SelectionData } from '#shared/utils/selection.ts'
  7. const formatDate = (date: string) => {
  8. const options = {
  9. weekday: 'long',
  10. month: 'long',
  11. day: 'numeric',
  12. year: 'numeric',
  13. } as const
  14. try {
  15. return new Date(date).toLocaleTimeString(i18n.locale(), options)
  16. } catch {
  17. return new Date(date).toLocaleTimeString('en-US', options)
  18. }
  19. }
  20. export const getReplyQuoteHeader = (
  21. config: ConfigList,
  22. article: TicketArticle,
  23. ) => {
  24. if (!config.ui_ticket_zoom_article_email_full_quote_header) return ''
  25. const date = formatDate(article.createdAt)
  26. const name = article.author.fullname || ''
  27. return `${i18n.t('On %s, %s wrote:', date, name)}<p>\n</p>`
  28. }
  29. export const getArticleSelection = (
  30. selection: SelectionData | undefined,
  31. article: TicketArticle,
  32. config: ConfigList,
  33. ) => {
  34. if (selection?.html) {
  35. const clean = selection.html
  36. if (clean) return { content: clean, full: false }
  37. }
  38. if (selection?.text) {
  39. return { content: textToHtml(selection.text), full: false }
  40. }
  41. if (config.ui_ticket_zoom_article_email_full_quote) {
  42. const cleanBody = textCleanup(article.bodyWithUrls)
  43. const content =
  44. article.contentType === 'text/html' ? cleanBody : textToHtml(cleanBody)
  45. return { content, full: true }
  46. }
  47. return { content: null, full: false }
  48. }