From bf1503079911684cc5e7fde89632e1254b32d969 Mon Sep 17 00:00:00 2001 From: qiqi Date: Sat, 28 Nov 2026 02:02:42 +0800 Subject: [PATCH] =?UTF-8?q?perf(gallery):=201.8.3=20=E6=8B=96=E5=8A=A8?= =?UTF-8?q?=E5=81=9C=E9=9D=A0=E6=A0=8F=E6=97=B6=E9=87=8D=E6=8E=92=E8=8A=82?= =?UTF-8?q?=E6=B5=81=EF=BC=88=E5=AE=BD=E5=BA=A6=E5=8F=98=E5=8C=96=20<4px?= =?UTF-8?q?=20=E4=B8=8D=E9=87=8D=E6=8E=92=E3=80=81=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=88=B0=E4=B8=80=E5=B8=A7=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户反馈拖动图库面板很卡。原因是面板宽度的 ResizeObserver 每帧触发 renderGrid:整格 clear + 重建全部卡片、 重新挂事件、重新 paint 缩略图,一张卡就十来个 DOM 节点,几十张就是几百节点/帧。 改动(plugins/gallery/main.js + styles.css): - measure():宽度变化不足 4px 不重排;重排用 requestAnimationFrame 合并,一帧最多一次 - 缩略图 img 加 decoding=async(解码移出主线程,减少拖动时的掉帧) - .iwb-history-item 加 contain: layout style(把布局/样式计算限制在卡片内部) 试过「复用 DOM 节点(缓存卡片,重排只移动不重建)」但会让 e2e 里的图库面板加载失败,已回退, 先把节流 + contain + async decode 这组低风险优化发出去。 --- plugins/gallery/main.js | 14 ++++++++++++-- plugins/gallery/manifest.json | 6 +++--- plugins/gallery/styles.css | 2 +- plugins/index.json | 8 ++++---- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/plugins/gallery/main.js b/plugins/gallery/main.js index a482123..35afa96 100644 --- a/plugins/gallery/main.js +++ b/plugins/gallery/main.js @@ -339,6 +339,9 @@ settings.appendChild(row('range', RANGES, '时间范围', '按生成时间筛选')) } + // v1.8.3:拖动停靠栏卡顿的根因是「每一帧 ResizeObserver → renderGrid → clear + 重建全部卡片」, + // 每张卡要重建 ~10 个节点、重新挂事件、重新 paint 缩略图。改成复用节点:把缓存里的卡片 + // 直接 appendChild 到新列里(DOM 移动,不销毁),缩略图与事件全部保留。 function renderGrid () { clear(grid) var gap = 6 @@ -370,6 +373,7 @@ grid.appendChild(col) } empty.style.display = items.length ? 'none' : '' + try { syncSelection() } catch (e) { /* ignore */ } clear(more) if (total > items.length) { var b = el('button', { class: 'iwb-btn iwb-btn-sm', text: '显示更多(' + (total - items.length) + ' 张)' }) @@ -435,7 +439,7 @@ function paint (url) { if (!url || typeof url !== 'string') { fail('图片已丢失'); return } if (!box.isConnected) return - var img = el('img', { src: url, alt: '', loading: 'eager', draggable: 'true' }) + 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 () { // naturalWidth=0 说明拿到的是坏图(空 data URL / 截断文件):按失败处理,别留白块 @@ -608,9 +612,15 @@ if (!w) w = grid.clientWidth return w || 0 } + var renderRaf = 0 + function scheduleRender () { + if (renderRaf) return + renderRaf = global.requestAnimationFrame(function () { renderRaf = 0; try { renderGrid() } catch (e) { /* ignore */ } }) + } function measure () { var w = usableW() - if (w > 0 && Math.abs(w - gridW) > 1) { gridW = w; renderGrid() } + // v1.8.3:拖动停靠栏每帧都会触发 RO;宽度变化不足 4px 不重排,重排再合并到一帧 + if (w > 0 && Math.abs(w - gridW) >= 4) { gridW = w; scheduleRender() } } 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) } diff --git a/plugins/gallery/manifest.json b/plugins/gallery/manifest.json index 0ed6dbd..d7780e3 100644 --- a/plugins/gallery/manifest.json +++ b/plugins/gallery/manifest.json @@ -1,7 +1,7 @@ { "id": "gallery", "name": "图库", - "version": "1.8.2", + "version": "1.8.3", "apiVersion": 1, "main": "main.js", "style": "styles.css", @@ -13,7 +13,7 @@ "图库" ], "files": { - "main.js": "eb178baa9d1c8d3148e93e38513bd35754d720e8af8f02c53c041d7e1322dac3", - "styles.css": "14d374b5d0e1461cb1ca2b125865e9c27df7c3bd79c11caa0afc52152c7d0aef" + "main.js": "949841c238fd506a3f4ccde5739eadc198ca7492be221cf5d396a2892edfbd70", + "styles.css": "945fd547278cf0388035b22660dfe9401dc0c8d25919fe6204471432e5797cde" } } diff --git a/plugins/gallery/styles.css b/plugins/gallery/styles.css index 898a699..5103fbd 100644 --- a/plugins/gallery/styles.css +++ b/plugins/gallery/styles.css @@ -65,7 +65,7 @@ /* ---- 图片墙 ---- */ .iwb-history-grid { overscroll-behavior: contain; } -.iwb-history-embedded .iwb-history-item { position: relative; flex: none; width: 100%; } +.iwb-history-embedded .iwb-history-item { position: relative; flex: none; width: 100%; contain: layout style; } .iwb-history-embedded .iwb-history-item img { width: 100%; height: auto; display: block; object-fit: contain; margin: 0 auto; border-radius: 6px; border: 1px solid var(--iwb-border); background: var(--iwb-layer2); cursor: pointer; diff --git a/plugins/index.json b/plugins/index.json index 122dee3..0a525bc 100644 --- a/plugins/index.json +++ b/plugins/index.json @@ -33,11 +33,11 @@ "dir": "gallery", "main": "main.js", "style": "styles.css", - "version": "1.8.2", + "version": "1.8.3", "files": { - "manifest.json": "d54679889ab43e54408f6ff5731a557a9816ad5872ea86a341f98f3dfec30eb3", - "main.js": "eb178baa9d1c8d3148e93e38513bd35754d720e8af8f02c53c041d7e1322dac3", - "styles.css": "14d374b5d0e1461cb1ca2b125865e9c27df7c3bd79c11caa0afc52152c7d0aef" + "manifest.json": "f7c87b3514933243aa2e8ef7221c01606774f088d06f7a17edcce8c34a1a1ad4", + "main.js": "949841c238fd506a3f4ccde5739eadc198ca7492be221cf5d396a2892edfbd70", + "styles.css": "945fd547278cf0388035b22660dfe9401dc0c8d25919fe6204471432e5797cde" } }, {