fix(promptlib): 1.5.3 加载重试 + 编辑页封面异步绘制 + 点击异常提示 + 单一数据源

This commit is contained in:
qiqi committed 2026-11-22 16:47:14 +08:00
1 parent 5e09a587e2
commit 013bb6ba13
1 file changed
+36 -13
+36 -13
View File
@@ -176,7 +176,6 @@
prompts = ((r && r.prompts) || []).map(normalize)
groups = (r && r.groups) || []
render()
convertInlineCovers()
return true
}).catch(function (e) { loadError = '读取失败:' + ((e && e.message) || e); prompts = []; render(); return false })
}
@@ -205,29 +204,52 @@
}
// v1.5.1:内联封面转插件文件。放在面板 load 之后执行(此时文件存储一定就绪),
// 避免激活早期 ctx.files 不可用导致「迁移看似成功、封面其实还在 JSON 里」。
function convertInlineCovers () {
// v1.5.2:内联封面转插件文件。文件存储可能晚于面板挂载才就绪,
// 失败就等 1.5s 重试(最多 6 次);只有全部成功才落盘,避免把 dataURL 又写回去。
function convertInlineCovers (attempt) {
attempt = attempt || 0
if (!FILEAPI) return
var need = prompts.filter(function (p) { return p && typeof p.cover === 'string' && p.cover.indexOf('data:') === 0 })
if (!need.length) return
var failed = 0
Promise.all(need.map(function (p) {
var c = p.cover
var h = 5381
for (var i = 0; i < c.length; i++) h = ((h << 5) + h + c.charCodeAt(i)) >>> 0
var name = 'c' + h.toString(36) + '.jpg'
return Promise.resolve(FILEAPI.put(name, c)).then(function (r) {
return r && r.ok ? Object.assign({}, p, { cover: { img: name } }) : p
}).catch(function () { return p })
})).then(function (items) {
var map = {}
items.forEach(function (x) { map[x.id] = x })
prompts = prompts.map(function (p) { return map[p.id] || p })
return persist()
}).then(function () { render() }).catch(function () { /* ignore */ })
if (r && r.ok) return { id: p.id, item: Object.assign({}, p, { cover: { img: name } }) }
failed++
return { id: p.id, item: p }
}).catch(function () { failed++; return { id: p.id, item: p } })
})).then(function (rows) {
if (failed && attempt < 6) {
setTimeout(function () { convertInlineCovers(attempt + 1) }, 1500)
return
}
if (!failed) {
var map = {}
rows.forEach(function (x) { map[x.id] = x.item })
prompts = prompts.map(function (p) { return map[p.id] || p })
notify('已把 ' + rows.length + ' 张封面迁移到插件目录')
persist().then(function () { render() })
}
}).catch(function () {})
}
function load () {
function load () { return loadOnce(0) }
try { ctx.storage && ctx.storage.set('diag', JSON.stringify({ files: !!ctx.files, keys: Object.keys(ctx.promptlib || {}).slice(0, 24), hasPut: typeof (ctx.promptlib || {}).filePut, hasData: !!ctx.data })) } catch (e) { /* ignore */ }
function loadOnce (attempt) {
return loadCore().then(function (ok) {
if ((prompts && prompts.length) || attempt >= 4) return ok
return new Promise(function (resolve) { setTimeout(resolve, 800) }).then(function () { return loadOnce(attempt + 1) })
})
}
function loadCore () {
// 唯一真源 = 插件数据文件。只有它**不存在**时才从宿主迁一次;
// 绝不因为一次读取为空就把宿主数据写回来(会把已迁移的封面对象覆盖回 dataURL,来回翻烧)。
if (!DATAAPI || typeof DATAAPI.read !== 'function') return legacyLoad()
return Promise.resolve(DATAAPI.read()).then(function (d) {
if (d && Array.isArray(d.prompts)) {
if (d && Array.isArray(d.prompts) && d.prompts.length) {
loadError = ''
prompts = d.prompts.map(normalize)
groups = Array.isArray(d.groups) ? d.groups : []
@@ -235,6 +257,7 @@
convertInlineCovers()
return true
}
if (d && Array.isArray(d.prompts) && !d.prompts.length) { prompts = []; groups = []; render(); return true }
return migrateFromHost()
}).catch(function () { return legacyLoad() })
}
@@ -916,7 +939,7 @@
e.dataTransfer.effectAllowed = 'copy'
} catch (err) { /* ignore */ }
})
card.addEventListener('click', function (e) { e.stopPropagation(); openDetailModal(p) })
card.addEventListener('click', function (e) { e.stopPropagation(); try { openDetailModal(p) } catch (err) { notify('详情打开失败:' + ((err && err.message) || err), 'err'); console.error('[promptlib] detail failed', err) } })
card.addEventListener('contextmenu', function (e) { openMenu(e, p) })
cards.appendChild(card)
})