92 lines
3.9 KiB
Markdown
92 lines
3.9 KiB
Markdown
# 插件 SDK(P3)
|
||
|
||
> 桌面版插件**全部从市场安装**,不再随应用发布。本文写给"想自己写插件"的人。
|
||
|
||
## 1. 两种插件来源
|
||
|
||
| 来源 | 位置 | 要求 |
|
||
|---|---|---|
|
||
| 市场(官方/社区) | `<资料目录>/plugins/*.js` + `plugins/index.json` | 清单带 `integrity`(sha256),加载时校验 |
|
||
| 本地开发(开发者模式) | `<资料目录>/plugins/local/<id>/plugin.js` | **不校验签名**,需在设置里打开「开发者模式」 |
|
||
|
||
资料目录默认 `~/Documents/ImageWorkbench`(macOS)。
|
||
|
||
## 2. 最小插件
|
||
|
||
一个文件即可,格式为 IIFE,向 `window.IWBPluginHost` 注册:
|
||
|
||
```js
|
||
;(function (global) {
|
||
'use strict'
|
||
global.IWBPluginHost.register({
|
||
id: 'hello-world', // 唯一 id(小写字母/数字/连字符)
|
||
name: '示例插件',
|
||
version: '0.1.0',
|
||
apiVersion: 1, // 宿主 API 版本,当前为 1
|
||
capabilities: ['nodeActions', 'canvas'],
|
||
}, function (ctx) {
|
||
ctx.nodeActions.register({
|
||
id: 'hello-world-count',
|
||
title: '示例:数一下节点',
|
||
icon: 'sparkles',
|
||
appliesTo: function (node) { return !!(node && node.type === 'ref') },
|
||
run: function () { console.log('节点数', ctx.canvas.getNodes().length); return true },
|
||
})
|
||
return { dispose: function () {} }
|
||
})
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|
||
```
|
||
|
||
完整可运行版本见仓库 `examples/plugin-hello/plugin.js`。
|
||
|
||
## 3. 能力(capabilities)
|
||
|
||
| 能力 | 作用 |
|
||
|---|---|
|
||
| `nodeActions` | 给节点右键/工具条加自定义动作 |
|
||
| `canvas` | 只读访问画布(`getNodes` 等) |
|
||
| `canvasWrite` | 写画布(`setNodeImage` 等),**必须同时声明** |
|
||
| `events` | 订阅宿主事件 |
|
||
| `storage` | 插件私有持久化(按插件 id 隔离) |
|
||
|
||
未声明的能力在 `ctx` 上是 undefined,调用会抛错。
|
||
|
||
## 4. 本地开发流程
|
||
|
||
1. 建目录:`<资料目录>/plugins/local/<你的插件 id>/plugin.js`
|
||
2. 设置 → 插件 → 勾选「**开发者模式**」
|
||
3. 重启应用(插件在启动时加载)
|
||
4. 改完代码后再重启一次即可重新加载
|
||
|
||
> 开发者模式关闭时,`plugins/local/` 下的插件**不会被加载**(避免误执行未签名代码)。
|
||
|
||
## 5. 发布到市场
|
||
|
||
1. 把插件文件放进插件源仓 `iwb-plugins-desktop/<id>/src/index.js`
|
||
2. 在桌面仓跑同步:`node scripts/sync-plugins-from-repo.js`
|
||
3. 生成市场:`node scripts/gen-market.js`
|
||
4. 把 `market/` 上传到 NAS `/vol8/web/iwb-market/`(`market.maqiart.eu.org` 的站点根目录)
|
||
|
||
```bash
|
||
# 只有索引和新增的插件文件会变;index.html 是手写页面,没改动就不用传
|
||
ssh [email protected] "rm -f /vol8/web/iwb-market/index.json"
|
||
scp market/index.json [email protected]:/vol8/web/iwb-market/index.json
|
||
scp market/plugins/plugin-<id>.<sha12>.js [email protected]:/vol8/web/iwb-market/plugins/
|
||
ssh [email protected] "chmod -R a+rX /vol8/web/iwb-market"
|
||
|
||
# 回读核验(不能只看命令退出码):索引条数 + 下载字节 sha256 与索引一致
|
||
curl -s "https://market.maqiart.eu.org/index.json?t=$(date +%s)" | head -c 200
|
||
```
|
||
|
||
> 上传前先 `ls /vol8/web/iwb-market/plugins` 对比本地:内容寻址命名下,同名文件必然同内容,只传新增的那个即可。
|
||
> NAS 上的文件是只读位(`-r-xr-xr-x`),覆盖前要先 `rm`,否则 `scp` 会报 Permission denied。
|
||
> **客户端二进制不含新命令时**(旧版本安装包):`open_browser_window` 等命令 invoke 会失败,
|
||
> 插件降级 `window.open` —— 表现为"点了跳系统浏览器"。新增宿主命令必须重新打包客户端才生效。
|
||
|
||
## 6. 注意事项
|
||
|
||
- **不要用 `alert` / `confirm` / `prompt`**:桌面端会阻塞 WebView(宿主已全部改页内确认)
|
||
- 插件与宿主同源,能读到画布数据 ⇒ 只装可信插件
|
||
- 插件 id 冲突会被拒绝注册(先注册者生效)
|
||
- `dispose` 里清理你自己加的 DOM/定时器/监听器
|