CommonFilePreview.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed, getCurrentInstance, ref } from 'vue'
  4. import type { StoredFile } from '#shared/graphql/types.ts'
  5. import { i18n } from '#shared/i18n.ts'
  6. import {
  7. canDownloadFile,
  8. canPreviewFile,
  9. humanizeFileSize,
  10. } from '#shared/utils/files.ts'
  11. import { getIconByContentType } from '#shared/utils/icons.ts'
  12. export interface Props {
  13. file: Pick<StoredFile, 'type' | 'name' | 'size'>
  14. downloadUrl?: string
  15. previewUrl?: string
  16. loading?: boolean
  17. noPreview?: boolean
  18. noRemove?: boolean
  19. wrapperClass?: string
  20. iconClass?: string
  21. sizeClass?: string
  22. }
  23. const props = withDefaults(defineProps<Props>(), {
  24. wrapperClass: 'border-gray-300',
  25. iconClass: 'border-gray-300',
  26. sizeClass: 'text-white/80',
  27. })
  28. const emit = defineEmits<{
  29. remove: []
  30. preview: [$event: Event]
  31. }>()
  32. const imageFailed = ref(false)
  33. const canPreview = computed(() => {
  34. const { file, previewUrl } = props
  35. if (!previewUrl || imageFailed.value) return false
  36. return canPreviewFile(file.type)
  37. })
  38. const canDownload = computed(() => canDownloadFile(props.file.type))
  39. const icon = computed(() => getIconByContentType(props.file.type))
  40. const componentType = computed(() => {
  41. if (props.downloadUrl) return 'CommonLink'
  42. if (canPreview.value) return 'button'
  43. return 'div'
  44. })
  45. const vm = getCurrentInstance()
  46. const ariaLabel = computed(() => {
  47. const listensForPreview = !!vm?.vnode.props?.onPreview
  48. if (canPreview.value && listensForPreview)
  49. return i18n.t('Preview %s', props.file.name) // opens a preview on the same page
  50. if (props.downloadUrl && canDownload.value)
  51. return i18n.t('Download %s', props.file.name) // directly downloads file
  52. if (props.downloadUrl && !canDownload.value)
  53. return i18n.t('Open %s', props.file.name) // opens file in another tab
  54. return props.file.name // cannot download and preview, probably just uploaded pdf
  55. })
  56. const onFileClick = (event: Event) => {
  57. if (canPreview.value) {
  58. event.preventDefault()
  59. emit('preview', event)
  60. }
  61. }
  62. </script>
  63. <template>
  64. <div
  65. class="focus-within:bg-blue-highlight mb-2 flex w-full items-center gap-2 rounded-2xl border-[0.5px] p-3 outline-none last:mb-0"
  66. :class="wrapperClass"
  67. >
  68. <Component
  69. :is="componentType"
  70. class="flex w-full select-none items-center gap-2 overflow-hidden text-left outline-none"
  71. :type="componentType === 'button' ? 'button' : undefined"
  72. :class="{ 'cursor-pointer': componentType !== 'div' }"
  73. :aria-label="ariaLabel"
  74. tabindex="0"
  75. :link="downloadUrl"
  76. :download="canDownload ? file.name : undefined"
  77. :target="!canDownload ? '_blank' : undefined"
  78. @click="onFileClick"
  79. @keydown.delete.prevent="$emit('remove')"
  80. @keydown.backspace.prevent="$emit('remove')"
  81. >
  82. <div
  83. v-if="!noPreview"
  84. class="flex h-9 w-9 items-center justify-center rounded border-[0.5px] p-1"
  85. :class="iconClass"
  86. >
  87. <img
  88. v-if="canPreview"
  89. class="max-h-8"
  90. :src="previewUrl"
  91. :alt="$t('Image of %s', file.name)"
  92. @error="imageFailed = true"
  93. />
  94. <CommonIcon
  95. v-else-if="loading"
  96. size="base"
  97. :label="$t('File \'%s\' is uploading', file.name)"
  98. name="loading"
  99. animation="spin"
  100. />
  101. <CommonIcon v-else size="base" decorative :name="icon" />
  102. </div>
  103. <div class="flex flex-1 flex-col overflow-hidden leading-4">
  104. <span class="truncate">
  105. {{ file.name }}
  106. </span>
  107. <span v-if="file.size" class="whitespace-nowrap" :class="sizeClass">
  108. {{ humanizeFileSize(file.size) }}
  109. </span>
  110. </div>
  111. </Component>
  112. <button
  113. v-if="!noRemove"
  114. type="button"
  115. tabindex="-1"
  116. :aria-label="i18n.t('Remove %s', file.name)"
  117. @click.stop.prevent="$emit('remove')"
  118. @keypress.space.prevent="$emit('remove')"
  119. >
  120. <CommonIcon
  121. class="text-gray ltr:right-2 rtl:left-2"
  122. name="close-small"
  123. size="base"
  124. decorative
  125. />
  126. </button>
  127. </div>
  128. </template>