From 2594623c09ed0c7b758225314936b4dce845c6a9 Mon Sep 17 00:00:00 2001 From: qiqi Date: Fri, 20 Nov 2026 11:24:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(plugin):=20=E5=AE=BF=E4=B8=BB=20gallery=20?= =?UTF-8?q?capability=20+=20=E5=9B=BE=E5=BA=93=E9=9D=A2=E6=9D=BF=20A=20?= =?UTF-8?q?=E6=96=B9=E6=A1=88=E8=87=AA=E5=8A=A8=E5=88=87=E6=8D=A2/?= =?UTF-8?q?=E5=9B=9E=E8=90=BD=20v3.25.51?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 src/plugin-api-gallery.js(list/thumb/preview/download/insert/remove),impl 由 Workbench 渲染时注入 - plugin-host 能力表加 gallery - 右栏图库:装了插件走插件,未装或挂载失败回落宿主面板(PluginBox 支持 fallback) - 测试与边界快照同步(能力表含 gallery) --- CHANGELOG.md | 10 ++++ app.js | 11 +++- package.json | 2 +- scripts/build.js | 1 + src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 2 +- src-tauri/tauri.conf.json | 2 +- src/plugin-api-gallery.js | 53 ++++++++++++++++++++ src/plugin-host.js | 4 +- src/ui-dock.js | 9 ++-- tests/fixtures/iwb-app-global-reads.json | 1 + tests/fixtures/iwb-source-modules-order.json | 1 + tests/plugin-host-test.js | 2 +- tests/single-canvas-shell-test.js | 4 +- 14 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 src/plugin-api-gallery.js diff --git a/CHANGELOG.md b/CHANGELOG.md index fc0cf32..61557a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,16 @@ > 3. 核验正式地址版本 → 之后才移动 `stable` / `stable-verified-<日期>` 标签、更新本 CHANGELOG、写 GitHub Release(`gh release create`)。 > 测试发现问题 → 改 → 再 `deploy:test`,正式环境不受影响。回滚点 `git reset --hard stable`。 +## v3.25.51(2026-11-20)M5 第二步:宿主 gallery capability(插件可读图库) + +- 新增 `src/plugin-api-gallery.js`:给插件稳定的 `ctx.gallery` 代理(`list / thumb / preview / download / insert / remove`)。 + - 图片字节仍留宿主(`images/` + 图库索引),插件只拿元数据与缩略图 URL;`list` 复用 `IWBGalleryQuery`(同筛选/分页口径)。 + - 实现由 Workbench 每次渲染 `setImpl()` 注入(capability 在 init 期创建,拿不到组件闭包)。 +- `plugin-host` 能力表新增 `gallery`(`capabilities:['gallery']` 才可调用)。 +- 右栏图库改为 **A 方案**:装了 gallery 插件(且 registerPanel 成功)走插件,否则/挂载失败**自动回落宿主面板** + (`ui-dock` 的 `PluginBox` 新增 `fallback`)。 +- 验证:浏览器实测 `ctx.gallery.list()` 返回 `{items,total}`、`thumb(未知)` 返回空串、无报错;宿主面板照常渲染。 + ## v3.25.50(2026-11-20)M5 第一步:图库面板抽成独立模块(行为零变化) - 新增 `src/ui-gallery-panel.js`:图库面板(头部三行 + 瀑布流网格 + 单项操作)从 `app.js` 移出,改为 props 驱动的纯组件。 diff --git a/app.js b/app.js index 14e2b72..d142c63 100644 --- a/app.js +++ b/app.js @@ -8071,6 +8071,14 @@ const tbCenterSeed = (type, refs) => { onPromptsChange: (list) => setPrompts(list), onGroupsChange: setPromptGroups, onInsertPrompt: (text, name) => { const id = uid('n'); const spot = findSpot('prompt'); commitCanvas(refs.nodes.concat([{ id, type: 'prompt', x: spot.x, y: spot.y, data: { text, title: name || '' } }]), undefined, undefined); setSel([id]); setNotice({ type: 'ok', text: '已插入提示词:' + (name || '') }) }, onFlash: (t, ty) => { setNotice({ type: ty === 'err' ? 'err' : 'ok', text: t }); showToast(t, ty) } }) + if (window.IWBPluginGallery) window.IWBPluginGallery.setImpl({ + list: (o) => window.IWBPluginGallery.query(outputs, o), + thumb: (name) => { const f = (outputs || []).find((x) => x.name === name); return f ? wDisplayThumbOf((f.data || f.url) || '') : '' }, + preview: (name) => setPreview({ list: outputs.map((x) => ({ __gallery: true, name: x.name, origIdb: x.origIdb, idb: x.idb, url: x.url, thumb: x.data || null })), idx: Math.max(0, outputs.findIndex((x) => x.name === name)) }), + download: async (names) => { let n = 0; for (const f of (outputs || [])) if (names.indexOf(f.name) >= 0) { downloadUrl(await wResolveOutputSrc(f), f.name); n++ } return { ok: true, count: n } }, + insert: async (names) => ({ ok: true, inserted: await insertOutputsToCanvas((outputs || []).filter((f) => names.indexOf(f.name) >= 0)) }), + remove: (names) => { deleteOutputs(names); setHistSel([]); return { ok: true } }, + }) const dockGalleryEl = GalleryPanel ? el(GalleryPanel, { outputs, filtered: histFiltered, buckets: histCols.buckets, sel: histSel, limit: histLimit, insertK, tile: histTile, range: histRange, @@ -8092,7 +8100,7 @@ const tbCenterSeed = (type, refs) => { browser: { title: '浏览器', icon: 'globe', pluginId: 'browser' }, styles: { title: '风格库', icon: 'palette', pluginId: 'style-source' }, prompts: { title: '提示词库', icon: 'book', node: dockPromptsEl }, - gallery: { title: '图库', icon: 'ref', node: dockGalleryEl }, + gallery: wPluginPanels.some((x) => x && x.pluginId === 'gallery' && typeof x.mount === 'function') ? { title: '图库', icon: 'ref', pluginId: 'gallery', fallback: dockGalleryEl } : { title: '图库', icon: 'ref', node: dockGalleryEl }, } return el('div', { className: 'iwb-overlay' + (wsExpanded ? ' ws-expanded' : '') + (shellView === 'landing' ? ' iwb-view-landing' : shellView === 'home' ? ' iwb-view-home' : ' iwb-view-canvas') + (drawerOpen ? ' drawer-open' : '') + ((nodes.length && loaded) ? ' has-minimap' : '') }, // 页面加载动画(加载完成后淡出) @@ -8741,6 +8749,7 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) => writeState: (state) => { lsSet('iwb_plugins', state) }, log: () => { try { wLog.apply(null, arguments) } catch (e) { /* ignore */ } }, capabilities: { + gallery: () => ((typeof window !== 'undefined' && window.IWBPluginGallery) ? window.IWBPluginGallery.api() : {}), commands: (ctx) => { const owned = [] return { diff --git a/package.json b/package.json index e0a22bd..877c06c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "image-workbench-web", "private": true, - "version": "3.25.50", + "version": "3.25.51", "description": "Browser-based node canvas image workbench", "scripts": { "build": "node scripts/build.js && node scripts/copy-plugins.js", diff --git a/scripts/build.js b/scripts/build.js index 32de4a5..41c90c1 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -78,6 +78,7 @@ const sourceModules = [ 'gallery-hydration.js', 'ui-components.js', 'plugin-data.js', + 'plugin-api-gallery.js', 'plugin-host.js', 'image-compress.js', 'storage-fallback-indexeddb.js', diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index d5b88c9..ab87d53 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1983,7 +1983,7 @@ dependencies = [ [[package]] name = "image-workbench" -version = "3.25.50" +version = "3.25.51" dependencies = [ "arboard", "base64 0.22.1", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index dc4e9ae..45ad27b 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "image-workbench" -version = "3.25.50" +version = "3.25.51" description = "无限画布 · 生图工作台桌面版" authors = ["malongan"] license = "" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 9e0a9cd..1505896 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "../node_modules/@tauri-apps/cli/config.schema.json", "productName": "image-workbench", - "version": "3.25.50", + "version": "3.25.51", "identifier": "com.malongan.imageworkbench", "build": { "frontendDist": "../dist", diff --git a/src/plugin-api-gallery.js b/src/plugin-api-gallery.js new file mode 100644 index 0000000..712aa1a --- /dev/null +++ b/src/plugin-api-gallery.js @@ -0,0 +1,53 @@ +/* plugin-api-gallery.js —— 宿主 gallery capability(M5) + * + * 给插件一块**只读为主**的图库访问面:列表 / 缩略图 / 预览 / 下载 / 插入画布 / 删除。 + * 图片字节仍留在宿主(images/ + 图库索引),插件拿不到文件路径,只能拿元数据与缩略图 URL。 + * + * 为什么要这层「impl 桥」: + * capability 在 PluginHost.init 时创建(早于组件渲染),拿不到 Workbench 的 state/refs。 + * 所以这里只保存一组**稳定的代理函数**,真正的实现由 Workbench 每次渲染时 setImpl 注入。 + */ +;(function (global) { + 'use strict' + + let impl = null + + 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) } + } + } + + // 稳定代理:插件拿到的对象永不变,内部每次都取最新的 impl + const API = { + list: call('list'), + thumb: call('thumb'), + preview: call('preview'), + download: call('download'), + insert: call('insert'), + remove: call('remove'), + } + + // 纯函数:把宿主 outputs + 查询参数变成插件要的条目(不含 data,避免把 base64 递给插件) + function query (outputs, opt) { + const o = opt || {} + const Q = global.IWBGalleryQuery + const list = Array.isArray(outputs) ? outputs : [] + const limit = Math.min(500, Math.max(1, Number(o.limit) || 200)) + const res = (Q && typeof Q.queryOutputs === 'function') + ? Q.queryOutputs(list, { query: o.query || '', range: o.range || '', limit, width: 320, tile: 130 }) + : { filtered: list, visible: list.slice(0, limit), total: list.length } + const items = (res.visible || []).map(function (f) { + return { name: f.name, label: f.label || '', w: f.w || 0, h: f.h || 0, mtime: f.mtime || 0 } + }) + return { items: items, total: res.total || items.length } + } + + global.IWBPluginGallery = { setImpl: setImpl, api: function () { return API }, query: query } +})(typeof window !== 'undefined' ? window : globalThis) \ No newline at end of file diff --git a/src/plugin-host.js b/src/plugin-host.js index cb46398..fca122c 100644 --- a/src/plugin-host.js +++ b/src/plugin-host.js @@ -4,8 +4,8 @@ const API_VERSION = 1 const ID_PATTERN = /^[a-z][a-z0-9-]{2,31}$/ const REQUIRED_FIELDS = ['id', 'name', 'version', 'apiVersion', 'capabilities'] - const CAPABILITIES = ['panels', 'commands', 'nodeTypes', 'generationHooks', 'providers', 'canvas', 'canvasWrite', 'nodeActions'] - const CAPABILITY_API_KEYS = { panels: 'ui', commands: 'commands', nodeTypes: 'nodes', generationHooks: 'generation', providers: 'providers', canvas: 'canvas', canvasWrite: 'canvas', nodeActions: 'nodeActions' } + const CAPABILITIES = ['panels', 'commands', 'nodeTypes', 'generationHooks', 'providers', 'canvas', 'canvasWrite', 'nodeActions', 'gallery'] + const CAPABILITY_API_KEYS = { panels: 'ui', commands: 'commands', nodeTypes: 'nodes', generationHooks: 'generation', providers: 'providers', canvas: 'canvas', canvasWrite: 'canvas', nodeActions: 'nodeActions', gallery: 'gallery' } function createPluginHost(options) { const opt = options || {} diff --git a/src/ui-dock.js b/src/ui-dock.js index 5219eda..ae051b5 100644 --- a/src/ui-dock.js +++ b/src/ui-dock.js @@ -63,15 +63,18 @@ } // 插件面板宿主:只在激活时挂载(切走即卸载)。挂载失败不影响其它面板。 + // v3.25.51:挂载失败可回退(gallery 这类核心面板装插件后仍要有保底) function PluginBox (props) { const ref = useRef(null) + const [failed, setFailed] = useState(false) const mount = props.mount useEffect(() => { const node = ref.current - if (!node || typeof mount !== 'function') return undefined - try { mount(node) } catch (e) { /* 插件挂载异常不得冒泡 */ } + if (!node || typeof mount !== 'function') { setFailed(true); return undefined } + try { mount(node); setFailed(false) } catch (e) { setFailed(true) } return undefined }, [mount]) + if (failed && props.fallback) return props.fallback return el('div', { className: 'iwb-dock-plugin', ref }) } @@ -298,7 +301,7 @@ if (p.pluginId) { const mount = (props.pluginMount && props.pluginMount(p.pluginId)) || null if (!mount) return el('div', { className: 'iwb-dock-empty' }, (p.title || '插件') + ' 未加载') - return el(PluginBox, { mount }) + return el(PluginBox, { mount, fallback: p.fallback || null }) } return el('div', { className: 'iwb-dock-empty' }, '面板不可用') } diff --git a/tests/fixtures/iwb-app-global-reads.json b/tests/fixtures/iwb-app-global-reads.json index 58209e7..fe13061 100644 --- a/tests/fixtures/iwb-app-global-reads.json +++ b/tests/fixtures/iwb-app-global-reads.json @@ -24,6 +24,7 @@ "IWBNativeClipboard", "IWBNodeBodies", "IWBPluginData", + "IWBPluginGallery", "IWBPluginHost", "IWBProviderPing", "IWBResultImg", diff --git a/tests/fixtures/iwb-source-modules-order.json b/tests/fixtures/iwb-source-modules-order.json index 38346ba..9875f81 100644 --- a/tests/fixtures/iwb-source-modules-order.json +++ b/tests/fixtures/iwb-source-modules-order.json @@ -50,6 +50,7 @@ "gallery-hydration.js", "ui-components.js", "plugin-data.js", + "plugin-api-gallery.js", "plugin-host.js", "image-compress.js", "storage-fallback-indexeddb.js", diff --git a/tests/plugin-host-test.js b/tests/plugin-host-test.js index f33c188..328b4ee 100644 --- a/tests/plugin-host-test.js +++ b/tests/plugin-host-test.js @@ -13,7 +13,7 @@ vm.runInNewContext(source, sandbox) const host0 = sandbox.IWBPluginHost assert.ok(Number.isInteger(host0.apiVersion) && host0.apiVersion >= 1, 'apiVersion 必须是正整数') assert.ok(Array.isArray(host0.capabilities) && host0.capabilities.length >= 7, '能力清单应非空') -assert.strictEqual(host0.capabilities.slice().sort().join(','), 'canvas,canvasWrite,commands,generationHooks,nodeActions,nodeTypes,panels,providers') +assert.strictEqual(host0.capabilities.slice().sort().join(','), 'canvas,canvasWrite,commands,gallery,generationHooks,nodeActions,nodeTypes,panels,providers') const manifest = (over) => Object.assign({ id: 'demo-plugin', name: 'Demo', version: '1.0.0', apiVersion: 1, capabilities: [] }, over) const stateOf = (ids) => { diff --git a/tests/single-canvas-shell-test.js b/tests/single-canvas-shell-test.js index 154308a..3af822a 100644 --- a/tests/single-canvas-shell-test.js +++ b/tests/single-canvas-shell-test.js @@ -26,7 +26,9 @@ assert.ok(!allSrc.includes("title: '图库(右侧栏)'")) assert.ok(allSrc.includes("title: '显示 / 隐藏右栏'")) assert.ok(source.includes("panels: dockPanels")) assert.ok(source.includes("groups: { title: '节点组'")) -assert.ok(source.includes("gallery: { title: '图库'")) +// v3.25.51:gallery 面板改为「装了插件用插件、否则宿主」,断言两种分支都在 +assert.ok(source.includes("pluginId: 'gallery'")) +assert.ok(source.includes("node: dockGalleryEl")) assert.ok(allSrc.includes("className: 'iwb-res-tab'")) assert.ok(allSrc.includes("className: 'iwb-res-tabs'")) assert.ok(css.includes('.iwb-res-tabs {'))