perf(gallery): 1.9.2 拖动路径再瘦身——列数滞回、滚动容器/内边距缓存、缩略图缓存上限
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 32s

- 列数滞回:切换阈值 ±12px 内保持原列数,避免拖动时 3/4 列来回跳(每次跳都要整轮重排)
- scrollerOf() 缓存滚动容器、usableW() 缓存内边距:原来每帧都 getComputedStyle,会强制样式重算
- 缩略图缓存加 FIFO 上限 400:200+ 张来回滚不会把 objectURL 一直攒在内存里
This commit is contained in:
qiqi committed 2026-11-28 07:53:57 +08:00
1 parent ff16f450c7
commit aefed6935b
3 files changed
+39 -15

No files matched your search

+34 -10
View File
@@ -94,6 +94,7 @@
var cfg = Object.assign({}, DEF)
var items = []
var thumbCache = {}
var thumbOrder = [] // 缩略图缓存的上限管理(FIFO)
var thumbPending = {} // 进行中的缩略图请求(v1.6.9)
var total = 0
var limit = 100
@@ -352,15 +353,30 @@
var scrollRaf = 0
// 找到真正滚动的祖先(虚拟列表要按它的 scrollTop/clientHeight 算可见窗口)
var scCache = null
var padCache = null
function scrollerOf () {
if (scCache && scCache.isConnected) return scCache
padCache = null
var p = grid && grid.parentNode
while (p && p.nodeType === 1) {
var oy = ''
try { oy = global.getComputedStyle ? global.getComputedStyle(p).overflowY : '' } catch (e) { oy = '' }
if (p.scrollHeight > p.clientHeight + 4 && /(auto|scroll)/.test(String(oy))) return p
if (p.scrollHeight > p.clientHeight + 4 && /(auto|scroll)/.test(String(oy))) { scCache = p; return p }
p = p.parentNode
}
return (grid && grid.parentNode) || null
scCache = (grid && grid.parentNode) || null
return scCache
}
// 列数滞回:在切换阈值 ±12px 内保持原列数。拖动时 3/4 列来回跳会触发整轮重排,是卡顿来源之一。
function colsFor (w, prev) {
var gap = 6
var tileW = Math.max(40, Number(cfg.tile) || 110)
var n = Math.max(1, Math.floor((w + gap) / (tileW + gap)))
if (!prev || n === prev) return n
var boundary = Math.min(n, prev) * (tileW + gap) - gap
if (Math.abs(w - boundary) < 12) return prev
return n
}
function computeLayout () {
var gap = 6
@@ -368,7 +384,7 @@
if (liveW > 0) gridW = liveW
if (!gridW) gridW = 320
var tileW = Math.max(40, Number(cfg.tile) || 110)
var n = Math.max(1, Math.floor((gridW + gap) / (tileW + gap)))
var n = colsFor(gridW, layoutState ? layoutState.n : 0)
var colW = Math.max(40, Math.floor((gridW - gap * (n - 1)) / n))
var cols = []
var heights = []
@@ -474,7 +490,12 @@
if (thumbCache[key]) { var u0 = thumbCache[key]; Promise.resolve().then(function () { paint(u0) }).catch(function () { /* ignore */ }); return }
if (!thumbPending[key]) {
thumbPending[key] = Promise.resolve(ctx.gallery.thumb(key)).then(function (url) {
if (url && typeof url === 'string') thumbCache[key] = url
if (url && typeof url === 'string') {
thumbCache[key] = url
thumbOrder.push(key)
// 上限 400:200+ 张的图库来回滚会把 objectURL 一直攒在内存里
while (thumbOrder.length > 400) { var oldK = thumbOrder.shift(); if (oldK && oldK !== key) { try { delete thumbCache[oldK] } catch (e2) { /* ignore */ } } }
}
delete thumbPending[key]
return url
}, function (e) { delete thumbPending[key]; throw e })
@@ -641,11 +662,13 @@
var sc = scrollerOf()
var w = 0
if (sc) {
var cs = null
try { cs = global.getComputedStyle ? global.getComputedStyle(sc) : null } catch (e) { cs = null }
var padL = cs ? (parseFloat(cs.paddingLeft) || 0) : 0
var padR = cs ? (parseFloat(cs.paddingRight) || 0) : 0
w = sc.clientWidth - padL - padR
// 内边距缓存:拖动时每帧 getComputedStyle 会强制样式重算(这是每帧的固定开销)
if (!padCache) {
var cs = null
try { cs = global.getComputedStyle ? global.getComputedStyle(sc) : null } catch (e) { cs = null }
padCache = { l: cs ? (parseFloat(cs.paddingLeft) || 0) : 0, r: cs ? (parseFloat(cs.paddingRight) || 0) : 0 }
}
w = sc.clientWidth - padCache.l - padCache.r
}
if (!w || w < 80) w = availableW()
return Math.max(80, Math.floor(w) - 4)
@@ -665,6 +688,7 @@
}
var measureTimer = 0
function measureNow () {
padCache = null // 尺寸/样式可能变了,重新量一次内边距
var w = usableW()
if (w > 0 && Math.abs(w - gridW) >= 4) { gridW = w; scheduleRender() }
}
@@ -674,7 +698,7 @@
if (!w || !layoutState) return false
var gap = 6
var tileW = Math.max(40, Number(cfg.tile) || 110)
var n = Math.max(1, Math.floor((w + gap) / (tileW + gap)))
var n = colsFor(w, layoutState.n)
if (n !== layoutState.n) return false
var colW = Math.max(40, Math.floor((w - gap * (n - 1)) / n))
var cols = grid.children
+2 -2
View File
@@ -1,7 +1,7 @@
{
"id": "gallery",
"name": "图库",
"version": "1.9.1",
"version": "1.9.2",
"apiVersion": 1,
"main": "main.js",
"style": "styles.css",
@@ -13,7 +13,7 @@
"图库"
],
"files": {
"main.js": "a91553f2aaa75b102a779238a3366ea73f9f243509d5fa3ef12f7d218c2e22cd",
"main.js": "c0103fcc9cb92fac3aecdabba7ef60af1fb935375d3670f30b1d6dce4231e73f",
"styles.css": "945fd547278cf0388035b22660dfe9401dc0c8d25919fe6204471432e5797cde"
}
}
+3 -3
View File
@@ -33,10 +33,10 @@
"dir": "gallery",
"main": "main.js",
"style": "styles.css",
"version": "1.9.1",
"version": "1.9.2",
"files": {
"manifest.json": "c40cc20456a6c51a99dd0372ade6238041293af2598b3c4ee0d5b4ad64b14892",
"main.js": "a91553f2aaa75b102a779238a3366ea73f9f243509d5fa3ef12f7d218c2e22cd",
"manifest.json": "850f277ab02ed4bf91e17f20d0e5f7048fa24557dc355348afd14e0402e296ec",
"main.js": "c0103fcc9cb92fac3aecdabba7ef60af1fb935375d3670f30b1d6dce4231e73f",
"styles.css": "945fd547278cf0388035b22660dfe9401dc0c8d25919fe6204471432e5797cde"
}
},