Files
image-workbench-desktop/src/runtime-ref-upload.js
T
2026-12-04 13:52:30 +08:00

43 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* ref-upload.js —— 参考图节点「上传到图库」动作(2026-12-04)
*
* 从 app.js 的节点气泡里抽出:气泡是装配层,逻辑放模块里(app.js 行数棘轮)。
* 依赖全部注入:fetchBlob(Rust 优先)/ blobToDataUrl / uploadRef / config / updateNode / notice。
*/
;(function (global) {
'use strict'
function create (deps) {
const d = deps || {}
const uploading = new Set()
const run = async (node, opts) => {
if (!node || node.type !== 'ref' || uploading.has(node.id)) return false
const data = node.data || {}
const hasLocal = !!(data.data || data.localRef)
const hasUrl = typeof data.url === 'string' && data.url
if (!hasLocal && !hasUrl) return false
const o = opts || {}
uploading.add(node.id)
try {
let payload = data.data || data.localRef || null
if (!payload) {
let blob = await d.fetchBlob(data.url)
if (!blob || !blob.size) {
try { const r = await d.fetch(data.url, { credentials: 'same-origin' }); if (r && r.ok) blob = await r.blob() } catch (e) { /* ignore */ }
}
if (!blob || !blob.size) throw new Error('图片下载失败')
payload = await d.blobToDataUrl(blob)
}
if (typeof o.notice === 'function') o.notice('正在上传到图库…')
const url = await d.uploadRef(payload, d.config(), data.name)
if (typeof o.updateNode === 'function') o.updateNode(node.id, { url: url, data: null, localRef: null, localName: null, name: String(url).split('/').pop() || '在线图片' })
if (typeof o.notice === 'function') o.notice('已上传到图库并填入链接')
return true
} catch (e) {
if (typeof o.notice === 'function') o.notice('上传失败:' + String((e && e.message) || e), 'err')
return false
} finally { uploading.delete(node.id) }
}
return { run: run, isUploading: (id) => uploading.has(id) }
}
global.IWBRefUpload = { create: create }
})(typeof window !== 'undefined' ? window : globalThis)