useTicketArticleReplyAction.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { nextTick } from 'vue'
  3. import type {
  4. EditorContentType,
  5. FieldEditorContext,
  6. } from '#shared/components/Form/fields/FieldEditor/types.ts'
  7. import type { FormRefParameter } from '#shared/components/Form/types.ts'
  8. import type { TicketArticlePerformOptions } from '#shared/entities/ticket-article/action/plugins/types.ts'
  9. import type { FormKitNode } from '@formkit/core'
  10. export const useTicketArticleReplyAction = (
  11. form: FormRefParameter,
  12. showArticleReply: () => void | Promise<void>,
  13. ) => {
  14. const openReplyForm: TicketArticlePerformOptions['openReplyForm'] = async (
  15. values = {},
  16. ) => {
  17. const formNode = form.value?.formNode as FormKitNode
  18. await showArticleReply()
  19. const { articleType, ...otherOptions } = values
  20. const typeNode = formNode.find('articleType', 'name')
  21. if (formNode.context) {
  22. Object.assign(formNode.context, { _open: true })
  23. }
  24. typeNode?.input(articleType, false)
  25. // Trigger new fields that depend on the articleType.
  26. await nextTick()
  27. for (const [key, value] of Object.entries(otherOptions)) {
  28. const node = formNode.find(key, 'name')
  29. node?.input(value, false)
  30. // TODO: make handling more generic(?)
  31. if (node && (key === 'to' || key === 'cc')) {
  32. const options = Array.isArray(value)
  33. ? value.map((v) => ({ value: v, label: v }))
  34. : [{ value, label: value }]
  35. node.emit('prop:options', options)
  36. }
  37. }
  38. formNode.emit('article-reply-open', articleType)
  39. const context = formNode.find('body', 'name')?.context as
  40. | FieldEditorContext
  41. | undefined
  42. context?.focus()
  43. nextTick(() => {
  44. if (formNode.context) {
  45. Object.assign(formNode.context, { _open: false })
  46. }
  47. })
  48. }
  49. const getNewArticleBody = (type: EditorContentType): string => {
  50. const bodyElement = form.value?.getNodeByName('body')
  51. if (!bodyElement) return ''
  52. const getEditorValue = bodyElement.context?.getEditorValue
  53. return typeof getEditorValue === 'function' ? getEditorValue(type) : ''
  54. }
  55. return {
  56. openReplyForm,
  57. getNewArticleBody,
  58. }
  59. }