/* plugin-api-grouplib.js —— 宿主 grouplib capability(M7:节点组库插件化) * * 给插件「收藏的节点组 + 节点的分组」的读写面: * list() → { groups, groupGroups } * save({groups, groupGroups}) → { ok } * remove(id) → { ok } * instantiate(id) → 在画布视口中心实例化(插件不碰画布状态) * notify(text, type) → 走宿主 toast/提示条 * * 与 gallery 同一套「impl 桥」:capability 在 PluginHost.init 时创建(早于 Workbench 渲染), * 因此这里只持有稳定代理,真实实现由 Workbench 每次渲染时 setImpl 注入。 * 同一份数据仍由宿主持有(iwb_web_groups / iwb_web_groupgroups),插件删了数据不丢。 */ ;(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) } } } // 数据变了下发一次(收藏节点组、其它标签页同步、插件自己 save 都会走到) function emit (reason) { for (const fn of listeners.slice()) { try { fn(reason || 'changed') } catch (e) { /* 插件回调异常不影响宿主 */ } } } const API = { list: call('list'), save: call('save'), remove: call('remove'), instantiate: call('instantiate'), notify: call('notify'), 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) } }, } global.IWBPluginGroupLib = { setImpl: setImpl, api: function () { return API }, emit: emit } })(typeof window !== 'undefined' ? window : globalThis)