feat(promptlib): v1.11.0 封面文件治理(保存即落文件 + 孤儿封面回收)

- 抽 coverFileName():迁移/保存/导入四处的内联 djb2 哈希合并为一处,
  同一张图不会再存出两个文件(文件名 = 内容哈希,天然去重)
- 上传封面、保存词条、另存副本都先 ensureCoverFile() 落成文件,
  数据 JSON 里只留 { img: 文件名 };失败退回内联 base64(宁可不省体积,不丢封面)
- 新增 plpGcCoverFiles():回收不再被任何词条引用的封面文件
  安全线:数据不是从插件 JSON 读来的不删 / keep 为空不删 / 只删图片扩展名 /
  刚 put 未引用的名字进 pending 跳过 / 两张词条共用一个封面天然保留
- 触发:面板加载后延迟 4s 自动一次 + 删词条后一次 + 设置页「清理未引用封面」手动入口
- 设置页占用行改成「封面 N 张 / 文件 M 个」,两者不等即说明有孤儿
- 样式散值归零(4px→6px、margin 13px→12px),插件尺度棘轮基线保持 0
- harness 31 → 44 项(含 plpGcCoverFiles 行为断言:只删未引用 / 空集合不删 / 无 FS 跳过)
This commit is contained in:
qiqi committed 2026-11-24 15:14:25 +08:00
1 parent f0568342b1
commit b10d9f3cc0
4 files changed
+224 -54

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@iwb-plugin/promptlib",
"private": true,
"version": "1.10.1",
"version": "1.11.0",
"description": "提示词库:右栏提示词列表(搜索/分组/收藏/新建编辑/插入画布/复制/删除),数据由宿主 promptlib 能力提供",
"ibwPlugin": {
"id": "promptlib",
+168 -47
View File
@@ -55,6 +55,14 @@
}
function uid (prefix) { return prefix + Date.now().toString(36) + Math.random().toString(36).slice(2, 7) }
function groupRef (name) { return { id: name, name: name } }
// v1.11.0:封面文件名 = 内容哈希(同名即同图,天然去重;不会因改名/移动分组产生新文件)。
// 抽成模块级:迁移、保存、设置面板导入三处必须用同一个算法,否则同一张图会存出两个文件。
function coverFileName (dataUrl) {
var s = String(dataUrl || '')
var h = 5381
for (var i = 0; i < s.length; i++) h = ((h << 5) + h + s.charCodeAt(i)) >>> 0
return 'c' + h.toString(36) + '.jpg'
}
// 文件沙箱:新客户端有独立 files 能力;老客户端退到 promptlib 能力上的 file* 方法。
function fileApi (ctx) {
@@ -165,6 +173,54 @@
// v1.10.1:分享封面开关已移除——文本分享恒纯文字、文件分享恒带封面,不再有全局开关。
// (旧的 shareWithCover() / plpSharePayload() 已删除;plpCoverUrl 仍供备份导出使用。)
// ===== v1.11.0:未引用封面回收(P1)=====
// 为什么要有:封面是「内容哈希命名的图片文件」。换封面 / 删词条 / 取消编辑都会留下不再被引用的文件,
// 久了 64MB 沙箱被垃圾占满(用户看到「总量超限,请先清理」却找不到可清的东西)。
// 判定放在模块级:面板(有 prompts 作用域)与设置页(自带 state)必须用**同一套**规则,
// 否则两处各删一半,第三处又不知道谁还在用。
// 安全线:keep 为空时不删(空集合 = 把封面全删的形状,只有人工显式确认的路径才允许)。
var PLP_GC_BUSY = false
function plpGcCoverFiles (FA, keepNames, opts) {
var o = opts || {}
if (!FA || typeof FA.list !== 'function' || typeof FA.remove !== 'function') return Promise.resolve({ removed: 0, bytes: 0, skipped: 'no-fs' })
if (PLP_GC_BUSY) return Promise.resolve({ removed: 0, bytes: 0, skipped: 'busy' })
var keep = {}
var kept = 0
;(keepNames || []).forEach(function (n) {
n = String(n || '')
if (n) { keep[n] = 1; kept++ }
})
if (!kept && !o.manual) return Promise.resolve({ removed: 0, bytes: 0, skipped: 'empty' })
PLP_GC_BUSY = true
return Promise.resolve(FA.list()).then(function (list) {
var rows = Array.isArray(list) ? list : ((list && list.files) || [])
var victims = rows.filter(function (f) {
var name = String((f && f.name) || '')
if (!name || keep[name]) return false
return /\.(jpg|jpeg|png|webp)$/i.test(name)
})
var bytes = 0
victims.forEach(function (f) { bytes += Number(f && f.size) || 0 })
var removed = 0
var seq = Promise.resolve()
victims.forEach(function (f) {
seq = seq.then(function () {
return Promise.resolve(FA.remove(f.name)).then(function (r) { if (!r || r.ok !== false) removed++ }).catch(function () {})
})
})
return seq.then(function () { PLP_GC_BUSY = false; return { removed: removed, bytes: bytes } })
}).catch(function () { PLP_GC_BUSY = false; return { removed: 0, bytes: 0, skipped: 'error' } })
}
// 从词条列表里取「被引用的封面文件名」
function plpCoverNamesOf (list) {
var out = []
;(list || []).forEach(function (p) {
var c = p && p.cover
if (c && typeof c === 'object' && c.img) out.push(String(c.img))
})
return out
}
function renderPluginSettings () {
var root = el('div', { class: 'iwb-plp-settings' })
var state = { prompts: [], groups: [] }
@@ -227,7 +283,9 @@
var st = rs[1] || {}
var covers = state.prompts.filter(function (p) { return p && p.cover }).length
var size = st.bytes || 0
usage.textContent = (rs[0] || 'plugin-data/promptlib') + ' · ' + (size > 1024 ? (size / 1024).toFixed(0) + ' KB' : size + ' B') + ' · ' + state.prompts.length + ' 条 · ' + state.groups.length + ' 个分组 · ' + covers + ' 张封面'
var files = Number(st.count) || 0
// v1.11.0:封面「引用数 / 文件数」分开显示 —— 两者不等就说明有孤儿文件可清。
usage.textContent = (rs[0] || 'plugin-data/promptlib') + ' · ' + (size > 1024 ? (size / 1024).toFixed(0) + ' KB' : size + ' B') + ' · ' + state.prompts.length + ' 条 · ' + state.groups.length + ' 个分组 · 封面 ' + covers + ' 张 / 文件 ' + files + ' 个'
})
}
@@ -274,9 +332,7 @@
return Promise.all(payload.prompts.map(function (p) {
var c = p && p.cover
if (typeof c !== 'string' || c.indexOf('data:') !== 0 || !PLP_CTX.files) return Promise.resolve(p)
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'
var name = coverFileName(c)
return Promise.resolve(PLP_CTX.files.put(name, c)).then(function (res) { return res && res.ok ? Object.assign({}, p, { cover: { img: name } }) : p }).catch(function () { return p })
})).then(function (incoming) {
var byId = {}
@@ -306,13 +362,39 @@
var s3 = el('div', { class: 'iwb-plp-ssec' })
s3.appendChild(el('div', { class: 'iwb-plp-stitle', text: '数据位置' }))
s3.appendChild(usage)
// v1.11.0:未引用封面回收。换封面/删词条会留下孤儿文件,久了把 64MB 沙箱占满。
// 自动清理已在面板加载后跑一次;这里是「我想立刻清」的显式入口(会先读一次最新数据做引用集合)。
var gcBtn = el('button', { class: 'iwb-btn iwb-btn-sm', title: '删除已不再被任何词条引用的封面图片文件', text: '清理未引用封面' })
var openBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '打开数据目录' })
openBtn.addEventListener('click', function () {
var ctx = PLP_CTX || {}
if (!ctx.promptlib || typeof ctx.promptlib.openPath !== 'function') { plpNotify('当前客户端不支持打开目录', 'err'); return }
Promise.resolve(ctx.promptlib.openPath('plugin-data/promptlib')).catch(function (e) { plpNotify('打开失败:' + ((e && e.message) || e), 'err') })
})
s3.appendChild(el('div', { class: 'iwb-plp-sbtns' }, openBtn))
gcBtn.addEventListener('click', function () {
var ctx = PLP_CTX || {}
var FA = fileApi(ctx)
if (!FA) { plpNotify('文件能力不可用,无法清理', 'err'); return }
gcBtn.disabled = true; status.textContent = '正在清理未引用封面…'
// 每次现读一次数据:设置页可能比面板更"新"(面板没开时用户刚删过词条)
Promise.resolve(plpLoad()).then(function (d) {
state.prompts = (d && d.prompts) || state.prompts
state.groups = (d && d.groups) || state.groups
return plpGcCoverFiles(FA, plpCoverNamesOf(state.prompts), { manual: true })
}).then(function (r) {
gcBtn.disabled = false
if (!r || !r.removed) {
status.textContent = (r && r.skipped === 'no-fs') ? '文件能力不可用,无法清理' : '没有未引用的封面(文件已经是最简状态)'
} else {
status.textContent = '✓ 已清理 ' + r.removed + ' 个未引用封面' + (r.bytes ? '(约 ' + Math.max(1, Math.round(r.bytes / 1024)) + ' KB)' : '')
}
refreshUsage(); paint(); plpNotify(status.textContent)
}).catch(function (e) {
gcBtn.disabled = false
status.textContent = '清理失败:' + ((e && e.message) || e)
})
})
s3.appendChild(el('div', { class: 'iwb-plp-sbtns' }, gcBtn, openBtn))
root.appendChild(s3)
plpLoad().then(function (d) { state.prompts = d.prompts || []; state.groups = d.groups || []; paint(); refreshUsage() })
@@ -361,11 +443,6 @@
var SCHEMA = 3
var migrateDone = false
function hashName (s) {
var h = 5381
for (var i = 0; i < s.length; i++) h = ((h << 5) + h + s.charCodeAt(i)) >>> 0
return 'c' + h.toString(36)
}
function coverNameOf (p) {
var c = p && p.cover
if (c && typeof c === 'object' && c.img) return String(c.img)
@@ -414,7 +491,7 @@
var jobs = list.map(function (p) {
var c = p && p.cover
if (!FILEAPI || typeof c !== 'string' || c.indexOf('data:') !== 0) return Promise.resolve(p)
var name = hashName(c) + '.jpg'
var name = coverFileName(c)
return Promise.resolve(FILEAPI.put(name, c)).then(function (res) {
return res && res.ok ? Object.assign({}, p, { cover: { img: name } }) : p
}).catch(function () { return p })
@@ -442,9 +519,7 @@
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'
var name = coverFileName(c)
return Promise.resolve(FILEAPI.put(name, c)).then(function (r) {
if (r && r.ok) return { id: p.id, item: Object.assign({}, p, { cover: { img: name } }) }
failed++
@@ -469,6 +544,46 @@
}
}).catch(function () {})
}
// ===== v1.11.0:封面文件治理(P1)=====
// 背景:封面是「按内容哈希命名的图片文件」。换封面 / 删词条 / 取消编辑都会留下**不再被引用的文件**,
// 时间久了 64MB 沙箱被垃圾占满(用户看到「总量超限,请先清理」却找不到东西可清)。
// 纪律(每条都对应一次真实误删风险):
// ① 自动 GC 只在「数据确实来自插件 JSON」时跑:数据没读到 ⇒ keep 集合为空 ⇒ 会把封面全删;
// ② 库为空时自动 GC 也不跑(keep 为空同样是删光的形状);
// ③ 刚 put 进沙箱、还没被 prompts 引用的名字进 pending,GC 必须跳过;
// ④ 只删图片扩展名的文件;两张词条共用同一封面 = 同一个名字,天然保留。
var pendingCoverNames = {}
var dataFromPlugin = false
// dataURL → { img: 文件名 };失败原样返回(宁可留着内联 base64,也不丢封面)
function ensureCoverFile (cover) {
if (!FILEAPI || typeof cover !== 'string' || cover.indexOf('data:') !== 0) return Promise.resolve(cover)
var name = coverFileName(cover)
pendingCoverNames[name] = 1
return Promise.resolve(FILEAPI.put(name, cover)).then(function (r) {
if (r && r.ok) return { img: name }
delete pendingCoverNames[name]
return cover
}).catch(function () { delete pendingCoverNames[name]; return cover })
}
// 回收未被任何词条引用的封面文件(判定在模块级 plpGcCoverFiles,设置页复用同一套)。
// 返回 { removed, bytes, skipped }
function gcCoverFiles (opts) {
var o = opts || {}
// 自动路径(启动后 / 删词条后):数据不是从插件 JSON 读来的就**不删**——
// 那种情况下 prompts 可能是空数组,keep 集合同样为空,一刀下去封面全没。
if (!o.manual && !dataFromPlugin) return Promise.resolve({ removed: 0, bytes: 0, skipped: 'no-source' })
var names = []
prompts.forEach(function (p) { var n = coverNameOf(p); if (n) names.push(n) })
Object.keys(pendingCoverNames).forEach(function (n) { names.push(n) })
return plpGcCoverFiles(FILEAPI, names, o)
}
// 启动后就地跑一次:延迟到 convertInlineCovers 的写入之后,避免删掉刚落盘的封面
function scheduleCoverGc (delay) {
setTimeout(function () { gcCoverFiles() }, delay || 4000)
}
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) {
@@ -486,11 +601,13 @@
loadError = ''
prompts = d.prompts.map(normalize)
groups = Array.isArray(d.groups) ? d.groups : []
dataFromPlugin = true
render()
convertInlineCovers()
scheduleCoverGc(4000)
return true
}
if (d && Array.isArray(d.prompts) && !d.prompts.length) { prompts = []; groups = []; render(); return true }
if (d && Array.isArray(d.prompts) && !d.prompts.length) { prompts = []; groups = []; dataFromPlugin = true; render(); return true }
return migrateFromHost()
}).catch(function () { return legacyLoad() })
}
@@ -1102,9 +1219,7 @@
return Promise.all(payload.prompts.map(function (p) {
var c = p && p.cover
if (typeof c !== 'string' || c.indexOf('data:') !== 0 || !FILEAPI) return Promise.resolve(p)
var h = 5381
for (var i2 = 0; i2 < c.length; i2++) h = ((h << 5) + h + c.charCodeAt(i2)) >>> 0
var name = 'c' + h.toString(36) + '.jpg'
var name = coverFileName(c)
return Promise.resolve(FILEAPI.put(name, c)).then(function (res) {
return res && res.ok ? Object.assign({}, p, { cover: { img: name } }) : p
}).catch(function () { return p })
@@ -1305,7 +1420,7 @@
var incoming = normalize({ id: uid('q'), name: name, text: item.text, group: String(item.group || ''), tags: item.tags || [] })
var cv = item.cover
var ready = (typeof cv === 'string' && cv.indexOf('data:') === 0 && FILEAPI)
? Promise.resolve(FILEAPI.put(hashName(cv) + '.jpg', cv)).then(function (res) { return res && res.ok ? Object.assign({}, incoming, { cover: { img: hashName(cv) + '.jpg' } }) : incoming }).catch(function () { return incoming })
? Promise.resolve(FILEAPI.put(coverFileName(cv), cv)).then(function (res) { return res && res.ok ? Object.assign({}, incoming, { cover: { img: coverFileName(cv) } }) : incoming }).catch(function () { return incoming })
: Promise.resolve(incoming)
return ready.then(function (p2) {
prompts = prompts.concat([p2])
@@ -1394,9 +1509,7 @@
return Promise.all(payload.prompts.map(function (p) {
var c = p && p.cover
if (typeof c !== 'string' || c.indexOf('data:') !== 0 || !FILEAPI) return Promise.resolve(p)
var h = 5381
for (var i2 = 0; i2 < c.length; i2++) h = ((h << 5) + h + c.charCodeAt(i2)) >>> 0
var name = 'c' + h.toString(36) + '.jpg'
var name = coverFileName(c)
return Promise.resolve(FILEAPI.put(name, c)).then(function (res) {
return res && res.ok ? Object.assign({}, p, { cover: { img: name } }) : p
}).catch(function () { return p })
@@ -1494,7 +1607,7 @@
ask('删除提示词「' + (p.name || '未命名') + '」?', { title: '删除提示词', okText: '删除' }).then(function (ok) {
if (!ok) return
dlg.close()
save(prompts.filter(function (x) { return x.id !== p.id })).then(function () { notify('已删除') })
save(prompts.filter(function (x) { return x.id !== p.id })).then(function () { notify('已删除'); scheduleCoverGc(1200) })
})
})
cp.addEventListener('click', function () { Promise.resolve(api.copy(filled())).then(function (r) { if (r && r.ok === false) notify('复制失败', 'err'); else notify('已复制全文') }) })
@@ -1561,15 +1674,19 @@
rd.onload = function () {
Promise.resolve(api.putCover(String(rd.result || ''))).then(function (r) {
if (!(r && r.ok && r.ref)) { notify('封面上传失败', 'err'); return }
draft.cover = r.ref
dirty = true
Object.keys(coverCache).forEach(function (k) { if (k.indexOf(draft.id + '|') === 0) delete coverCache[k] })
Promise.resolve(api.coverUrl(r.ref)).then(function (u) {
if (u && u.url) {
draft._coverUrl = u.url
coverCache[draft.id + '|s:' + String(r.ref).slice(-32)] = u.url
}
repaint(); render(); notify('封面已更新')
// v1.11.0:上传即落成文件(数据里只留文件名)——
// 之前 draft.cover 存的是 dataURL,doSave 直接把它写进 JSON,一条封面就把数据文件顶大几十 KB。
return ensureCoverFile(r.ref).then(function (cover) {
draft.cover = cover
dirty = true
Object.keys(coverCache).forEach(function (k) { if (k.indexOf(draft.id + '|') === 0) delete coverCache[k] })
return resolveCoverUrl(cover).then(function (u) {
if (u) {
draft._coverUrl = u
coverCache[draft.id + '|s:' + String(cover).slice(-32)] = u
}
repaint(); render(); notify('封面已更新')
})
})
}).catch(function () { notify('封面上传失败', 'err') })
}
@@ -1706,23 +1823,29 @@
var text = String(c.text || '').trim()
if (!text) { notify('提示词内容不能为空', 'err'); return }
var name = String(c.name || '').trim() || ('提示词 ' + text.slice(0, 8) + '…')
var item = { id: c.id, name: name, text: text, tags: c.tags, group: String(c.group || '').trim(), cover: c.cover, vars: c.vars || [] }
var list = isNew ? prompts.concat([item]) : prompts.map(function (x) { return x.id === item.id ? item : x })
var gs = groupsOf().slice()
if (item.group && !gs.some(function (g) { return g.name === item.group })) gs.push(groupRef(item.group))
close()
save(list, gs).then(function () {
notify(isNew ? '已创建' : '已保存')
if (!isNew) { try { openDetailModal(item) } catch (e) { /* ignore */ } }
})
// v1.11.0:保存前把内联封面落成文件(数据里只留 { img }),避免 base64 把 JSON 顶大
saveBtn.disabled = true
ensureCoverFile(c.cover).then(function (finalCover) {
var item = { id: c.id, name: name, text: text, tags: c.tags, group: String(c.group || '').trim(), cover: finalCover, vars: c.vars || [] }
var list = isNew ? prompts.concat([item]) : prompts.map(function (x) { return x.id === item.id ? item : x })
var gs = groupsOf().slice()
if (item.group && !gs.some(function (g) { return g.name === item.group })) gs.push(groupRef(item.group))
close()
return save(list, gs).then(function () {
notify(isNew ? '已创建' : '已保存')
if (!isNew) { try { openDetailModal(item) } catch (e) { /* ignore */ } }
})
}).catch(function () { saveBtn.disabled = false; notify('保存失败', 'err') })
}
saveBtn.addEventListener('click', doSave)
cancelBtn.addEventListener('click', function () { close(); if (!isNew && p) { try { openDetailModal(p) } catch (e) { /* ignore */ } } })
dupBtn.addEventListener('click', function () {
var c = collect()
var copy = Object.assign({}, c, { id: uid('q'), name: (String(c.name || '提示词').trim()) + ' 副本' })
close()
save(prompts.concat([copy])).then(function () { notify('已另存副本') })
ensureCoverFile(c.cover).then(function (cover) {
var copy = Object.assign({}, c, { id: uid('q'), name: (String(c.name || '提示词').trim()) + ' 副本', cover: cover })
return save(prompts.concat([copy])).then(function () { notify('已另存副本') })
}).catch(function () { notify('另存副本失败', 'err') })
})
insBtn.addEventListener('click', function () {
var c = collect()
@@ -1878,7 +2001,7 @@
global.IWBPluginHost.register({
id: 'promptlib',
name: '提示词库',
version: '1.10.1',
version: '1.11.0',
apiVersion: 1,
capabilities: ['panels', 'promptlib', 'nodeTypes', 'canvas', 'canvasWrite'],
data: { schemaVersion: 3, maxBytes: 4 * 1024 * 1024 },
@@ -1917,9 +2040,7 @@
return Promise.all(list.map(function (p) {
var c = p && p.cover
if (typeof c !== 'string' || c.indexOf('data:') !== 0) return Promise.resolve(p)
var h = 5381
for (var i2 = 0; i2 < c.length; i2++) h = ((h << 5) + h + c.charCodeAt(i2)) >>> 0
var name = 'c' + h.toString(36) + '.jpg'
var name = coverFileName(c)
return Promise.resolve(FA.put(name, c)).then(function (res) {
return res && res.ok ? Object.assign({}, p, { cover: { img: name } }) : p
}).catch(function () { return p })
+3 -3
View File
@@ -351,7 +351,7 @@ flex: 1 1 auto;
/* v1.10.1:详情底部分享组(文本/文件分享)与插入组之间的竖向分割线 */
.iwb-plp-dialog .iwb-plp-foot-vsep {
display: inline-block; width: 1px; height: 18px; flex: 0 0 auto;
align-self: center; margin: 0 13px; background: var(--iwb-border);
align-self: center; margin: 0 12px; background: var(--iwb-border);
}
.iwb-plp-dialog .iwb-section-sub {
margin-top: 2px;
@@ -568,12 +568,12 @@ body.iwb-plp-dragging, body.iwb-plp-dragging *{user-select:none!important;-webki
文字仍由 textarea 自己画(可见),所以插入点/点击/选区完全不受影响。
注:v1.8.20 停用高亮层是因为当时「透明 textarea + 背层画文字」两层文字错位;本版不画文字,只画色块,不会复发。 */
.iwb-plp-ewrap .iwb-plp-hl{display:block!important;color:transparent!important;-webkit-text-fill-color:transparent!important}
.iwb-plp-hlvar{color:transparent!important;-webkit-text-fill-color:transparent!important;background:color-mix(in srgb, var(--iwb-plp-accent, #7c4dff) 16%, transparent);border-radius:4px;box-shadow:0 0 0 1px color-mix(in srgb, var(--iwb-plp-accent, #7c4dff) 22%, transparent)}
.iwb-plp-hlvar{color:transparent!important;-webkit-text-fill-color:transparent!important;background:color-mix(in srgb, var(--iwb-plp-accent, #7c4dff) 16%, transparent);border-radius:6px;box-shadow:0 0 0 1px color-mix(in srgb, var(--iwb-plp-accent, #7c4dff) 22%, transparent)}
.iwb-plp-ewrap .iwb-plp-etext{color:var(--iwb-text,#2a2f36)!important;background:transparent!important}
/* v1.8.21:正文文字必须可见(覆盖旧的 transparent !important) */
.iwb-plp-dialog .iwb-plp-emid .iwb-plp-ewrap .iwb-plp-etext{color:var(--iwb-text,#2a2f36)!important;caret-color:var(--iwb-text,#2a2f36)!important;-webkit-text-fill-color:var(--iwb-text,#2a2f36)!important}
/* v1.9.3:右侧变量名与详情正文里的变量也用主色呼应(只读区,零功能风险) */
.iwb-plp-dialog .iwb-plp-vname{color:var(--iwb-plp-accent,#5a3fd0)}
.iwb-plp-dialog .iwb-plp-dvar{color:var(--iwb-plp-accent,#5a3fd0);background:var(--iwb-plp-accent-soft,#f1ecff);border-radius:4px;padding:0 2px}
.iwb-plp-dialog .iwb-plp-dvar{color:var(--iwb-plp-accent,#5a3fd0);background:var(--iwb-plp-accent-soft,#f1ecff);border-radius:6px;padding:0 2px}
/* 回滚开关:若真机发现任何点击/光标异常,把下行打开即回到 v1.8.20 纯 textarea 状态。
.iwb-plp-ewrap .iwb-plp-hl{display:none!important} */
+52 -3
View File
@@ -20,9 +20,9 @@ const ok = (name, cond) => {
}
// ---- 1) 版本 / manifest ----
ok("manifest.version = 1.10.1", /version:\s*'1\.10\.1'/.test(src))
ok('package.json version = 1.10.1',
JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')).version === '1.10.1')
ok("manifest.version = 1.11.0", /version:\s*'1\.11\.0'/.test(src))
ok('package.json version = 1.11.0',
JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')).version === '1.11.0')
ok('manifest 已移除「分享时附带封面图片」开关声明',
!/shareWithCover:\s*\{\s*type:\s*'boolean'/.test(src))
ok('源码不再定义/读取 shareWithCover 设置',
@@ -100,5 +100,54 @@ ok('高亮层跟随 textarea 横向滚动',
ok('右侧变量名与详情变量用主色呼应',
/\.iwb-plp-vname\{color:var\(--iwb-plp-accent/.test(css) && /\.iwb-plp-dvar\{color:var\(--iwb-plp-accent/.test(css))
// ---- 5) v1.11.0 封面文件治理(P1)----
// 目标:① 保存/上传时把内联 base64 落成文件;② 孤儿封面能被回收;③ 回收不能误删。
// 纯文本断言跑不了真实 FS ⇒ 这里用「函数抽出来 + 假 fileApi」跑行为。
ok('封面命名统一走 coverFileName(哈希只在它内部实现一次)',
/function coverFileName \(dataUrl\)/.test(src) && (src.match(/var h = 5381/g) || []).length === 1 && (src.match(/coverFileName\(/g) || []).length >= 6)
ok('保存路径先落文件(ensureCoverFile 包住 doSave)',
/function doSave \(\)[\s\S]{0,600}ensureCoverFile\(c\.cover\)/.test(src))
ok('上传封面即落文件(不再把 dataURL 写回 draft.cover)',
/api\.putCover\([\s\S]{0,400}ensureCoverFile\(r\.ref\)/.test(src))
ok('删词条后安排一次回收', /已删除'[\s\S]{0,80}scheduleCoverGc\(/.test(src))
ok('启动加载后安排一次回收(等 convertInlineCovers 写完)',
/convertInlineCovers\(\)[\s\S]{0,120}scheduleCoverGc\(4000\)/.test(src))
ok('自动回收有「数据来源」闸门(不是插件 JSON 读来的就不删)',
/!o\.manual && !dataFromPlugin\) return Promise\.resolve\(\{ removed: 0, bytes: 0, skipped: 'no-source' \}\)/.test(src))
ok('keep 为空时不删(空集合 = 全删的形状)',
/if \(!kept && !o\.manual\) return Promise\.resolve\(\{ removed: 0, bytes: 0, skipped: 'empty' \}\)/.test(src))
ok('只删图片扩展名文件', src.includes('/\\.(jpg|jpeg|png|webp)$/i.test(name)'))
ok('设置页有显式清理入口 + 现读一次数据',
/text: '清理未引用封面'/.test(src) && /plpGcCoverFiles\(FA, plpCoverNamesOf\(state\.prompts\), \{ manual: true \}\)/.test(src))
ok('pending(刚 put 未被引用)不会被回收',
/Object\.keys\(pendingCoverNames\)\.forEach\(function \(n\) \{ names\.push\(n\) \}\)/.test(src))
// 行为:抽出 plpGcCoverFiles 本体,用假 fileApi 验证「只删未被引用的图片文件」
const gcMatch = src.match(/ function plpGcCoverFiles \(FA, keepNames, opts\) \{[\s\S]*?\n \}\n/)
ok('从 src 抽出 plpGcCoverFiles', !!gcMatch)
if (gcMatch) {
const removed = []
const fakeFA = {
list: async () => [
{ name: 'cused1.jpg', size: 100 },
{ name: 'corphan1.jpg', size: 200 },
{ name: 'cshared.jpg', size: 300 },
{ name: 'notes.txt', size: 1 },
],
remove: async (name) => { removed.push(name); return { ok: true } },
}
const gcSandbox = { Promise, JSON, String, Number, Array, Object, RegExp, console }
vm.createContext(gcSandbox)
vm.runInContext('var PLP_GC_BUSY = false\n' + gcMatch[0] + '\nthis.gc = plpGcCoverFiles', gcSandbox)
const r1 = await gcSandbox.gc(fakeFA, ['cused1.jpg'], {})
ok('回收:只删未引用图片(保留引用项与非图片文件)',
JSON.stringify(removed) === '["corphan1.jpg","cshared.jpg"]')
ok('回收:返回删除数与字节数', r1.removed === 2 && r1.bytes === 500)
const r2 = await gcSandbox.gc(fakeFA, [], {})
ok('回收:keep 为空且非人工 ⇒ 不删(skipped=empty)', r2.skipped === 'empty' && r2.removed === 0)
const r3 = await gcSandbox.gc(null, [], {})
ok('回收:无文件能力 ⇒ skipped=no-fs', r3.skipped === 'no-fs')
}
console.log(failed ? ('✗ ' + failed + ' 项失败') : '✓ 全部通过')
if (failed) process.exitCode = 1