ArticleBubble.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 CommonUserAvatar from '#shared/components/CommonUserAvatar/CommonUserAvatar.vue'
  5. import { computed, nextTick, onMounted, ref, watch } from 'vue'
  6. import { i18n } from '#shared/i18n.ts'
  7. import { textToHtml } from '#shared/utils/helpers.ts'
  8. import { useSessionStore } from '#shared/stores/session.ts'
  9. import type {
  10. TicketArticleSecurityState,
  11. TicketArticlesQuery,
  12. } from '#shared/graphql/types.ts'
  13. import type { ConfidentTake } from '#shared/types/utils.ts'
  14. import type { ImageViewerFile } from '#shared/composables/useImageViewer.ts'
  15. import { useImageViewer } from '#shared/composables/useImageViewer.ts'
  16. import CommonFilePreview from '#mobile/components/CommonFilePreview/CommonFilePreview.vue'
  17. import stopEvent from '#shared/utils/events.ts'
  18. import { getIdFromGraphQLId } from '#shared/graphql/utils.ts'
  19. import type { TicketArticleAttachment } from '#shared/entities/ticket/types.ts'
  20. import { useRouter } from 'vue-router'
  21. import { useApplicationStore } from '#shared/stores/application.ts'
  22. import { isStandalone } from '#shared/utils/pwa.ts'
  23. import { useArticleToggleMore } from '../../composable/useArticleToggleMore.ts'
  24. import { useArticleAttachments } from '../../composable/useArticleAttachments.ts'
  25. import ArticleSecurityBadge from './ArticleSecurityBadge.vue'
  26. import { useArticleSeen } from '../../composable/useArticleSeen.ts'
  27. interface Props {
  28. position: 'left' | 'right'
  29. content: string
  30. internal: boolean
  31. user?: Maybe<ConfidentTake<TicketArticlesQuery, 'articles.edges.node.author'>>
  32. security?: Maybe<TicketArticleSecurityState>
  33. contentType: string
  34. ticketInternalId: number
  35. articleId: string
  36. attachments: TicketArticleAttachment[]
  37. }
  38. const props = defineProps<Props>()
  39. const emit = defineEmits<{
  40. (e: 'showContext'): void
  41. (e: 'seen'): void
  42. }>()
  43. const session = useSessionStore()
  44. const colorClasses = computed(() => {
  45. const { internal, position } = props
  46. if (internal) return 'border border-blue bg-black'
  47. if (position === 'left') return 'border border-black bg-white text-black'
  48. return 'border border-black bg-blue text-black'
  49. })
  50. const bubbleClasses = computed(() => {
  51. const { internal, position } = props
  52. if (internal) return undefined
  53. return {
  54. 'rounded-bl-sm': position === 'left',
  55. 'rounded-br-sm': position === 'right',
  56. }
  57. })
  58. const username = computed(() => {
  59. const { user } = props
  60. if (!user) return ''
  61. if (session.user?.id === user.id) {
  62. return i18n.t('Me')
  63. }
  64. return user.fullname
  65. })
  66. const body = computed(() => {
  67. if (props.contentType !== 'text/html') {
  68. return textToHtml(props.content)
  69. }
  70. return props.content
  71. })
  72. const colorsClasses = computed(() => {
  73. if (props.internal) {
  74. return {
  75. top: 'border-t-[0.5px] border-t-white/50',
  76. amount: 'text-white/60',
  77. file: 'border-white/40',
  78. icon: 'border-white/40',
  79. }
  80. }
  81. return {
  82. top: 'border-t-[0.5px] border-black',
  83. amount: 'text-black/60',
  84. file: 'border-black',
  85. icon: 'border-black',
  86. }
  87. })
  88. const { shownMore, bubbleElement, hasShowMore, toggleShowMore } =
  89. useArticleToggleMore()
  90. const articleInternalId = computed(() => getIdFromGraphQLId(props.articleId))
  91. const { attachments: articleAttachments } = useArticleAttachments({
  92. ticketInternalId: props.ticketInternalId,
  93. articleInternalId: articleInternalId.value,
  94. attachments: computed(() => props.attachments),
  95. })
  96. const inlineAttachments = ref<ImageViewerFile[]>([])
  97. const { showImage } = useImageViewer(
  98. computed(() => [...inlineAttachments.value, ...articleAttachments.value]),
  99. )
  100. const previewImage = (event: Event, attachment: TicketArticleAttachment) => {
  101. stopEvent(event)
  102. showImage(attachment)
  103. }
  104. const router = useRouter()
  105. const app = useApplicationStore()
  106. const getRedirectRoute = (url: URL): string | undefined => {
  107. if (url.pathname.startsWith('/mobile')) {
  108. return url.href.slice(`${url.origin}/mobile`.length)
  109. }
  110. const route = router.resolve(`/${url.hash.slice(1)}${url.search}`)
  111. if (route.name !== 'Error') {
  112. return route.fullPath
  113. }
  114. }
  115. const openLink = (target: string, path: string) => {
  116. // keep links inside PWA inside the app
  117. if (!isStandalone() && target && target !== '_self') {
  118. window.open(`/mobile${path}`, target)
  119. } else {
  120. router.push(path)
  121. }
  122. }
  123. const handleLinkClick = (link: HTMLAnchorElement, event: Event) => {
  124. const fqdnOrigin = `${window.location.protocol}//${app.config.fqdn}${
  125. window.location.port ? `:${window.location.port}` : ''
  126. }`
  127. try {
  128. const url = new URL(link.href)
  129. if (url.origin === window.location.origin || url.origin === fqdnOrigin) {
  130. const redirectRoute = getRedirectRoute(url)
  131. if (redirectRoute) {
  132. openLink(link.target, redirectRoute)
  133. event.preventDefault()
  134. }
  135. }
  136. } catch {
  137. // skip
  138. }
  139. }
  140. // user links has fqdn in its href, but if it changes the link becomes invalid
  141. // to bypass that we replace the href with the correct one
  142. const patchUserMentionLinks = (link: HTMLAnchorElement) => {
  143. const userId = link.dataset.mentionUserId
  144. if (userId) {
  145. link.href = `${window.location.origin}/mobile/users/${userId}`
  146. }
  147. }
  148. const setupLinksHandlers = (element: HTMLDivElement) => {
  149. const links = element.querySelectorAll('a')
  150. links.forEach((link) => {
  151. if ('__handled' in link) return
  152. Object.defineProperty(link, '__handled', { value: true })
  153. patchUserMentionLinks(link)
  154. link.addEventListener('click', (event) => handleLinkClick(link, event))
  155. })
  156. }
  157. const populateInlineAttachments = (element: HTMLDivElement) => {
  158. const images = element.querySelectorAll('img')
  159. inlineAttachments.value = []
  160. images.forEach((image) => {
  161. const mime = image.alt?.match(/\.(jpe?g)$/i) ? 'image/jpeg' : 'image/png'
  162. const preview: ImageViewerFile = {
  163. name: image.alt,
  164. inline: image.src,
  165. type: mime,
  166. }
  167. image.classList.add('cursor-pointer')
  168. const index = inlineAttachments.value.push(preview) - 1
  169. image.onclick = () => showImage(inlineAttachments.value[index])
  170. })
  171. }
  172. watch(
  173. () => props.content,
  174. async () => {
  175. await nextTick()
  176. if (bubbleElement.value) {
  177. setupLinksHandlers(bubbleElement.value)
  178. populateInlineAttachments(bubbleElement.value)
  179. }
  180. },
  181. )
  182. onMounted(() => {
  183. if (bubbleElement.value) {
  184. setupLinksHandlers(bubbleElement.value)
  185. populateInlineAttachments(bubbleElement.value)
  186. }
  187. })
  188. useArticleSeen(bubbleElement, emit)
  189. const onContextClick = () => {
  190. emit('showContext')
  191. nextTick(() => {
  192. // remove selection because pointerdown event will leave it as is,
  193. // all actions inside the context should already have accessed it synchronously
  194. window.getSelection()?.removeAllRanges()
  195. })
  196. }
  197. </script>
  198. <template>
  199. <div
  200. :id="`article-${articleInternalId}`"
  201. role="comment"
  202. class="Article relative flex pb-4"
  203. :class="{
  204. Internal: internal,
  205. 'Right flex-row-reverse': position === 'right',
  206. Left: position === 'left',
  207. }"
  208. :data-created-by="user?.id"
  209. >
  210. <div
  211. class="h-6 w-6 self-end"
  212. :class="{
  213. 'ltr:mr-2 rtl:ml-2': position === 'left',
  214. 'ltr:ml-2 rtl:mr-2': position === 'right',
  215. }"
  216. >
  217. <CommonUserAvatar v-if="user" size="xs" :entity="user" />
  218. </div>
  219. <div class="Border">
  220. <div
  221. class="content flex flex-col overflow-hidden rounded-3xl px-4 pb-3 pt-2"
  222. :class="[bubbleClasses, colorClasses]"
  223. >
  224. <div
  225. class="flex items-center text-xs font-bold"
  226. data-test-id="article-username"
  227. >
  228. <span class="truncate break-words">
  229. {{ username }}
  230. </span>
  231. </div>
  232. <div
  233. ref="bubbleElement"
  234. data-test-id="article-content"
  235. class="overflow-hidden text-base"
  236. >
  237. <div class="Content" v-html="body" />
  238. </div>
  239. <div
  240. v-if="hasShowMore"
  241. class="relative"
  242. :class="{
  243. BubbleGradient: hasShowMore && !shownMore,
  244. }"
  245. ></div>
  246. <div
  247. v-if="attachments.length"
  248. class="mb-2 mt-1"
  249. :class="colorsClasses.top"
  250. >
  251. <div class="py-1 text-xs" :class="colorsClasses.amount">
  252. {{
  253. attachments.length === 1
  254. ? $t('1 attached file')
  255. : $t('%s attached files', attachments.length)
  256. }}
  257. </div>
  258. <CommonFilePreview
  259. v-for="attachment of articleAttachments"
  260. :key="attachment.internalId"
  261. :file="attachment"
  262. :download-url="attachment.downloadUrl"
  263. :preview-url="attachment.preview"
  264. :no-preview="!$c.ui_ticket_zoom_attachments_preview"
  265. :wrapper-class="colorsClasses.file"
  266. :icon-class="colorsClasses.icon"
  267. :size-class="colorsClasses.amount"
  268. no-remove
  269. @preview="previewImage($event, attachment)"
  270. />
  271. </div>
  272. <div
  273. class="absolute bottom-0 flex gap-1"
  274. :class="[
  275. position === 'left'
  276. ? 'flex-row-reverse ltr:left-10 rtl:right-10'
  277. : 'ltr:right-10 rtl:left-10',
  278. ]"
  279. >
  280. <ArticleSecurityBadge
  281. v-if="security"
  282. :article-id="articleId"
  283. :success-class="colorClasses"
  284. :security="security"
  285. />
  286. <button
  287. v-if="hasShowMore"
  288. :class="[
  289. colorClasses,
  290. 'flex h-7 items-center justify-center rounded-md px-2 font-semibold',
  291. ]"
  292. type="button"
  293. @click="toggleShowMore()"
  294. @keydown.enter.prevent="toggleShowMore()"
  295. >
  296. {{ shownMore ? $t('See less') : $t('See more') }}
  297. </button>
  298. <button
  299. :class="[
  300. colorClasses,
  301. 'flex h-7 w-7 items-center justify-center rounded-md',
  302. ]"
  303. type="button"
  304. data-name="article-context"
  305. :aria-label="$t('Article actions')"
  306. @pointerdown="onContextClick()"
  307. @keydown.enter.prevent="onContextClick()"
  308. >
  309. <CommonIcon
  310. name="more-vertical"
  311. size="small"
  312. decorative
  313. data-ignore-click
  314. />
  315. </button>
  316. </div>
  317. </div>
  318. </div>
  319. </div>
  320. </template>
  321. <style scoped>
  322. .Content {
  323. word-break: break-word;
  324. }
  325. .Article:not(.Internal) {
  326. &.Right .Content,
  327. &.Left .Content {
  328. :deep(a) {
  329. @apply text-black underline;
  330. }
  331. }
  332. }
  333. .BubbleGradient::before {
  334. content: '';
  335. position: absolute;
  336. left: 0;
  337. right: 0;
  338. bottom: 0;
  339. height: 50px;
  340. pointer-events: none;
  341. }
  342. .Right:not(.Internal) .BubbleGradient::before {
  343. background: linear-gradient(
  344. rgba(255, 255, 255, 0),
  345. theme('colors.blue.DEFAULT')
  346. );
  347. }
  348. .Left:not(.Internal) .BubbleGradient::before {
  349. background: linear-gradient(rgba(255, 255, 255, 0), theme('colors.white'));
  350. }
  351. .Border {
  352. overflow: hidden;
  353. }
  354. .Internal .Border {
  355. background: repeating-linear-gradient(
  356. 45deg,
  357. theme('colors.blue.DEFAULT'),
  358. theme('colors.blue.DEFAULT') 5px,
  359. theme('colors.blue.dark') 5px,
  360. theme('colors.blue.dark') 10px
  361. );
  362. background-size: 14px 14px;
  363. background-position: -1px;
  364. padding: 4px;
  365. border-radius: calc(1.5rem + 4px);
  366. margin: -4px;
  367. }
  368. .Internal .BubbleGradient::before {
  369. background: linear-gradient(
  370. rgba(255, 255, 255, 0),
  371. theme('colors.black.DEFAULT')
  372. );
  373. }
  374. </style>