fix(album): 1.1.5 重选文件夹就地更新+缩略图缓存自愈

This commit is contained in:
qiqi committed 2026-11-26 08:46:50 +08:00
1 parent 8a851ad941
commit 135823494b
2 files changed
+59 -13

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@iwb-plugin/album",
"private": true,
"version": "1.1.4",
"version": "1.1.5",
"description": "相册:一个节点装多张本地图(只引用不复制),宫格自适应分页,一图一任务 / 批量两种模式批量生图",
"ibwPlugin": {
"id": "album",
+58 -12
View File
@@ -66,6 +66,9 @@
const isImagePath = (p) => IMG_EXT.indexOf(extOf(p)) >= 0
const fileRefOf = (src) => (src && typeof src === 'object' && typeof src.__file === 'string') ? src.__file : ''
const nameOf = (p) => String(p || '').replace(/^.*[\\/]/, '')
// Windows 路径大小写不敏感;重选同一文件夹时 dialog 可能回不同大小写,必须按平台归一化后再去重
const WIN = typeof navigator !== 'undefined' && /Windows/i.test(String(navigator.userAgent || ''))
const normPath = (p) => { const s = String(p || '').replace(/\\/g, '/'); return WIN ? s.toLowerCase() : s }
const str = (v) => (v == null ? '' : String(v))
// ── 缩略图(插件私有缓存,几十 KB/张,不碰画布存储)────────────
@@ -143,6 +146,7 @@
const st = {
nodeId: node.id,
thumbs: new Map(), // key → dataURL(内存缓存)
forceThumbs: new Set(), // 需要绕过旧缩略图缓存、重新读原图的 key(重选文件夹/缩略图损坏)
pending: new Map(), // key → Promise(并发去重:格子挂载和预生成会同时要同一张)
missing: new Set(), // 失联文件的 key
busy: '',
@@ -354,11 +358,25 @@
title: (it && it.name ? it.name : ('图 ' + (index + 1))) + (missing ? '(文件已不在原位置:点一下重新定位)' : ''),
})
const img = el('img', { alt: '', draggable: 'false' })
// 缩略图缓存可能来自旧路径/旧版本,dataURL 本身坏了也会命中;onerror 时绕过缓存重读原图一次。
let thumbRetried = false
const applyThumb = (url) => {
if (!url || img.isConnected === false) 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 */ }
loadThumb(ctx, st, it, key, true).then((next) => { if (next) applyThumb(next) })
}
img.src = url
}
const cached = st.thumbs.get(key)
if (cached) img.src = cached
if (cached) applyThumb(cached)
// 不设 img.src = '':空字符串会向页面 URL 发一次请求(控制台 ERR_UNKNOWN_URL_SCHEME)。
// 不设 src 属性时 <img> 什么都不做,缩略图到了再赋上即可。
else loadThumb(ctx, st, it, key).then((url) => { if (url && img.isConnected !== false) img.src = url })
else loadThumb(ctx, st, it, key).then(applyThumb)
btn.appendChild(img)
btn.appendChild(el('span', { className: 'iwb-album-idx' }, String(index + 1)))
// 批次角标(右上角那个「1」)已删除:顶栏「N 张 → M 个任务」已经说清楚,角标是重复信息,
@@ -442,19 +460,20 @@
// ── 缩略图取用:内存 → 插件文件 → 现做 ───────────────────
// 并发去重(1.1.0):格子一挂载就各自要缩略图,而 addPaths 又顺序预热一遍 ⇒ 同一张图会被读两次盘。
// 用 pending 表把同 key 的请求合成一个 Promise(否则导入 30 张会解 60 次原图,内存直接翻倍)。
async function loadThumb (ctx, st, it, key) {
if (st.thumbs.has(key)) return st.thumbs.get(key)
async function loadThumb (ctx, st, it, key, force) {
const forced = !!force || !!(st.forceThumbs && st.forceThumbs.has(key))
if (!forced && st.thumbs.has(key)) return st.thumbs.get(key)
const flight = st.pending && st.pending.get(key)
if (flight) return flight
const p = loadThumbInner(ctx, st, it, key).finally(() => { if (st.pending) st.pending.delete(key) })
if (flight && !forced) return flight
const p = loadThumbInner(ctx, st, it, key, forced).finally(() => { if (st.pending) st.pending.delete(key) })
if (st.pending) st.pending.set(key, p)
return p
}
async function loadThumbInner (ctx, st, it, key) {
if (st.thumbs.has(key)) return st.thumbs.get(key)
async function loadThumbInner (ctx, st, it, key, force) {
if (!force && st.thumbs.has(key)) return st.thumbs.get(key)
const name = thumbNameOf(key)
try {
if (ctx.files) {
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 }
}
@@ -470,6 +489,7 @@
const thumb = await makeThumb(dataUrl)
if (!thumb) return ''
st.thumbs.set(key, thumb)
if (st.forceThumbs) st.forceThumbs.delete(key)
// 内存里已经有一张就够本会话用了;写盘失败(配额满)不影响显示
try { if (ctx.files) await ctx.files.put(name, thumb) } catch (e) { /* ignore */ }
return thumb
@@ -645,7 +665,7 @@
function clearAll (ctx, st) {
const items = itemsOf(st.node)
if (!items.length) return
st.thumbs.clear(); st.pending.clear(); st.missing.clear()
st.thumbs.clear(); st.pending.clear(); st.missing.clear(); if (st.forceThumbs) st.forceThumbs.clear()
st.checkedMissing = false // 清空后再导入要重新做一次失联检测
closeViewer(st) // 正在看大图时清空 ⇒ 先把浮层收掉(别留个没有数据的空查看器)
update(st.props, st.node, { items: [], page: 0, pageRange: null }, true)
@@ -696,7 +716,33 @@
st.busy = '导入 ' + (i + 1) + '/' + list.length; st.sig = ''; sync(ctx, st, st.node, st.props)
const ref = await ctx.images.reference(list[i])
if (!ref || !ref.ok) continue
items.push({ src: { __file: ref.item.path }, name: ref.item.name, size: ref.item.size, mtime: ref.item.mtime, on: true })
const nextSrc = { __file: ref.item.path }
// 重选同一文件夹:按归一化路径就地更新,不追加重复项(旧实现会让坏格一直留在前面,
// 新加的重复项排到后面/下一页,看起来就是「重新选择也不出缩略图」)。
let at = -1
for (let k = 0; k < items.length; k++) {
const p0 = fileRefOf(items[k] && items[k].src)
if (p0 && normPath(p0) === normPath(ref.item.path)) { at = k; break }
}
if (at >= 0) {
const old = items[at]
const oldKey = thumbKeyOf(old)
items[at] = Object.assign({}, old, { src: nextSrc, name: ref.item.name, size: ref.item.size, mtime: ref.item.mtime, on: true })
st.missing.delete(fileRefOf(old && old.src))
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 */ }
// 路径大小写/分隔符不同 ⇒ 新 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 */ }
}
} else {
items.push({ src: nextSrc, name: ref.item.name, size: ref.item.size, mtime: ref.item.mtime, on: true })
}
}
st.busy = ''
st.checkedMissing = false // 新图要重新做一次失联检测(否则清空后再导入,检测永远不再跑)
@@ -812,7 +858,7 @@
global.IWBPluginHost.register({
id: 'album',
name: '相册',
version: '1.1.4',
version: '1.1.5',
apiVersion: 1,
capabilities: ['nodeTypes', 'images', 'files'],
settings: {