Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat(selection): implement Shift+click range selection for photos

Ahmet Ömerda9da0c625a7
diff --git a/app/composables/useShiftRangeSelection.ts b/app/composables/useShiftRangeSelection.ts
new file mode 100644
index 00000000..9558559c
--- /dev/null
+++ b/app/composables/useShiftRangeSelection.ts
@@ -0,0 +1,96 @@
+import type { TableRow } from '@nuxt/ui'
+
+type RowSelectionState = Record<string, boolean>
+
+interface RangeSelectableTable<T> {
+ getRowModel(): { rows: TableRow<T>[] }
+ setRowSelection(
+ updater: (previous: RowSelectionState) => RowSelectionState,
+ ): void
+}
+
+/**
+ * Shift+click range selection over an ordered list of items.
+ */
+export function useShiftRangeSelection() {
+ let anchorId: string | null = null
+ let pendingShiftKey = false
+
+ function getRangeIds(
+ orderedIds: readonly string[],
+ targetId: string,
+ shiftKey: boolean,
+ ): string[] {
+ const anchorIndex =
+ shiftKey && anchorId !== null ? orderedIds.indexOf(anchorId) : -1
+ const targetIndex = orderedIds.indexOf(targetId)
+
+ anchorId = targetId
+
+ if (anchorIndex === -1 || targetIndex === -1) {
+ return [targetId]
+ }
+
+ return anchorIndex <= targetIndex
+ ? orderedIds.slice(anchorIndex, targetIndex + 1)
+ : orderedIds.slice(targetIndex, anchorIndex + 1).reverse()
+ }
+
+ function resetAnchor() {
+ anchorId = null
+ }
+
+ function captureModifiers(event: MouseEvent) {
+ pendingShiftKey = event.shiftKey
+ }
+
+ function preventShiftTextSelection(event: MouseEvent) {
+ if (event.shiftKey) {
+ event.preventDefault()
+ }
+ }
+
+ function toggleTableRow<T>(
+ table: RangeSelectableTable<T>,
+ row: TableRow<T>,
+ value: boolean,
+ ) {
+ const shiftKey = pendingShiftKey
+ pendingShiftKey = false
+
+ const rows = table.getRowModel().rows
+ const rangeIds = new Set(
+ getRangeIds(
+ rows.map((r) => r.id),
+ row.id,
+ shiftKey,
+ ),
+ )
+
+ if (rangeIds.size === 1) {
+ row.toggleSelected(value)
+ return
+ }
+
+ table.setRowSelection((previous) => {
+ const next: RowSelectionState = { ...previous }
+ for (const rangeRow of rows) {
+ if (!rangeIds.has(rangeRow.id) || !rangeRow.getCanSelect()) continue
+ if (value) {
+ next[rangeRow.id] = true
+ } else {
+ delete next[rangeRow.id]
+ }
+ }
+ return next
+ })
+ }
+
+ return {
+ getRangeIds,
+ resetAnchor,
+ captureModifiers,
+ preventShiftTextSelection,
+ toggleTableRow,
+ }
+}
diff --git a/app/pages/dashboard/albums.vue b/app/pages/dashboard/albums.vue
index 88936f68..09190bde 100644
--- a/app/pages/dashboard/albums.vue
+++ b/app/pages/dashboard/albums.vue
@@ -286,6 +286,15 @@ const togglePhotoSelection = (photoId: string) => {
}
}
+const selectorRangeSelection = useShiftRangeSelection()
+
+watch(
+ () => draftSelectedPhotoIds.value.length === 0,
+ (isEmpty) => {
+ if (isEmpty) selectorRangeSelection.resetAnchor()
+ },
+)
+
const openPhotoSelector = () => {
draftSelectedPhotoIds.value = [...selectedPhotoIds.value]
draftCoverPhotoId.value =
@@ -293,6 +302,7 @@ const openPhotoSelector = () => {
? coverPhotoId.value
: ''
isSelectorFilterOpen.value = false
+ selectorRangeSelection.resetAnchor()
isPhotoSelectorOpen.value = true
}
@@ -311,17 +321,31 @@ const confirmPhotoSelection = () => {
isPhotoSelectorOpen.value = false
}
-const toggleDraftPhotoSelection = (photoId: string) => {
- const index = draftSelectedPhotoIds.value.indexOf(photoId)
- if (index > -1) {
- draftSelectedPhotoIds.value.splice(index, 1)
- if (draftCoverPhotoId.value === photoId) {
- draftCoverPhotoId.value = ''
+const toggleDraftPhotoSelection = (photoId: string, event: MouseEvent) => {
+ const shouldSelect = !draftSelectedPhotoIds.value.includes(photoId)
+ const rangeIds = selectorRangeSelection.getRangeIds(
+ selectorFilteredPhotos.value.map((photo) => photo.id),
+ photoId,
+ event.shiftKey,
+ )
+
+ if (shouldSelect) {
+ const selected = new Set(draftSelectedPhotoIds.value)
+ for (const id of rangeIds) {
+ if (!selected.has(id)) {
+ draftSelectedPhotoIds.value.push(id)
+ }
}
return
}
- draftSelectedPhotoIds.value.push(photoId)
+ const removed = new Set(rangeIds)
+ draftSelectedPhotoIds.value = draftSelectedPhotoIds.value.filter(
+ (id) => !removed.has(id),
+ )
+ if (removed.has(draftCoverPhotoId.value)) {
+ draftCoverPhotoId.value = ''
+ }
}
const setDraftCoverPhoto = (photoId: string) => {
@@ -962,6 +986,16 @@ const columns = computed<any[]>(() => [
</div>
</div>
+ <p
+ class="hidden items-center gap-1.5 text-xs text-neutral-500 pointer-fine:flex dark:text-neutral-400"
+ >
+ <Icon
+ name="tabler:info-circle"
+ class="size-3.5 shrink-0"
+ />
+ {{ $t('ui.action.select.rangeTip') }}
+ </p>
+
<div class="flex flex-wrap gap-1">
<UBadge
v-if="selectedCounts.tags"
@@ -1088,7 +1122,10 @@ const columns = computed<any[]>(() => [
v-for="photo in selectorFilteredPhotos"
:key="photo.id"
class="group text-left rounded-lg focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-400/60"
- @click="toggleDraftPhotoSelection(photo.id)"
+ @mousedown="
+ selectorRangeSelection.preventShiftTextSelection
+ "
+ @click="toggleDraftPhotoSelection(photo.id, $event)"
>
<div
class="relative aspect-square overflow-hidden rounded-lg border bg-gray-200/90 transition-all duration-200 dark:bg-neutral-700/80"
diff --git a/app/pages/dashboard/photos.vue b/app/pages/dashboard/photos.vue
index c9f8dcc0..25ce26f6 100644
--- a/app/pages/dashboard/photos.vue
+++ b/app/pages/dashboard/photos.vue
@@ -552,6 +552,15 @@ watch(isEditModalOpen, (open) => {
// 表格多选状态
const rowSelection = ref({})
const table: any = useTemplateRef('table')
+const getPhotoRowId = (photo: Photo) => photo.id
+const rowRangeSelection = useShiftRangeSelection()
+
+watch(
+ () => Object.keys(rowSelection.value).length === 0,
+ (isEmpty) => {
+ if (isEmpty) rowRangeSelection.resetAnchor()
+ },
+)
// 列可见性状态,选择结果保存在 localStorage,刷新后恢复
const columnVisibility = useDashboardPhotoColumnVisibility()
@@ -789,13 +798,21 @@ const columns = computed<TableColumn<Photo>[]>(() => [
table.toggleAllPageRowsSelected(!!value),
'aria-label': $t('dashboard.photos.table.selectAllAria'),
}),
- cell: ({ row }) =>
- h(UCheckbox, {
- modelValue: row.getIsSelected(),
- 'onUpdate:modelValue': (value: boolean | 'indeterminate') =>
- row.toggleSelected(!!value),
- 'aria-label': $t('dashboard.photos.table.selectRowAria'),
- }),
+ cell: ({ row, table }) =>
+ h(
+ 'div',
+ {
+ class: 'flex',
+ onClickCapture: rowRangeSelection.captureModifiers,
+ onMousedown: rowRangeSelection.preventShiftTextSelection,
+ },
+ h(UCheckbox, {
+ modelValue: row.getIsSelected(),
+ 'onUpdate:modelValue': (value: boolean | 'indeterminate') =>
+ rowRangeSelection.toggleTableRow(table, row, !!value),
+ 'aria-label': $t('dashboard.photos.table.selectRowAria'),
+ }),
+ ),
enableHiding: false,
},
{
@@ -2401,6 +2418,7 @@ onUnmounted(() => {
ref="table"
v-model:row-selection="rowSelection"
v-model:column-visibility="columnVisibility"
+ :get-row-id="getPhotoRowId"
:column-pinning="{
right: ['actions'],
}"
@@ -2466,75 +2484,89 @@ onUnmounted(() => {
>
<div
v-if="selectedRowsCount > 0"
- class="fixed bottom-8 left-1/2 -translate-x-1/2 px-2 py-1.5 bg-white/90 dark:bg-neutral-900/90 backdrop-blur-xl shadow-xl rounded-full border border-neutral-200/80 dark:border-neutral-800/80 z-60 flex items-center gap-3 sm:gap-6 shadow-black/5 dark:shadow-black/20"
+ class="fixed bottom-8 left-1/2 -translate-x-1/2 z-60 w-max flex flex-col items-center gap-2"
>
+ <p
+ class="hidden items-center gap-1.5 max-w-[calc(100vw-2rem)] px-3 py-1 rounded-full bg-white/80 dark:bg-neutral-900/80 backdrop-blur-xl border border-neutral-200/80 dark:border-neutral-800/80 text-xs text-neutral-500 dark:text-neutral-400 pointer-fine:flex"
+ >
+ <Icon
+ name="tabler:info-circle"
+ class="size-3.5 shrink-0"
+ />
+ {{ $t('ui.action.select.rangeTip') }}
+ </p>
+
<div
- class="pl-4 pr-1 border-r border-neutral-200 dark:border-neutral-800 min-w-max"
+ class="px-2 py-1.5 bg-white/90 dark:bg-neutral-900/90 backdrop-blur-xl shadow-xl rounded-full border border-neutral-200/80 dark:border-neutral-800/80 flex items-center gap-3 sm:gap-6 shadow-black/5 dark:shadow-black/20"
>
- <p
- class="text-sm font-medium tracking-wide text-neutral-700 dark:text-neutral-200"
+ <div
+ class="pl-4 pr-1 border-r border-neutral-200 dark:border-neutral-800 min-w-max"
>
- {{
- $t('dashboard.photos.selection.selected', {
- count: selectedRowsCount,
- total: totalRowsCount,
- })
- }}
- </p>
- </div>
+ <p
+ class="text-sm font-medium tracking-wide text-neutral-700 dark:text-neutral-200"
+ >
+ {{
+ $t('dashboard.photos.selection.selected', {
+ count: selectedRowsCount,
+ total: totalRowsCount,
+ })
+ }}
+ </p>
+ </div>
- <div class="flex items-center gap-1 sm:gap-1.5 pr-2">
- <UButton
- color="neutral"
- variant="ghost"
- size="sm"
- class="rounded-full text-neutral-600 hover:text-neutral-900 hover:bg-neutral-100 dark:text-neutral-400 dark:hover:text-white dark:hover:bg-neutral-800"
- icon="tabler:refresh"
- @click="handleBatchReprocess"
- >
- <span class="hidden sm:inline">{{
- $t('dashboard.photos.selection.batchReprocess')
- }}</span>
- </UButton>
+ <div class="flex items-center gap-1 sm:gap-1.5 pr-2">
+ <UButton
+ color="neutral"
+ variant="ghost"
+ size="sm"
+ class="rounded-full text-neutral-600 hover:text-neutral-900 hover:bg-neutral-100 dark:text-neutral-400 dark:hover:text-white dark:hover:bg-neutral-800"
+ icon="tabler:refresh"
+ @click="handleBatchReprocess"
+ >
+ <span class="hidden sm:inline">{{
+ $t('dashboard.photos.selection.batchReprocess')
+ }}</span>
+ </UButton>
- <UButton
- color="neutral"
- variant="ghost"
- size="sm"
- class="rounded-full text-neutral-600 hover:text-neutral-900 hover:bg-neutral-100 dark:text-neutral-400 dark:hover:text-white dark:hover:bg-neutral-800"
- icon="tabler:map-off"
- @click="handleBatchEraseLocation"
- >
- <span class="hidden sm:inline">{{
- $t('dashboard.photos.selection.batchEraseLocation')
- }}</span>
- </UButton>
+ <UButton
+ color="neutral"
+ variant="ghost"
+ size="sm"
+ class="rounded-full text-neutral-600 hover:text-neutral-900 hover:bg-neutral-100 dark:text-neutral-400 dark:hover:text-white dark:hover:bg-neutral-800"
+ icon="tabler:map-off"
+ @click="handleBatchEraseLocation"
+ >
+ <span class="hidden sm:inline">{{
+ $t('dashboard.photos.selection.batchEraseLocation')
+ }}</span>
+ </UButton>
- <UButton
- color="neutral"
- variant="ghost"
- size="sm"
- class="rounded-full text-neutral-600 hover:text-neutral-900 hover:bg-neutral-100 dark:text-neutral-400 dark:hover:text-white dark:hover:bg-neutral-800"
- icon="tabler:download"
- @click="handleBatchDownload"
- >
- <span class="hidden sm:inline">{{
- $t('dashboard.photos.selection.batchDownload')
- }}</span>
- </UButton>
+ <UButton
+ color="neutral"
+ variant="ghost"
+ size="sm"
+ class="rounded-full text-neutral-600 hover:text-neutral-900 hover:bg-neutral-100 dark:text-neutral-400 dark:hover:text-white dark:hover:bg-neutral-800"
+ icon="tabler:download"
+ @click="handleBatchDownload"
+ >
+ <span class="hidden sm:inline">{{
+ $t('dashboard.photos.selection.batchDownload')
+ }}</span>
+ </UButton>
- <UButton
- color="error"
- variant="ghost"
- size="sm"
- class="rounded-full hover:bg-error-50 dark:hover:bg-error-500/20"
- icon="tabler:trash"
- @click="handleBatchDelete"
- >
- <span class="hidden sm:inline">{{
- $t('dashboard.photos.selection.batchDelete')
- }}</span>
- </UButton>
+ <UButton
+ color="error"
+ variant="ghost"
+ size="sm"
+ class="rounded-full hover:bg-error-50 dark:hover:bg-error-500/20"
+ icon="tabler:trash"
+ @click="handleBatchDelete"
+ >
+ <span class="hidden sm:inline">{{
+ $t('dashboard.photos.selection.batchDelete')
+ }}</span>
+ </UButton>
+ </div>
</div>
</div>
</transition>
diff --git a/i18n/locales/en.json b/i18n/locales/en.json
index bc7a73a4..a63273f9 100644
--- a/i18n/locales/en.json
+++ b/i18n/locales/en.json
@@ -28,6 +28,9 @@
"searchPlaceholder": "Search filenames, etc...",
"shuffleTooltip": "Shuffle list"
},
+ "select": {
+ "rangeTip": "Tip: hold Shift and click to select a range of photos"
+ },
"sort": {
"tooltip": "Photo Sorting",
"title": "Sort By",
diff --git a/i18n/locales/ja.json b/i18n/locales/ja.json
index 9993e27d..d2888f78 100644
--- a/i18n/locales/ja.json
+++ b/i18n/locales/ja.json
@@ -28,6 +28,9 @@
"searchPlaceholder": "ファイル名などで検索...",
"shuffleTooltip": "リストをシャッフル"
},
+ "select": {
+ "rangeTip": "ヒント:Shift キーを押しながらクリックすると、範囲内の写真をまとめて選択できます"
+ },
"sort": {
"tooltip": "写真の並び替え",
"title": "並び替え",
diff --git a/i18n/locales/ru.json b/i18n/locales/ru.json
index 8f2952ab..90267bad 100644
--- a/i18n/locales/ru.json
+++ b/i18n/locales/ru.json
@@ -28,6 +28,9 @@
"searchPlaceholder": "Поиск по именам файлов и другим данным…",
"shuffleTooltip": "Перемешать список"
},
+ "select": {
+ "rangeTip": "Совет: удерживайте Shift и щёлкните, чтобы выбрать диапазон фотографий"
+ },
"sort": {
"tooltip": "Сортировка фотографий",
"title": "Сортировать по",
diff --git a/i18n/locales/zh-Hans.json b/i18n/locales/zh-Hans.json
index 66404553..e8c4ee47 100644
--- a/i18n/locales/zh-Hans.json
+++ b/i18n/locales/zh-Hans.json
@@ -49,6 +49,9 @@
"logout": {
"tooltip": "登出"
},
+ "select": {
+ "rangeTip": "提示:按住 Shift 键并点击,即可选择一个范围内的照片"
+ },
"sort": {
"options": {
"dateTakenAsc": "日期(最旧的在前)",
diff --git a/i18n/locales/zh-Hant-HK.json b/i18n/locales/zh-Hant-HK.json
index 4bfca950..a76d951e 100644
--- a/i18n/locales/zh-Hant-HK.json
+++ b/i18n/locales/zh-Hant-HK.json
@@ -49,6 +49,9 @@
"logout": {
"tooltip": "登出"
},
+ "select": {
+ "rangeTip": "提示:按住 Shift 鍵並點擊,即可選擇一個範圍內的相片"
+ },
"sort": {
"options": {
"dateTakenAsc": "日期(最舊的在前)",
diff --git a/i18n/locales/zh-Hant-TW.json b/i18n/locales/zh-Hant-TW.json
index 19a31d8e..5d9304bd 100644
--- a/i18n/locales/zh-Hant-TW.json
+++ b/i18n/locales/zh-Hant-TW.json
@@ -49,6 +49,9 @@
"logout": {
"tooltip": "登出"
},
+ "select": {
+ "rangeTip": "提示:按住 Shift 鍵並點擊,即可選取一個範圍內的照片"
+ },
"sort": {
"options": {
"dateTakenAsc": "日期(最舊的在前)",