ArticleBubble.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. /* eslint-disable vue/no-v-html */
  4. import { computed, nextTick, onMounted, ref, watch } from 'vue'
  5. import CommonFilePreview from '#shared/components/CommonFilePreview/CommonFilePreview.vue'
  6. import CommonUserAvatar from '#shared/components/CommonUserAvatar/CommonUserAvatar.vue'
  7. import { useArticleToggleMore } from '#shared/composables/useArticleToggleMore.ts'
  8. import { useAttachments } from '#shared/composables/useAttachments.ts'
  9. import { useHtmlInlineImages } from '#shared/composables/useHtmlInlineImages.ts'
  10. import { useHtmlLinks } from '#shared/composables/useHtmlLinks.ts'
  11. import type { ImageViewerFile } from '#shared/composables/useImageViewer.ts'
  12. import { useImageViewer } from '#shared/composables/useImageViewer.ts'
  13. import type { Attachment } from '#shared/entities/attachment/types.ts'
  14. import type {
  15. TicketArticleSecurityState,
  16. TicketArticlesQuery,
  17. } from '#shared/graphql/types.ts'
  18. import { getIdFromGraphQLId } from '#shared/graphql/utils.ts'
  19. import { i18n } from '#shared/i18n.ts'
  20. import { useSessionStore } from '#shared/stores/session.ts'
  21. import type { ConfidentTake } from '#shared/types/utils.ts'
  22. import stopEvent from '#shared/utils/events.ts'
  23. import { textToHtml } from '#shared/utils/helpers.ts'
  24. import { useArticleSeen } from '../../composable/useArticleSeen.ts'
  25. import ArticleRemoteContentBadge from './ArticleRemoteContentBadge.vue'
  26. import ArticleSecurityBadge from './ArticleSecurityBadge.vue'
  27. import ArticleWhatsappMediaBadge from './ArticleWhatsappMediaBadge.vue'
  28. interface Props {
  29. position: 'left' | 'right'
  30. content: string
  31. internal: boolean
  32. user?: Maybe<ConfidentTake<TicketArticlesQuery, 'articles.edges.node.author'>>
  33. security?: Maybe<TicketArticleSecurityState>
  34. contentType: string
  35. ticketInternalId: number
  36. articleId: string
  37. attachments: Attachment[]
  38. remoteContentWarning?: string
  39. mediaError?: boolean | null
  40. }
  41. const props = defineProps<Props>()
  42. const emit = defineEmits<{
  43. 'show-context': []
  44. seen: []
  45. }>()
  46. const session = useSessionStore()
  47. const colorClasses = computed(() => {
  48. const { internal, position } = props
  49. if (internal) return 'border border-blue bg-black'
  50. if (position === 'left') return 'border border-black bg-white text-black'
  51. return 'border border-black bg-blue text-black'
  52. })
  53. const bubbleClasses = computed(() => {
  54. const { internal, position } = props
  55. if (internal) return undefined
  56. return {
  57. 'rounded-bl-sm': position === 'left',
  58. 'rounded-br-sm': position === 'right',
  59. }
  60. })
  61. const username = computed(() => {
  62. const { user } = props
  63. if (!user) return ''
  64. if (session.user?.id === user.id) {
  65. return i18n.t('Me')
  66. }
  67. return user.fullname
  68. })
  69. const body = computed(() => {
  70. if (props.contentType !== 'text/html') {
  71. return textToHtml(props.content)
  72. }
  73. return props.content
  74. })
  75. const colorsClasses = computed(() => {
  76. if (props.internal) {
  77. return {
  78. top: body.value.length ? 'border-t-[0.5px] border-t-white/50' : '',
  79. amount: 'text-white/60',
  80. file: 'border-white/40',
  81. icon: 'border-white/40',
  82. }
  83. }
  84. return {
  85. top: body.value.length ? 'border-t-[0.5px] border-black' : '',
  86. amount: 'text-black/60',
  87. file: 'border-black',
  88. icon: 'border-black',
  89. }
  90. })
  91. const { shownMore, bubbleElement, hasShowMore, toggleShowMore } =
  92. useArticleToggleMore()
  93. const articleInternalId = computed(() => getIdFromGraphQLId(props.articleId))
  94. const { attachments: articleAttachments } = useAttachments({
  95. attachments: computed(() => props.attachments),
  96. })
  97. const inlineImages = ref<ImageViewerFile[]>([])
  98. const { showImage } = useImageViewer(
  99. computed(() => [...inlineImages.value, ...articleAttachments.value]),
  100. )
  101. const previewImage = (event: Event, attachment: Attachment) => {
  102. stopEvent(event)
  103. showImage(attachment)
  104. }
  105. const { setupLinksHandlers } = useHtmlLinks('/mobile')
  106. const { populateInlineImages } = useHtmlInlineImages(inlineImages, (index) =>
  107. showImage(inlineImages.value[index]),
  108. )
  109. watch(
  110. () => props.content,
  111. async () => {
  112. await nextTick()
  113. if (bubbleElement.value) {
  114. setupLinksHandlers(bubbleElement.value)
  115. populateInlineImages(bubbleElement.value)
  116. }
  117. },
  118. )
  119. onMounted(() => {
  120. if (bubbleElement.value) {
  121. setupLinksHandlers(bubbleElement.value)
  122. populateInlineImages(bubbleElement.value)
  123. }
  124. })
  125. useArticleSeen(bubbleElement, emit)
  126. const onContextClick = () => {
  127. emit('show-context')
  128. nextTick(() => {
  129. // remove selection because pointerdown event will leave it as is,
  130. // all actions inside the context should already have accessed it synchronously
  131. window.getSelection()?.removeAllRanges()
  132. })
  133. }
  134. </script>
  135. <template>
  136. <!-- It is the correct role comment -->
  137. <!-- eslint-disable vuejs-accessibility/aria-role -->
  138. <div
  139. :id="`article-${articleInternalId}`"
  140. role="comment"
  141. class="Article relative flex pb-4"
  142. :class="{
  143. Internal: internal,
  144. 'Right flex-row-reverse': position === 'right',
  145. Left: position === 'left',
  146. }"
  147. :data-created-by="user?.id"
  148. >
  149. <div
  150. class="h-6 w-6 self-end"
  151. :class="{
  152. 'ltr:mr-2 rtl:ml-2': position === 'left',
  153. 'ltr:ml-2 rtl:mr-2': position === 'right',
  154. }"
  155. >
  156. <CommonUserAvatar v-if="user" size="xs" :entity="user" />
  157. </div>
  158. <div class="Border">
  159. <div
  160. class="content flex flex-col overflow-hidden rounded-3xl px-4 pb-3 pt-2"
  161. :class="[bubbleClasses, colorClasses]"
  162. >
  163. <div
  164. class="flex items-center text-xs font-bold"
  165. data-test-id="article-username"
  166. >
  167. <span class="truncate break-words">
  168. {{ username }}
  169. </span>
  170. </div>
  171. <div
  172. ref="bubbleElement"
  173. data-test-id="article-content"
  174. class="overflow-hidden text-base"
  175. >
  176. <div class="Content" v-html="body" />
  177. </div>
  178. <div
  179. v-if="hasShowMore"
  180. class="relative"
  181. :class="{
  182. BubbleGradient: hasShowMore && !shownMore,
  183. }"
  184. ></div>
  185. <div
  186. v-if="attachments.length"
  187. class="mb-2 mt-1"
  188. :class="colorsClasses.top"
  189. >
  190. <div class="py-1 text-xs" :class="colorsClasses.amount">
  191. {{
  192. attachments.length === 1
  193. ? $t('1 attached file')
  194. : $t('%s attached files', attachments.length)
  195. }}
  196. </div>
  197. <CommonFilePreview
  198. v-for="attachment of articleAttachments"
  199. :key="attachment.internalId"
  200. :file="attachment"
  201. :download-url="attachment.downloadUrl"
  202. :preview-url="attachment.preview"
  203. :no-preview="!$c.ui_ticket_zoom_attachments_preview"
  204. :wrapper-class="colorsClasses.file"
  205. :icon-class="colorsClasses.icon"
  206. :size-class="colorsClasses.amount"
  207. no-remove
  208. @preview="previewImage($event, attachment)"
  209. />
  210. </div>
  211. <div
  212. class="absolute bottom-0 flex gap-1"
  213. :class="[
  214. position === 'left'
  215. ? 'flex-row-reverse ltr:left-10 rtl:right-10'
  216. : 'ltr:right-10 rtl:left-10',
  217. ]"
  218. >
  219. <ArticleWhatsappMediaBadge
  220. v-if="props.mediaError"
  221. :article-id="articleId"
  222. :media-error="props.mediaError"
  223. />
  224. <ArticleSecurityBadge
  225. v-if="security"
  226. :article-id="articleId"
  227. :success-class="colorClasses"
  228. :security="security"
  229. />
  230. <ArticleRemoteContentBadge
  231. v-if="remoteContentWarning"
  232. :class="colorClasses"
  233. :original-formatting-url="remoteContentWarning"
  234. />
  235. <button
  236. v-if="hasShowMore"
  237. :class="[
  238. colorClasses,
  239. 'flex h-7 items-center justify-center rounded-md px-2 font-semibold',
  240. ]"
  241. type="button"
  242. @click="toggleShowMore()"
  243. @keydown.enter.prevent="toggleShowMore()"
  244. >
  245. {{ shownMore ? $t('See less') : $t('See more') }}
  246. </button>
  247. <button
  248. :class="[
  249. colorClasses,
  250. 'flex h-7 w-7 items-center justify-center rounded-md',
  251. ]"
  252. type="button"
  253. data-name="article-context"
  254. :aria-label="$t('Article actions')"
  255. @pointerdown="onContextClick()"
  256. @keydown.enter.prevent="onContextClick()"
  257. >
  258. <CommonIcon
  259. name="more-vertical"
  260. size="small"
  261. decorative
  262. data-ignore-click
  263. />
  264. </button>
  265. </div>
  266. </div>
  267. </div>
  268. </div>
  269. </template>
  270. <style scoped>
  271. .Content {
  272. word-break: break-word;
  273. }
  274. .Article:not(.Internal) {
  275. &.Right .Content,
  276. &.Left .Content {
  277. :deep(a) {
  278. @apply text-black underline;
  279. }
  280. }
  281. }
  282. .BubbleGradient::before {
  283. content: '';
  284. position: absolute;
  285. left: 0;
  286. right: 0;
  287. bottom: 0;
  288. height: 50px;
  289. pointer-events: none;
  290. }
  291. .Right:not(.Internal) .BubbleGradient::before {
  292. background: linear-gradient(
  293. rgba(255, 255, 255, 0),
  294. theme('colors.blue.DEFAULT')
  295. );
  296. }
  297. .Left:not(.Internal) .BubbleGradient::before {
  298. background: linear-gradient(rgba(255, 255, 255, 0), theme('colors.white'));
  299. }
  300. .Border {
  301. overflow: hidden;
  302. }
  303. .Internal .Border {
  304. background: repeating-linear-gradient(
  305. 45deg,
  306. theme('colors.blue.DEFAULT'),
  307. theme('colors.blue.DEFAULT') 5px,
  308. theme('colors.blue.dark') 5px,
  309. theme('colors.blue.dark') 10px
  310. );
  311. background-size: 14px 14px;
  312. background-position: -1px;
  313. padding: 4px;
  314. border-radius: calc(1.5rem + 4px);
  315. margin: -4px;
  316. }
  317. .Internal .BubbleGradient::before {
  318. background: linear-gradient(
  319. rgba(255, 255, 255, 0),
  320. theme('colors.black.DEFAULT')
  321. );
  322. }
  323. </style>