asset-viewer.svelte 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <script lang="ts">
  2. import { createEventDispatcher, onMount, onDestroy } from 'svelte';
  3. import { fly } from 'svelte/transition';
  4. import AsserViewerNavBar from './asset-viewer-nav-bar.svelte';
  5. import ChevronRight from 'svelte-material-icons/ChevronRight.svelte';
  6. import ChevronLeft from 'svelte-material-icons/ChevronLeft.svelte';
  7. import PhotoViewer from './photo-viewer.svelte';
  8. import DetailPanel from './detail-panel.svelte';
  9. import { goto } from '$app/navigation';
  10. import { downloadAssets } from '$lib/stores/download';
  11. import VideoViewer from './video-viewer.svelte';
  12. import AlbumSelectionModal from '../shared-components/album-selection-modal.svelte';
  13. import {
  14. api,
  15. AddAssetsResponseDto,
  16. AssetResponseDto,
  17. AssetTypeEnum,
  18. AlbumResponseDto
  19. } from '@api';
  20. import {
  21. notificationController,
  22. NotificationType
  23. } from '../shared-components/notification/notification';
  24. import { assetStore } from '$lib/stores/assets.store';
  25. export let asset: AssetResponseDto;
  26. $: {
  27. appearsInAlbums = [];
  28. api.albumApi.getAllAlbums(undefined, asset.id).then((result) => {
  29. appearsInAlbums = result.data;
  30. });
  31. }
  32. const dispatch = createEventDispatcher();
  33. let halfLeftHover = false;
  34. let halfRightHover = false;
  35. let isShowDetail = false;
  36. let appearsInAlbums: AlbumResponseDto[] = [];
  37. let isShowAlbumPicker = false;
  38. let addToSharedAlbum = true;
  39. const onKeyboardPress = (keyInfo: KeyboardEvent) => handleKeyboardPress(keyInfo.key);
  40. onMount(() => {
  41. document.addEventListener('keydown', onKeyboardPress);
  42. });
  43. onDestroy(() => {
  44. document.removeEventListener('keydown', onKeyboardPress);
  45. });
  46. const handleKeyboardPress = (key: string) => {
  47. switch (key) {
  48. case 'Escape':
  49. closeViewer();
  50. return;
  51. case 'Delete':
  52. deleteAsset();
  53. return;
  54. case 'i':
  55. isShowDetail = !isShowDetail;
  56. return;
  57. case 'ArrowLeft':
  58. navigateAssetBackward();
  59. return;
  60. case 'ArrowRight':
  61. navigateAssetForward();
  62. return;
  63. }
  64. };
  65. const closeViewer = () => {
  66. dispatch('close');
  67. };
  68. const navigateAssetForward = (e?: Event) => {
  69. e?.stopPropagation();
  70. dispatch('navigate-next');
  71. };
  72. const navigateAssetBackward = (e?: Event) => {
  73. e?.stopPropagation();
  74. dispatch('navigate-previous');
  75. };
  76. const showDetailInfoHandler = () => {
  77. isShowDetail = !isShowDetail;
  78. };
  79. const downloadFile = async () => {
  80. try {
  81. const imageName = asset.exifInfo?.imageName ? asset.exifInfo?.imageName : asset.id;
  82. const imageExtension = asset.originalPath.split('.')[1];
  83. const imageFileName = imageName + '.' + imageExtension;
  84. // If assets is already download -> return;
  85. if ($downloadAssets[imageFileName]) {
  86. return;
  87. }
  88. $downloadAssets[imageFileName] = 0;
  89. const { data, status } = await api.assetApi.downloadFile(asset.id, false, false, {
  90. responseType: 'blob',
  91. onDownloadProgress: (progressEvent) => {
  92. if (progressEvent.lengthComputable) {
  93. const total = progressEvent.total;
  94. const current = progressEvent.loaded;
  95. $downloadAssets[imageFileName] = Math.floor((current / total) * 100);
  96. }
  97. }
  98. });
  99. if (!(data instanceof Blob)) {
  100. return;
  101. }
  102. if (status === 200) {
  103. const fileUrl = URL.createObjectURL(data);
  104. const anchor = document.createElement('a');
  105. anchor.href = fileUrl;
  106. anchor.download = imageFileName;
  107. document.body.appendChild(anchor);
  108. anchor.click();
  109. document.body.removeChild(anchor);
  110. URL.revokeObjectURL(fileUrl);
  111. // Remove item from download list
  112. setTimeout(() => {
  113. const copy = $downloadAssets;
  114. delete copy[imageFileName];
  115. $downloadAssets = copy;
  116. }, 2000);
  117. }
  118. } catch (e) {
  119. console.error('Error downloading file ', e);
  120. notificationController.show({
  121. type: NotificationType.Error,
  122. message: 'Error downloading file, check console for more details.'
  123. });
  124. }
  125. };
  126. const deleteAsset = async () => {
  127. try {
  128. if (
  129. window.confirm(
  130. `Caution! Are you sure you want to delete this asset? This step also deletes this asset in the album(s) to which it belongs. You can not undo this action!`
  131. )
  132. ) {
  133. const { data: deletedAssets } = await api.assetApi.deleteAsset({
  134. ids: [asset.id]
  135. });
  136. navigateAssetForward();
  137. for (const asset of deletedAssets) {
  138. if (asset.status == 'SUCCESS') {
  139. assetStore.removeAsset(asset.id);
  140. }
  141. }
  142. }
  143. } catch (e) {
  144. notificationController.show({
  145. type: NotificationType.Error,
  146. message: 'Error deleting this asset, check console for more details'
  147. });
  148. console.error('Error deleteSelectedAssetHandler', e);
  149. }
  150. };
  151. const toggleFavorite = async () => {
  152. const { data } = await api.assetApi.updateAssetById(asset.id, {
  153. isFavorite: !asset.isFavorite
  154. });
  155. asset.isFavorite = data.isFavorite;
  156. };
  157. const openAlbumPicker = (shared: boolean) => {
  158. isShowAlbumPicker = true;
  159. addToSharedAlbum = shared;
  160. };
  161. const showAddNotification = (dto: AddAssetsResponseDto) => {
  162. notificationController.show({
  163. message: `Added ${dto.successfullyAdded} to ${dto.album?.albumName}`,
  164. type: NotificationType.Info
  165. });
  166. if (dto.successfullyAdded === 1 && dto.album) {
  167. appearsInAlbums = [...appearsInAlbums, dto.album];
  168. }
  169. };
  170. const handleAddToNewAlbum = () => {
  171. isShowAlbumPicker = false;
  172. api.albumApi.createAlbum({ albumName: 'Untitled', assetIds: [asset.id] }).then((response) => {
  173. const album = response.data;
  174. goto('/albums/' + album.id);
  175. });
  176. };
  177. const handleAddToAlbum = async (event: CustomEvent<{ album: AlbumResponseDto }>) => {
  178. isShowAlbumPicker = false;
  179. const album = event.detail.album;
  180. api.albumApi
  181. .addAssetsToAlbum(album.id, { assetIds: [asset.id] })
  182. .then((response) => showAddNotification(response.data));
  183. };
  184. </script>
  185. <section
  186. id="immich-asset-viewer"
  187. class="fixed h-screen w-screen top-0 overflow-y-hidden bg-black z-[999] grid grid-rows-[64px_1fr] grid-cols-4"
  188. >
  189. <div class="col-start-1 col-span-4 row-start-1 row-span-1 z-[1000] transition-transform">
  190. <AsserViewerNavBar
  191. {asset}
  192. on:goBack={closeViewer}
  193. on:showDetail={showDetailInfoHandler}
  194. on:download={downloadFile}
  195. on:delete={deleteAsset}
  196. on:favorite={toggleFavorite}
  197. on:addToAlbum={() => openAlbumPicker(false)}
  198. on:addToSharedAlbum={() => openAlbumPicker(true)}
  199. />
  200. </div>
  201. <div
  202. class={`row-start-2 row-span-end col-start-1 col-span-2 flex place-items-center hover:cursor-pointer w-3/4 mb-[60px] ${
  203. asset.type === AssetTypeEnum.Video ? '' : 'z-[999]'
  204. }`}
  205. on:mouseenter={() => {
  206. halfLeftHover = true;
  207. halfRightHover = false;
  208. }}
  209. on:mouseleave={() => {
  210. halfLeftHover = false;
  211. }}
  212. on:click={navigateAssetBackward}
  213. >
  214. <button
  215. class="rounded-full p-3 hover:bg-gray-500 hover:text-gray-700 z-[1000] text-gray-500 mx-4"
  216. class:navigation-button-hover={halfLeftHover}
  217. on:click={navigateAssetBackward}
  218. >
  219. <ChevronLeft size="36" />
  220. </button>
  221. </div>
  222. <div class="row-start-1 row-span-full col-start-1 col-span-4">
  223. {#key asset.id}
  224. {#if asset.type === AssetTypeEnum.Image}
  225. <PhotoViewer assetId={asset.id} on:close={closeViewer} />
  226. {:else}
  227. <VideoViewer assetId={asset.id} on:close={closeViewer} />
  228. {/if}
  229. {/key}
  230. </div>
  231. <div
  232. class={`row-start-2 row-span-full col-start-3 col-span-2 flex justify-end place-items-center hover:cursor-pointer w-3/4 justify-self-end mb-[60px] ${
  233. asset.type === AssetTypeEnum.Video ? '' : 'z-[500]'
  234. }`}
  235. on:click={navigateAssetForward}
  236. on:mouseenter={() => {
  237. halfLeftHover = false;
  238. halfRightHover = true;
  239. }}
  240. on:mouseleave={() => {
  241. halfRightHover = false;
  242. }}
  243. >
  244. <button
  245. class="rounded-full p-3 hover:bg-gray-500 hover:text-gray-700 text-gray-500 mx-4"
  246. class:navigation-button-hover={halfRightHover}
  247. on:click={navigateAssetForward}
  248. >
  249. <ChevronRight size="36" />
  250. </button>
  251. </div>
  252. {#if isShowDetail}
  253. <div
  254. transition:fly={{ duration: 150 }}
  255. id="detail-panel"
  256. class="bg-immich-bg w-[360px] row-span-full transition-all overflow-y-auto dark:bg-immich-dark-bg dark:border-l dark:border-l-immich-dark-gray"
  257. translate="yes"
  258. >
  259. <DetailPanel {asset} albums={appearsInAlbums} on:close={() => (isShowDetail = false)} />
  260. </div>
  261. {/if}
  262. {#if isShowAlbumPicker}
  263. <AlbumSelectionModal
  264. shared={addToSharedAlbum}
  265. on:newAlbum={handleAddToNewAlbum}
  266. on:newSharedAlbum={handleAddToNewAlbum}
  267. on:album={handleAddToAlbum}
  268. on:close={() => (isShowAlbumPicker = false)}
  269. />
  270. {/if}
  271. </section>
  272. <style>
  273. #immich-asset-viewer {
  274. contain: layout;
  275. }
  276. .navigation-button-hover {
  277. background-color: rgb(107 114 128 / var(--tw-bg-opacity));
  278. color: rgb(55 65 81 / var(--tw-text-opacity));
  279. transition: all 150ms;
  280. }
  281. </style>