fix(plugins): 暂时回退提示词插件到稳定版 1.4.10(1.5.0 点击详情回归待修复)
This commit is contained in:
1 parent
a46d874d33
commit
5ccc6c879e
5 files changed
+68
-475
No files matched your search
@@ -8240,12 +8240,6 @@ 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({
|
||||
|
||||
+51
-432
@@ -56,22 +56,6 @@
|
||||
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')
|
||||
@@ -101,142 +85,59 @@
|
||||
}
|
||||
return seen
|
||||
}
|
||||
// ===== v1.5.0:插件自持数据(schemaVersion 3)=====
|
||||
var FILEAPI = fileApi(ctx)
|
||||
var DATAAPI = ctx.data || null
|
||||
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 detectVars (text) {
|
||||
var out = [], seen = {}
|
||||
var re = /\{([^{}\n]{1,24})\}/g, m
|
||||
var str = String(text || '')
|
||||
while ((m = re.exec(str))) {
|
||||
var k = String(m[1] || '').trim()
|
||||
if (!k || seen[k]) continue
|
||||
seen[k] = 1
|
||||
out.push({ key: k, label: k, desc: '', def: '' })
|
||||
}
|
||||
return out
|
||||
}
|
||||
function varsOf (p) {
|
||||
if (!p) return []
|
||||
if (Array.isArray(p.vars) && p.vars.length) {
|
||||
return p.vars.map(function (v) {
|
||||
return { key: String((v && v.key) || ''), label: String((v && (v.label || v.key)) || ''), desc: String((v && v.desc) || ''), def: String((v && v.def) || '') }
|
||||
}).filter(function (v) { return v.key })
|
||||
}
|
||||
return detectVars(p.text)
|
||||
}
|
||||
function fillTpl (tpl, vals) {
|
||||
return String(tpl || '').replace(/\{([^{}\n]{1,24})\}/g, function (all, k) {
|
||||
var key = String(k || '').trim()
|
||||
var v = (vals && Object.prototype.hasOwnProperty.call(vals, key)) ? String(vals[key] == null ? '' : vals[key]) : ''
|
||||
return v ? v : all
|
||||
})
|
||||
}
|
||||
function coverNameOf (p) {
|
||||
var c = p && p.cover
|
||||
if (c && typeof c === 'object' && c.img) return String(c.img)
|
||||
return ''
|
||||
}
|
||||
function normalize (p) {
|
||||
return Object.assign({}, p, { vars: varsOf(p) })
|
||||
}
|
||||
|
||||
function persist () {
|
||||
var groupsOut = groupsOf()
|
||||
var payload = { schemaVersion: SCHEMA, prompts: prompts, groups: groupsOut, updatedAt: Date.now() }
|
||||
var jobs = []
|
||||
if (DATAAPI && typeof DATAAPI.write === 'function') {
|
||||
jobs.push(Promise.resolve().then(function () { return DATAAPI.write(payload) }).catch(function () { /* 数据层失败下面还有宿主兜底 */ }))
|
||||
}
|
||||
// 过渡期双写:宿主仍持有 library/prompts.json,保证老客户端与「加到提示词库」不断档
|
||||
if (api && typeof api.save === 'function') {
|
||||
jobs.push(Promise.resolve(api.save({ prompts: prompts, groups: groupsOut })).catch(function () { /* 宿主失败由调用方提示 */ }))
|
||||
}
|
||||
return Promise.all(jobs).then(function () { return { ok: true } })
|
||||
}
|
||||
function save (list, gs) {
|
||||
prompts = list || prompts
|
||||
if (gs) groups = gs
|
||||
render()
|
||||
return persist()
|
||||
return Promise.resolve(api.save({ prompts: prompts, groups: groupsOf() })).then(function (r) {
|
||||
if (r && r.ok === false) { notify(r.error || '提示词库保存失败,请重试', 'err'); return r }
|
||||
return r || { ok: true }
|
||||
}).catch(function () { notify('提示词库保存失败,请重试', 'err') })
|
||||
}
|
||||
function legacyLoad () {
|
||||
function load () {
|
||||
if (typeof api.list !== 'function') { loadError = '提示词能力未就绪(客户端需 ≥ 3.25.57)'; prompts = []; render(); return Promise.resolve(false) }
|
||||
return Promise.resolve(api.list()).then(function (r) {
|
||||
loadError = ''
|
||||
prompts = ((r && r.prompts) || []).map(normalize)
|
||||
prompts = (r && r.prompts) || []
|
||||
groups = (r && r.groups) || []
|
||||
render()
|
||||
convertInlineCovers()
|
||||
slimCovers()
|
||||
return true
|
||||
}).catch(function (e) { loadError = '读取失败:' + ((e && e.message) || e); prompts = []; render(); return false })
|
||||
}
|
||||
function migrateFromHost () {
|
||||
if (migrateDone) return Promise.resolve(false)
|
||||
migrateDone = true
|
||||
return Promise.resolve(typeof api.list === 'function' ? api.list() : { prompts: [], groups: [] }).then(function (r) {
|
||||
var list = (r && r.prompts) || []
|
||||
var gs = (r && r.groups) || []
|
||||
if (!list.length) { prompts = []; groups = gs; render(); return false }
|
||||
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'
|
||||
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 })
|
||||
})
|
||||
return Promise.all(jobs).then(function (items) {
|
||||
prompts = items.map(normalize)
|
||||
groups = gs
|
||||
render()
|
||||
return persist().then(function () { notify('已把 ' + items.length + ' 条提示词迁移到插件数据目录'); return true })
|
||||
})
|
||||
}).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) {
|
||||
if (d && Array.isArray(d.prompts)) {
|
||||
loadError = ''
|
||||
prompts = d.prompts.map(normalize)
|
||||
groups = Array.isArray(d.groups) ? d.groups : []
|
||||
render()
|
||||
convertInlineCovers()
|
||||
return true
|
||||
|
||||
// v1.4.0:一次性瘦身 —— 老版本把封面存成 768px(≈50KB/张)。
|
||||
// 现在上限 448px,检测到超大封面就重新压一遍并保存一次(每个会话最多一次)。
|
||||
var slimDone = false
|
||||
function slimCovers () {
|
||||
if (slimDone) return
|
||||
slimDone = true
|
||||
// v1.4.9:除了 >60KB 的,也把老版本存成 PNG 的封面重压一次(448px PNG 仍有 300KB+)
|
||||
var big = prompts.filter(function (p) {
|
||||
var c = p && p.cover
|
||||
if (typeof c !== 'string' || c.indexOf('data:') !== 0) return false
|
||||
// v1.4.10:阈值提到 150KB,且只在 PNG(体积虚高)时迁移 —— 否则 JPEG 封面每次挂载都会被再压一次
|
||||
return c.length > 150000 || c.indexOf('data:image/png') === 0
|
||||
})
|
||||
if (!big.length) return
|
||||
var next = prompts.slice()
|
||||
var i = 0
|
||||
var changed = false
|
||||
function step () {
|
||||
if (i >= big.length) {
|
||||
if (changed) save(next).then(function () { notify('已压缩 ' + big.length + ' 张封面') }).catch(function () { /* ignore */ })
|
||||
return
|
||||
}
|
||||
return migrateFromHost()
|
||||
}).catch(function () { return legacyLoad() })
|
||||
var p = big[i++]
|
||||
Promise.resolve(api.putCover(p.cover)).then(function (r) {
|
||||
if (r && r.ok && r.ref && String(r.ref).length < p.cover.length) {
|
||||
next = next.map(function (x) { return x.id === p.id ? Object.assign({}, x, { cover: r.ref }) : x })
|
||||
changed = true
|
||||
}
|
||||
step()
|
||||
}).catch(function () { step() })
|
||||
}
|
||||
step()
|
||||
}
|
||||
|
||||
// ---- 搜索 / 过滤 ----
|
||||
@@ -435,22 +336,6 @@
|
||||
var coverPending = {}
|
||||
function coverOf (p) {
|
||||
if (!p || !p.cover) return ''
|
||||
// v1.5.0:封面存插件文件(cover={img}),按名字读一次并缓存
|
||||
var fname = coverNameOf(p)
|
||||
if (fname && FILEAPI) {
|
||||
var fk = p.id + '|f:' + fname
|
||||
if (coverCache[fk]) return coverCache[fk]
|
||||
if (!coverPending[fk]) {
|
||||
coverPending[fk] = 1
|
||||
try {
|
||||
Promise.resolve(FILEAPI.get(fname)).then(function (r) {
|
||||
delete coverPending[fk]
|
||||
if (r && r.ok && r.url) { coverCache[fk] = r.url; render() }
|
||||
}).catch(function () { delete coverPending[fk] })
|
||||
} catch (e) { delete coverPending[fk] }
|
||||
}
|
||||
return ''
|
||||
}
|
||||
var ck = (p.cover && typeof p.cover === 'object') ? ('b:' + String(p.cover.__blob || p.cover.__cache || 'obj')) : ('s:' + String(p.cover).slice(-32))
|
||||
var key = p.id + '|' + ck
|
||||
var hit = coverCache[key]
|
||||
@@ -467,19 +352,6 @@
|
||||
}
|
||||
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
|
||||
@@ -567,60 +439,6 @@
|
||||
return { close: close, card: card, body: bodyEl, foot: footEl }
|
||||
}
|
||||
|
||||
// ===== v1.5.0:变量编辑器(新建/编辑弹窗用)=====
|
||||
// 变量来自正文的 {名字}:自动检测、可改标签/默认值/说明;留空时渲染保留 {原名}。
|
||||
function makeVarsEditor (text, initial) {
|
||||
var box = el('div', { class: 'iwb-plp-vars' })
|
||||
var head = el('div', { class: 'iwb-plp-vhead' })
|
||||
head.appendChild(el('span', { text: '变量' }))
|
||||
head.appendChild(el('span', { class: 'iwb-plp-vhint', text: '从正文 {…} 自动检测,可改标签 / 默认值 / 说明' }))
|
||||
var chips = el('div', { class: 'iwb-plp-vchips' })
|
||||
var rows = el('div', { class: 'iwb-plp-vrows' })
|
||||
var prev = el('div', { class: 'iwb-plp-vprev' })
|
||||
box.appendChild(head); box.appendChild(chips); box.appendChild(rows); box.appendChild(prev)
|
||||
var list = []
|
||||
if (Array.isArray(initial) && initial.length) list = initial.map(function (v) { return { key: String(v.key || ''), label: String(v.label || v.key || ''), desc: String(v.desc || ''), def: String(v.def || '') } })
|
||||
function values () {
|
||||
var m = {}
|
||||
list.forEach(function (v) { if (v.def) m[v.key] = v.def })
|
||||
return m
|
||||
}
|
||||
function paint () {
|
||||
clear(chips); clear(rows)
|
||||
list.forEach(function (v) {
|
||||
var chip = el('span', { class: 'iwb-plp-vchip', text: v.label || v.key })
|
||||
var x = el('i', { class: 'iwb-plp-vx', text: '×', title: '忽略这个变量' })
|
||||
x.addEventListener('click', function () { list = list.filter(function (z) { return z.key !== v.key }); paint() })
|
||||
chip.appendChild(x)
|
||||
chips.appendChild(chip)
|
||||
var row = el('div', { class: 'iwb-plp-vrow' })
|
||||
row.appendChild(el('span', { class: 'iwb-plp-vk', text: '{' + v.key + '}' }))
|
||||
var def = el('input', { class: 'iwb-input iwb-plp-vin', placeholder: '默认值(可留空)' })
|
||||
def.value = v.def || ''
|
||||
def.addEventListener('input', function () { v.def = def.value; paintPrev() })
|
||||
var desc = el('input', { class: 'iwb-input iwb-plp-vin', placeholder: '说明(如:如「11.9」)' })
|
||||
desc.value = v.desc || ''
|
||||
desc.addEventListener('input', function () { v.desc = desc.value })
|
||||
row.appendChild(def); row.appendChild(desc)
|
||||
rows.appendChild(row)
|
||||
})
|
||||
if (!list.length) rows.appendChild(el('div', { class: 'iwb-hint', text: '正文里没有 {变量};写入 {产品名} 这样的大括号即会被识别。' }))
|
||||
paintPrev()
|
||||
}
|
||||
function paintPrev () { prev.textContent = fillTpl(text, values()) }
|
||||
function sync (nextText) {
|
||||
text = String(nextText || '')
|
||||
var found = detectVars(text)
|
||||
var old = {}
|
||||
list.forEach(function (v) { old[v.key] = v })
|
||||
list = found.map(function (v) { return old[v.key] ? Object.assign({}, v, old[v.key]) : v })
|
||||
paint()
|
||||
}
|
||||
if (!list.length) list = detectVars(text)
|
||||
paint()
|
||||
return { node: box, sync: sync, list: function () { return list }, values: values }
|
||||
}
|
||||
|
||||
function paintCoverInto (node, p) {
|
||||
node.textContent = ''
|
||||
var url = p && p._coverUrl ? p._coverUrl : coverOf(p)
|
||||
@@ -641,39 +459,11 @@
|
||||
paintCoverInto(cov, p)
|
||||
var right = el('div', { class: 'iwb-plp-dright' })
|
||||
right.appendChild(el('div', { class: 'iwb-hint', text: [p.group || '未分组'].concat(p.tags || []).join(' · ') + ' · ' + String(p.text || '').length + ' 字' }))
|
||||
var vlist = varsOf(p)
|
||||
var vals = {}
|
||||
var txt = el('div', { class: 'iwb-plp-dtext', text: p.text || '' })
|
||||
function filled () { return vlist.length ? fillTpl(p.text, vals) : String(p.text || '') }
|
||||
if (vlist.length) {
|
||||
var vbox = el('div', { class: 'iwb-plp-dvars' })
|
||||
vlist.forEach(function (v) {
|
||||
var row = el('div', { class: 'iwb-plp-vrow' })
|
||||
row.appendChild(el('span', { class: 'iwb-plp-vk', text: v.label || v.key }))
|
||||
var inp = el('input', { class: 'iwb-input iwb-plp-vin', placeholder: v.desc || v.def || '' })
|
||||
inp.value = v.def || ''
|
||||
inp.addEventListener('input', function () { vals[v.key] = inp.value; txt.textContent = filled() })
|
||||
row.appendChild(inp)
|
||||
vbox.appendChild(row)
|
||||
})
|
||||
right.appendChild(vbox)
|
||||
right.appendChild(el('div', { class: 'iwb-hint', text: '预览(留空的变量保留 {原名})' }))
|
||||
}
|
||||
right.appendChild(txt)
|
||||
bodyEl.appendChild(cov); bodyEl.appendChild(right)
|
||||
var del = el('button', { class: 'iwb-btn iwb-btn-sm iwb-btn-danger', text: '删除' })
|
||||
var cp = el('button', { class: 'iwb-btn iwb-btn-sm', text: '复制全文' })
|
||||
var insTpl = null
|
||||
if (ctx.nodes && ctx.canvas && typeof ctx.canvas.createNode === 'function') {
|
||||
insTpl = el('button', { class: 'iwb-btn iwb-btn-sm iwb-btn-primary', title: '插入可填变量的模板节点', text: '插入模板节点' })
|
||||
insTpl.addEventListener('click', function () {
|
||||
var id = ctx.canvas.createNode('ptpl', null, null, {
|
||||
promptId: p.id, name: p.name || '提示词模板', tpl: p.text || '', vdefs: vlist, vars: vals,
|
||||
cover: coverNameOf(p), refOn: false, text: filled(),
|
||||
})
|
||||
if (id) { notify('已插入模板节点'); dlg.close() } else notify('插入失败', 'err')
|
||||
})
|
||||
}
|
||||
var ins = el('button', { class: 'iwb-btn iwb-btn-sm', text: '插入画布' })
|
||||
del.addEventListener('click', function () {
|
||||
ask('删除提示词「' + (p.name || '未命名') + '」?', { title: '删除提示词', okText: '删除' }).then(function (ok) {
|
||||
@@ -682,22 +472,20 @@
|
||||
save(prompts.filter(function (x) { return x.id !== p.id })).then(function () { notify('已删除') })
|
||||
})
|
||||
})
|
||||
cp.addEventListener('click', function () { Promise.resolve(api.copy(filled())).then(function (r) { if (r && r.ok === false) notify('复制失败', 'err'); else notify('已复制全文') }) })
|
||||
ins.addEventListener('click', function () { Promise.resolve(api.insert(filled(), p.name || '')).then(function () { notify('已插入画布:' + (p.name || '提示词')); dlg.close() }).catch(function () { notify('插入画布失败', 'err') }) })
|
||||
cp.addEventListener('click', function () { Promise.resolve(api.copy(p.text || '')).then(function (r) { if (r && r.ok === false) notify('复制失败', 'err'); else notify('已复制全文') }) })
|
||||
ins.addEventListener('click', function () { Promise.resolve(api.insert(p.text || '', p.name || '')).then(function () { notify('已插入画布:' + (p.name || '提示词')); dlg.close() }).catch(function () { notify('插入画布失败', 'err') }) })
|
||||
var insImg = null
|
||||
if (p.cover) {
|
||||
insImg = el('button', { class: 'iwb-btn iwb-btn-sm', title: '把这条提示词的封面作为参考图插入画布', text: '插入图片' })
|
||||
insImg.addEventListener('click', function () {
|
||||
var covName = coverNameOf(p)
|
||||
var covData = (covName && FILEAPI) ? Promise.resolve(FILEAPI.get(covName)).then(function (x) { return x && x.ok ? x.url : '' }).catch(function () { return '' }) : Promise.resolve(typeof p.cover === 'string' ? p.cover : '')
|
||||
covData.then(function (u) { return api.insertImage(u, p.name || '') }).then(function (r) {
|
||||
Promise.resolve(api.insertImage(p.cover, p.name || '')).then(function (r) {
|
||||
if (r && r.ok === false) { notify((r && r.error) || '插入封面失败', 'err'); return }
|
||||
notify('已插入封面图片')
|
||||
dlg.close()
|
||||
}).catch(function () { notify('插入封面失败', 'err') })
|
||||
})
|
||||
}
|
||||
footEl.appendChild(del); footEl.appendChild(el('span', { class: 'iwb-drawer-spacer' })); if (insTpl) footEl.appendChild(insTpl); footEl.appendChild(cp)
|
||||
footEl.appendChild(del); footEl.appendChild(el('span', { class: 'iwb-drawer-spacer' })); footEl.appendChild(cp)
|
||||
if (insImg) footEl.appendChild(insImg)
|
||||
footEl.appendChild(ins)
|
||||
},
|
||||
@@ -723,13 +511,7 @@
|
||||
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 })
|
||||
resolveCoverUrl(draft.cover).then(function (u) {
|
||||
if (u) paintCoverInto(cov, { id: draft.id, cover: draft.cover, group: draft.group, _coverUrl: u })
|
||||
})
|
||||
}
|
||||
repaint()
|
||||
function repaint () { paintCoverInto(cov, { id: draft.id, cover: draft.cover, group: draft.group }) }
|
||||
upBtn.addEventListener('click', function () { try { fileIn.click() } catch (e) { /* ignore */ } })
|
||||
fileIn.addEventListener('change', function () {
|
||||
var f = fileIn.files && fileIn.files[0]
|
||||
@@ -763,17 +545,8 @@
|
||||
left.appendChild(el('label', { class: 'iwb-plp-lrow' }, el('span', { text: '标签' }), tagsIn))
|
||||
var textIn = el('textarea', { class: 'iwb-input iwb-plp-etext', placeholder: '提示词正文' })
|
||||
textIn.value = draft.text || ''
|
||||
var varEd = makeVarsEditor(draft.text || '', draft.vars || [])
|
||||
var syncTimer = 0
|
||||
textIn.addEventListener('input', function () {
|
||||
dirty = true
|
||||
if (syncTimer) clearTimeout(syncTimer)
|
||||
syncTimer = setTimeout(function () { varEd.sync(textIn.value) }, 200)
|
||||
})
|
||||
var mid = el('div', { class: 'iwb-plp-emid' })
|
||||
mid.appendChild(textIn)
|
||||
mid.appendChild(varEd.node)
|
||||
bodyEl.appendChild(left); bodyEl.appendChild(mid)
|
||||
textIn.addEventListener('input', function () { dirty = true })
|
||||
bodyEl.appendChild(left); bodyEl.appendChild(textIn)
|
||||
var dupBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '另存副本' })
|
||||
var insBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '插入画布' })
|
||||
footEl.appendChild(dupBtn); footEl.appendChild(el('span', { class: 'iwb-drawer-spacer' })); footEl.appendChild(insBtn)
|
||||
@@ -785,7 +558,6 @@
|
||||
tags: String(tagsIn.value || '').split(',').map(function (s) { return s.trim() }).filter(Boolean),
|
||||
text: textIn.value,
|
||||
cover: draft.cover || '',
|
||||
vars: varEd.list().filter(function (v) { return v && v.key }),
|
||||
}
|
||||
}
|
||||
function doSave () {
|
||||
@@ -793,7 +565,7 @@
|
||||
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 item = { id: c.id, name: name, text: text, tags: c.tags, group: String(c.group || '').trim(), cover: c.cover }
|
||||
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))
|
||||
@@ -825,7 +597,7 @@
|
||||
var top = el('div', { class: 'iwb-row' })
|
||||
var search = el('input', { class: 'iwb-input iwb-grow', placeholder: '搜索名称 / 内容 / 标签…', value: query })
|
||||
search.addEventListener('input', function () { query = search.value; render(); var n = wrap.querySelector('input.iwb-input'); if (n) { n.focus(); try { n.setSelectionRange(n.value.length, n.value.length) } catch (e) { /* ignore */ } } })
|
||||
var newBtn = el('button', { class: 'iwb-btn iwb-btn-sm iwb-btn-primary', title: '新建提示词', text: '+ 新建提示词' })
|
||||
var newBtn = el('button', { class: 'iwb-btn iwb-btn-sm', title: '新建提示词', text: '+ 新建' })
|
||||
newBtn.addEventListener('click', function () { openEditModal(null, true) })
|
||||
top.appendChild(search); top.appendChild(newBtn)
|
||||
fixed.appendChild(top)
|
||||
@@ -904,8 +676,6 @@
|
||||
var card = el('div', { class: 'iwb-plp-card' + (p.cover ? ' has-cover' : ' no-cover'), draggable: 'true', title: '单击查看详情 · 拖到画布插入 · 右键移动到分组' })
|
||||
card.style.background = coverUrl ? 'var(--iwb-plugin-inset)' : gradOf(p)
|
||||
if (coverUrl) card.appendChild(el('img', { class: 'iwb-plp-cover', src: coverUrl, alt: '', draggable: 'false' }))
|
||||
// 紫色小点:表示这条是带变量的模板(不喧宾夺主,鼠标悬停看数量)
|
||||
if (varsOf(p).length) card.appendChild(el('i', { class: 'iwb-plp-vdot', title: varsOf(p).length + ' 个变量' }))
|
||||
card.appendChild(el('div', { class: 'iwb-plp-t', text: p.name || '未命名' }))
|
||||
card.addEventListener('dragstart', function (e) {
|
||||
try {
|
||||
@@ -951,56 +721,11 @@
|
||||
global.IWBPluginHost.register({
|
||||
id: 'promptlib',
|
||||
name: '提示词库',
|
||||
version: '1.5.0',
|
||||
version: '1.4.10',
|
||||
apiVersion: 1,
|
||||
capabilities: ['panels', 'promptlib', 'nodeTypes', 'canvasWrite'],
|
||||
data: { schemaVersion: 3, maxBytes: 4 * 1024 * 1024 },
|
||||
capabilities: ['panels', 'promptlib'],
|
||||
data: { schemaVersion: 1, maxBytes: 65536 },
|
||||
}, function (ctx) {
|
||||
// v1.5.0:插件激活时就把旧数据迁到插件目录(不等面板打开),
|
||||
// 这样即使用户从不打开面板,数据也已经是插件自持的。
|
||||
// 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
|
||||
}
|
||||
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: '提示词库',
|
||||
@@ -1008,112 +733,6 @@
|
||||
// 宿主只调用 mount(node)(一个参数):必须在这里注入工厂 ctx,否则面板里 ctx 是 undefined ⇒ 空白
|
||||
mount: function (host) { return mount(host, ctx) },
|
||||
})
|
||||
|
||||
// ===== v1.5.0:提示词模板节点(与风格节点同一套机制)=====
|
||||
// 只出不入:被连入边会让上游文本顶掉模板;变量填完由下游生图节点取 data.text。
|
||||
function renderTplNode (node, props, host) {
|
||||
var st = host.__plp
|
||||
if (!st) { host.textContent = ''; st = host.__plp = buildTplSkeleton(host) }
|
||||
st.node = node
|
||||
st.props = props
|
||||
syncTplNode(st, (node && node.data) || {})
|
||||
}
|
||||
function buildTplSkeleton (host) {
|
||||
var root = el('div', { class: 'iwb-plp-node' })
|
||||
var head = el('div', { class: 'iwb-plp-nhead' })
|
||||
var thumb = el('img', { class: 'iwb-plp-nthumb', alt: '' })
|
||||
thumb.style.display = 'none'
|
||||
var title = el('div', { class: 'iwb-plp-ntitle' })
|
||||
head.appendChild(thumb); head.appendChild(title)
|
||||
var vars = el('div', { class: 'iwb-plp-nvars' })
|
||||
var refRow = el('label', { class: 'iwb-plp-nref' })
|
||||
var refChk = el('input', { type: 'checkbox' })
|
||||
var refTxt = el('span', { text: '封面作参考图' })
|
||||
var refNum = el('em', { text: '0' })
|
||||
refRow.appendChild(refChk); refRow.appendChild(refTxt); refRow.appendChild(refNum)
|
||||
var prev = el('div', { class: 'iwb-plp-nprev' })
|
||||
root.appendChild(head); root.appendChild(vars); root.appendChild(refRow); root.appendChild(prev)
|
||||
host.appendChild(root)
|
||||
return { root: root, thumb: thumb, title: title, vars: vars, refChk: refChk, refNum: refNum, prev: prev, inputs: {}, key: '' }
|
||||
}
|
||||
function syncTplNode (st, d) {
|
||||
var name = String(d.name || '提示词模板')
|
||||
if (st.title.textContent !== name) st.title.textContent = name
|
||||
var tpl = String(d.tpl || '')
|
||||
var defs = Array.isArray(d.vdefs) && d.vdefs.length ? d.vdefs : (function () {
|
||||
var out = [], seen = {}, re = /\{([^{}\n]{1,24})\}/g, m
|
||||
while ((m = re.exec(tpl))) {
|
||||
var k = String(m[1] || '').trim()
|
||||
if (!k || seen[k]) continue
|
||||
seen[k] = 1
|
||||
out.push({ key: k, label: k, def: '' })
|
||||
}
|
||||
return out
|
||||
})()
|
||||
var key = tpl + '|' + defs.map(function (v) { return v.key }).join(',')
|
||||
if (st.key !== key) {
|
||||
st.key = key
|
||||
st.inputs = {}
|
||||
clear(st.vars)
|
||||
defs.forEach(function (v) {
|
||||
var row = el('div', { class: 'iwb-plp-nrow' })
|
||||
row.appendChild(el('span', { class: 'iwb-plp-nk', text: v.label || v.key }))
|
||||
var inp = el('input', { class: 'iwb-input iwb-plp-nin', placeholder: v.def || '' })
|
||||
inp.value = String((d.vars && d.vars[v.key]) || v.def || '')
|
||||
inp.addEventListener('input', function () { scheduleUpdate(st, d) })
|
||||
st.inputs[v.key] = inp
|
||||
row.appendChild(inp)
|
||||
st.vars.appendChild(row)
|
||||
})
|
||||
}
|
||||
// 缩略图:封面存插件文件,按名字读一次
|
||||
var cover = String(d.cover || '')
|
||||
if (cover && ctx.files && st.thumbSrc !== cover) {
|
||||
st.thumbSrc = cover
|
||||
Promise.resolve(ctx.files.get(cover)).then(function (r) {
|
||||
if (r && r.ok && r.url) { st.thumb.src = r.url; st.thumb.style.display = '' }
|
||||
else st.thumb.style.display = 'none'
|
||||
}).catch(function () { st.thumb.style.display = 'none' })
|
||||
}
|
||||
if (!cover) st.thumb.style.display = 'none'
|
||||
if (!st.refBound) {
|
||||
st.refBound = true
|
||||
st.refChk.addEventListener('change', function () {
|
||||
var on = Boolean(st.refChk.checked)
|
||||
var cover = String((st.node && st.node.data && st.node.data.cover) || '')
|
||||
var done = function (url) {
|
||||
try { st.props.onUpdate(st.node.id, { refOn: on, refUrl: on ? String(url || '') : '' }) } catch (e) { /* ignore */ }
|
||||
st.refNum.textContent = on ? '1 张' : '0'
|
||||
}
|
||||
if (!on || !cover || !ctx.files) { done(''); return }
|
||||
Promise.resolve(ctx.files.get(cover)).then(function (r) { done(r && r.ok ? r.url : '') }).catch(function () { done('') })
|
||||
})
|
||||
}
|
||||
var refOn = d.refOn === true
|
||||
if (st.refChk.checked !== refOn) st.refChk.checked = refOn
|
||||
st.refNum.textContent = refOn ? '1 张' : '0'
|
||||
var vals = {}
|
||||
Object.keys(st.inputs).forEach(function (k) { vals[k] = st.inputs[k].value })
|
||||
var text = fillTpl(tpl, vals)
|
||||
st.prev.textContent = text
|
||||
}
|
||||
function scheduleUpdate (st, d) {
|
||||
if (st.timer) clearTimeout(st.timer)
|
||||
st.timer = setTimeout(function () {
|
||||
var vals = {}
|
||||
Object.keys(st.inputs).forEach(function (k) { vals[k] = st.inputs[k].value })
|
||||
var patch = { vars: vals, text: fillTpl(String(d.tpl || ''), vals) }
|
||||
try { st.props.onUpdate(st.node.id, patch) } catch (e) { /* ignore */ }
|
||||
}, 300)
|
||||
}
|
||||
if (ctx.nodes && typeof ctx.nodes.register === 'function') {
|
||||
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 {}
|
||||
})
|
||||
})(typeof window !== 'undefined' ? window : globalThis)
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"id": "promptlib",
|
||||
"name": "提示词库",
|
||||
"version": "1.5.0",
|
||||
"version": "1.4.10",
|
||||
"apiVersion": 1,
|
||||
"main": "main.js",
|
||||
"style": "styles.css",
|
||||
@@ -13,7 +13,7 @@
|
||||
"提示词"
|
||||
],
|
||||
"files": {
|
||||
"main.js": "c3e175b2749fafc4d9daf42ab722bcbb9db7f09951e7000b7fa6241598edb386",
|
||||
"styles.css": "48579039ffb5ddb610f0b17100b281e8f3c55e7485fc9ba9ab115d88399af900"
|
||||
"main.js": "75dc31dc6270aa10e73c90eeb09bb349a949d17c39940574e28f12c5e463ceaf",
|
||||
"styles.css": "cc71e071379c84131e525afeaa5757674f499da195ccb42536c019a3ef5d8473"
|
||||
}
|
||||
}
|
||||
@@ -137,32 +137,3 @@
|
||||
.iwb-promptlib .iwb-plp-card { display: inline-block; width: 100%; margin: 0 0 8px; break-inside: avoid; -webkit-column-break-inside: avoid; aspect-ratio: auto; }
|
||||
.iwb-promptlib .iwb-plp-cover { width: 100%; height: auto; object-fit: contain; }
|
||||
.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: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}
|
||||
.iwb-plp-ntitle{font-weight:600;font-size:12.5px;line-height:1.3;overflow:hidden;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical}
|
||||
.iwb-plp-nvars{display:flex;flex-direction:column;gap:4px}
|
||||
.iwb-plp-nrow{display:flex;align-items:center;gap:6px;height:26px}
|
||||
.iwb-plp-nk{width:64px;flex:0 0 64px;color:var(--iwb-text2,#5b6472);font-size:11.5px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
||||
.iwb-plp-nin{flex:1;height:24px;font-size:11.5px}
|
||||
.iwb-plp-nref{display:flex;align-items:center;gap:6px;font-size:11.5px;color:var(--iwb-text2,#5b6472);border-top:1px dashed var(--iwb-border,#e3e5e9);padding-top:6px}
|
||||
.iwb-plp-nref em{font-style:normal;color:var(--iwb-text2,#8b929c)}
|
||||
.iwb-plp-nprev{margin-top:2px;border-top:1px dashed var(--iwb-border,#e3e5e9);padding-top:6px;color:var(--iwb-text2,#5b6472);font-size:11.5px;line-height:1.6;max-height:88px;overflow:hidden}
|
||||
|
||||
/* v1.5.0:变量编辑器 / 详情变量 */
|
||||
.iwb-plp-emid{flex:1;min-width:0;display:flex;flex-direction:column;gap:8px}
|
||||
.iwb-plp-vars{border:1px solid var(--iwb-border,#e3e5e9);border-radius:10px;padding:8px 10px;display:flex;flex-direction:column;gap:6px;background:var(--iwb-plugin-surface,#fff)}
|
||||
.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: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}
|
||||
.iwb-plp-vin{flex:1;height:26px;font-size:11.5px}
|
||||
.iwb-plp-vprev{margin-top:2px;border-top:1px dashed var(--iwb-border,#e3e5e9);padding-top:6px;font-size:11.5px;line-height:1.6;color:var(--iwb-text2,#5b6472);max-height:72px;overflow:auto;white-space:pre-wrap}
|
||||
.iwb-plp-dvars{display:flex;flex-direction:column;gap:5px;border:1px solid var(--iwb-border,#e3e5e9);border-radius:10px;padding:8px 10px}
|
||||
@@ -39,6 +39,14 @@
|
||||
}
|
||||
}
|
||||
|
||||
function fileOp (name) {
|
||||
return function () {
|
||||
const F = (typeof global !== 'undefined' && global.IWBStorageFile) ? global.IWBStorageFile.pluginFiles : null
|
||||
if (!F || typeof F[name] !== 'function') return Promise.reject(new Error('文件能力未就绪'))
|
||||
const args = Array.prototype.slice.call(arguments)
|
||||
try { return Promise.resolve(F[name].apply(F, ['promptlib'].concat(args))) } catch (e) { return Promise.reject(e) }
|
||||
}
|
||||
}
|
||||
const API = {
|
||||
list: call('list'),
|
||||
save: call('save'),
|
||||
@@ -50,11 +58,12 @@
|
||||
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'),
|
||||
// 文件沙箱(v3.26.36):直接调宿主 pluginFiles,不占 app.js 行数;插件 id 固定 promptlib。
|
||||
filePut: fileOp('put'),
|
||||
fileGet: fileOp('get'),
|
||||
fileList: fileOp('list'),
|
||||
fileRemove: fileOp('remove'),
|
||||
fileStat: fileOp('stat'),
|
||||
onChanged: function (fn) {
|
||||
if (typeof fn !== 'function') return function () {}
|
||||
listeners.push(fn)
|
||||
|
||||
Reference in new issue
Block a user