- 新增 src/plugin-api-gallery.js(list/thumb/preview/download/insert/remove),impl 由 Workbench 渲染时注入 - plugin-host 能力表加 gallery - 右栏图库:装了插件走插件,未装或挂载失败回落宿主面板(PluginBox 支持 fallback) - 测试与边界快照同步(能力表含 gallery)
53 lines
2.2 KiB
JavaScript
53 lines
2.2 KiB
JavaScript
/* 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) |