Files
image-workbench-desktop/src/image-remote-cache.js
T
qiqi 9b78535791 feat(gallery): v3.26.61 出图即入图库 + 插入落地本地副本(能标注)
用户报障:① 生图后图库不显示上游传来的图 ② 图库多个入口插入的是链接,画布不能标注。
用户裁定 B6 = 复制:多占空间可以接受,要求不丢数据。

出图即见
- handleCompletedJob 每张完成即 archiveEntryNow(串行队列 + 等缩略图),不再只在整轮结束写一次
- 宿主新增 IWBPluginGallery.emit / onChanged;图库插件订阅后 260ms 防抖重拉
  (此前只有「面板可见且距上次拉取 >5s」一个入口,生成时面板一直可见 ⇒ 永远不刷)

插入能标注
- insertOutputsToCanvas 取源改本地字节优先:原图档 → 清单 data → 缩略图 → 按需抓远程
  (桌面端走 Rust 请求不受 CORS)→ 按档缩尺寸 → {__blob} 引用(字节落 images/canvas/)
- 抓不到才保留原链接,节点 data.srcUrl 留痕 + toast 提示「可能无法标注」;图库拖入同一收口
- getNodeImage 候选补 data.localRef(远程参考图本地缓存字段):此前漏读它 ⇒ 缓存命中过,
  标注插件仍只看到 http 链接并判「远程图片不支持标注」,这是报障②的直接成因

批量插入不再叠图
- 旧实现循环里每张各自 findSpot,而 findSpot 只看已提交节点 ⇒ 同批全落一处;
  改为一次 commit + 4 列栅格铺开

gallery 1.7.0
- 格子「插入」补 insertK(此前忽略插入尺寸档,与右键/顶部不一致)
- 插入补「插入中…」状态;删除改为等宿主删完再重拉(固定 250ms 会拉到未删完的清单)

结构
- 新增 src/gallery-sync.js(约 200 行),app.js 只 attach 装配;新增 tests/gallery-sync-test.js(11 项)
- 行数棘轮 9095 → 9115:+13 行是注入表与 commit 分发(闭包组件内 state),+6 行是纠错注释;
  同时新增一个 src 模块 ⇒ 占比棘轮反而下降。理由已写入棘轮文件

口径纠错(用户要求把旧的不准确描述改掉)
- 图库/缩略图/原图档在桌面端是 images/thumbs、images/gallery 文件,不是 IndexedDB
- 1K 插入档降的是长边像素不是体积(PNG 源仍无损,1024 复杂图 ~500KB~1MB)
- docs/plans/gallery-insert-perf-v324.md 加时效注记(网页版时期口径)
2026-11-24 17:11:01 +08:00

90 lines
4.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.
/* image-remote-cache.js —— 远程参考图本地缓存(v3.25.80)
*
* 目的:远程参考图(如 ibb.co)每次打开画布都重新下载,慢且耗流量。
* 做法:空闲时下载一次,写入本地引用存储,节点上只记 data.localRef(显示用)。
*
* 约束(用户裁定):节点 data.url 保持原样 ⇒ **提交生成时仍然用链接**。
* 谁在读 localRef(v3.26.61 修正):插件取图走 canvasBridge.getNodeImage,它的候选列表里必须有
* localRef —— 早先漏了它,于是缓存明明命中,标注插件仍只看到 data.url 的 http 链接,
* 直接判「远程图片不支持标注」(用户报障的直接成因)。改这个字段的写入时机/格式前先看那里。
*/
;(function (global) {
'use strict'
const React = global.React || {}
function useRemoteRefCache (opts) {
const o = opts || {}
const tried = React.useRef(new Set())
React.useEffect(() => {
if (!o.enabled) return undefined
const pend = (o.nodes || []).filter((n) => n && n.type === 'ref' && n.data && !n.data.data && !n.data.localRef &&
/^https?:\/\//i.test(String(n.data.url || '')) && !tried.current.has(n.id))
if (!pend.length) return undefined
const timer = setTimeout(() => {
(async () => {
for (const n of pend) {
tried.current.add(n.id)
try {
const url = String((n.data && n.data.url) || '')
if (!url) continue
const resp = await fetch(url, { credentials: 'omit' })
if (!resp || !resp.ok) continue
const blob = await resp.blob()
if (!blob || !blob.size) continue
const dataUrl = await new Promise((res, rej) => {
const rd = new FileReader()
rd.onload = () => res(String(rd.result || ''))
rd.onerror = () => rej(new Error('read-failed'))
rd.readAsDataURL(blob)
})
if (!dataUrl) continue
const ref = await o.writeRef(dataUrl)
if (ref) { o.updateNode(n.id, { localRef: ref }); break }
} catch (e) { /* 跨域/离线:保留 url 显示,不重试 */ }
}
})()
}, 2500)
return () => clearTimeout(timer)
}, [o.enabled, o.activeWsId, o.nodes])
}
// 非 Hook 版本:宿主在自己的 effect 里调用,返回 stop()。
function startRemoteRefCache (opts) {
const o = opts || {}
const tried = new Set()
const timer = setInterval(() => {
let st = null
try { st = typeof o.getState === 'function' ? o.getState() : null } catch (e) { st = null }
if (!st || !st.enabled || !Array.isArray(st.nodes)) return
const next = st.nodes.find((n) => n && n.type === 'ref' && n.data && !n.data.data && !n.data.localRef &&
/^https?:\/\//i.test(String(n.data.url || '')) && !tried.has(n.id))
if (!next) return
tried.add(next.id)
const url = String((next.data && next.data.url) || '')
if (!url) return
;(async () => {
try {
const resp = await fetch(url, { credentials: 'omit' })
if (!resp || !resp.ok) return
const blob = await resp.blob()
if (!blob || !blob.size) return
const dataUrl = await new Promise((res, rej) => {
const rd = new FileReader()
rd.onload = () => res(String(rd.result || ''))
rd.onerror = () => rej(new Error('read-failed'))
rd.readAsDataURL(blob)
})
if (!dataUrl) return
const ref = await o.writeRef(dataUrl)
if (ref && typeof o.updateNode === 'function') o.updateNode(next.id, { localRef: ref })
} catch (e) { /* 跨域/离线:保留 url 显示,不重试 */ }
})()
}, 4000)
return () => clearInterval(timer)
}
function attach (opts) { return startRemoteRefCache(opts) }
global.IWBImageRemoteCache = { useRemoteRefCache, startRemoteRefCache, attach }
})(typeof window !== 'undefined' ? window : globalThis)