From ff16f450c728b73f8086a1198abc99527a708153 Mon Sep 17 00:00:00 2001 From: qiqi Date: Sat, 28 Nov 2026 07:45:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(gallery):=201.9.1=20=E6=8B=96=E5=8A=A8?= =?UTF-8?q?=E5=8F=AA=E6=94=B9=E5=88=97=E5=AE=BD=E4=B8=8D=E9=87=8D=E5=BB=BA?= =?UTF-8?q?=20+=20=E4=BF=AE=E3=80=8C=E9=83=A8=E5=88=86=E5=8D=A1=E7=89=87?= =?UTF-8?q?=E6=B0=B8=E8=BF=9C=E5=81=9C=E5=9C=A8=E5=8A=A0=E8=BD=BD=E4=B8=AD?= =?UTF-8?q?=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 两个问题: 1) 拖动停靠栏仍然卡:虚拟列表虽然把渲染量降下来了,但每帧仍走 computeLayout + 重建约 40 张卡。 现在列数不变时只改已有列的宽度(几条 style 写入),不重建任何节点;列数变化或停手 220ms 后才完整重排, 松手(pointerup)立刻 flush 一次修正 y 偏移。 2) 有些图片不显示:虚拟列表滚动时卡片会被先移除再挂载,paint() 里 `if (!box.isConnected) return` 直接放弃, 结果这些卡片永远停在「加载中…」(缩略图其实已经缓存好,只是没人挂上去)。已去掉该提前返回。 另加兜底:找不到真正的滚动容器、或内容不足一屏时,不做虚拟化(渲染全部),避免窗口算错出现空档。 --- plugins/gallery/main.js | 36 ++++++++++++++++++++++++++++++----- plugins/gallery/manifest.json | 4 ++-- plugins/index.json | 6 +++--- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/plugins/gallery/main.js b/plugins/gallery/main.js index a837d51..7071a85 100644 --- a/plugins/gallery/main.js +++ b/plugins/gallery/main.js @@ -405,8 +405,9 @@ } if (force || !layoutState) layoutState = computeLayout() var L = layoutState - var top = sc ? sc.scrollTop : 0 - var viewH = (sc && sc.clientHeight) || 600 + var virtual = !!(sc && sc.scrollHeight > sc.clientHeight + 40) + var top = virtual ? sc.scrollTop : 0 + var viewH = virtual ? sc.clientHeight : 100000 // 滚动幅度不大且已有内容 ⇒ 不重建(滚动事件很密,重建一次就是几十个节点) if (!force && winState.top >= 0 && grid.firstChild && Math.abs(top - winState.top) < viewH * 0.6 && winState.viewH === viewH) return winState.top = top @@ -482,7 +483,8 @@ } function paint (url) { if (!url || typeof url !== 'string') { fail('图片已丢失'); return } - if (!box.isConnected) return + // v1.9.1:不再因为「此刻不在文档里」直接放弃 —— 虚拟列表滚动时会先离屏再挂载, + // 放弃会导致某些卡片永远停在「加载中…」(缩略图缓存已存,只是没人把它挂上去)。 var img = el('img', { src: url, alt: '', loading: 'eager', decoding: 'async', draggable: 'true' }) if (it.w && it.h) img.setAttribute('style', 'aspect-ratio:' + it.w + ' / ' + it.h) img.addEventListener('load', function () { @@ -661,11 +663,35 @@ if (renderRaf) return renderRaf = global.requestAnimationFrame(function () { renderRaf = 0; try { layoutState = null; winState.top = -1; renderGrid(true) } catch (e) { /* ignore */ } }) } - function measure () { + var measureTimer = 0 + function measureNow () { var w = usableW() - // v1.8.3:拖动停靠栏每帧都会触发 RO;宽度变化不足 4px 不重排,重排再合并到一帧 if (w > 0 && Math.abs(w - gridW) >= 4) { gridW = w; scheduleRender() } } + // 拖动中的廉价重排:列数没变就只改进已有的列宽(几条 style 写入),不重建卡片 + function quickWidth () { + var w = usableW() + 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))) + if (n !== layoutState.n) return false + var colW = Math.max(40, Math.floor((w - gap * (n - 1)) / n)) + var cols = grid.children + for (var i = 0; i < cols.length; i++) { try { cols[i].style.width = colW + 'px' } catch (e) { /* ignore */ } } + gridW = w + return true + } + function measure () { + // v1.9.1:拖动过程中不重建卡片(卡顿主因)。列数不变时只改列宽, + // 尾部防抖 140ms 再做一次完整重排(修正 y 偏移),松手立刻 flush。 + if (quickWidth()) { clearTimeout(measureTimer); measureTimer = setTimeout(measureNow, 220); return } + clearTimeout(measureTimer) + measureTimer = setTimeout(measureNow, 140) + } + if (global.document && global.document.addEventListener) { + global.document.addEventListener('pointerup', function () { clearTimeout(measureTimer); measureNow() }, true) + } var ro = typeof ResizeObserver === 'function' ? new ResizeObserver(function () { measure() }) : null if (ro) { if (wrap) ro.observe(wrap); if (wrap && wrap.parentNode) ro.observe(wrap.parentNode); ro.observe(grid) } measure() diff --git a/plugins/gallery/manifest.json b/plugins/gallery/manifest.json index 113e1c5..8ae253c 100644 --- a/plugins/gallery/manifest.json +++ b/plugins/gallery/manifest.json @@ -1,7 +1,7 @@ { "id": "gallery", "name": "图库", - "version": "1.9.0", + "version": "1.9.1", "apiVersion": 1, "main": "main.js", "style": "styles.css", @@ -13,7 +13,7 @@ "图库" ], "files": { - "main.js": "4d43d5cb1a2a0920ff4ca97b736ecb375458bc732e3c3d51d074e91e8ea1ae2b", + "main.js": "a91553f2aaa75b102a779238a3366ea73f9f243509d5fa3ef12f7d218c2e22cd", "styles.css": "945fd547278cf0388035b22660dfe9401dc0c8d25919fe6204471432e5797cde" } } diff --git a/plugins/index.json b/plugins/index.json index 52e84ac..183266e 100644 --- a/plugins/index.json +++ b/plugins/index.json @@ -33,10 +33,10 @@ "dir": "gallery", "main": "main.js", "style": "styles.css", - "version": "1.9.0", + "version": "1.9.1", "files": { - "manifest.json": "1691a20948091b6f9b0053461b489baa29a70ec5fc263a1c7e4d76d39f87a42b", - "main.js": "4d43d5cb1a2a0920ff4ca97b736ecb375458bc732e3c3d51d074e91e8ea1ae2b", + "manifest.json": "c40cc20456a6c51a99dd0372ade6238041293af2598b3c4ee0d5b4ad64b14892", + "main.js": "a91553f2aaa75b102a779238a3366ea73f9f243509d5fa3ef12f7d218c2e22cd", "styles.css": "945fd547278cf0388035b22660dfe9401dc0c8d25919fe6204471432e5797cde" } },