FileItem.svelte 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <script lang="ts">
  2. import { createEventDispatcher, getContext } from 'svelte';
  3. import { formatFileSize } from '$lib/utils';
  4. import FileItemModal from './FileItemModal.svelte';
  5. import GarbageBin from '../icons/GarbageBin.svelte';
  6. import Spinner from './Spinner.svelte';
  7. import Tooltip from './Tooltip.svelte';
  8. const i18n = getContext('i18n');
  9. const dispatch = createEventDispatcher();
  10. export let className = 'w-60';
  11. export let colorClassName = 'bg-white dark:bg-gray-850 border border-gray-50 dark:border-white/5';
  12. export let url: string | null = null;
  13. export let dismissible = false;
  14. export let loading = false;
  15. export let item = null;
  16. export let edit = false;
  17. export let small = false;
  18. export let name: string;
  19. export let type: string;
  20. export let size: number;
  21. import { deleteFileById } from '$lib/apis/files';
  22. let showModal = false;
  23. </script>
  24. {#if item}
  25. <FileItemModal bind:show={showModal} bind:item {edit} />
  26. {/if}
  27. <button
  28. class="relative group p-1.5 {className} flex items-center gap-1 {colorClassName} {small
  29. ? 'rounded-xl'
  30. : 'rounded-2xl'} text-left"
  31. type="button"
  32. on:click={async () => {
  33. if (item?.file?.data?.content) {
  34. showModal = !showModal;
  35. } else {
  36. if (url) {
  37. if (type === 'file') {
  38. window.open(`${url}/content`, '_blank').focus();
  39. } else {
  40. window.open(`${url}`, '_blank').focus();
  41. }
  42. }
  43. }
  44. dispatch('click');
  45. }}
  46. >
  47. {#if !small}
  48. <div class="p-3 bg-black/20 dark:bg-white/10 text-white rounded-xl">
  49. {#if !loading}
  50. <svg
  51. xmlns="http://www.w3.org/2000/svg"
  52. viewBox="0 0 24 24"
  53. fill="currentColor"
  54. class=" size-5"
  55. >
  56. <path
  57. fill-rule="evenodd"
  58. d="M5.625 1.5c-1.036 0-1.875.84-1.875 1.875v17.25c0 1.035.84 1.875 1.875 1.875h12.75c1.035 0 1.875-.84 1.875-1.875V12.75A3.75 3.75 0 0 0 16.5 9h-1.875a1.875 1.875 0 0 1-1.875-1.875V5.25A3.75 3.75 0 0 0 9 1.5H5.625ZM7.5 15a.75.75 0 0 1 .75-.75h7.5a.75.75 0 0 1 0 1.5h-7.5A.75.75 0 0 1 7.5 15Zm.75 2.25a.75.75 0 0 0 0 1.5H12a.75.75 0 0 0 0-1.5H8.25Z"
  59. clip-rule="evenodd"
  60. />
  61. <path
  62. d="M12.971 1.816A5.23 5.23 0 0 1 14.25 5.25v1.875c0 .207.168.375.375.375H16.5a5.23 5.23 0 0 1 3.434 1.279 9.768 9.768 0 0 0-6.963-6.963Z"
  63. />
  64. </svg>
  65. {:else}
  66. <Spinner />
  67. {/if}
  68. </div>
  69. {/if}
  70. {#if !small}
  71. <div class="flex flex-col justify-center -space-y-0.5 px-2.5 w-full">
  72. <div class=" dark:text-gray-100 text-sm font-medium line-clamp-1 mb-1">
  73. {name}
  74. </div>
  75. <div class=" flex justify-between text-gray-500 text-xs line-clamp-1">
  76. {#if type === 'file'}
  77. {$i18n.t('File')}
  78. {:else if type === 'doc'}
  79. {$i18n.t('Document')}
  80. {:else if type === 'collection'}
  81. {$i18n.t('Collection')}
  82. {:else}
  83. <span class=" capitalize line-clamp-1">{type}</span>
  84. {/if}
  85. {#if size}
  86. <span class="capitalize">{formatFileSize(size)}</span>
  87. {/if}
  88. </div>
  89. </div>
  90. {:else}
  91. <Tooltip content={name} className="flex flex-col w-full" placement="top-start">
  92. <div class="flex flex-col justify-center -space-y-0.5 px-2.5 w-full">
  93. <div class=" dark:text-gray-100 text-sm flex justify-between items-center">
  94. {#if loading}
  95. <div class=" shrink-0 mr-2">
  96. <Spinner className="size-4" />
  97. </div>
  98. {/if}
  99. <div class="font-medium line-clamp-1 flex-1">{name}</div>
  100. <div class="text-gray-500 text-xs capitalize shrink-0">{formatFileSize(size)}</div>
  101. </div>
  102. </div>
  103. </Tooltip>
  104. {/if}
  105. {#if dismissible}
  106. <div class=" absolute -top-1 -right-1">
  107. <button
  108. class=" bg-white text-black border border-white rounded-full group-hover:visible invisible transition"
  109. type="button"
  110. on:click|stopPropagation={() => {
  111. dispatch('dismiss');
  112. }}
  113. >
  114. <svg
  115. xmlns="http://www.w3.org/2000/svg"
  116. viewBox="0 0 20 20"
  117. fill="currentColor"
  118. class="w-4 h-4"
  119. >
  120. <path
  121. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  122. />
  123. </svg>
  124. </button>
  125. <!-- <button
  126. class=" p-1 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-full group-hover:visible invisible transition"
  127. type="button"
  128. on:click={() => {
  129. }}
  130. >
  131. <GarbageBin />
  132. </button> -->
  133. </div>
  134. {/if}
  135. </button>