From b318bae3d2df595324fa1734025d571bba3f0264 Mon Sep 17 00:00:00 2001 From: qiqi Date: Sat, 21 Nov 2026 23:48:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(gallery):=201.6.9=20=E7=BC=A9=E7=95=A5?= =?UTF-8?q?=E5=9B=BE=20Promise=20=E5=85=B1=E4=BA=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gallery/package.json | 2 +- gallery/src/index.js | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/gallery/package.json b/gallery/package.json index bf6e82a..24bfa9e 100644 --- a/gallery/package.json +++ b/gallery/package.json @@ -1,7 +1,7 @@ { "name": "@iwb-plugin/gallery", "private": true, - "version": "1.6.7", + "version": "1.6.9", "description": "图库:右栏图片墙(筛选/多选/预览/插入画布/下载/删除),数据由宿主 gallery 能力提供", "ibwPlugin": { "id": "gallery", diff --git a/gallery/src/index.js b/gallery/src/index.js index 23de22c..a169768 100644 --- a/gallery/src/index.js +++ b/gallery/src/index.js @@ -80,6 +80,8 @@ var cfg = Object.assign({}, DEF) var items = [] + var thumbCache = {} + var thumbPending = {} // 进行中的缩略图请求(v1.6.9) var total = 0 var limit = 100 var picked = {} @@ -348,7 +350,20 @@ holder.style.cursor = '' holder.onclick = null holder.textContent = '加载中…' - Promise.resolve(ctx.gallery.thumb(it.name)).then(function (url) { + // v1.6.9:缩略图地址按名字缓存,且进行中的请求共享同一 Promise —— + // 重排/重测会重建 DOM,不共享就会对同一张图重复取 N 次。 + var key = it.name + if (thumbCache[key]) { var u0 = thumbCache[key]; setTimeout(function () { paint(u0) }, 0); return } + if (!thumbPending[key]) { + thumbPending[key] = Promise.resolve(ctx.gallery.thumb(key)).then(function (url) { + if (url && typeof url === 'string') thumbCache[key] = url + delete thumbPending[key] + return url + }, function (e) { delete thumbPending[key]; throw e }) + } + thumbPending[key].then(paint).catch(function () { fail('缩略图加载失败(点击重试)') }) + } + function paint (url) { if (!url || typeof url !== 'string') { fail('图片已丢失'); return } if (!box.isConnected) return var img = el('img', { src: url, alt: '', loading: 'lazy', draggable: 'true' }) @@ -385,7 +400,6 @@ // 加载成功后再替换占位(避免破图标 + 长 title 占满格子) if (img.complete && img.naturalWidth > 0) { img.style.display = ''; if (holder.parentNode === box) box.replaceChild(img, holder) } else if (img.complete && !img.naturalWidth) { /* 立即失败(坏图)*/ tries++; if (tries <= 2) setTimeout(boot, 400 * tries); else { if (img.parentNode === box) box.replaceChild(holder, img); fail('缩略图加载失败(点击重试)') } } - }).catch(function () { fail('缩略图加载失败(点击重试)') }) } boot() box.addEventListener('contextmenu', function (e) { openMenu(e, it) }) @@ -513,7 +527,7 @@ global.IWBPluginHost.register({ id: 'gallery', name: '图库', - version: '1.6.7', + version: '1.6.9', apiVersion: 1, capabilities: ['panels', 'gallery'], data: { schemaVersion: 1, maxBytes: 65536 },