2023-01-14 23:49:47 -06:00
|
|
|
<script lang="ts">
|
2024-04-24 15:24:19 -04:00
|
|
|
import Portal from '../portal/portal.svelte';
|
2023-07-01 00:50:47 -04:00
|
|
|
import Thumbnail from '$lib/components/assets/thumbnail/thumbnail.svelte';
|
2023-08-01 04:27:56 +03:00
|
|
|
import { assetViewingStore } from '$lib/stores/asset-viewing.store';
|
2024-02-17 11:00:55 -06:00
|
|
|
import type { BucketPosition, Viewport } from '$lib/stores/assets.store';
|
2024-02-14 06:38:57 -08:00
|
|
|
import { handleError } from '$lib/utils/handle-error';
|
2024-02-17 11:00:55 -06:00
|
|
|
import { type AssetResponseDto } from '@immich/sdk';
|
2024-02-14 06:38:57 -08:00
|
|
|
import { createEventDispatcher, onDestroy } from 'svelte';
|
|
|
|
import AssetViewer from '../../asset-viewer/asset-viewer.svelte';
|
2024-02-17 11:00:55 -06:00
|
|
|
import justifiedLayout from 'justified-layout';
|
|
|
|
import { getAssetRatio } from '$lib/utils/asset-utils';
|
|
|
|
import { calculateWidth } from '$lib/utils/timeline-util';
|
2024-04-24 15:24:19 -04:00
|
|
|
import { navigate } from '$lib/utils/navigation';
|
2024-05-02 04:17:40 +08:00
|
|
|
import { AppRoute, AssetAction } from '$lib/constants';
|
|
|
|
import { goto } from '$app/navigation';
|
2024-02-12 20:50:47 -05:00
|
|
|
|
|
|
|
const dispatch = createEventDispatcher<{ intersected: { container: HTMLDivElement; position: BucketPosition } }>();
|
2023-07-01 00:50:47 -04:00
|
|
|
|
|
|
|
export let assets: AssetResponseDto[];
|
|
|
|
export let selectedAssets: Set<AssetResponseDto> = new Set();
|
|
|
|
export let disableAssetSelect = false;
|
|
|
|
export let showArchiveIcon = false;
|
2024-02-17 11:00:55 -06:00
|
|
|
export let viewport: Viewport;
|
2023-07-01 00:50:47 -04:00
|
|
|
|
2024-05-02 04:17:40 +08:00
|
|
|
let { isViewing: isViewerOpen, asset: viewingAsset, setAsset } = assetViewingStore;
|
2023-08-01 04:27:56 +03:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
let currentViewAssetIndex = 0;
|
|
|
|
$: isMultiSelectionMode = selectedAssets.size > 0;
|
|
|
|
|
2024-04-24 15:24:19 -04:00
|
|
|
const viewAssetHandler = async (asset: AssetResponseDto) => {
|
2023-07-01 00:50:47 -04:00
|
|
|
currentViewAssetIndex = assets.findIndex((a) => a.id == asset.id);
|
2024-04-24 15:24:19 -04:00
|
|
|
setAsset(assets[currentViewAssetIndex]);
|
|
|
|
await navigate({ targetRoute: 'current', assetId: $viewingAsset.id });
|
2023-07-01 00:50:47 -04:00
|
|
|
};
|
|
|
|
|
2024-03-15 17:01:35 +01:00
|
|
|
const selectAssetHandler = (asset: AssetResponseDto) => {
|
2024-02-02 04:18:00 +01:00
|
|
|
let temporary = new Set(selectedAssets);
|
2023-07-01 00:50:47 -04:00
|
|
|
|
|
|
|
if (selectedAssets.has(asset)) {
|
2024-02-02 04:18:00 +01:00
|
|
|
temporary.delete(asset);
|
2023-07-01 00:50:47 -04:00
|
|
|
} else {
|
2024-02-02 04:18:00 +01:00
|
|
|
temporary.add(asset);
|
2023-07-01 00:50:47 -04:00
|
|
|
}
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
selectedAssets = temporary;
|
2023-07-01 00:50:47 -04:00
|
|
|
};
|
|
|
|
|
2024-05-02 04:17:40 +08:00
|
|
|
const handleNext = async () => {
|
2023-07-01 00:50:47 -04:00
|
|
|
try {
|
|
|
|
if (currentViewAssetIndex < assets.length - 1) {
|
2024-04-24 15:24:19 -04:00
|
|
|
setAsset(assets[++currentViewAssetIndex]);
|
|
|
|
await navigate({ targetRoute: 'current', assetId: $viewingAsset.id });
|
2023-07-01 00:50:47 -04:00
|
|
|
}
|
2024-02-02 04:18:00 +01:00
|
|
|
} catch (error) {
|
|
|
|
handleError(error, 'Cannot navigate to the next asset');
|
2023-07-01 00:50:47 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-02 04:17:40 +08:00
|
|
|
const handlePrevious = async () => {
|
2023-07-01 00:50:47 -04:00
|
|
|
try {
|
|
|
|
if (currentViewAssetIndex > 0) {
|
2024-04-24 15:24:19 -04:00
|
|
|
setAsset(assets[--currentViewAssetIndex]);
|
|
|
|
await navigate({ targetRoute: 'current', assetId: $viewingAsset.id });
|
2023-07-01 00:50:47 -04:00
|
|
|
}
|
2024-02-02 04:18:00 +01:00
|
|
|
} catch (error) {
|
|
|
|
handleError(error, 'Cannot navigate to previous asset');
|
2023-07-01 00:50:47 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-02 04:17:40 +08:00
|
|
|
const handleAction = async (action: AssetAction, asset: AssetResponseDto) => {
|
|
|
|
switch (action) {
|
|
|
|
case AssetAction.ARCHIVE:
|
|
|
|
case AssetAction.DELETE:
|
|
|
|
case AssetAction.TRASH: {
|
|
|
|
assets.splice(
|
|
|
|
assets.findIndex((a) => a.id === asset.id),
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
assets = assets;
|
|
|
|
if (assets.length === 0) {
|
|
|
|
await goto(AppRoute.PHOTOS);
|
|
|
|
} else if (currentViewAssetIndex === assets.length) {
|
|
|
|
await handlePrevious();
|
|
|
|
} else {
|
|
|
|
setAsset(assets[currentViewAssetIndex]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-22 10:54:58 -05:00
|
|
|
onDestroy(() => {
|
2024-05-02 04:17:40 +08:00
|
|
|
$isViewerOpen = false;
|
2023-11-22 10:54:58 -05:00
|
|
|
});
|
2024-02-17 11:00:55 -06:00
|
|
|
|
|
|
|
$: geometry = (() => {
|
|
|
|
const justifiedLayoutResult = justifiedLayout(
|
|
|
|
assets.map((asset) => getAssetRatio(asset)),
|
|
|
|
{
|
|
|
|
boxSpacing: 2,
|
|
|
|
containerWidth: Math.floor(viewport.width),
|
|
|
|
containerPadding: 0,
|
|
|
|
targetRowHeightTolerance: 0.15,
|
|
|
|
targetRowHeight: 235,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...justifiedLayoutResult,
|
|
|
|
containerWidth: calculateWidth(justifiedLayoutResult.boxes),
|
|
|
|
};
|
|
|
|
})();
|
2023-01-14 23:49:47 -06:00
|
|
|
</script>
|
|
|
|
|
|
|
|
{#if assets.length > 0}
|
2024-02-17 11:00:55 -06:00
|
|
|
<div class="relative" style="height: {geometry.containerHeight}px;width: {geometry.containerWidth}px ">
|
|
|
|
{#each assets as asset, i (i)}
|
|
|
|
<div
|
|
|
|
class="absolute"
|
|
|
|
style="width: {geometry.boxes[i].width}px; height: {geometry.boxes[i].height}px; top: {geometry.boxes[i]
|
|
|
|
.top}px; left: {geometry.boxes[i].left}px"
|
|
|
|
>
|
2023-07-01 00:50:47 -04:00
|
|
|
<Thumbnail
|
|
|
|
{asset}
|
|
|
|
readonly={disableAssetSelect}
|
2024-03-15 17:01:35 +01:00
|
|
|
onClick={(e) => (isMultiSelectionMode ? selectAssetHandler(e) : viewAssetHandler(e))}
|
|
|
|
on:select={(e) => selectAssetHandler(e.detail.asset)}
|
2024-02-12 20:50:47 -05:00
|
|
|
on:intersected={(event) =>
|
|
|
|
i === Math.max(1, assets.length - 7) ? dispatch('intersected', event.detail) : undefined}
|
2023-07-01 00:50:47 -04:00
|
|
|
selected={selectedAssets.has(asset)}
|
2024-06-08 16:33:23 -04:00
|
|
|
isMultiSelectState={isMultiSelectionMode}
|
2023-07-01 00:50:47 -04:00
|
|
|
{showArchiveIcon}
|
2024-02-17 11:00:55 -06:00
|
|
|
thumbnailWidth={geometry.boxes[i].width}
|
|
|
|
thumbnailHeight={geometry.boxes[i].height}
|
2023-07-01 00:50:47 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|
2023-01-14 23:49:47 -06:00
|
|
|
{/if}
|
|
|
|
|
|
|
|
<!-- Overlay Asset Viewer -->
|
2024-05-02 04:17:40 +08:00
|
|
|
{#if $isViewerOpen}
|
2024-04-24 15:24:19 -04:00
|
|
|
<Portal target="body">
|
2024-05-02 04:17:40 +08:00
|
|
|
<AssetViewer
|
|
|
|
asset={$viewingAsset}
|
|
|
|
on:action={({ detail: action }) => handleAction(action.type, action.asset)}
|
|
|
|
on:previous={handlePrevious}
|
|
|
|
on:next={handleNext}
|
|
|
|
/>
|
2024-04-24 15:24:19 -04:00
|
|
|
</Portal>
|
2023-01-14 23:49:47 -06:00
|
|
|
{/if}
|