diff --git a/album/PITFALLS.md b/album/PITFALLS.md index d6ee049..5cb2bd6 100644 --- a/album/PITFALLS.md +++ b/album/PITFALLS.md @@ -34,3 +34,17 @@ - [ ] 深色/浅色主题下描边都可见 - [ ] 选中态(1px)与 hover(2px)叠加不冲突 - [ ] 减少动效偏好下无缩放 + +## 6. 缩略图两个坑会互相伪装(1.1.6) + +- **`applyThumb` 里不能判 `img.isConnected`**:命中内存缓存那条分支是在 `btn.appendChild(img)` **之前**调用的, + 此刻元素必然「未连接」⇒ 直接 return ⇒ `src` 永远赋不上。症状极具误导性: + **导入后整片空白,切走画布再回来却正常**(换宿主后内存缓存为空,走的是异步分支,那时元素已连接)。 + 磁盘缓存其实一直是好的,所以看日志/看缓存文件都会得出「缩略图读到了」的错误结论。 + - 正确做法:同步分支直接 `img.src = url`;存活判断只放在**异步回调**里(等图期间宫格可能已被重建丢掉)。 + - 回归测试:`image-workbench-desktop/tests/iwb-album-thumbs.spec.js`(断言重建后每格都有 src)。 +- **JPEG 没有 alpha**:`canvas.toDataURL('image/jpeg')` 会把透明像素编码成**纯黑** `rgba(0,0,0,255)`, + 抠图/透明 PNG 在宫格里就是一格格黑方块。含透明像素必须走 PNG(隔点采样 alpha 通道判定), + 不含透明的照片仍走 JPEG(省体积)。 +- **改编码策略必须换缓存名**:文件名里带版本代号(`t2-`),且读缓存时两种后缀都试、 + 写成功后再删旧版本与另一格式 —— 否则用户升级后仍旧命中旧的黑底 JPEG,修复等于没发。 diff --git a/album/package.json b/album/package.json index a001b58..8eacce4 100644 --- a/album/package.json +++ b/album/package.json @@ -1,7 +1,7 @@ { "name": "@iwb-plugin/album", "private": true, - "version": "1.1.5", + "version": "1.1.6", "description": "相册:一个节点装多张本地图(只引用不复制),宫格自适应分页,一图一任务 / 批量两种模式批量生图", "ibwPlugin": { "id": "album", diff --git a/album/src/index.js b/album/src/index.js index f440e25..c636da3 100644 --- a/album/src/index.js +++ b/album/src/index.js @@ -72,7 +72,26 @@ const str = (v) => (v == null ? '' : String(v)) // ── 缩略图(插件私有缓存,几十 KB/张,不碰画布存储)──────────── - const thumbNameOf = (key) => 't-' + hash32(key) + '.jpg' + // 编码代号(1.1.6): + // · 1.1.5 及以前一律 JPEG —— 而 JPEG 没有 alpha,canvas 的透明像素编码后是**纯黑** + // (Chromium 实测 rgba(0,0,0,255))⇒ 抠图/透明 PNG 在相册里是一格格黑方块。 + // · 现在「含 alpha 走 PNG,否则 JPEG 0.85」;代号进文件名,旧黑底缓存自然不再命中。 + const THUMB_REV = '2' + const thumbBase = (key) => 't' + THUMB_REV + '-' + hash32(key) + // 宿主文件白名单只认 jpg/jpeg/png/webp,后缀必须与实际字节一致(读回时按文件头嗅探 mime) + const thumbNameOf = (key, ext) => thumbBase(key) + '.' + (ext === 'png' ? 'png' : 'jpg') + const thumbNames = (key) => [thumbNameOf(key, 'png'), thumbNameOf(key, 'jpg')] + // 1.1.5 及以前的缓存名(永远是 JPEG),写新图时顺手清掉,别让黑底缩略图在磁盘里越积越多 + const oldThumbName = (key) => 't-' + hash32(key) + '.jpg' + // keepExt:刚写成功的那一份不能删(png/jpg 只留一个),其余变体清掉 + function dropThumbFile (ctx, key, keepExt) { + if (!ctx || !ctx.files || !key) return + const keep = keepExt ? thumbNameOf(key, keepExt) : '' + for (const n of thumbNames(key).concat([oldThumbName(key)])) { + if (keep && n === keep) continue + try { const p = ctx.files.remove(n); if (p && typeof p.catch === 'function') p.catch(() => {}) } catch (e) { /* ignore */ } + } + } // 缩略图缓存键(1.1.0 修「串味」):旧实现是 `fileRefOf(src) || it.name`,一旦固化(src 变成 // {__blob})或从别处导入,路径就没了 ⇒ 键退化成纯文件名 ⇒ 不同目录下的 1.png 共用同一张缩略图。 @@ -93,6 +112,9 @@ return 'n:' + str(n) } + // 返回 { url, ext };失败返回 null。 + // alpha 判定:缩放后隔 4 个像素采样 alpha 通道(256² 画布全读也就 256KB 一次性缓冲)。 + // 只有确实含透明像素才付 PNG 的体积代价 —— 照片仍走 JPEG,缓存总量不会膨胀。 function makeThumb (dataUrl) { return new Promise((resolve) => { try { @@ -106,12 +128,18 @@ cv.width = w; cv.height = h const c2 = cv.getContext('2d') c2.drawImage(img, 0, 0, w, h) - resolve(cv.toDataURL('image/jpeg', THUMB_Q)) - } catch (e) { resolve('') } + let alpha = false + try { + const d = c2.getImageData(0, 0, w, h).data + for (let i = 3; i < d.length; i += 16) { if (d[i] < 250) { alpha = true; break } } + } catch (e) { /* 读不出像素就按不透明处理(IMG 被污染时 JPEG 也不会更糟) */ } + if (alpha) resolve({ url: cv.toDataURL('image/png'), ext: 'png' }) + else resolve({ url: cv.toDataURL('image/jpeg', THUMB_Q), ext: 'jpg' }) + } catch (e) { resolve(null) } } - img.onerror = () => resolve('') + img.onerror = () => resolve(null) img.src = dataUrl - } catch (e) { resolve('') } + } catch (e) { resolve(null) } }) } @@ -361,22 +389,28 @@ // 缩略图缓存可能来自旧路径/旧版本,dataURL 本身坏了也会命中;onerror 时绕过缓存重读原图一次。 let thumbRetried = false const applyThumb = (url) => { - if (!url || img.isConnected === false) return + if (!url) return img.onerror = () => { if (thumbRetried) return thumbRetried = true st.thumbs.delete(key) if (st.forceThumbs) st.forceThumbs.add(key) - try { if (ctx.files) ctx.files.remove(thumbNameOf(key)) } catch (e) { /* ignore */ } + dropThumbFile(ctx, key) loadThumb(ctx, st, it, key, true).then((next) => { if (next) applyThumb(next) }) } img.src = url } const cached = st.thumbs.get(key) + // ⚠ 命中内存缓存时 img **还没** appendChild(本函数末尾才挂),所以这里不能判存活: + // 1.1.5 把「存活判断」提进了 applyThumb,于是同步分支永远 isConnected === false ⇒ 直接 return, + // 每次「导入 / 翻页 / 切密度 / 拖拽排序」引发宫格重建后,内存里已有的缩略图全都赋不上 src, + // 格子一片空白(切走再切回来才正常 —— 那时内存缓存是空的,走异步分支)。 + // 存活判断只对异步回来的那一支有意义:那时格子可能已被下一次重建丢掉。 if (cached) applyThumb(cached) // 不设 img.src = '':空字符串会向页面 URL 发一次请求(控制台 ERR_UNKNOWN_URL_SCHEME)。 // 不设 src 属性时 什么都不做,缩略图到了再赋上即可。 - else loadThumb(ctx, st, it, key).then(applyThumb) + // 异步分支才判存活(等图期间宫格可能已被下一次重建丢掉,旧 img 不该再赋值) + else loadThumb(ctx, st, it, key).then((u) => { if (u && img.isConnected) applyThumb(u) }) btn.appendChild(img) btn.appendChild(el('span', { className: 'iwb-album-idx' }, String(index + 1))) // 批次角标(右上角那个「1」)已删除:顶栏「N 张 → M 个任务」已经说清楚,角标是重复信息, @@ -471,11 +505,14 @@ } async function loadThumbInner (ctx, st, it, key, force) { if (!force && st.thumbs.has(key)) return st.thumbs.get(key) - const name = thumbNameOf(key) + // 读缓存要试两个后缀:同一个 key 可能存成 png(含透明)或 jpg(照片)。 + // 先 png 不会误命中 —— 透明图只可能存 png,照片只可能存 jpg。 try { if (!force && ctx.files) { - const got = await ctx.files.get(name) - if (got && got.ok && got.url) { st.thumbs.set(key, got.url); return got.url } + for (const name of thumbNames(key)) { + const got = await ctx.files.get(name) + if (got && got.ok && got.url) { st.thumbs.set(key, got.url); return got.url } + } } } catch (e) { /* 没缓存就现做 */ } let dataUrl = '' @@ -486,13 +523,20 @@ else dataUrl = await ctx.images.dataUrl(src) } catch (e) { dataUrl = '' } if (!dataUrl) return '' - const thumb = await makeThumb(dataUrl) - if (!thumb) return '' - st.thumbs.set(key, thumb) + const made = await makeThumb(dataUrl) + if (!made || !made.url) return '' + st.thumbs.set(key, made.url) if (st.forceThumbs) st.forceThumbs.delete(key) // 内存里已经有一张就够本会话用了;写盘失败(配额满)不影响显示 - try { if (ctx.files) await ctx.files.put(name, thumb) } catch (e) { /* ignore */ } - return thumb + try { + if (ctx.files) { + const put = await ctx.files.put(thumbNameOf(key, made.ext), made.url) + // 写成功才清旧:换格式留下的另一份、以及 1.1.5 的黑底 JPEG。 + // 写失败(如 64MB 配额满)就先都留着,别把唯一一份也删了。 + if (!put || put.ok !== false) dropThumbFile(ctx, key, made.ext) + } + } catch (e) { /* ignore */ } + return made.url } // ── 看大图:插件自带全屏查看器(1.1.2 起支持 ← → 切换)────────── @@ -732,13 +776,13 @@ st.missing.delete(ref.item.path) st.thumbs.delete(oldKey) if (st.forceThumbs) st.forceThumbs.add(oldKey) - try { if (ctx.files) await ctx.files.remove(thumbNameOf(oldKey)) } catch (e) { /* ignore */ } + dropThumbFile(ctx, oldKey) // 路径大小写/分隔符不同 ⇒ 新 key 也要清掉旧缓存并强制重读 const newKey = thumbKeyOf(items[at]) if (newKey !== oldKey) { st.thumbs.delete(newKey) if (st.forceThumbs) st.forceThumbs.add(newKey) - try { if (ctx.files) await ctx.files.remove(thumbNameOf(newKey)) } catch (e) { /* ignore */ } + dropThumbFile(ctx, newKey) } } else { items.push({ src: nextSrc, name: ref.item.name, size: ref.item.size, mtime: ref.item.mtime, on: true }) @@ -762,8 +806,10 @@ } st.busy = '' st.btnAdd.textContent = '+ 添加图片' - st.sig = '' - sync(ctx, st, st.node, st.props) + // 预生成只是「提前把缩略图做进缓存」:格子自己的 loadThumb 早在上面那次 sync 里就发起了, + // pending 表已把同一张合成一个 Promise。这里**不再**置空 sig 强制整块重建 —— + // 1.1.5 每次导入末尾都会多建一遍全部格子(500 张导入 = 白建 500 个 DOM),是肉眼可见的卡顿; + // 而它当年还掩盖了缩略图赋值的问题(重建恰好把所有格子都变成「命中缓存 → 空白」)。 } async function importDropped (ctx, st, files) { @@ -783,12 +829,12 @@ if (!src) continue const name = f.name || ('图 ' + (items.length + 1)) items.push({ src, name: name, size: f.size || 0, on: true }) - const thumb = await makeThumb(dataUrl) + const made = await makeThumb(dataUrl) // 键必须和 cellOf / loadThumb 完全一致(thumbKeyOf),否则写进缓存也命中不到 - if (thumb) { + if (made && made.url) { const key = thumbKeyOf(items[items.length - 1]) - st.thumbs.set(key, thumb) - try { if (ctx.files) await ctx.files.put(thumbNameOf(key), thumb) } catch (e) { /* ignore */ } + st.thumbs.set(key, made.url) + try { if (ctx.files) await ctx.files.put(thumbNameOf(key, made.ext), made.url) } catch (e) { /* ignore */ } } } st.busy = '' @@ -858,7 +904,7 @@ global.IWBPluginHost.register({ id: 'album', name: '相册', - version: '1.1.5', + version: '1.1.6', apiVersion: 1, capabilities: ['nodeTypes', 'images', 'files'], settings: {