feat(album): 相册节点宿主侧——多图源任务展开(一图一任务/批量分批)+组合扇出+ctx.images(引用/导入)+{__file}本地文件引用+保留原图
This commit is contained in:
1 parent
64da475c7f
commit
e3d2e449e1
22 files changed
+1055
-22
No files matched your search
@@ -0,0 +1,68 @@
|
||||
/* 插件图片能力:ctx.images(2026-11-26,首个使用方 = 相册节点)
|
||||
*
|
||||
* 为什么需要:相册要「只引用本地文件、不复制字节」,同时也要能把拖入/固化时的字节写进宿主存储。
|
||||
* 插件自己做不到这两件事:
|
||||
* · 引用:需要 fs scope 授权 + 绝对路径(只有宿主能调 grant_paths / dialog)
|
||||
* · 落地:需要宿主的唯一收口 wImgRefWrite({__blob} 主通道 + 缩略图预生成)
|
||||
*
|
||||
* 本模块只做「参数校验 + 分派」,宿主能力全部经 create(deps) 注入:
|
||||
* fileRef —— IWBImageFileRef(路径 ↔ 字节,含 pick/grant/stat)
|
||||
* importDataUrl —— (dataUrl, opts) => Promise<{ok, ref|null, data|null, error?}>(走 wImportImage 唯一收口)
|
||||
* readDataUrl —— (ref|path) => Promise<string>(http/data/引用对象 → 真实字节)
|
||||
* thumbUrlOf —— (ref|src) => string(768 显示缩略图,同步)
|
||||
*/
|
||||
;(function (global) {
|
||||
'use strict'
|
||||
const g = global || {}
|
||||
|
||||
const create = (deps) => {
|
||||
const D = deps || {}
|
||||
const need = (name) => {
|
||||
const fn = D[name]
|
||||
if (typeof fn !== 'function') throw new Error('IWBPluginApiImages 缺少注入依赖: ' + name)
|
||||
return fn
|
||||
}
|
||||
const fileRef = need('fileRef')
|
||||
const importDataUrl = need('importDataUrl')
|
||||
const readDataUrl = need('readDataUrl')
|
||||
const thumbUrlOf = need('thumbUrlOf')
|
||||
|
||||
const str = (v) => (v == null ? '' : String(v))
|
||||
const IMAGE_EXT = ['png', 'jpg', 'jpeg', 'webp', 'gif', 'avif', 'bmp', 'svg']
|
||||
const extOf = (p) => { const m = /\.([a-z0-9]{1,5})$/i.exec(str(p)); return m ? m[1].toLowerCase() : '' }
|
||||
const isImagePath = (p) => IMAGE_EXT.indexOf(extOf(p)) >= 0
|
||||
|
||||
return (ctx) => ({
|
||||
// 是否支持「本地文件引用」(网页版 false ⇒ 插件应退回 importPath/importDataUrl)
|
||||
canReference: () => fileRef.available(),
|
||||
// 系统选择器:返回绝对路径数组(桌面端);网页端返回 []
|
||||
pick: (opts) => fileRef.pick(opts),
|
||||
// 单张引用:只读元信息,不复制字节
|
||||
reference: async (path) => {
|
||||
if (!isImagePath(path)) return { ok: false, reason: 'not-image' }
|
||||
const r = await fileRef.reference(path)
|
||||
return r && r.ok ? { ok: true, item: { path: r.path, name: r.name, size: r.size, mtime: r.mtime } } : { ok: false, reason: (r && r.reason) || 'stat-failed' }
|
||||
},
|
||||
// 失联检测(相册打开画布时批量调用)
|
||||
stat: (path) => fileRef.stat(path),
|
||||
// 读字节(base64 dataURL):只给「需要预览/上传」的显式场景
|
||||
dataUrl: (v) => readDataUrl(v),
|
||||
// 缩略图地址(同步;宿主内部 768 缓存,不解析原图字节)
|
||||
thumb: (v) => thumbUrlOf(v),
|
||||
// 把本地文件字节写进宿主存储(拖拽=复制、以及「固化」按钮)
|
||||
importPath: async (path, opts) => {
|
||||
if (!isImagePath(path)) return { ok: false, error: 'not-image' }
|
||||
const url = await fileRef.readDataUrl(path)
|
||||
if (!url) return { ok: false, error: 'read-failed' }
|
||||
return importDataUrl(url, opts)
|
||||
},
|
||||
// 把任意 dataURL 写进宿主存储
|
||||
importDataUrl: (dataUrl, opts) => {
|
||||
if (!/^data:image\//i.test(str(dataUrl))) return Promise.resolve({ ok: false, error: 'not-data-url' })
|
||||
return importDataUrl(str(dataUrl), opts)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
global.IWBPluginApiImages = Object.freeze({ create })
|
||||
})(typeof window !== 'undefined' ? window : globalThis)
|
||||
Reference in new issue
Block a user