Refactor duplicated patterns into shared helpers

This commit is contained in:
Alan Grainger
2026-06-05 12:46:28 +12:00
parent c1b58011aa
commit cefe2e81ff
7 changed files with 294 additions and 110 deletions
+63 -48
View File
@@ -123,6 +123,10 @@ function writePersistedState (open: boolean) {
}
// ----- content rendering ---------------------------------------------------
//
// Sidebar UI placement for each metadata field. Server-side gating lives in
// `gallery/exif.ts` (rules table); legacy-config migration lives in
// `config/migrations.ts`. Adding a new field requires updating all three.
function renderContents (root: HTMLElement, item: GalleryItem | undefined) {
root.replaceChildren()
@@ -213,67 +217,78 @@ function renderDateRow (iso: string): HTMLElement {
return row
}
function renderFileRow (exif: GalleryExif): HTMLElement | null {
if (!exif.fileName && !exif.width && !exif.fileSizeInByte) return null
const row = makeRow(ICON_IMAGE)
interface RowSpec {
icon: string
// Title paragraph at the top of the row body. Falsy values are omitted.
label?: string | null
labelClass?: string
// Stats rendered as `<span>` chips in a flex row beneath the label.
// Nullish entries are skipped, so callers can write
// `exif.iso != null ? 'ISO ' + exif.iso : null` and not branch.
stats: Array<string | null | undefined>
}
/**
* Build one info-sidebar row. Returns `null` when the row would have
* neither a label nor any non-null stat, so the caller can skip appending.
*/
function renderRow (spec: RowSpec): HTMLElement | null {
const presentStats = spec.stats.filter((s): s is string => s != null && s !== '')
if (!spec.label && presentStats.length === 0) return null
const row = makeRow(spec.icon)
const body = row.querySelector('.ipp-sidebar-row-body') as HTMLElement
if (exif.fileName) {
const name = document.createElement('p')
name.className = 'ipp-sidebar-filename'
name.textContent = exif.fileName
body.appendChild(name)
if (spec.label) {
const p = document.createElement('p')
if (spec.labelClass) p.className = spec.labelClass
p.textContent = spec.label
body.appendChild(p)
}
if (exif.width && exif.height) {
if (presentStats.length) {
const stats = document.createElement('div')
stats.className = 'ipp-sidebar-stats'
const mp = Math.round((exif.width * exif.height) / 1_000_000)
if (mp >= 1) stats.appendChild(makeStat(mp + ' MP'))
stats.appendChild(makeStat(exif.width + ' x ' + exif.height))
if (exif.fileSizeInByte) stats.appendChild(makeStat(formatBytes(exif.fileSizeInByte)))
body.appendChild(stats)
} else if (exif.fileSizeInByte) {
const stats = document.createElement('div')
stats.className = 'ipp-sidebar-stats'
stats.appendChild(makeStat(formatBytes(exif.fileSizeInByte)))
for (const s of presentStats) stats.appendChild(makeStat(s))
body.appendChild(stats)
}
return row
}
function renderFileRow (exif: GalleryExif): HTMLElement | null {
const hasDims = !!(exif.width && exif.height)
const mp = hasDims ? Math.round((exif.width! * exif.height!) / 1_000_000) : 0
return renderRow({
icon: ICON_IMAGE,
label: exif.fileName,
labelClass: 'ipp-sidebar-filename',
stats: [
hasDims && mp >= 1 ? mp + ' MP' : null,
hasDims ? exif.width + ' x ' + exif.height : null,
exif.fileSizeInByte ? formatBytes(exif.fileSizeInByte) : null
]
})
}
function renderCameraRow (exif: GalleryExif): HTMLElement | null {
if (!exif.make && !exif.model && exif.exposureTime == null && exif.iso == null) return null
const row = makeRow(ICON_CAMERA)
const body = row.querySelector('.ipp-sidebar-row-body') as HTMLElement
const label = [exif.make, exif.model].filter(Boolean).join(' ').trim()
if (label) {
const p = document.createElement('p')
p.textContent = label
body.appendChild(p)
}
const stats = document.createElement('div')
stats.className = 'ipp-sidebar-stats'
if (exif.exposureTime) stats.appendChild(makeStat(exif.exposureTime + ' s'))
if (exif.iso != null) stats.appendChild(makeStat('ISO ' + exif.iso))
if (stats.childElementCount) body.appendChild(stats)
return row
return renderRow({
icon: ICON_CAMERA,
label: [exif.make, exif.model].filter(Boolean).join(' ').trim() || null,
stats: [
exif.exposureTime ? exif.exposureTime + ' s' : null,
exif.iso != null ? 'ISO ' + exif.iso : null
]
})
}
function renderLensRow (exif: GalleryExif): HTMLElement | null {
if (!exif.lensModel && exif.fNumber == null && exif.focalLength == null) return null
const row = makeRow(ICON_IRIS)
const body = row.querySelector('.ipp-sidebar-row-body') as HTMLElement
if (exif.lensModel) {
const p = document.createElement('p')
p.className = 'ipp-sidebar-lens'
p.textContent = exif.lensModel
body.appendChild(p)
}
const stats = document.createElement('div')
stats.className = 'ipp-sidebar-stats'
if (exif.fNumber != null) stats.appendChild(makeStat('f/' + exif.fNumber))
if (exif.focalLength != null) stats.appendChild(makeStat(exif.focalLength + ' mm'))
if (stats.childElementCount) body.appendChild(stats)
return row
return renderRow({
icon: ICON_IRIS,
label: exif.lensModel,
labelClass: 'ipp-sidebar-lens',
stats: [
exif.fNumber != null ? 'f/' + exif.fNumber : null,
exif.focalLength != null ? exif.focalLength + ' mm' : null
]
})
}
function renderLocation (exif: GalleryExif): HTMLElement {
+30 -21
View File
@@ -56,18 +56,7 @@ export function virtualize () {
if (l.top > bottom) break
neededTiles.add(l.index)
}
for (const [index, el] of state.renderedTiles) {
if (!neededTiles.has(index)) {
el.remove()
state.renderedTiles.delete(index)
}
}
for (const index of neededTiles) {
if (state.renderedTiles.has(index)) continue
const tile = createTile(index)
state.container.appendChild(tile)
state.renderedTiles.set(index, tile)
}
syncRendered(neededTiles, state.renderedTiles, createTile)
// Group headers (when grouping is enabled; headers is empty otherwise)
const neededHeaders = new Set<string>()
@@ -76,19 +65,39 @@ export function virtualize () {
if (h.top > bottom) break
neededHeaders.add(h.label)
}
for (const [label, el] of state.renderedHeaders) {
if (!neededHeaders.has(label)) {
syncRendered(neededHeaders, state.renderedHeaders, (label) => {
const h = state.headers.find(x => x.label === label)
return h ? createHeader(h) : null
})
}
/**
* Reconcile a Map of rendered DOM elements against the set of keys that
* should currently be on screen: remove elements whose key is no longer
* needed, then create + append elements for keys that aren't rendered yet.
*
* The `create` callback may return `null` when the key can't be resolved
* (e.g. a stale header label that no longer matches any layout entry); in
* that case nothing is added for the key and the Map is left alone.
*/
function syncRendered<K> (
needed: Set<K>,
rendered: Map<K, HTMLElement>,
create: (key: K) => HTMLElement | null
): void {
if (!state.container) return
for (const [key, el] of rendered) {
if (!needed.has(key)) {
el.remove()
state.renderedHeaders.delete(label)
rendered.delete(key)
}
}
for (const label of neededHeaders) {
if (state.renderedHeaders.has(label)) continue
const h = state.headers.find(x => x.label === label)
if (!h) continue
const el = createHeader(h)
for (const key of needed) {
if (rendered.has(key)) continue
const el = create(key)
if (!el) continue
state.container.appendChild(el)
state.renderedHeaders.set(label, el)
rendered.set(key, el)
}
}