41 lines
1.5 KiB
JavaScript
41 lines
1.5 KiB
JavaScript
// 示例插件(P3 SDK 模板):把本文件放到
|
|
// <资料目录>/plugins/local/hello-world/plugin.js
|
|
// 然后在 设置 → 插件 → 打开「开发者模式」,重启应用即可生效。
|
|
//
|
|
// 契约:调用 window.IWBPluginHost.register(manifest, factory)
|
|
// manifest: { id, name, version, apiVersion, capabilities }
|
|
// factory(ctx): 注册入口;返回 { dispose } 供停用/卸载时清理
|
|
;(function (global) {
|
|
'use strict'
|
|
|
|
global.IWBPluginHost.register({
|
|
id: 'hello-world',
|
|
name: '示例插件',
|
|
version: '0.1.0',
|
|
apiVersion: 1,
|
|
capabilities: ['nodeActions', 'canvas'],
|
|
}, function (ctx) {
|
|
// 读取当前画布节点(只读)
|
|
function countNodes () {
|
|
try {
|
|
return (ctx.canvas && typeof ctx.canvas.getNodes === 'function') ? ctx.canvas.getNodes().length : 0
|
|
} catch (e) { return -1 }
|
|
}
|
|
|
|
// 注册一个节点右键动作:只在参考图/输出节点上出现
|
|
ctx.nodeActions.register({
|
|
id: 'hello-world-count',
|
|
title: '示例:数一下节点',
|
|
icon: 'sparkles',
|
|
appliesTo: function (node) { return !!(node && (node.type === 'ref' || node.type === 'out')) },
|
|
run: function () {
|
|
// 注意:桌面端不要用 alert/confirm(会阻塞 WebView),用 console 或宿主提供的 UI
|
|
console.log('[hello-world] 当前画布节点数:' + countNodes())
|
|
return true
|
|
},
|
|
})
|
|
|
|
return { dispose: function () { /* 清理定时器/监听器 */ } }
|
|
})
|
|
})(typeof window !== 'undefined' ? window : globalThis)
|