From 3483cc41749214e2162f4b4aad2a77c8e0d64d6e Mon Sep 17 00:00:00 2001 From: qiqi Date: Sun, 22 Nov 2026 21:56:03 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=8F=92=E4=BB=B6=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E8=A7=84=E8=8C=83=EF=BC=88=E7=BB=93=E6=9E=84/=E8=83=BD?= =?UTF-8?q?=E5=8A=9B/=E8=8A=82=E7=82=B9/=E8=AE=BE=E7=BD=AE=E9=A1=B5/?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F/=E6=B5=8B=E8=AF=95/=E5=8F=91=E5=B8=83?= =?UTF-8?q?=EF=BC=89+=20=E5=AE=98=E7=BD=91=20/dev/=20=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/standards/plugin-development.md | 166 +++++++++++++++++++++++ scripts/gen-release-page.js | 1 + scripts/gen-site.js | 76 +++++++++++ site/_dev.tpl.html | 30 +++++ site/dev/index.html | 194 +++++++++++++++++++++++++++ site/download/index.html | 1 + site/index.html | 5 +- site/plugins/index.html | 3 +- 8 files changed, 473 insertions(+), 3 deletions(-) create mode 100644 docs/standards/plugin-development.md create mode 100644 site/_dev.tpl.html create mode 100644 site/dev/index.html diff --git a/docs/standards/plugin-development.md b/docs/standards/plugin-development.md new file mode 100644 index 0000000..081681d --- /dev/null +++ b/docs/standards/plugin-development.md @@ -0,0 +1,166 @@ +# Image Workbench 插件开发规范 + +> 版本:v1(2026-11-22)。面向官方与第三方插件;示例以 promptlib(提示词库)与 style-source(风格库)为准。 +> 相关文档:plugin-node-drag.md(节点拖入/指针交互踩坑)、plugin-node-outkind.md(下游连线声明)、plugin-theme.md(主题契约)。 + +## 1. 目录与基本结构 + +插件是「一个目录 + 一个 IIFE 入口」,由宿主在启用时动态加载并调用 register。 + + my-plugin/ + package.json # 版本 + ibwPlugin 元数据 + src/index.js # 入口(IIFE,末尾 IWBPluginHost.register) + styles.css # 样式(必须自包含) + README.md + +package.json: + + { + "name": "@iwb-plugin/my-plugin", + "version": "1.0.0", + "ibwPlugin": { "id": "my-plugin", "entry": "src/index.js", "apiVersion": 1 } + } + +入口骨架: + + ;(function (global) { + 'use strict' + if (!global.IWBPluginHost) return + global.IWBPluginHost.register({ + id: 'my-plugin', + name: '我的插件', + version: '1.0.0', + capabilities: ['panels', 'canvas', 'canvasWrite'], + settings: { panel: { type: 'custom', label: '设置', render: renderSettings } }, + mount: mount, + unmount: unmount, + }, function (ctx) { + // setup:注册面板/节点/命令 + return { dispose () {} } + }) + })(typeof window !== 'undefined' ? window : globalThis) + +## 2. 能力(capabilities)与 ctx 映射 + +只申请用到的能力;能力名与 ctx 键不同: + +| capability | ctx 键 | 用途 | +|---|---|---| +| panels | ctx.ui | 侧栏/独立面板注册 | +| promptlib | ctx.promptlib | 提示词库数据 + 文件 + 备份 | +| nodeTypes | ctx.nodes | 画布节点类型注册 | +| canvas | ctx.canvas | 只读:getNodes / getSelectedNode / screenToWorld | +| canvasWrite | ctx.canvasWrite | 写入:createNode / createImageNode / setNodeImage | +| nodeActions | ctx.nodeActions | 节点右键/气泡动作 | +| commands | ctx.commands | 命令面板 | +| settings | ctx.settings | KV 配置 | +| data | ctx.data | 插件数据文件 | + +**注意**:createNode 在 ctx.canvasWrite,不在 ctx.canvas。调错只会提示“宿主不支持”。 + +## 3. 面板 + + ctx.ui.registerPanel({ + id: 'my-panel', + title: '我的插件', + mount (host) { /* 渲染到 host */ }, + unmount (host) { /* 清理定时器/监听 */ } + }) + +- mount 只调用一次,内部自己管理重渲染。 +- 面板宽度可变:使用栅格/瀑布流,不要写死 px 宽度。 +- 面板内所有 DOM 事件在 unmount 时移除。 + +## 4. 节点类型 + + ctx.nodes.register({ + id: 'tpl', + title: '模板节点', + defaults: { outKind: 'prompt' }, + ports: { in: false, out: true }, + height: 360, + render (node, props, host) { /* 首次建 DOM,之后 sync */ } + }) + +宿主会生成类型 plugin:<插件id>:<变体id>。 + +**必须遵守**: + +1. 输出文本/提示词 → data.outKind = 'prompt'(注册默认值、拖入数据、旧节点在 render 里自愈)。 +2. 拖入画布用指针拖拽,不用原生 HTML5 DnD: + + pointerdown → pointermove(>5px) 幽灵卡片 + → pointerup → ctx.canvas.screenToWorld(x,y) + → ctx.canvasWrite.createNode('tpl', wx, wy, data) + +3. 输入框:未选中时 pointer-events: none,选中后才可编辑;只读 textarea 同样处理。 +4. 单击 = 选中/拖动,双击 = 编辑;不要用单击打开详情。 +5. 节点 render 必须可增量 sync,避免整块重建(输入框会丢焦点)。 + +## 5. 数据与文件 + +- 小数据:ctx.promptlib(或 ctx.data)JSON,单文件 < 1MB。 +- 大文件(封面/图片):ctx.promptlib.filePut/fileGet/fileList,落地在 plugin-data//files/。 +- dataUrlToBytes() 返回 { mime, bytes },不是 Uint8Array。 +- 备份:提供导出/导入 JSON;导入按 id 合并,提供“覆盖同 id”选项。 +- 迁移:旧格式在 setup 里做一次性迁移,失败要提示而不是静默丢数据。 + +## 6. 设置页面 + +宿主设置页内嵌渲染,不弹独立页面: + + settings: { + panel: { type: 'custom', label: '分组与备份', render: renderSettings } + } + +render 返回 DOM;宿主负责挂载/卸载。设置页适合放: + +- 分组管理(新建/改名/排序/删除) +- 数据备份(导出/导入) +- 数据位置(打开数据目录) +- 插件级开关/默认值 + +## 7. 样式规范 + +1. **自包含**:宿主基础样式会变;用到的宿主类按作用域复制一份(.iwb-myplugin …)。 +2. 统一前缀:面板/节点类名 my-plugin-*,避免与宿主 .iwb-* 冲突。 +3. 颜色走 CSS 变量:var(--iwb-text)、--iwb-panel、--iwb-border、--iwb-plugin-accent。 +4. 动画克制:只做 hover/press 反馈;不使用持续浮动动画。 +5. 深色主题:不写死 #fff/#000;主题契约测试会扫描。 +6. 悬停放大只缩放内层背景,不缩放卡片本体(避免命中区域漂移/闪烁)。 +7. 图片/封面 pointer-events: none,点击永远落到卡片。 + +## 8. 错误处理与提示 + +- 所有用户可见错误用 ctx.promptlib.notify / ctx.ui.toast,文案给“下一步怎么办”。 +- 异步链必须有 catch;不要吞掉错误后静默。 +- 崩溃日志:window.onerror 由宿主统一采集,插件不要自己覆盖。 +- 定时器/监听在 unmount/dispose 清理,避免面板切换后重复执行。 + +## 9. 测试与门禁 + +提交前至少: + + node scripts/validate-plugins.js # manifest/能力/入口校验 + node scripts/theme-contract-test.js # 主题变量 + node tests/plugin--render-test.js # jsdom 渲染/交互 + +jsdom 可覆盖:面板渲染、节点 render/sync、拖拽插入(stub screenToWorld/createNode)、双击详情、保存回详情、封面 img。 + +## 10. 发布与市场 + +1. 改 package.json version 与入口 version(两处一致)。 +2. node scripts/sync-plugins-from-repo.js(同步到客户端 plugins/) +3. node scripts/gen-market.js(生成 market/ 与 zip) +4. 上传 market 到 NAS(tar + scp + 解压) +5. 客户端发版:set-version → CHANGELOG → 锚点 → verify:ci → build → publish-update → releases/site → tag/push + +## 11. 常见坑(务必先读) + +- 原生 HTML5 拖放:WKWebView 吞 pointerup,节点落地后第一次点击异常 → 用指针拖拽。 +- ctx.canvas 没有 createNode → 用 ctx.canvasWrite。 +- 输入框拦截拖动 → 未选中时 pointer-events:none。 +- 只读 textarea 仍可选中文本 → pointer-events:none。 +- dataUrlToBytes 返回对象 → 取 .bytes。 +- 封面直接存 dataURL 会让 JSON 膨胀 → 迁移到文件,只存 { img }。 +- 插件节点不声明 outKind → 只能中继,无法直连生图。 diff --git a/scripts/gen-release-page.js b/scripts/gen-release-page.js index 2ca14eb..97c6bc2 100644 --- a/scripts/gen-release-page.js +++ b/scripts/gen-release-page.js @@ -90,6 +90,7 @@ const html = ` 首页 下载 插件 + 开发 源码
diff --git a/scripts/gen-site.js b/scripts/gen-site.js index b095f85..f11c4d7 100644 --- a/scripts/gen-site.js +++ b/scripts/gen-site.js @@ -28,6 +28,7 @@ const nav = (active) => [ ' 首页', ' 下载', ' 插件', + ' 开发', ' 源码', '
', '
', @@ -83,6 +84,69 @@ const tags = [] for (const p of plugins) for (const t of (p.tags || [])) if (tags.indexOf(t) < 0) tags.push(t) const tagChips = tags.map((t) => '').join('\n ') +function mdToHtml (md) { + const esc2 = (s) => String(s).replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' }[c])) + const inline = (s) => esc2(s) + .replace(/`([^`]+)`/g, '$1') + .replace(/\*\*([^*]+)\*\*/g, '$1') + .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '$1') + const lines = String(md).split('\n') + const out = [] + let i = 0 + let inCode = false + let code = [] + let inList = '' + const closeList = () => { if (inList) { out.push(''); inList = '' } } + while (i < lines.length) { + const line = lines[i] + if (line.trim().indexOf('```') === 0) { + if (!inCode) { inCode = true; code = [] } else { + inCode = false + out.push('
' + esc2(code.join('\n')) + '
') + } + i++; continue + } + if (inCode) { code.push(line); i++; continue } + if (!line.trim()) { closeList(); i++; continue } + if (/^---+$/.test(line.trim())) { closeList(); out.push('
'); i++; continue } + const h = line.match(/^(#{1,4})\s+(.*)$/) + if (h) { closeList(); out.push('' + inline(h[2]) + ''); i++; continue } + if (line.trim().indexOf('|') === 0) { + const rows = [] + while (i < lines.length && lines[i].trim().indexOf('|') === 0) rows.push(lines[i++]) + const cells = rows.map((r) => r.trim().replace(/^\||\|$/g, '').split('|').map((c) => c.trim())) + if (cells.length > 1) { + out.push('' + cells[0].map((c) => '').join('') + '') + for (let r = 2; r < cells.length; r++) out.push('' + cells[r].map((c) => '').join('') + '') + out.push('
' + inline(c) + '
' + inline(c) + '
') + } + continue + } + if (/^ \S/.test(line)) { + closeList() + const block = [] + while (i < lines.length && (/^ /.test(lines[i]) || !lines[i].trim())) { block.push(lines[i].replace(/^ /, '')); i++ } + while (block.length && !block[block.length - 1].trim()) block.pop() + out.push('
' + esc2(block.join('\n')) + '
') + continue + } + const li = line.match(/^\s*[-*]\s+(.*)$/) + if (li) { + if (inList !== 'ul') { closeList(); out.push('
    '); inList = 'ul' } + out.push('
  • ' + inline(li[1]) + '
  • '); i++; continue + } + const ol = line.match(/^\s*\d+\.\s+(.*)$/) + if (ol) { + if (inList !== 'ol') { closeList(); out.push('
      '); inList = 'ol' } + out.push('
    1. ' + inline(ol[1]) + '
    2. '); i++; continue + } + closeList(); out.push('

      ' + inline(line) + '

      '); i++ + } + closeList() + if (inCode) out.push('
      ' + esc2(code.join('\n')) + '
      ') + return out.join('\n') +} + function render(tpl, map) { let out = fs.readFileSync(path.join(site, tpl), 'utf8') for (const k of Object.keys(map)) out = out.split('').join(map[k]) @@ -113,4 +177,16 @@ pluginsPage = pluginsPage.replace('/assets/style.css', '/assets/style.css?v=' + fs.writeFileSync(path.join(site, 'plugins', 'index.html'), pluginsPage) fs.writeFileSync(path.join(site, 'plugins-index.json'), JSON.stringify(market, null, 2) + '\n') +// 插件开发规范(由 docs/standards/plugin-development.md 生成) +const devMdPath = path.join(root, 'docs', 'standards', 'plugin-development.md') +if (fs.existsSync(devMdPath)) { + const devPage = render('_dev.tpl.html', { + NAV: nav('dev'), + FOOTER: footer, + DEV_CONTENT: mdToHtml(fs.readFileSync(devMdPath, 'utf8')), + }).replace('/assets/style.css', '/assets/style.css?v=' + STAMP) + fs.mkdirSync(path.join(site, 'dev'), { recursive: true }) + fs.writeFileSync(path.join(site, 'dev', 'index.html'), devPage) +} + console.log('站点已生成:首页 / 插件页(' + plugins.length + ' 个插件,' + tags.length + ' 个标签,版本 v' + V + ')') diff --git a/site/_dev.tpl.html b/site/_dev.tpl.html new file mode 100644 index 0000000..161418a --- /dev/null +++ b/site/_dev.tpl.html @@ -0,0 +1,30 @@ + + +插件开发规范 · Image Workbench + + + + +
      +
      + +
      +
      + + diff --git a/site/dev/index.html b/site/dev/index.html new file mode 100644 index 0000000..046be85 --- /dev/null +++ b/site/dev/index.html @@ -0,0 +1,194 @@ + + +插件开发规范 · Image Workbench + + + + +
      +
      +

      Image Workbench 插件开发规范

      +

      > 版本:v1(2026-11-22)。面向官方与第三方插件;示例以 promptlib(提示词库)与 style-source(风格库)为准。

      +

      > 相关文档:plugin-node-drag.md(节点拖入/指针交互踩坑)、plugin-node-outkind.md(下游连线声明)、plugin-theme.md(主题契约)。

      +

      1. 目录与基本结构

      +

      插件是「一个目录 + 一个 IIFE 入口」,由宿主在启用时动态加载并调用 register。

      +
      my-plugin/
      +  package.json          # 版本 + ibwPlugin 元数据
      +  src/index.js          # 入口(IIFE,末尾 IWBPluginHost.register)
      +  styles.css            # 样式(必须自包含)
      +  README.md
      +

      package.json:

      +
      {
      +  "name": "@iwb-plugin/my-plugin",
      +  "version": "1.0.0",
      +  "ibwPlugin": { "id": "my-plugin", "entry": "src/index.js", "apiVersion": 1 }
      +}
      +

      入口骨架:

      +
      ;(function (global) {
      +  'use strict'
      +  if (!global.IWBPluginHost) return
      +  global.IWBPluginHost.register({
      +    id: 'my-plugin',
      +    name: '我的插件',
      +    version: '1.0.0',
      +    capabilities: ['panels', 'canvas', 'canvasWrite'],
      +    settings: { panel: { type: 'custom', label: '设置', render: renderSettings } },
      +    mount: mount,
      +    unmount: unmount,
      +  }, function (ctx) {
      +    // setup:注册面板/节点/命令
      +    return { dispose () {} }
      +  })
      +})(typeof window !== 'undefined' ? window : globalThis)
      +

      2. 能力(capabilities)与 ctx 映射

      +

      只申请用到的能力;能力名与 ctx 键不同:

      + + + + + + + + + + +
      capabilityctx 键用途
      panelsctx.ui侧栏/独立面板注册
      promptlibctx.promptlib提示词库数据 + 文件 + 备份
      nodeTypesctx.nodes画布节点类型注册
      canvasctx.canvas只读:getNodes / getSelectedNode / screenToWorld
      canvasWritectx.canvasWrite写入:createNode / createImageNode / setNodeImage
      nodeActionsctx.nodeActions节点右键/气泡动作
      commandsctx.commands命令面板
      settingsctx.settingsKV 配置
      datactx.data插件数据文件
      +

      注意:createNode 在 ctx.canvasWrite,不在 ctx.canvas。调错只会提示“宿主不支持”。

      +

      3. 面板

      +
      ctx.ui.registerPanel({
      +  id: 'my-panel',
      +  title: '我的插件',
      +  mount (host) { /* 渲染到 host */ },
      +  unmount (host) { /* 清理定时器/监听 */ }
      +})
      +
        +
      • mount 只调用一次,内部自己管理重渲染。
      • +
      • 面板宽度可变:使用栅格/瀑布流,不要写死 px 宽度。
      • +
      • 面板内所有 DOM 事件在 unmount 时移除。
      • +
      +

      4. 节点类型

      +
      ctx.nodes.register({
      +  id: 'tpl',
      +  title: '模板节点',
      +  defaults: { outKind: 'prompt' },
      +  ports: { in: false, out: true },
      +  height: 360,
      +  render (node, props, host) { /* 首次建 DOM,之后 sync */ }
      +})
      +

      宿主会生成类型 plugin:<插件id>:<变体id>。

      +

      必须遵守:

      +
        +
      1. 输出文本/提示词 → data.outKind = 'prompt'(注册默认值、拖入数据、旧节点在 render 里自愈)。
      2. +
      3. 拖入画布用指针拖拽,不用原生 HTML5 DnD:
      4. +
      +
      pointerdown → pointermove(>5px) 幽灵卡片
      +→ pointerup → ctx.canvas.screenToWorld(x,y)
      +→ ctx.canvasWrite.createNode('tpl', wx, wy, data)
      +
        +
      1. 输入框:未选中时 pointer-events: none,选中后才可编辑;只读 textarea 同样处理。
      2. +
      3. 单击 = 选中/拖动,双击 = 编辑;不要用单击打开详情。
      4. +
      5. 节点 render 必须可增量 sync,避免整块重建(输入框会丢焦点)。
      6. +
      +

      5. 数据与文件

      +
        +
      • 小数据:ctx.promptlib(或 ctx.data)JSON,单文件 < 1MB。
      • +
      • 大文件(封面/图片):ctx.promptlib.filePut/fileGet/fileList,落地在 plugin-data/<id>/files/。
      • +
      • dataUrlToBytes() 返回 { mime, bytes },不是 Uint8Array。
      • +
      • 备份:提供导出/导入 JSON;导入按 id 合并,提供“覆盖同 id”选项。
      • +
      • 迁移:旧格式在 setup 里做一次性迁移,失败要提示而不是静默丢数据。
      • +
      +

      6. 设置页面

      +

      宿主设置页内嵌渲染,不弹独立页面:

      +
      settings: {
      +  panel: { type: 'custom', label: '分组与备份', render: renderSettings }
      +}
      +

      render 返回 DOM;宿主负责挂载/卸载。设置页适合放:

      +
        +
      • 分组管理(新建/改名/排序/删除)
      • +
      • 数据备份(导出/导入)
      • +
      • 数据位置(打开数据目录)
      • +
      • 插件级开关/默认值
      • +
      +

      7. 样式规范

      +
        +
      1. 自包含:宿主基础样式会变;用到的宿主类按作用域复制一份(.iwb-myplugin …)。
      2. +
      3. 统一前缀:面板/节点类名 my-plugin-*,避免与宿主 .iwb-* 冲突。
      4. +
      5. 颜色走 CSS 变量:var(--iwb-text)、--iwb-panel、--iwb-border、--iwb-plugin-accent。
      6. +
      7. 动画克制:只做 hover/press 反馈;不使用持续浮动动画。
      8. +
      9. 深色主题:不写死 #fff/#000;主题契约测试会扫描。
      10. +
      11. 悬停放大只缩放内层背景,不缩放卡片本体(避免命中区域漂移/闪烁)。
      12. +
      13. 图片/封面 pointer-events: none,点击永远落到卡片。
      14. +
      +

      8. 错误处理与提示

      +
        +
      • 所有用户可见错误用 ctx.promptlib.notify / ctx.ui.toast,文案给“下一步怎么办”。
      • +
      • 异步链必须有 catch;不要吞掉错误后静默。
      • +
      • 崩溃日志:window.onerror 由宿主统一采集,插件不要自己覆盖。
      • +
      • 定时器/监听在 unmount/dispose 清理,避免面板切换后重复执行。
      • +
      +

      9. 测试与门禁

      +

      提交前至少:

      +
      node scripts/validate-plugins.js        # manifest/能力/入口校验
      +node scripts/theme-contract-test.js     # 主题变量
      +node tests/plugin-<id>-render-test.js   # jsdom 渲染/交互
      +

      jsdom 可覆盖:面板渲染、节点 render/sync、拖拽插入(stub screenToWorld/createNode)、双击详情、保存回详情、封面 img。

      +

      10. 发布与市场

      +
        +
      1. 改 package.json version 与入口 version(两处一致)。
      2. +
      3. node scripts/sync-plugins-from-repo.js(同步到客户端 plugins/)
      4. +
      5. node scripts/gen-market.js(生成 market/ 与 zip)
      6. +
      7. 上传 market 到 NAS(tar + scp + 解压)
      8. +
      9. 客户端发版:set-version → CHANGELOG → 锚点 → verify:ci → build → publish-update → releases/site → tag/push
      10. +
      +

      11. 常见坑(务必先读)

      +
        +
      • 原生 HTML5 拖放:WKWebView 吞 pointerup,节点落地后第一次点击异常 → 用指针拖拽。
      • +
      • ctx.canvas 没有 createNode → 用 ctx.canvasWrite。
      • +
      • 输入框拦截拖动 → 未选中时 pointer-events:none。
      • +
      • 只读 textarea 仍可选中文本 → pointer-events:none。
      • +
      • dataUrlToBytes 返回对象 → 取 .bytes。
      • +
      • 封面直接存 dataURL 会让 JSON 膨胀 → 迁移到文件,只存 { img }。
      • +
      • 插件节点不声明 outKind → 只能中继,无法直连生图。
      • +
      +
      +
      + + diff --git a/site/download/index.html b/site/download/index.html index 1d0c2cc..6d41e8d 100644 --- a/site/download/index.html +++ b/site/download/index.html @@ -39,6 +39,7 @@ 首页 下载 插件 + 开发 源码
diff --git a/site/index.html b/site/index.html index 18b491a..cf2aeee 100644 --- a/site/index.html +++ b/site/index.html @@ -2,7 +2,7 @@ Image Workbench · 本地优先的 AI 生图工作台 - +
-
+
v3.26.38 · macOS / Windows · 数据在本地 diff --git a/site/plugins/index.html b/site/plugins/index.html index 43848b2..26b3e5c 100644 --- a/site/plugins/index.html +++ b/site/plugins/index.html @@ -1,7 +1,7 @@ 插件市场 · Image Workbench - +