2022-05-27 14:02:06 -05:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { fade } from 'svelte/transition';
|
2023-02-13 16:27:15 -06:00
|
|
|
import { onMount } from 'svelte';
|
2022-07-16 23:52:00 -05:00
|
|
|
import LoadingSpinner from '../shared-components/loading-spinner.svelte';
|
2022-07-10 21:41:45 -05:00
|
|
|
import { api, AssetResponseDto } from '@api';
|
2022-11-16 22:04:37 +01:00
|
|
|
import {
|
|
|
|
|
notificationController,
|
|
|
|
|
NotificationType
|
|
|
|
|
} from '../shared-components/notification/notification';
|
2023-05-29 16:12:58 +02:00
|
|
|
import { useZoomImageWheel } from '@zoom-image/svelte';
|
2022-05-27 14:02:06 -05:00
|
|
|
|
2023-02-15 18:56:19 +01:00
|
|
|
export let asset: AssetResponseDto;
|
2023-01-09 14:16:08 -06:00
|
|
|
export let publicSharedKey = '';
|
2023-05-29 16:12:58 +02:00
|
|
|
let imgElement: HTMLDivElement;
|
2022-06-03 11:04:30 -05:00
|
|
|
|
2022-10-29 00:18:28 +02:00
|
|
|
let assetData: string;
|
|
|
|
|
|
2023-02-13 16:27:15 -06:00
|
|
|
let copyImageToClipboard: (src: string) => Promise<Blob>;
|
2023-02-17 17:41:52 +01:00
|
|
|
let canCopyImagesToClipboard: () => boolean;
|
2023-02-13 16:27:15 -06:00
|
|
|
|
|
|
|
|
onMount(async () => {
|
2023-02-17 17:41:52 +01:00
|
|
|
// Import hack :( see https://github.com/vadimkorr/svelte-carousel/issues/27#issuecomment-851022295
|
|
|
|
|
// TODO: Move to regular import once the package correctly supports ESM.
|
2023-02-13 16:27:15 -06:00
|
|
|
const module = await import('copy-image-clipboard');
|
|
|
|
|
copyImageToClipboard = module.copyImageToClipboard;
|
2023-02-17 17:41:52 +01:00
|
|
|
canCopyImagesToClipboard = module.canCopyImagesToClipboard;
|
2023-02-13 16:27:15 -06:00
|
|
|
});
|
|
|
|
|
|
2022-05-27 14:02:06 -05:00
|
|
|
const loadAssetData = async () => {
|
2022-07-26 12:28:07 -05:00
|
|
|
try {
|
2023-05-28 04:52:22 +03:00
|
|
|
const { data } = await api.assetApi.serveFile(
|
2023-06-01 22:19:25 -04:00
|
|
|
{ id: asset.id, isThumb: false, isWeb: true, key: publicSharedKey },
|
2023-05-28 04:52:22 +03:00
|
|
|
{
|
|
|
|
|
responseType: 'blob'
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-07-26 12:28:07 -05:00
|
|
|
|
|
|
|
|
if (!(data instanceof Blob)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-07-10 21:41:45 -05:00
|
|
|
|
2022-10-29 00:18:28 +02:00
|
|
|
assetData = URL.createObjectURL(data);
|
2022-07-26 12:28:07 -05:00
|
|
|
return assetData;
|
2022-09-08 17:30:49 +02:00
|
|
|
} catch {
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
2022-05-27 14:02:06 -05:00
|
|
|
};
|
2022-10-29 00:18:28 +02:00
|
|
|
|
2023-02-17 17:41:52 +01:00
|
|
|
const handleKeypress = async ({ metaKey, ctrlKey, key }: KeyboardEvent) => {
|
|
|
|
|
if ((metaKey || ctrlKey) && key === 'c') {
|
2022-11-16 22:04:37 +01:00
|
|
|
await doCopy();
|
2022-10-29 00:18:28 +02:00
|
|
|
}
|
|
|
|
|
};
|
2022-11-16 22:04:37 +01:00
|
|
|
|
2023-02-17 17:41:52 +01:00
|
|
|
const doCopy = async () => {
|
|
|
|
|
if (!canCopyImagesToClipboard()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await copyImageToClipboard(assetData);
|
|
|
|
|
notificationController.show({
|
|
|
|
|
type: NotificationType.Info,
|
|
|
|
|
message: 'Copied image to clipboard.',
|
|
|
|
|
timeout: 3000
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Error [photo-viewer]:', err);
|
|
|
|
|
notificationController.show({
|
|
|
|
|
type: NotificationType.Error,
|
|
|
|
|
message: 'Copying image to clipboard failed.'
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-11-16 22:04:37 +01:00
|
|
|
};
|
2023-05-29 16:12:58 +02:00
|
|
|
|
|
|
|
|
const { createZoomImage: createZoomImageWheel } = useZoomImageWheel();
|
|
|
|
|
$: if (imgElement) {
|
|
|
|
|
createZoomImageWheel(imgElement, {
|
|
|
|
|
wheelZoomRatio: 0.06
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-05-27 14:02:06 -05:00
|
|
|
</script>
|
|
|
|
|
|
2023-02-17 17:41:52 +01:00
|
|
|
<svelte:window on:keydown={handleKeypress} on:copyImage={doCopy} />
|
2022-10-29 00:18:28 +02:00
|
|
|
|
2022-07-16 23:52:00 -05:00
|
|
|
<div
|
|
|
|
|
transition:fade={{ duration: 150 }}
|
|
|
|
|
class="flex place-items-center place-content-center h-full select-none"
|
|
|
|
|
>
|
2023-02-15 18:56:19 +01:00
|
|
|
{#await loadAssetData()}
|
|
|
|
|
<LoadingSpinner />
|
|
|
|
|
{:then assetData}
|
2023-05-29 16:12:58 +02:00
|
|
|
<div bind:this={imgElement} class="h-full w-full">
|
|
|
|
|
<img
|
|
|
|
|
transition:fade={{ duration: 150 }}
|
|
|
|
|
src={assetData}
|
|
|
|
|
alt={asset.id}
|
|
|
|
|
class="object-contain h-full w-full"
|
|
|
|
|
draggable="false"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2023-02-15 18:56:19 +01:00
|
|
|
{/await}
|
2022-05-27 14:02:06 -05:00
|
|
|
</div>
|