Skip to content

feat(web): add keyboard shortcut to stack selected photos #5983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 4 additions & 43 deletions web/src/lib/components/photos-page/actions/stack-action.svelte
Original file line number Diff line number Diff line change
@@ -1,58 +1,19 @@
<script lang="ts">
import MenuOption from '$lib/components/shared-components/context-menu/menu-option.svelte';
import {
NotificationType,
notificationController,
} from '$lib/components/shared-components/notification/notification';
import type { OnStack } from '$lib/utils/actions';
import { handleError } from '$lib/utils/handle-error';
import { updateAssets } from '@immich/sdk';
import { getAssetControlContext } from '../asset-select-control-bar.svelte';
import type { OnStack } from '$lib/utils/actions';
import { stackAssets } from '$lib/utils/asset-utils';
import { mdiImageMultipleOutline } from '@mdi/js';

export let onStack: OnStack | undefined;

const { clearSelect, getOwnedAssets } = getAssetControlContext();

const handleStack = async () => {
try {
const assets = [...getOwnedAssets()];
const parent = assets.at(0);

if (parent == undefined) {
return;
}

const children = assets.slice(1);
const ids = children.map(({ id }) => id);

if (children.length > 0) {
await updateAssets({ assetBulkUpdateDto: { ids, stackParentId: parent.id } });
}

let childrenCount = parent.stackCount ?? 0;
for (const asset of children) {
asset.stackParentId = parent?.id;
// Add grand-children's count to new parent
childrenCount += asset.stackCount == undefined ? 1 : asset.stackCount + 1;
// Reset children stack info
asset.stackCount = null;
asset.stack = [];
}

parent.stackCount = childrenCount;
await stackAssets([...getOwnedAssets()], (ids) => {
onStack?.(ids);

notificationController.show({
message: `Stacked ${ids.length + 1} assets`,
type: NotificationType.Info,
timeout: 1500,
});

clearSelect();
} catch (error) {
handleError(error, `Unable to stack`);
}
});
};
</script>

Expand Down
9 changes: 9 additions & 0 deletions web/src/lib/components/photos-page/asset-grid.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import Scrollbar from '../shared-components/scrollbar/scrollbar.svelte';
import ShowShortcuts from '../shared-components/show-shortcuts.svelte';
import AssetDateGroup from './asset-date-group.svelte';
import { stackAssets } from '$lib/utils/asset-utils';
import DeleteAssetDialog from './delete-asset-dialog.svelte';
import { handlePromiseError } from '$lib/utils';
import { selectAllAssets } from '$lib/utils/asset-utils';
Expand Down Expand Up @@ -85,6 +86,13 @@
handlePromiseError(trashOrDelete(true));
};

const onStackAssets = async () => {
await stackAssets(Array.from($selectedAssets), (ids) => {
assetStore.removeAssets(ids);
dispatch('escape');
});
};

$: shortcutList = (() => {
if ($isSearchEnabled || $showAssetViewer) {
return [];
Expand All @@ -102,6 +110,7 @@
{ shortcut: { key: 'Delete' }, onShortcut: onDelete },
{ shortcut: { key: 'Delete', shift: true }, onShortcut: onForceDelete },
{ shortcut: { key: 'D', ctrl: true }, onShortcut: () => deselectAllAssets() },
{ shortcut: { key: 's' }, onShortcut: () => onStackAssets() },
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
actions: [
{ key: ['f'], action: 'Favorite or unfavorite photo' },
{ key: ['i'], action: 'Show or hide info' },
{ key: ['s'], action: 'Stack selected photos' },
{ key: ['⇧', 'a'], action: 'Archive or unarchive photo' },
{ key: ['⇧', 'd'], action: 'Download' },
{ key: ['Space'], action: 'Play or pause video' },
Expand Down
40 changes: 40 additions & 0 deletions web/src/lib/utils/asset-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createAlbum,
defaults,
getDownloadInfo,
updateAssets,
type AssetResponseDto,
type AssetTypeEnum,
type DownloadInfoDto,
Expand Down Expand Up @@ -270,6 +271,45 @@ export const getSelectedAssets = (assets: Set<AssetResponseDto>, user: UserRespo
return ids;
};

export async function stackAssets(assets: Array<AssetResponseDto>, onStack: (ds: string[]) => void) {
try {
const parent = assets.at(0);

if (parent == undefined) {
return;
}

const children = assets.slice(1);
const ids = children.map(({ id }) => id);

if (children.length > 0) {
await updateAssets({ assetBulkUpdateDto: { ids, stackParentId: parent.id } });
}

let childrenCount = parent.stackCount || 1;
for (const asset of children) {
asset.stackParentId = parent.id;
// Add grand-children's count to new parent
childrenCount += asset.stackCount || 1;
// Reset children stack info
asset.stackCount = null;
asset.stack = [];
}

parent.stackCount = childrenCount;

notificationController.show({
message: `Stacked ${ids.length + 1} assets`,
type: NotificationType.Info,
timeout: 1500,
});

onStack(ids);
} catch (error) {
handleError(error, `Unable to stack`);
}
}

export const selectAllAssets = async (assetStore: AssetStore, assetInteractionStore: AssetInteractionStore) => {
if (get(isSelectingAllAssets)) {
// Selection is already ongoing
Expand Down
Loading