mirror of
https://github.com/immich-app/immich.git
synced 2026-03-03 03:07:02 +00:00
Compare commits
1 Commits
push-zlzxx
...
6bc1033647
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bc1033647 |
63
web/src/lib/actions/image-loader.svelte.ts
Normal file
63
web/src/lib/actions/image-loader.svelte.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { cancelImageUrl } from '$lib/utils/sw-messaging';
|
||||
|
||||
const destroyImageElement = (
|
||||
imgElement: HTMLImageElement,
|
||||
currentSrc: string | undefined,
|
||||
handleLoad: () => void,
|
||||
handleError: () => void,
|
||||
) => {
|
||||
imgElement.removeEventListener('load', handleLoad);
|
||||
imgElement.removeEventListener('error', handleError);
|
||||
cancelImageUrl(currentSrc);
|
||||
imgElement.remove();
|
||||
};
|
||||
|
||||
const createImageElement = (
|
||||
src: string | undefined,
|
||||
|
||||
onLoad: () => void,
|
||||
onError: () => void,
|
||||
onStart?: () => void,
|
||||
) => {
|
||||
if (!src) {
|
||||
return undefined;
|
||||
}
|
||||
const img = document.createElement('img');
|
||||
|
||||
img.addEventListener('load', onLoad);
|
||||
img.addEventListener('error', onError);
|
||||
|
||||
onStart?.();
|
||||
img.src = src;
|
||||
|
||||
return img;
|
||||
};
|
||||
|
||||
export function loadImage(
|
||||
src: string,
|
||||
|
||||
onLoad: () => void,
|
||||
onError: () => void,
|
||||
onStart?: () => void,
|
||||
) {
|
||||
let destroyed = false;
|
||||
const wrapper = (fn: (() => void) | undefined) => () => {
|
||||
if (destroyed) {
|
||||
return;
|
||||
}
|
||||
fn?.();
|
||||
};
|
||||
const wrappedOnLoad = wrapper(onLoad);
|
||||
const wrappedOnError = wrapper(onError);
|
||||
const wrappedOnStart = wrapper(onStart);
|
||||
const img = createImageElement(src, wrappedOnLoad, wrappedOnError, wrappedOnStart);
|
||||
if (!img) {
|
||||
return () => {};
|
||||
}
|
||||
return () => {
|
||||
destroyed = true;
|
||||
destroyImageElement(img, src, wrappedOnLoad, wrappedOnError);
|
||||
};
|
||||
}
|
||||
|
||||
export type LoadImageFunction = typeof loadImage;
|
||||
11
web/src/lib/components/AlphaBackground.svelte
Normal file
11
web/src/lib/components/AlphaBackground.svelte
Normal file
@@ -0,0 +1,11 @@
|
||||
<script lang="ts">
|
||||
import type { ClassValue } from 'svelte/elements';
|
||||
|
||||
interface Props {
|
||||
class?: ClassValue;
|
||||
}
|
||||
|
||||
let { class: className = '' }: Props = $props();
|
||||
</script>
|
||||
|
||||
<div class="absolute h-full w-full bg-gray-300 dark:bg-gray-700 {className}"></div>
|
||||
@@ -4,7 +4,38 @@ import { AssetMediaSize, type AssetResponseDto } from '@immich/sdk';
|
||||
|
||||
type AllAssetMediaSize = AssetMediaSize | 'all';
|
||||
|
||||
type AssetLoadState = 'loading' | 'cancelled';
|
||||
|
||||
class ImageManager {
|
||||
// track recently canceled assets, so know if an load "error" is due to
|
||||
// cancelation
|
||||
private assetStates = new Map<string, AssetLoadState>();
|
||||
private readonly MAX_TRACKED_ASSETS = 10;
|
||||
|
||||
private trackAction(asset: AssetResponseDto, action: AssetLoadState) {
|
||||
// Remove if exists to reset insertion order
|
||||
this.assetStates.delete(asset.id);
|
||||
this.assetStates.set(asset.id, action);
|
||||
|
||||
// Only keep recent assets (Map maintains insertion order)
|
||||
if (this.assetStates.size > this.MAX_TRACKED_ASSETS) {
|
||||
const firstKey = this.assetStates.keys().next().value!;
|
||||
this.assetStates.delete(firstKey);
|
||||
}
|
||||
}
|
||||
|
||||
isCanceled(asset: AssetResponseDto) {
|
||||
return 'cancelled' === this.assetStates.get(asset.id);
|
||||
}
|
||||
|
||||
trackLoad(asset: AssetResponseDto) {
|
||||
this.trackAction(asset, 'loading');
|
||||
}
|
||||
|
||||
trackCancelled(asset: AssetResponseDto) {
|
||||
this.trackAction(asset, 'cancelled');
|
||||
}
|
||||
|
||||
preload(asset: AssetResponseDto | undefined, size: AssetMediaSize = AssetMediaSize.Preview) {
|
||||
if (!asset) {
|
||||
return;
|
||||
@@ -15,6 +46,8 @@ class ImageManager {
|
||||
return;
|
||||
}
|
||||
|
||||
this.trackLoad(asset);
|
||||
|
||||
const img = new Image();
|
||||
img.src = url;
|
||||
}
|
||||
@@ -24,6 +57,8 @@ class ImageManager {
|
||||
return;
|
||||
}
|
||||
|
||||
this.trackCancelled(asset);
|
||||
|
||||
const sizes = size === 'all' ? Object.values(AssetMediaSize) : [size];
|
||||
for (const size of sizes) {
|
||||
const url = getAssetMediaUrl({ id: asset.id, size, cacheKey: asset.thumbhash });
|
||||
|
||||
@@ -198,13 +198,10 @@ export const getAssetUrl = ({
|
||||
sharedLink,
|
||||
forceOriginal = false,
|
||||
}: {
|
||||
asset: AssetResponseDto | undefined;
|
||||
asset: AssetResponseDto;
|
||||
sharedLink?: SharedLinkResponseDto;
|
||||
forceOriginal?: boolean;
|
||||
}) => {
|
||||
if (!asset) {
|
||||
return;
|
||||
}
|
||||
const id = asset.id;
|
||||
const cacheKey = asset.thumbhash;
|
||||
if (sharedLink && (!sharedLink.allowDownload || !sharedLink.showMetadata)) {
|
||||
|
||||
54
web/src/lib/utils/layout-utils.spec.ts
Normal file
54
web/src/lib/utils/layout-utils.spec.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { scaleToFit } from '$lib/utils/layout-utils';
|
||||
|
||||
describe('scaleToFit', () => {
|
||||
const tests = [
|
||||
{
|
||||
name: 'landscape image in square container',
|
||||
dimensions: { width: 2000, height: 1000 },
|
||||
container: { width: 500, height: 500 },
|
||||
expected: { width: 500, height: 250 },
|
||||
},
|
||||
{
|
||||
name: 'portrait image in square container',
|
||||
dimensions: { width: 1000, height: 2000 },
|
||||
container: { width: 500, height: 500 },
|
||||
expected: { width: 250, height: 500 },
|
||||
},
|
||||
{
|
||||
name: 'square image in square container',
|
||||
dimensions: { width: 1000, height: 1000 },
|
||||
container: { width: 500, height: 500 },
|
||||
expected: { width: 500, height: 500 },
|
||||
},
|
||||
{
|
||||
name: 'landscape image in landscape container',
|
||||
dimensions: { width: 1600, height: 900 },
|
||||
container: { width: 800, height: 600 },
|
||||
expected: { width: 800, height: 450 },
|
||||
},
|
||||
{
|
||||
name: 'portrait image in portrait container',
|
||||
dimensions: { width: 900, height: 1600 },
|
||||
container: { width: 600, height: 800 },
|
||||
expected: { width: 450, height: 800 },
|
||||
},
|
||||
{
|
||||
name: 'image matches container exactly',
|
||||
dimensions: { width: 500, height: 300 },
|
||||
container: { width: 500, height: 300 },
|
||||
expected: { width: 500, height: 300 },
|
||||
},
|
||||
{
|
||||
name: 'image smaller than container scales up',
|
||||
dimensions: { width: 100, height: 50 },
|
||||
container: { width: 400, height: 400 },
|
||||
expected: { width: 400, height: 200 },
|
||||
},
|
||||
];
|
||||
|
||||
for (const { name, dimensions, container, expected } of tests) {
|
||||
it(`should handle ${name}`, () => {
|
||||
expect(scaleToFit(dimensions, container)).toEqual(expected);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -129,3 +129,19 @@ export type CommonPosition = {
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
|
||||
// Scales dimensions to fit within a container (like object-fit: contain)
|
||||
export const scaleToFit = (
|
||||
dimensions: { width: number; height: number },
|
||||
container: { width: number; height: number },
|
||||
) => {
|
||||
const scaleX = container.width / dimensions.width;
|
||||
const scaleY = container.height / dimensions.height;
|
||||
|
||||
const scale = Math.min(scaleX, scaleY);
|
||||
|
||||
return {
|
||||
width: dimensions.width * scale,
|
||||
height: dimensions.height * scale,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user