/* image-thumb-pick.js —— 缩略图 key / 按需切原图 / 入库预热(v3.25.73) * * 拆出来的原因:逻辑需要画布侧的映射(引用表、缓存 Map、引用解析函数), * 但规则要求 app.js 只做装配(行数棘轮),所以这里只放纯逻辑,依赖全部注入。 * * 参考实现:basketikun/infinite-canvas 的 lib/image-thumbnail.ts#pickImageSource * —— 用「屏幕需要的像素」决定用缩略图还是原图,而不是「有缩略图就一直用」。 */ ;(function (global) { 'use strict' function create (deps) { const d = deps || {} const cache = d.cache // Map const max = Math.max(64, Number(d.max) || 768) // v3.26.121:小档(256)缓存与队列;缺省不启用(只有注入时才分档) const smallCache = d.smallCache instanceof Map ? d.smallCache : null const smallQueue = typeof d.smallQueue === 'function' ? d.smallQueue : null const smallMax = Math.max(64, Number(d.smallMax) || 256) // 稳定 key:引用图用 ref:(跨会话不变);blob URL 反查引用;其余走原规则 function keyOf (v) { if (v && typeof v === 'object') { const k = typeof d.refKeyOf === 'function' ? d.refKeyOf(v) : '' return k ? ('ref:' + k) : '' } const str = String(v || '') if (str.indexOf('blob:') === 0) { try { const ref = d.refByUrl instanceof Map ? d.refByUrl.get(str) : null const rk = ref && typeof d.refKeyOf === 'function' ? d.refKeyOf(ref) : '' if (rk) return 'ref:' + rk } catch (e) { /* ignore */ } } return typeof d.rawKeyOf === 'function' ? d.rawKeyOf(str) : '' } // 屏幕上需要的长边 = 渲染长边 × 缩放 × dpr。 // v3.26.111(内存):**有缩略图就不再为了“再清晰一点”去解码原图**,最多忍到 2× 缩略图长边; // 原来一超过 max(1024)就整屏切原图 —— 实测放大到 2× 时 90 个节点同时解 4K 原图, // WebContent footprint 冲到 1.2G / 峰值 2.6G,缩放停手那一下也最卡。 // 现在放大后是缩略图“放大显示”(1.3~2× 轻微发虚),换取解码量恒定。 const ORIG_HARD_CAP = Math.max(1200, max * 2) function srcForNode (v, renderedLongEdge, scale) { const key = keyOf(v) const raw = key && cache ? cache.get(key) : '' // v3.26.112:缓存里可能是「被当缩略图存下来的原图 dataURL」(>512KB)——不可用,按缺失处理。 const thumb = (typeof raw === 'string' && raw.indexOf('data:') === 0 && raw.length > 512 * 1024) ? '' : raw const dpr = (typeof global.devicePixelRatio === 'number' && global.devicePixelRatio > 0) ? global.devicePixelRatio : 1 const s = Number(scale) const need = Math.max(1, Number(renderedLongEdge) || 0) * (Number.isFinite(s) && s > 0 ? s : 1) * dpr // v3.26.121:LOD 分档(mipmap 思路)。Chromium 按绘制尺寸解码,所以每档都不该超配: // need ≤ smallMax(256)→ 用小图(fit 视图下节点 ~180px×dpr1.5 ≈ 270,正好落这档) // 否则 ≤ ORIG_HARD_CAP → 512 缩略图 // 再大 → 原图 // 小图没就绪时先给 512(已有就秒出)或占位,绝不为了"小一档"去同步解原图。 if (key && smallCache && need <= smallMax) { const sm = smallCache.get(key) if (sm) return sm const orig0 = (v && typeof v === 'object') ? ((typeof d.resolveImg === 'function' ? d.resolveImg(v) : '') || '') : String(v || '') if (orig0 && !/^https?:/i.test(orig0) && typeof smallQueue === 'function') smallQueue(key, orig0) if (thumb) return thumb return /^https?:/i.test(orig0) ? orig0 : (thumb || '') } if (thumb && need <= ORIG_HARD_CAP) return thumb const orig = (v && typeof v === 'object') ? ((typeof d.resolveImg === 'function' ? d.resolveImg(v) : '') || '') : String(v || '') // v3.26.118:远程 http(s)(图床/生成结果)**不进缩略图队列**。 // 跨域图 fetch 必然失败(线上 CSP 与对方 CORS 都拦),实测只换来每条 3 次重试与一条 // thumb-fail img-error 日志,像素一点没省。本地图(blob:/data:/file:)照旧排队生成。 if (!thumb && key && orig && typeof d.queue === 'function' && !/^https?:/i.test(orig)) { d.queue(key, orig) // v3.26.111(3):本地图(blob:/data:/file:)缩略图还没就绪时先**占位**, // 不拿原图解码 —— 否则一次首屏就是 100 张 × 4MB。远程 http(s) 生成不了缩略图,仍直出。 if (!/^https?:/i.test(orig)) return '' } return orig } // 入库预热:字节还在内存里,此时生成缩略图最便宜 function prime (ref, dataUrl) { try { if (!ref || typeof dataUrl !== 'string' || dataUrl.indexOf('data:') !== 0) return false const key = keyOf(ref) if (!key || (cache && cache.has(key)) || typeof d.queue !== 'function') return false return d.queue(key, dataUrl) } catch (e) { return false } } return { keyOf: keyOf, srcForNode: srcForNode, prime: prime } } global.IWBImageThumbPick = { create: create } })(typeof window !== 'undefined' ? window : globalThis)