Skip to content

Commit 53fb3a3

Browse files
feat(web): avoid duplicate call + small refactor (immich-app#1731)
1 parent 6b38929 commit 53fb3a3

File tree

5 files changed

+56
-106
lines changed

5 files changed

+56
-106
lines changed

web/package-lock.json

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"svelte": "^3.44.0",
5353
"svelte-check": "^2.7.1",
5454
"svelte-jester": "^2.3.2",
55-
"svelte-keydown": "^0.5.0",
5655
"svelte-preprocess": "^4.10.7",
5756
"tailwindcss": "^3.0.24",
5857
"tslib": "^2.3.1",
@@ -70,7 +69,6 @@
7069
"lodash-es": "^4.17.21",
7170
"luxon": "^3.1.1",
7271
"socket.io-client": "^4.5.1",
73-
"svelte-keydown": "^0.5.0",
7472
"svelte-material-icons": "^2.0.2"
7573
}
7674
}

web/src/lib/components/asset-viewer/asset-viewer.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@
304304
on:onVideoEnded={() => (shouldPlayMotionPhoto = false)}
305305
/>
306306
{:else}
307-
<PhotoViewer {publicSharedKey} assetId={asset.id} on:close={closeViewer} />
307+
<PhotoViewer {publicSharedKey} {asset} on:close={closeViewer} />
308308
{/if}
309309
{:else}
310310
<VideoViewer {publicSharedKey} assetId={asset.id} on:close={closeViewer} />
Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
11
<script lang="ts">
22
import { fade } from 'svelte/transition';
3-
4-
import { onMount } from 'svelte';
53
import LoadingSpinner from '../shared-components/loading-spinner.svelte';
64
import { api, AssetResponseDto } from '@api';
7-
import Keydown from 'svelte-keydown';
5+
import { copyImageToClipboard } from 'copy-image-clipboard';
86
import {
97
notificationController,
108
NotificationType
119
} from '../shared-components/notification/notification';
1210
13-
export let assetId: string;
11+
export let asset: AssetResponseDto;
1412
export let publicSharedKey = '';
1513
16-
let assetInfo: AssetResponseDto;
1714
let assetData: string;
1815
19-
let copyImageToClipboard: (src: string) => Promise<Blob>;
20-
21-
onMount(async () => {
22-
const { data } = await api.assetApi.getAssetById(assetId, {
23-
params: {
24-
key: publicSharedKey
25-
}
26-
});
27-
assetInfo = data;
28-
29-
//Import hack :( see https://github.com/vadimkorr/svelte-carousel/issues/27#issuecomment-851022295
30-
const module = await import('copy-image-clipboard');
31-
copyImageToClipboard = module.copyImageToClipboard;
32-
});
33-
3416
const loadAssetData = async () => {
3517
try {
36-
const { data } = await api.assetApi.serveFile(assetInfo.id, false, true, {
18+
const { data } = await api.assetApi.serveFile(asset.id, false, true, {
3719
params: {
3820
key: publicSharedKey
3921
},
@@ -51,42 +33,51 @@
5133
}
5234
};
5335
54-
const handleKeypress = async (keyEvent: CustomEvent<string>) => {
55-
if (keyEvent.detail == 'Control-c' || keyEvent.detail == 'Meta-c') {
36+
const handleKeypress = async ({ metaKey, ctrlKey, key }: KeyboardEvent) => {
37+
if ((metaKey || ctrlKey) && key === 'c') {
5638
await doCopy();
5739
}
5840
};
5941
6042
export const doCopy = async () => {
61-
await copyImageToClipboard(assetData);
62-
notificationController.show({
63-
type: NotificationType.Info,
64-
message: 'Copied image to clipboard.',
65-
timeout: 3000
66-
});
43+
try {
44+
await copyImageToClipboard(assetData);
45+
notificationController.show({
46+
type: NotificationType.Info,
47+
message: 'Copied image to clipboard.',
48+
timeout: 3000
49+
});
50+
} catch (err) {
51+
console.error(err);
52+
notificationController.show({
53+
type: NotificationType.Error,
54+
message: 'Copying image to clipboard failed. Click here to learn more.',
55+
timeout: 5000,
56+
action: {
57+
type: 'link',
58+
target:
59+
'https://github.com/LuanEdCosta/copy-image-clipboard#enable-clipboard-api-features-in-firefox'
60+
}
61+
});
62+
}
6763
};
6864
</script>
6965

70-
<Keydown on:combo={handleKeypress} />
71-
72-
<svelte:window on:copyImage={async () => await doCopy()} />
66+
<svelte:window on:keydown={handleKeypress} on:copyImage={doCopy} />
7367

7468
<div
7569
transition:fade={{ duration: 150 }}
7670
class="flex place-items-center place-content-center h-full select-none"
7771
>
78-
{#if assetInfo}
79-
{#await loadAssetData()}
80-
<LoadingSpinner />
81-
{:then assetData}
82-
<img
83-
transition:fade={{ duration: 150 }}
84-
src={assetData}
85-
alt={assetId}
86-
class="object-contain h-full transition-all"
87-
loading="lazy"
88-
draggable="false"
89-
/>
90-
{/await}
91-
{/if}
72+
{#await loadAssetData()}
73+
<LoadingSpinner />
74+
{:then assetData}
75+
<img
76+
transition:fade={{ duration: 150 }}
77+
src={assetData}
78+
alt={asset.id}
79+
class="object-contain h-full transition-all"
80+
draggable="false"
81+
/>
82+
{/await}
9283
</div>
Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,17 @@
11
<script lang="ts">
22
import { fade } from 'svelte/transition';
3-
4-
import { createEventDispatcher, onMount } from 'svelte';
3+
import { createEventDispatcher } from 'svelte';
54
import LoadingSpinner from '../shared-components/loading-spinner.svelte';
6-
import { api, AssetResponseDto, getFileUrl } from '@api';
5+
import { getFileUrl } from '@api';
76
87
export let assetId: string;
98
export let publicSharedKey = '';
10-
let asset: AssetResponseDto;
119
1210
let isVideoLoading = true;
13-
let videoUrl: string;
1411
const dispatch = createEventDispatcher();
1512
16-
onMount(async () => {
17-
const { data: assetInfo } = await api.assetApi.getAssetById(assetId, {
18-
params: {
19-
key: publicSharedKey
20-
}
21-
});
22-
23-
await loadVideoData(assetInfo);
24-
25-
asset = assetInfo;
26-
});
27-
28-
const loadVideoData = async (assetInfo: AssetResponseDto) => {
29-
isVideoLoading = true;
30-
31-
videoUrl = getFileUrl(assetInfo.id, false, true, publicSharedKey);
32-
33-
return assetInfo;
34-
};
35-
36-
const handleCanPlay = (ev: Event) => {
37-
const playerNode = ev.target as HTMLVideoElement;
13+
const handleCanPlay = (ev: Event & { currentTarget: HTMLVideoElement }) => {
14+
const playerNode = ev.currentTarget;
3815
3916
playerNode.muted = true;
4017
playerNode.play();
@@ -48,21 +25,19 @@
4825
transition:fade={{ duration: 150 }}
4926
class="flex place-items-center place-content-center h-full select-none"
5027
>
51-
{#if asset}
52-
<video
53-
controls
54-
class="h-full object-contain"
55-
on:canplay={handleCanPlay}
56-
on:ended={() => dispatch('onVideoEnded')}
57-
>
58-
<source src={videoUrl} type="video/mp4" />
59-
<track kind="captions" />
60-
</video>
61-
62-
{#if isVideoLoading}
63-
<div class="absolute flex place-items-center place-content-center">
64-
<LoadingSpinner />
65-
</div>
66-
{/if}
28+
<video
29+
controls
30+
class="h-full object-contain"
31+
src={getFileUrl(assetId, false, true, publicSharedKey)}
32+
on:canplay={handleCanPlay}
33+
on:ended={() => dispatch('onVideoEnded')}
34+
>
35+
<track kind="captions" />
36+
</video>
37+
38+
{#if isVideoLoading}
39+
<div class="absolute flex place-items-center place-content-center">
40+
<LoadingSpinner />
41+
</div>
6742
{/if}
6843
</div>

0 commit comments

Comments
 (0)