From 013bb6ba135fa4044536d0f586560ce0b7af2d89 Mon Sep 17 00:00:00 2001 From: qiqi Date: Sun, 22 Nov 2026 16:47:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(promptlib):=201.5.3=20=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E9=87=8D=E8=AF=95=20+=20=E7=BC=96=E8=BE=91=E9=A1=B5=E5=B0=81?= =?UTF-8?q?=E9=9D=A2=E5=BC=82=E6=AD=A5=E7=BB=98=E5=88=B6=20+=20=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E5=BC=82=E5=B8=B8=E6=8F=90=E7=A4=BA=20+=20=E5=8D=95?= =?UTF-8?q?=E4=B8=80=E6=95=B0=E6=8D=AE=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- promptlib/src/index.js | 49 +++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/promptlib/src/index.js b/promptlib/src/index.js index 4e22fd7..38945b3 100644 --- a/promptlib/src/index.js +++ b/promptlib/src/index.js @@ -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) })