Prepping for v3.0 release

Fetch album assets from timeline endpoint is the major change
This commit is contained in:
Alan Grainger
2026-06-22 06:21:51 +12:00
parent e50d5711fc
commit 494fbe36c7
18 changed files with 808 additions and 117 deletions
+1
View File
@@ -25,6 +25,7 @@ function init () {
state.lightboxConfig = params.lightboxConfig || {}
if (params.metadataConfig) state.metadataConfig = params.metadataConfig
state.groupByDate = !!params.groupByDate
state.metaBase = params.metaBase || ''
state.container = document.getElementById('gallery')
if (!state.container) return
+5
View File
@@ -80,6 +80,8 @@ export function registerCaption (lightbox: LightboxInstance) {
}
render()
pswp.on('change', render)
// Re-render when a lazy album item's description arrives after open.
state.slideRefreshers.push(render)
}
})
})
@@ -113,6 +115,9 @@ export function registerDownloadButton (lightbox: LightboxInstance) {
}
update()
pswp.on('change', update)
// Refresh the href/filename when a lazy album item's real download
// filename arrives after open.
state.slideRefreshers.push(update)
}
})
})
+16 -3
View File
@@ -16,6 +16,7 @@ import {
registerFullscreenButton
} from './lightbox-ui.js'
import { registerSidebar } from './sidebar.js'
import { registerLazyDetail } from './metadata.js'
/**
* Replace the current history entry with the same URL minus any hash.
@@ -33,9 +34,12 @@ function parseVideoData (item: GalleryItem): { src: string, type: string } {
try {
const data = JSON.parse(item.videoData || '{}')
const source = (data.source && data.source[0]) || {}
return { src: source.src || '', type: source.type || 'video/mp4' }
// type may be empty for lazy album videos (the content-type probe is
// deferred); an empty type attribute is omitted below so the browser
// falls back to the proxy's response Content-Type.
return { src: source.src || '', type: source.type || '' }
} catch (e) {
return { src: '', type: 'video/mp4' }
return { src: '', type: '' }
}
}
@@ -52,11 +56,12 @@ function buildDataSource () {
return state.items.map(item => {
if (item.type === 'VIDEO') {
const v = parseVideoData(item)
const typeAttr = v.type ? ' type="' + escapeAttr(v.type) + '"' : ''
return {
html:
'<div class="pswp__video-wrap">' +
'<video controls playsinline poster="' + escapeAttr(item.thumbnailUrl) + '">' +
'<source src="' + escapeAttr(v.src) + '" type="' + escapeAttr(v.type) + '">' +
'<source src="' + escapeAttr(v.src) + '"' + typeAttr + '>' +
'</video>' +
'</div>'
}
@@ -127,11 +132,19 @@ export function initLightbox () {
state.lightbox = new PhotoSwipeLightbox(lightboxOptions)
// PhotoSwipe recreates its pswp instance (and re-fires uiRegister) on every
// open, so the UI elements below re-register their slide refreshers each
// time. Clear the list before each open so it can't grow unbounded.
state.lightbox.on('beforeOpen', () => { state.slideRefreshers = [] })
registerBackButton(state.lightbox)
if (state.lightboxConfig.showDownload) registerDownloadButton(state.lightbox)
registerFullscreenButton(state.lightbox)
if (state.metadataConfig.descriptionInCaption) registerCaption(state.lightbox)
if (state.metadataConfig.sidebarHasContent) registerSidebar(state.lightbox)
// Lazy album items load their exif / description / filename on open; this
// refreshes the UI elements above once detail arrives.
registerLazyDetail(state.lightbox)
// Hash navigation + back-button handling: push a history entry on open so
// the mobile back gesture closes the lightbox; sync the hash with the
+90
View File
@@ -0,0 +1,90 @@
/*
Lazy per-asset metadata loading for album shares. Album items arrive as
grid-only data (id, type, thumbhash, dimensions); their exif / description /
real download filename are fetched from the `/meta/` route the first time
the item opens in the lightbox - mirroring how Immich's own web client loads
shared-album detail on demand. Individual shares bake everything in and
never reach this module (no `metaBase`).
*/
import { state } from './state.js'
import type { GalleryItem, AssetMetadata } from '../shared/types.js'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type LightboxInstance = any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type PswpInstance = any
// One in-flight (then resolved) request per asset id, so paging back and
// forth - or prefetching a neighbour we then navigate to - reuses the fetch.
const detailCache = new Map<string, Promise<AssetMetadata | null>>()
function fetchDetail (id: string): Promise<AssetMetadata | null> {
const cached = detailCache.get(id)
if (cached) return cached
const promise = fetch(state.metaBase + '/' + encodeURIComponent(id))
.then(res => (res.ok ? res.json() as Promise<AssetMetadata> : null))
.catch(() => null)
detailCache.set(id, promise)
return promise
}
/**
* Copy fetched detail onto the grid item in place and clear `needsDetail`.
* Only fills fields the server sent (all are config/share gated), so a share
* with metadata disabled simply leaves them unset.
*/
function applyMeta (item: GalleryItem, meta: AssetMetadata): void {
if (meta.exif) item.exif = meta.exif
if (meta.description) item.description = meta.description
if (meta.downloadFilename) item.downloadFilename = meta.downloadFilename
item.needsDetail = false
}
/**
* Ensure `item`'s detail is loaded. Resolves true when this call is the one
* that applied the detail (so the caller can refresh the view), false if the
* item needs no detail, the fetch failed, or another call already applied it.
*/
function ensureDetail (item: GalleryItem | undefined): Promise<boolean> {
if (!item || !item.needsDetail) return Promise.resolve(false)
return fetchDetail(item.id).then(meta => {
if (!meta || !item.needsDetail) return false
applyMeta(item, meta)
return true
})
}
/**
* Warm the cache for a neighbouring slide without touching the view, so
* paging next/prev shows detail instantly (matches Immich's prefetch).
*/
function prefetch (item: GalleryItem | undefined): void {
if (item && item.needsDetail) fetchDetail(item.id)
}
function loadCurrent (pswp: PswpInstance): void {
const index = pswp.currIndex
ensureDetail(state.items[index]).then(applied => {
// Only refresh if the user is still on the slide we loaded for.
if (applied && pswp.currIndex === index) {
state.slideRefreshers.forEach(fn => fn())
}
})
prefetch(state.items[index + 1])
prefetch(state.items[index - 1])
}
/**
* Hook lazy detail loading into the lightbox. Loads the opened slide's detail
* (and prefetches its neighbours) on open and on every slide change. No-op
* unless the share exposed a `metaBase` (album shares only).
*/
export function registerLazyDetail (lightbox: LightboxInstance): void {
if (!state.metaBase) return
lightbox.on('uiRegister', () => {
const pswp = lightbox.pswp
loadCurrent(pswp)
pswp.on('change', () => loadCurrent(pswp))
})
}
+2
View File
@@ -41,6 +41,8 @@ export function registerSidebar (lightbox: LightboxInstance) {
const renderSidebar = () => renderContents(el, state.items[pswp.currIndex])
renderSidebar()
pswp.on('change', renderSidebar)
// Re-render when a lazy album item's detail arrives after open.
state.slideRefreshers.push(renderSidebar)
pswp.on('destroy', () => {
document.documentElement.classList.remove('ipp-sidebar-open')
})
+7 -3
View File
@@ -49,8 +49,6 @@ export interface GroupSpec {
}
// PhotoSwipeLightbox is an external module with no TS types in this project.
// Typing it as `unknown` would push casts to every call site; `any` is the
// pragmatic choice and stays scoped to this single state slot.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type PhotoSwipeLightboxInstance = any
@@ -85,5 +83,11 @@ export const state = {
downloadBtn: null as HTMLButtonElement | null,
// Info sidebar open/closed. Persisted to localStorage so the choice
// survives navigation between slides and gallery reloads.
sidebarOpen: false
sidebarOpen: false,
// Base URL (`/share/meta/<key>`) for the on-demand per-asset metadata route.
metaBase: '',
// Re-render callbacks registered by lightbox UI elements (sidebar, caption,
// download button). Each re-renders for the current slide. Invoked after a
// lazy item's detail arrives so the just-opened slide reflects it.
slideRefreshers: [] as Array<() => void>
}