Files
image-workbench-desktop/src/plugin-api-promptlib.js
T
qiqi 0b20c5597c
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 2m33s
feat(promptlib): 详情插入图片(封面参考图节点)(v3.26.1 / 1.3.2)
2026-11-21 21:42:30 +08:00

71 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* plugin-api-promptlib.js —— 宿主 promptlib capability(M6:提示词库插件化)
*
* 给插件「提示词 + 提示词分组」的读写面:
* list() → { prompts, groups }
* save({prompts, groups}) → 落盘(宿主仍持有数据:iwb_web_prompts + 分组缓存)
* insert(text, name) → 在画布视口中心插入提示词节点(插件不碰画布状态)
* copy(text) → 走宿主剪贴板(失败有可见提示)
* insertImage(cover, name) → 把封面作为参考图节点插入画布
* notify(text, type) → 宿主 toast / 提示条
* putCover(dataUrl) → 封面入宿主图片库(返回 ref)
* coverUrl(ref) → 封面展示地址
* removeCover(ref) → 解引用(图片由 GC 回收)
* onChanged(fn) → 数据变化(收藏/其它标签页同步/插件自己保存)
*
* 与 gallery / grouplib 同一套「impl 桥」:capability 在 PluginHost.init 时创建(早于 Workbench 渲染),
* 这里只持有稳定代理,真实实现由 Workbench 每次渲染时 setImpl 注入。
*/
;(function (global) {
'use strict'
let impl = null
const listeners = []
function setImpl (next) {
impl = (next && typeof next === 'object') ? next : null
}
function call (name) {
return function () {
const fn = impl && impl[name]
if (typeof fn !== 'function') return Promise.reject(new Error('提示词能力未就绪'))
try { return Promise.resolve(fn.apply(null, arguments)) } catch (e) { return Promise.reject(e) }
}
}
function emit (reason) {
for (const fn of listeners.slice()) {
try { fn(reason || 'changed') } catch (e) { /* 插件回调异常不影响宿主 */ }
}
}
const API = {
list: call('list'),
save: call('save'),
insert: call('insert'),
copy: call('copy'),
insertImage: call('insertImage'),
notify: call('notify'),
putCover: call('putCover'),
coverUrl: call('coverUrl'),
removeCover: call('removeCover'),
onChanged: function (fn) {
if (typeof fn !== 'function') return function () {}
listeners.push(fn)
return function () { const i = listeners.indexOf(fn); if (i >= 0) listeners.splice(i, 1) }
},
}
// 纯函数:旧数据/缺分组时从 prompts 推导分组(与宿主 promptGroupsOf 一致)
function groupsOf (prompts, existing) {
if (Array.isArray(existing) && existing.length) return existing
const seen = []
for (const p of (Array.isArray(prompts) ? prompts : [])) {
const g = String((p && p.group) || '').trim()
if (g && !seen.some((x) => x.name === g)) seen.push({ id: g, name: g })
}
return seen
}
global.IWBPluginPromptLib = { setImpl: setImpl, api: function () { return API }, emit: emit, groupsOf: groupsOf }
})(typeof window !== 'undefined' ? window : globalThis)