wip(plugins): promptlib 1.5.0 变量/模板节点 + 封面走 promptlib 文件方法

This commit is contained in:
qiqi committed 2026-11-22 16:27:57 +08:00
1 parent c9b8bdf27c
commit a46d874d33
7 files changed
+128 -35

No files matched your search

+6
View File
@@ -8240,6 +8240,12 @@ const tbCenterSeed = (type, refs) => {
putCover: (u) => wPromptCoverCtl().put(u),
coverUrl: (r) => wPromptCoverCtl().url(r),
removeCover: () => ({ ok: true }),
// v3.26.36:封面/备份文件走插件沙箱目录 plugin-data/promptlib/files
filePut: (name, dataUrl) => (window.IWBStorageFile && window.IWBStorageFile.pluginFiles) ? window.IWBStorageFile.pluginFiles.put('promptlib', name, dataUrl) : { ok: false, error: '文件能力未就绪' },
fileGet: (name) => (window.IWBStorageFile && window.IWBStorageFile.pluginFiles) ? window.IWBStorageFile.pluginFiles.get('promptlib', name) : { ok: false, error: '文件能力未就绪' },
fileList: () => (window.IWBStorageFile && window.IWBStorageFile.pluginFiles) ? window.IWBStorageFile.pluginFiles.list('promptlib') : [],
fileRemove: (name) => (window.IWBStorageFile && window.IWBStorageFile.pluginFiles) ? window.IWBStorageFile.pluginFiles.remove('promptlib', name) : { ok: false },
fileStat: () => (window.IWBStorageFile && window.IWBStorageFile.pluginFiles) ? window.IWBStorageFile.pluginFiles.stat('promptlib') : { ok: false, files: [], bytes: 0 },
})
}
if (window.IWBPluginGallery) window.IWBPluginGallery.setImpl({
+2 -2
View File
@@ -46,8 +46,8 @@
"style": "styles.css",
"version": "1.5.0",
"files": {
"main.js": "85c2b59eb3bbb6be7fba3559c71b0eb85230138e82855180499fd32d286b6394",
"styles.css": "dfb968cb1ba7553ac37366235393a0c9ae4d8430ef4d54d9823cd553055e81ec"
"main.js": "c3e175b2749fafc4d9daf42ab722bcbb9db7f09951e7000b7fa6241598edb386",
"styles.css": "48579039ffb5ddb610f0b17100b281e8f3c55e7485fc9ba9ab115d88399af900"
}
},
{
+108 -27
View File
@@ -56,6 +56,22 @@
function uid (prefix) { return prefix + Date.now().toString(36) + Math.random().toString(36).slice(2, 7) }
function groupRef (name) { return { id: name, name: name } }
// 文件沙箱:新客户端有独立 files 能力;老客户端退到 promptlib 能力上的 file* 方法。
function fileApi (ctx) {
if (ctx.files && typeof ctx.files.put === 'function') return ctx.files
var P = ctx.promptlib
if (P && typeof P.filePut === 'function') {
return {
put: function (n, d) { return P.filePut(n, d) },
get: function (n) { return P.fileGet(n) },
list: function () { return P.fileList() },
remove: function (n) { return P.fileRemove(n) },
stat: function () { return P.fileStat() },
}
}
return null
}
function mount (host, ctx) {
clear(host)
host.classList.add('iwb-plugin-node-body')
@@ -86,7 +102,7 @@
return seen
}
// ===== v1.5.0:插件自持数据(schemaVersion 3)=====
var FILEAPI = ctx.files || null
var FILEAPI = fileApi(ctx)
var DATAAPI = ctx.data || null
var SCHEMA = 3
var migrateDone = false
@@ -160,6 +176,7 @@
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 })
}
@@ -186,6 +203,27 @@
})
}).catch(function () { return false })
}
// v1.5.1:内联封面转插件文件。放在面板 load 之后执行(此时文件存储一定就绪),
// 避免激活早期 ctx.files 不可用导致「迁移看似成功、封面其实还在 JSON 里」。
function convertInlineCovers () {
if (!FILEAPI) return
var need = prompts.filter(function (p) { return p && typeof p.cover === 'string' && p.cover.indexOf('data:') === 0 })
if (!need.length) return
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 */ })
}
function load () {
if (!DATAAPI || typeof DATAAPI.read !== 'function') return legacyLoad()
return Promise.resolve(DATAAPI.read()).then(function (d) {
@@ -194,6 +232,7 @@
prompts = d.prompts.map(normalize)
groups = Array.isArray(d.groups) ? d.groups : []
render()
convertInlineCovers()
return true
}
return migrateFromHost()
@@ -428,6 +467,19 @@
}
return ''
}
// 把封面的各种形态(dataURL / {img} / 旧 ref)统一解析成可显示 URL。
// 编辑弹窗只 paint 一次,必须等解析完成再画,否则永远空白(v1.5.1 修复)。
function resolveCoverUrl (cover) {
if (!cover) return Promise.resolve('')
if (typeof cover === 'string') {
if (cover.indexOf('data:') === 0) return Promise.resolve(cover)
return Promise.resolve().then(function () { return api.coverUrl(cover) }).then(function (r) { return (r && r.ok && r.url) || '' }).catch(function () { return '' })
}
if (cover && cover.img && FILEAPI) {
return Promise.resolve(FILEAPI.get(String(cover.img))).then(function (r) { return (r && r.ok && r.url) || '' }).catch(function () { return '' })
}
return Promise.resolve().then(function () { return api.coverUrl(cover) }).then(function (r) { return (r && r.ok && r.url) || '' }).catch(function () { return '' })
}
function gradOf (p) {
var str = String((p && (p.group || p.name)) || 'x')
var h = 0
@@ -671,7 +723,13 @@
var fileIn = el('input', { type: 'file', accept: 'image/*', style: 'display:none' })
var upBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '上传图片' })
var delCover = el('button', { class: 'iwb-btn iwb-btn-sm', text: '删除封面' })
function repaint () { paintCoverInto(cov, { id: draft.id, cover: draft.cover, group: draft.group }) }
function repaint () {
paintCoverInto(cov, { id: draft.id, cover: draft.cover, group: draft.group })
resolveCoverUrl(draft.cover).then(function (u) {
if (u) paintCoverInto(cov, { id: draft.id, cover: draft.cover, group: draft.group, _coverUrl: u })
})
}
repaint()
upBtn.addEventListener('click', function () { try { fileIn.click() } catch (e) { /* ignore */ } })
fileIn.addEventListener('change', function () {
var f = fileIn.files && fileIn.files[0]
@@ -895,35 +953,54 @@
name: '提示词库',
version: '1.5.0',
apiVersion: 1,
capabilities: ['panels', 'promptlib', 'files', 'nodeTypes', 'canvasWrite'],
capabilities: ['panels', 'promptlib', 'nodeTypes', 'canvasWrite'],
data: { schemaVersion: 3, maxBytes: 4 * 1024 * 1024 },
}, function (ctx) {
// v1.5.0:插件激活时就把旧数据迁到插件目录(不等面板打开),
// 这样即使用户从不打开面板,数据也已经是插件自持的。
try {
if (ctx.data && ctx.files && ctx.promptlib && typeof ctx.promptlib.list === 'function') {
Promise.resolve(ctx.data.read()).then(function (d) {
if (d && Array.isArray(d.prompts)) return null
return Promise.resolve(ctx.promptlib.list()).then(function (r) {
var list = (r && r.prompts) || []
var gs = (r && r.groups) || []
if (!list.length) return null
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'
return Promise.resolve(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 (items) {
return ctx.data.write({ schemaVersion: 3, prompts: items, groups: gs, updatedAt: Date.now() })
}).then(function () { return true })
})
}).catch(function () { /* 迁移失败不阻塞插件 */ })
// v1.5.0:插件激活时完成两件事:旧数据迁入插件目录、内联封面转成插件文件(幂等)。
// ctx.files 可能晚于插件激活才就绪(文件存储异步初始化)⇒ 失败重试几次,别把「数据没迁」当正常。
function migratePlpData (attempt) {
attempt = attempt || 0
var FA = fileApi(ctx)
if (!FA) {
if (attempt < 6) setTimeout(function () { migratePlpData(attempt + 1) }, 2000)
return
}
} catch (e) { /* ignore */ }
var gs = []
Promise.resolve(ctx.data && ctx.data.read ? ctx.data.read() : {}).then(function (d) {
var list = (d && Array.isArray(d.prompts)) ? d.prompts : null
gs = (d && Array.isArray(d.groups)) ? d.groups : []
if (!list && ctx.promptlib && typeof ctx.promptlib.list === 'function') {
return Promise.resolve(ctx.promptlib.list()).then(function (r) {
list = (r && r.prompts) || []
gs = (r && r.groups) || gs
return list
})
}
return list || []
}).then(function (list) {
if (!Array.isArray(list) || !list.length) return null
var need = list.some(function (p) { return p && typeof p.cover === 'string' && p.cover.indexOf('data:') === 0 })
if (!need) return null
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'
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 })
})).then(function (items) {
return ctx.data.write({ schemaVersion: 3, prompts: items, groups: gs, updatedAt: Date.now() })
})
}).catch(function (err) {
try { ctx.storage && ctx.storage.set('lastError', 'migrate:' + String((err && err.message) || err)) } catch (e2) { /* ignore */ }
if (attempt < 3) setTimeout(function () { migratePlpData(attempt + 1) }, 3000)
})
}
try { migratePlpData(0) } catch (e) { /* ignore */ }
ctx.ui.registerPanel({
id: PANEL_ID,
title: '提示词库',
@@ -1030,7 +1107,11 @@
}, 300)
}
if (ctx.nodes && typeof ctx.nodes.register === 'function') {
ctx.nodes.register({ id: 'ptpl', title: '提示词模板', defaults: {}, ports: { in: false, out: true }, height: 360, render: renderTplNode })
try {
ctx.nodes.register({ id: 'ptpl', title: '提示词模板', defaults: {}, ports: { in: false, out: true }, height: 360, render: renderTplNode })
} catch (e) {
try { ctx.storage && ctx.storage.set('lastError', 'node:' + String((e && e.message) || e)) } catch (e2) { /* ignore */ }
}
}
return {}
+2 -2
View File
@@ -13,7 +13,7 @@
"提示词"
],
"files": {
"main.js": "85c2b59eb3bbb6be7fba3559c71b0eb85230138e82855180499fd32d286b6394",
"styles.css": "dfb968cb1ba7553ac37366235393a0c9ae4d8430ef4d54d9823cd553055e81ec"
"main.js": "c3e175b2749fafc4d9daf42ab722bcbb9db7f09951e7000b7fa6241598edb386",
"styles.css": "48579039ffb5ddb610f0b17100b281e8f3c55e7485fc9ba9ab115d88399af900"
}
}
+3 -3
View File
@@ -139,7 +139,7 @@
.iwb-promptlib .iwb-plp-card.no-cover { aspect-ratio: 1 / 1; }
/* v1.5.0:模板节点 + 变量标记 */
.iwb-plp-vdot{position:absolute;right:8px;top:8px;width:7px;height:7px;border-radius:50%;background:#7c4dff;box-shadow:0 0 0 2px rgba(255,255,255,.75)}
.iwb-plp-vdot{position:absolute;right:8px;top:8px;width:7px;height:7px;border-radius:50%;background:var(--iwb-plp-accent, #7c4dff);box-shadow:0 0 0 2px var(--iwb-plp-dot-ring, rgba(255,255,255,.75))}
.iwb-plp-node{display:flex;flex-direction:column;gap:6px;padding:8px 10px;font-size:12px;color:var(--iwb-text,#1c2333)}
.iwb-plp-nhead{display:flex;align-items:center;gap:8px}
.iwb-plp-nthumb{width:44px;height:44px;border-radius:8px;object-fit:cover;flex:0 0 auto}
@@ -158,8 +158,8 @@
.iwb-plp-vhead{display:flex;align-items:center;gap:8px;font-size:11.5px;color:var(--iwb-text2,#5b6472)}
.iwb-plp-vhint{color:var(--iwb-text2,#8b929c);font-size:11px}
.iwb-plp-vchips{display:flex;flex-wrap:wrap;gap:6px}
.iwb-plp-vchip{display:inline-flex;align-items:center;gap:4px;padding:1px 7px;border-radius:6px;background:rgba(124,77,255,.10);color:#5a4bd0;font-size:11.5px}
.iwb-plp-vx{font-style:normal;cursor:pointer;color:#8b7ff0;font-weight:700}
.iwb-plp-vchip{display:inline-flex;align-items:center;gap:4px;padding:1px 7px;border-radius:6px;background:color-mix(in srgb, var(--iwb-plp-accent, #7c4dff) 10%, transparent);color:var(--iwb-plp-accent, #5a4bd0);font-size:11.5px}
.iwb-plp-vx{font-style:normal;cursor:pointer;color:var(--iwb-plp-accent, #8b7ff0);font-weight:700}
.iwb-plp-vrows{display:flex;flex-direction:column;gap:5px}
.iwb-plp-vrow{display:flex;align-items:center;gap:8px}
.iwb-plp-vk{width:92px;flex:0 0 92px;font-size:11.5px;color:var(--iwb-text2,#5b6472);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
+1 -1
View File
@@ -1983,7 +1983,7 @@ dependencies = [
[[package]]
name = "image-workbench"
version = "3.26.35"
version = "3.26.36"
dependencies = [
"arboard",
"base64 0.22.1",
+6
View File
@@ -49,6 +49,12 @@
putCover: call('putCover'),
coverUrl: call('coverUrl'),
removeCover: call('removeCover'),
// v3.26.36:封面等插件文件的沙箱读写(挂在 promptlib 能力上,避免新增能力权限面)
filePut: call('filePut'),
fileGet: call('fileGet'),
fileList: call('fileList'),
fileRemove: call('fileRemove'),
fileStat: call('fileStat'),
onChanged: function (fn) {
if (typeof fn !== 'function') return function () {}
listeners.push(fn)