ArticleBubble.vue 10 KB

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