ArticleReply.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { useActiveElement, useLocalStorage, useWindowSize } from '@vueuse/core'
  4. import { computed, nextTick, ref, watch, type MaybeRef } from 'vue'
  5. import type { TicketById } from '#shared/entities/ticket/types'
  6. import type { AppSpecificTicketArticleType } from '#shared/entities/ticket-article/action/plugins/types.ts'
  7. import { useSessionStore } from '#shared/stores/session.ts'
  8. import type { ButtonVariant } from '#shared/types/button.ts'
  9. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  10. import ResizeLine from '#desktop/components/ResizeLine/ResizeLine.vue'
  11. import { useResizeLine } from '#desktop/components/ResizeLine/useResizeLine.ts'
  12. import { useElementScroll } from '#desktop/composables/useElementScroll.ts'
  13. interface Props {
  14. ticket: TicketById
  15. newArticlePresent?: boolean
  16. createArticleType?: string | null
  17. ticketArticleTypes: AppSpecificTicketArticleType[]
  18. isTicketCustomer?: boolean
  19. hasInternalArticle?: boolean
  20. parentReachedBottomScroll: boolean
  21. }
  22. const props = defineProps<Props>()
  23. defineEmits<{
  24. 'show-article-form': [
  25. articleType: string,
  26. performReply: AppSpecificTicketArticleType['performReply'],
  27. ]
  28. 'discard-form': []
  29. }>()
  30. const currentTicketArticleType = computed(() => {
  31. if (props.isTicketCustomer) return 'web'
  32. if (props.createArticleType === 'phone') return 'email'
  33. return props.createArticleType
  34. })
  35. const allowedArticleTypes = computed(() => {
  36. return ['note', 'phone', currentTicketArticleType.value]
  37. })
  38. const availableArticleTypes = computed(() => {
  39. const availableArticleTypes = props.ticketArticleTypes.filter((type) =>
  40. allowedArticleTypes.value.includes(type.value),
  41. )
  42. const hasEmail = availableArticleTypes.some((type) => type.value === 'email')
  43. let primaryTicketArticleType = currentTicketArticleType.value
  44. if (availableArticleTypes.length === 2) {
  45. primaryTicketArticleType = props.createArticleType
  46. }
  47. return availableArticleTypes.map((type) => {
  48. return {
  49. articleType: type.value,
  50. label:
  51. primaryTicketArticleType === type.value && hasEmail
  52. ? __('Add reply')
  53. : type.buttonLabel,
  54. icon: type.icon,
  55. variant:
  56. primaryTicketArticleType === type.value ||
  57. (type.value === 'phone' &&
  58. !hasEmail &&
  59. availableArticleTypes.length === 2)
  60. ? 'primary'
  61. : 'secondary',
  62. performReply: (() =>
  63. type.performReply?.(
  64. props.ticket,
  65. )) as AppSpecificTicketArticleType['performReply'],
  66. }
  67. })
  68. })
  69. const { userId } = useSessionStore()
  70. const pinned = useLocalStorage(`${userId}-article-reply-pinned`, false)
  71. const togglePinned = () => {
  72. pinned.value = !pinned.value
  73. }
  74. const articlePanel = ref<HTMLElement>()
  75. // Scroll the new article panel into view whenever:
  76. // - an article is being added
  77. // - the panel is being unpinned
  78. watch(
  79. () => [props.newArticlePresent, pinned.value],
  80. ([newArticlePresent, pinned]) => {
  81. if (!newArticlePresent || pinned) return
  82. nextTick(() => {
  83. // NB: Give editor a chance to initialize its height.
  84. setTimeout(() => {
  85. articlePanel.value?.scrollIntoView?.(true)
  86. }, 300)
  87. })
  88. },
  89. )
  90. const DEFAULT_ARTICLE_PANEL_HEIGHT = 290
  91. const MINIMUM_ARTICLE_PANEL_HEIGHT = 150
  92. const articlePanelHeight = useLocalStorage(
  93. `${userId}-article-reply-height`,
  94. DEFAULT_ARTICLE_PANEL_HEIGHT,
  95. )
  96. const { height: screenHeight } = useWindowSize()
  97. const articlePanelMaxHeight = computed(() => screenHeight.value / 2)
  98. const resizeLine = ref<InstanceType<typeof ResizeLine>>()
  99. const resizeCallback = (valueY: number) => {
  100. if (
  101. valueY >= articlePanelMaxHeight.value ||
  102. valueY < MINIMUM_ARTICLE_PANEL_HEIGHT
  103. )
  104. return
  105. articlePanelHeight.value = valueY
  106. }
  107. // a11y keyboard navigation
  108. const activeElement = useActiveElement()
  109. const handleKeyStroke = (e: KeyboardEvent, adjustment: number) => {
  110. if (
  111. !articlePanelHeight.value ||
  112. activeElement.value !== resizeLine.value?.resizeLine
  113. )
  114. return
  115. e.preventDefault()
  116. const newHeight = articlePanelHeight.value + adjustment
  117. if (newHeight >= articlePanelMaxHeight.value) return
  118. resizeCallback(newHeight)
  119. }
  120. const { startResizing } = useResizeLine(
  121. resizeCallback,
  122. resizeLine.value?.resizeLine,
  123. handleKeyStroke,
  124. { orientation: 'horizontal', offsetThreshold: 56 }, // bottom bar height in px
  125. )
  126. const resetHeight = () => {
  127. articlePanelHeight.value = DEFAULT_ARTICLE_PANEL_HEIGHT
  128. }
  129. const articleForm = ref<HTMLElement>()
  130. const { reachedTop: articleFormReachedTop } = useElementScroll(
  131. articleForm as MaybeRef<HTMLElement>,
  132. )
  133. defineExpose({
  134. articlePanel,
  135. })
  136. </script>
  137. <template>
  138. <div
  139. v-if="newArticlePresent"
  140. ref="articlePanel"
  141. class="mx-auto w-full"
  142. :class="{
  143. 'max-w-6xl px-12 py-4': !pinned,
  144. 'sticky bottom-0 z-20 border-t border-t-neutral-300 bg-neutral-50 dark:border-t-gray-900 dark:bg-gray-500':
  145. pinned,
  146. }"
  147. aria-labelledby="article-reply-form-title"
  148. role="complementary"
  149. :aria-expanded="!pinned"
  150. v-bind="$attrs"
  151. >
  152. <div
  153. :class="{
  154. 'bg-stripes relative z-0 rounded-xl outline outline-1 outline-blue-700 before:rounded-2xl':
  155. hasInternalArticle && !pinned,
  156. 'border-stripes': hasInternalArticle && pinned,
  157. }"
  158. :style="{
  159. height: pinned ? `${articlePanelHeight}px` : 'auto',
  160. }"
  161. >
  162. <ResizeLine
  163. v-if="pinned"
  164. ref="resizeLine"
  165. class="group absolute h-3 w-full -translate-y-1.5 py-1"
  166. :label="$t('Resize article panel')"
  167. orientation="horizontal"
  168. :values="{
  169. max: articlePanelMaxHeight,
  170. min: MINIMUM_ARTICLE_PANEL_HEIGHT,
  171. current: articlePanelHeight,
  172. }"
  173. @mousedown-event="startResizing"
  174. @touchstart-event="startResizing"
  175. @dblclick="resetHeight"
  176. />
  177. <div
  178. class="flex h-full flex-col"
  179. :class="{
  180. 'rounded-xl border border-neutral-300 bg-neutral-50 dark:border-gray-900 dark:bg-gray-500':
  181. !pinned,
  182. }"
  183. >
  184. <div
  185. class="flex h-10 items-center p-3"
  186. :class="{
  187. 'bg-neutral-50 dark:bg-gray-500': pinned,
  188. 'border-b border-b-transparent': pinned && articleFormReachedTop,
  189. 'border-b border-b-neutral-300 dark:border-b-gray-900':
  190. pinned && !articleFormReachedTop,
  191. }"
  192. >
  193. <CommonLabel
  194. id="article-reply-form-title"
  195. class="text-stone-200 ltr:mr-auto rtl:ml-auto dark:text-neutral-500"
  196. tag="h2"
  197. size="small"
  198. >
  199. {{ $t('Reply') }}
  200. </CommonLabel>
  201. <CommonButton
  202. v-tooltip="$t('Discard unsaved reply')"
  203. class="text-red-500 ltr:mr-2 rtl:ml-2"
  204. variant="none"
  205. icon="trash"
  206. @click="$emit('discard-form')"
  207. />
  208. <CommonButton
  209. v-tooltip="pinned ? $t('Unpin this panel') : $t('Pin this panel')"
  210. :icon="pinned ? 'pin' : 'pin-angle'"
  211. variant="neutral"
  212. size="small"
  213. @click="togglePinned"
  214. />
  215. </div>
  216. <div
  217. id="ticketArticleReplyForm"
  218. ref="articleForm"
  219. class="h-full px-3 pb-3"
  220. :class="{
  221. 'overflow-y-auto': pinned,
  222. 'my-[5px] px-4 pt-2': hasInternalArticle && pinned,
  223. }"
  224. ></div>
  225. </div>
  226. </div>
  227. </div>
  228. <div
  229. v-else-if="newArticlePresent !== undefined"
  230. class="-:border-t-transparent sticky bottom-0 z-20 flex w-full justify-center gap-2.5 border-t py-1.5"
  231. :class="{
  232. 'border-t-neutral-100 bg-neutral-50 dark:border-t-gray-900 dark:bg-gray-500':
  233. parentReachedBottomScroll,
  234. }"
  235. >
  236. <CommonButton
  237. v-for="button in availableArticleTypes"
  238. :key="button.articleType"
  239. :prefix-icon="button.icon"
  240. :variant="button.variant as ButtonVariant"
  241. size="large"
  242. @click="
  243. $emit('show-article-form', button.articleType, button.performReply)
  244. "
  245. >
  246. {{ $t(button.label) }}
  247. </CommonButton>
  248. </div>
  249. </template>
  250. <style scoped>
  251. .border-stripes {
  252. position: relative;
  253. background-color: theme('colors.neutral.50');
  254. &::before {
  255. content: '';
  256. position: absolute;
  257. left: 0;
  258. top: 40px;
  259. bottom: 0;
  260. right: 0;
  261. border: 5px solid transparent;
  262. background-image: repeating-linear-gradient(
  263. 45deg,
  264. theme('colors.blue.400'),
  265. theme('colors.blue.400') 5px,
  266. theme('colors.blue.700') 5px,
  267. theme('colors.blue.700') 10px
  268. );
  269. background-position: -1px;
  270. background-attachment: fixed;
  271. mask:
  272. linear-gradient(#fff 0 0) padding-box,
  273. linear-gradient(#fff 0 0);
  274. mask-composite: exclude;
  275. }
  276. &::after {
  277. content: '';
  278. position: absolute;
  279. left: 0;
  280. top: 40px;
  281. bottom: 0;
  282. right: 0;
  283. outline: 1px solid theme('colors.blue.700');
  284. outline-offset: -5px;
  285. pointer-events: none;
  286. }
  287. }
  288. [data-theme='dark'] .border-stripes {
  289. background-color: theme('colors.gray.500');
  290. &::before {
  291. background-image: repeating-linear-gradient(
  292. 45deg,
  293. theme('colors.blue.700'),
  294. theme('colors.blue.700') 5px,
  295. theme('colors.blue.900') 5px,
  296. theme('colors.blue.900') 10px
  297. );
  298. }
  299. }
  300. </style>