Skip to content

Commit 6bf2e8d

Browse files
feat: add album keyboard shortcuts (immich-app#16442)
* 15712: Added keyboard shortcuts for opening add to album modal and highlighting/selecting an album to add to. * 15712: Re-factored logic from template code into script. Extracted new album button into separate cmponent. * 15712: Document new keyboard shortucts now that they work everywhere. * 15712: Extract some constants/helper functions. * 15712: Missing comma. * 15712: Pulled logic out into separate unit testable class. * 15712: Added a unit test. * 15712: Move the modal back up to keep the github PR happy. * 15712: PR feedback - renamed typescript files and switch to class bind directive. * 15712:Move selection modal into correct package. * 15712: Better naming of module and files.
1 parent 366f237 commit 6bf2e8d

9 files changed

+455
-116
lines changed

web/src/lib/components/asset-viewer/actions/add-to-album-action.svelte

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
2+
import { shortcut } from '$lib/actions/shortcut';
23
import type { OnAction } from '$lib/components/asset-viewer/actions/action';
3-
import AlbumSelectionModal from '$lib/components/shared-components/album-selection-modal.svelte';
4+
import AlbumSelectionModal from '$lib/components/shared-components/album-selection/album-selection-modal.svelte';
45
import MenuOption from '$lib/components/shared-components/context-menu/menu-option.svelte';
56
import Portal from '$lib/components/shared-components/portal/portal.svelte';
67
import { AssetAction } from '$lib/constants';
@@ -34,6 +35,10 @@
3435
};
3536
</script>
3637

38+
<svelte:window
39+
use:shortcut={{ shortcut: { key: 'l', shift: shared }, onShortcut: () => (showSelectionModal = true) }}
40+
/>
41+
3742
<MenuOption
3843
icon={shared ? mdiShareVariantOutline : mdiImageAlbum}
3944
text={shared ? $t('add_to_shared_album') : $t('add_to_album')}

web/src/lib/components/asset-viewer/album-list-item.svelte

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@
33
import { type AlbumResponseDto } from '@immich/sdk';
44
import { normalizeSearchString } from '$lib/utils/string-utils.js';
55
import AlbumListItemDetails from './album-list-item-details.svelte';
6+
import type { Action } from 'svelte/action';
7+
import { SCROLL_PROPERTIES } from '$lib/components/shared-components/album-selection/album-selection-utils';
68
79
interface Props {
810
album: AlbumResponseDto;
911
searchQuery?: string;
12+
selected: boolean;
1013
onAlbumClick: () => void;
1114
}
1215
13-
let { album, searchQuery = '', onAlbumClick }: Props = $props();
16+
let { album, searchQuery = '', selected = false, onAlbumClick }: Props = $props();
17+
18+
const scrollIntoViewIfSelected: Action = (node) => {
19+
$effect(() => {
20+
if (selected) {
21+
node.scrollIntoView(SCROLL_PROPERTIES);
22+
}
23+
});
24+
};
1425
1526
let albumNameArray: string[] = $state(['', '', '']);
1627
@@ -31,7 +42,10 @@
3142
<button
3243
type="button"
3344
onclick={onAlbumClick}
45+
use:scrollIntoViewIfSelected
3446
class="flex w-full gap-4 px-6 py-2 text-left transition-colors hover:bg-gray-200 dark:hover:bg-gray-700 rounded-xl"
47+
class:bg-gray-200={selected}
48+
class:dark:bg-gray-700={selected}
3549
>
3650
<span class="h-12 w-12 shrink-0 rounded-xl bg-slate-300">
3751
{#if album.albumThumbnailAssetId}

web/src/lib/components/photos-page/actions/add-to-album.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import AlbumSelectionModal from '$lib/components/shared-components/album-selection-modal.svelte';
2+
import AlbumSelectionModal from '$lib/components/shared-components/album-selection/album-selection-modal.svelte';
33
import MenuOption from '$lib/components/shared-components/context-menu/menu-option.svelte';
44
import { addAssetsToAlbum, addAssetsToNewAlbum } from '$lib/utils/asset-utils';
55
import type { AlbumResponseDto } from '@immich/sdk';

web/src/lib/components/shared-components/album-selection-modal.svelte

-113
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<script lang="ts">
2+
import { type AlbumResponseDto, getAllAlbums } from '@immich/sdk';
3+
import { onMount } from 'svelte';
4+
import AlbumListItem from '../../asset-viewer/album-list-item.svelte';
5+
import NewAlbumListItem from './new-album-list-item.svelte';
6+
import FullScreenModal from '$lib/components/shared-components/full-screen-modal.svelte';
7+
import { initInput } from '$lib/actions/focus';
8+
import { t } from 'svelte-i18n';
9+
import { albumViewSettings } from '$lib/stores/preferences.store';
10+
import {
11+
AlbumModalRowConverter,
12+
AlbumModalRowType,
13+
isSelectableRowType,
14+
} from '$lib/components/shared-components/album-selection/album-selection-utils';
15+
16+
let albums: AlbumResponseDto[] = $state([]);
17+
let recentAlbums: AlbumResponseDto[] = $state([]);
18+
let loading = $state(true);
19+
let search = $state('');
20+
let selectedRowIndex: number = $state(-1);
21+
22+
interface Props {
23+
onNewAlbum: (search: string) => void;
24+
onAlbumClick: (album: AlbumResponseDto) => void;
25+
shared: boolean;
26+
onClose: () => void;
27+
}
28+
29+
let { onNewAlbum, onAlbumClick, shared, onClose }: Props = $props();
30+
31+
onMount(async () => {
32+
albums = await getAllAlbums({ shared: shared || undefined });
33+
recentAlbums = albums.sort((a, b) => (new Date(a.createdAt) > new Date(b.createdAt) ? -1 : 1)).slice(0, 3);
34+
loading = false;
35+
});
36+
37+
const rowConverter = new AlbumModalRowConverter(shared, $albumViewSettings.sortBy, $albumViewSettings.sortOrder);
38+
const albumModalRows = $derived(rowConverter.toModalRows(search, recentAlbums, albums, selectedRowIndex));
39+
const selectableRowCount = $derived(albumModalRows.filter((row) => isSelectableRowType(row.type)).length);
40+
41+
const onkeydown = (e: KeyboardEvent) => {
42+
switch (e.key) {
43+
case 'ArrowUp': {
44+
e.preventDefault();
45+
if (selectedRowIndex > 0) {
46+
selectedRowIndex--;
47+
} else {
48+
selectedRowIndex = selectableRowCount - 1;
49+
}
50+
break;
51+
}
52+
case 'ArrowDown': {
53+
e.preventDefault();
54+
if (selectedRowIndex < selectableRowCount - 1) {
55+
selectedRowIndex++;
56+
} else {
57+
selectedRowIndex = 0;
58+
}
59+
break;
60+
}
61+
case 'Enter': {
62+
e.preventDefault();
63+
const selectedRow = albumModalRows.find((row) => row.selected);
64+
if (selectedRow) {
65+
if (selectedRow.type === AlbumModalRowType.NEW_ALBUM) {
66+
onNewAlbum(search);
67+
} else if (selectedRow.type === AlbumModalRowType.ALBUM_ITEM && selectedRow.album) {
68+
onAlbumClick(selectedRow.album);
69+
}
70+
selectedRowIndex = -1;
71+
}
72+
break;
73+
}
74+
default: {
75+
selectedRowIndex = -1;
76+
}
77+
}
78+
};
79+
80+
const handleAlbumClick = (album: AlbumResponseDto) => () => onAlbumClick(album);
81+
</script>
82+
83+
<FullScreenModal title={shared ? $t('add_to_shared_album') : $t('add_to_album')} {onClose}>
84+
<div class="mb-2 flex max-h-[400px] flex-col">
85+
{#if loading}
86+
{#each { length: 3 } as _}
87+
<div class="flex animate-pulse gap-4 px-6 py-2">
88+
<div class="h-12 w-12 rounded-xl bg-slate-200"></div>
89+
<div class="flex flex-col items-start justify-center gap-2">
90+
<span class="h-4 w-36 animate-pulse bg-slate-200"></span>
91+
<div class="flex animate-pulse gap-1">
92+
<span class="h-3 w-8 bg-slate-200"></span>
93+
<span class="h-3 w-20 bg-slate-200"></span>
94+
</div>
95+
</div>
96+
</div>
97+
{/each}
98+
{:else}
99+
<input
100+
class="border-b-4 border-immich-bg bg-immich-bg px-6 py-2 text-2xl focus:border-immich-primary dark:border-immich-dark-gray dark:bg-immich-dark-gray dark:focus:border-immich-dark-primary"
101+
placeholder={$t('search')}
102+
{onkeydown}
103+
bind:value={search}
104+
use:initInput
105+
/>
106+
<div class="immich-scrollbar overflow-y-auto">
107+
{#each albumModalRows as row}
108+
{#if row.type === AlbumModalRowType.NEW_ALBUM}
109+
<NewAlbumListItem selected={row.selected || false} {onNewAlbum} searchQuery={search} />
110+
{:else if row.type === AlbumModalRowType.SECTION}
111+
<p class="px-5 py-3 text-xs">{row.text}</p>
112+
{:else if row.type === AlbumModalRowType.MESSAGE}
113+
<p class="px-5 py-1 text-sm">{row.text}</p>
114+
{:else if row.type === AlbumModalRowType.ALBUM_ITEM && row.album}
115+
<AlbumListItem
116+
album={row.album}
117+
selected={row.selected || false}
118+
searchQuery={search}
119+
onAlbumClick={handleAlbumClick(row.album)}
120+
/>
121+
{/if}
122+
{/each}
123+
</div>
124+
{/if}
125+
</div>
126+
</FullScreenModal>

0 commit comments

Comments
 (0)