2022-05-27 14:02:06 -05:00
|
|
|
<script lang="ts">
|
2024-06-07 14:22:46 -04:00
|
|
|
import { shortcuts } from '$lib/actions/shortcut';
|
2023-12-05 16:43:15 +01:00
|
|
|
import { photoViewer } from '$lib/stores/assets.store';
|
|
|
|
|
import { boundingBoxesArray } from '$lib/stores/people.store';
|
2024-02-13 15:42:29 +01:00
|
|
|
import { alwaysLoadOriginalFile } from '$lib/stores/preferences.store';
|
2024-06-07 14:22:46 -04:00
|
|
|
import { SlideshowLook, SlideshowState, slideshowLookCssMapping, slideshowStore } from '$lib/stores/slideshow.store';
|
2024-02-14 08:09:49 -05:00
|
|
|
import { photoZoomState } from '$lib/stores/zoom-image.store';
|
2024-06-07 14:22:46 -04:00
|
|
|
import { getAssetOriginalUrl, getAssetThumbnailUrl, handlePromiseError } from '$lib/utils';
|
2024-02-14 08:09:49 -05:00
|
|
|
import { isWebCompatibleImage } from '$lib/utils/asset-utils';
|
|
|
|
|
import { getBoundingBox } from '$lib/utils/people-utils';
|
2024-06-07 14:22:46 -04:00
|
|
|
import { getAltText } from '$lib/utils/thumbnail-util';
|
|
|
|
|
import { AssetTypeEnum, type AssetResponseDto, AssetMediaSize } from '@immich/sdk';
|
|
|
|
|
import { zoomImageAction, zoomed } from '$lib/actions/zoom-image';
|
|
|
|
|
import { canCopyImagesToClipboard, copyImageToClipboard } from 'copy-image-clipboard';
|
|
|
|
|
import { onDestroy } from 'svelte';
|
|
|
|
|
|
2024-02-14 08:09:49 -05:00
|
|
|
import { fade } from 'svelte/transition';
|
|
|
|
|
import LoadingSpinner from '../shared-components/loading-spinner.svelte';
|
|
|
|
|
import { NotificationType, notificationController } from '../shared-components/notification/notification';
|
2024-06-04 21:53:00 +02:00
|
|
|
import { t } from 'svelte-i18n';
|
2024-04-19 12:49:29 +02:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
export let asset: AssetResponseDto;
|
2024-06-07 14:22:46 -04:00
|
|
|
export let preloadAssets: AssetResponseDto[] | undefined = undefined;
|
2023-07-16 03:31:33 +02:00
|
|
|
export let element: HTMLDivElement | undefined = undefined;
|
2023-10-27 15:34:01 -05:00
|
|
|
export let haveFadeTransition = true;
|
2023-07-01 00:50:47 -04:00
|
|
|
|
2024-06-07 14:22:46 -04:00
|
|
|
export let copyImage: (() => Promise<void>) | null = null;
|
|
|
|
|
export let zoomToggle: (() => void) | null = null;
|
2023-07-01 00:50:47 -04:00
|
|
|
|
2024-06-07 14:22:46 -04:00
|
|
|
const { slideshowState, slideshowLook } = slideshowStore;
|
2023-07-01 00:50:47 -04:00
|
|
|
|
2024-06-07 14:22:46 -04:00
|
|
|
let assetFileUrl: string = '';
|
|
|
|
|
let imageLoaded: boolean = false;
|
|
|
|
|
let imageError: boolean = false;
|
|
|
|
|
let forceUseOriginal: boolean = false;
|
|
|
|
|
|
|
|
|
|
$: isWebCompatible = isWebCompatibleImage(asset);
|
|
|
|
|
$: useOriginalByDefault = isWebCompatible && $alwaysLoadOriginalFile;
|
|
|
|
|
$: useOriginalImage = useOriginalByDefault || forceUseOriginal;
|
|
|
|
|
// when true, will force loading of the original image
|
|
|
|
|
$: forceUseOriginal = forceUseOriginal || ($photoZoomState.currentZoom > 1 && isWebCompatible);
|
|
|
|
|
|
|
|
|
|
$: preload(useOriginalImage, preloadAssets);
|
|
|
|
|
$: imageLoaderUrl = getAssetUrl(asset.id, useOriginalImage, asset.checksum);
|
|
|
|
|
|
|
|
|
|
photoZoomState.set({
|
|
|
|
|
currentRotation: 0,
|
|
|
|
|
currentZoom: 1,
|
|
|
|
|
enable: true,
|
|
|
|
|
currentPositionX: 0,
|
|
|
|
|
currentPositionY: 0,
|
|
|
|
|
});
|
|
|
|
|
$zoomed = false;
|
2024-05-28 13:15:50 +02:00
|
|
|
|
2023-09-20 05:16:53 +02:00
|
|
|
onDestroy(() => {
|
2023-12-05 16:43:15 +01:00
|
|
|
$boundingBoxesArray = [];
|
2023-09-20 05:16:53 +02:00
|
|
|
});
|
|
|
|
|
|
2024-06-07 14:22:46 -04:00
|
|
|
const preload = (useOriginal: boolean, preloadAssets?: AssetResponseDto[]) => {
|
|
|
|
|
for (const preloadAsset of preloadAssets || []) {
|
|
|
|
|
if (preloadAsset.type === AssetTypeEnum.Image) {
|
|
|
|
|
let img = new Image();
|
|
|
|
|
img.src = getAssetUrl(preloadAsset.id, useOriginal, preloadAsset.checksum);
|
2024-05-28 13:15:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-23 20:26:22 -04:00
|
|
|
};
|
|
|
|
|
|
2024-06-07 14:22:46 -04:00
|
|
|
const getAssetUrl = (id: string, useOriginal: boolean, checksum: string) => {
|
|
|
|
|
return useOriginal
|
|
|
|
|
? getAssetOriginalUrl({ id, checksum })
|
|
|
|
|
: getAssetThumbnailUrl({ id, size: AssetMediaSize.Preview, checksum });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
copyImage = async () => {
|
2023-07-01 00:50:47 -04:00
|
|
|
if (!canCopyImagesToClipboard()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2024-06-07 14:22:46 -04:00
|
|
|
await copyImageToClipboard(assetFileUrl);
|
2023-07-01 00:50:47 -04:00
|
|
|
notificationController.show({
|
|
|
|
|
type: NotificationType.Info,
|
2024-06-04 21:53:00 +02:00
|
|
|
message: $t('copied_image_to_clipboard'),
|
2023-07-01 00:50:47 -04:00
|
|
|
timeout: 3000,
|
|
|
|
|
});
|
2024-02-02 04:18:00 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error [photo-viewer]:', error);
|
2023-07-01 00:50:47 -04:00
|
|
|
notificationController.show({
|
|
|
|
|
type: NotificationType.Error,
|
|
|
|
|
message: 'Copying image to clipboard failed.',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-07 14:22:46 -04:00
|
|
|
zoomToggle = () => {
|
|
|
|
|
$zoomed = $zoomed ? false : true;
|
2023-07-01 00:50:47 -04:00
|
|
|
};
|
|
|
|
|
|
2024-05-24 10:56:36 +02:00
|
|
|
const onCopyShortcut = (event: KeyboardEvent) => {
|
2024-06-12 13:13:10 +02:00
|
|
|
if (window.getSelection()?.type === 'Range') {
|
2024-03-15 00:16:55 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2024-05-24 10:56:36 +02:00
|
|
|
event.preventDefault();
|
2024-06-07 14:22:46 -04:00
|
|
|
handlePromiseError(copyImage());
|
2024-03-15 00:16:55 +01:00
|
|
|
};
|
2022-05-27 14:02:06 -05:00
|
|
|
</script>
|
|
|
|
|
|
2024-03-15 00:16:55 +01:00
|
|
|
<svelte:window
|
|
|
|
|
use:shortcuts={[
|
2024-05-24 10:56:36 +02:00
|
|
|
{ shortcut: { key: 'c', ctrl: true }, onShortcut: onCopyShortcut, preventDefault: false },
|
|
|
|
|
{ shortcut: { key: 'c', meta: true }, onShortcut: onCopyShortcut, preventDefault: false },
|
2024-03-15 00:16:55 +01:00
|
|
|
]}
|
|
|
|
|
/>
|
2024-06-07 14:22:46 -04:00
|
|
|
{#if imageError}
|
2024-06-12 13:13:10 +02:00
|
|
|
<div class="h-full flex items-center justify-center">{$t('error_loading_image')}</div>
|
2024-06-07 14:22:46 -04:00
|
|
|
{/if}
|
|
|
|
|
<div bind:this={element} class="relative h-full select-none">
|
|
|
|
|
<img
|
|
|
|
|
style="display:none"
|
|
|
|
|
src={imageLoaderUrl}
|
|
|
|
|
alt={getAltText(asset)}
|
|
|
|
|
on:load={() => ((imageLoaded = true), (assetFileUrl = imageLoaderUrl))}
|
|
|
|
|
on:error={() => (imageError = imageLoaded = true)}
|
|
|
|
|
/>
|
2024-03-14 17:12:32 -04:00
|
|
|
{#if !imageLoaded}
|
2024-05-28 13:15:50 +02:00
|
|
|
<div class="flex h-full items-center justify-center">
|
2024-04-21 06:06:49 +02:00
|
|
|
<LoadingSpinner />
|
|
|
|
|
</div>
|
2024-06-07 14:22:46 -04:00
|
|
|
{:else if !imageError}
|
|
|
|
|
<div use:zoomImageAction class="h-full w-full" transition:fade={{ duration: haveFadeTransition ? 150 : 0 }}>
|
2024-05-28 13:15:50 +02:00
|
|
|
{#if $slideshowState !== SlideshowState.None && $slideshowLook === SlideshowLook.BlurredBackground}
|
2024-04-21 06:06:49 +02:00
|
|
|
<img
|
2024-06-07 14:22:46 -04:00
|
|
|
src={assetFileUrl}
|
2024-04-21 06:06:49 +02:00
|
|
|
alt={getAltText(asset)}
|
2024-05-28 13:15:50 +02:00
|
|
|
class="absolute top-0 left-0 -z-10 object-cover h-full w-full blur-lg"
|
2024-04-21 06:06:49 +02:00
|
|
|
draggable="false"
|
|
|
|
|
/>
|
2024-05-28 13:15:50 +02:00
|
|
|
{/if}
|
|
|
|
|
<img
|
|
|
|
|
bind:this={$photoViewer}
|
2024-06-07 14:22:46 -04:00
|
|
|
src={assetFileUrl}
|
2024-05-28 13:15:50 +02:00
|
|
|
alt={getAltText(asset)}
|
|
|
|
|
class="h-full w-full {$slideshowState === SlideshowState.None
|
|
|
|
|
? 'object-contain'
|
|
|
|
|
: slideshowLookCssMapping[$slideshowLook]}"
|
|
|
|
|
draggable="false"
|
|
|
|
|
/>
|
|
|
|
|
{#each getBoundingBox($boundingBoxesArray, $photoZoomState, $photoViewer) as boundingbox}
|
|
|
|
|
<div
|
|
|
|
|
class="absolute border-solid border-white border-[3px] rounded-lg"
|
|
|
|
|
style="top: {boundingbox.top}px; left: {boundingbox.left}px; height: {boundingbox.height}px; width: {boundingbox.width}px;"
|
|
|
|
|
/>
|
|
|
|
|
{/each}
|
|
|
|
|
</div>
|
2024-03-14 17:12:32 -04:00
|
|
|
{/if}
|
2022-05-27 14:02:06 -05:00
|
|
|
</div>
|