diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cbce8b..b807bda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,24 @@ > 3. 核验正式地址版本 → 之后才移动 `stable` / `stable-verified-<日期>` 标签、更新本 CHANGELOG、写 GitHub Release(`gh release create`)。 > 测试发现问题 → 改 → 再 `deploy:test`,正式环境不受影响。回滚点 `git reset --hard stable`。 +## v3.26.124(2026-11-30)插件图片策略:宿主三层 API + 全部插件改走小图 + +上一版把画布这条路封死了(原生缩略图),但插件那条还开着:相册取整幅原图再 canvas 缩放、 +批量导出逐个整幅解码、提示词库把大封面存进 plugin-data。这一版把"取图"收回到宿主。 + +- 宿主新增三层图片能力(ctx.images): + · thumb(v,{max=320}) → 原生小图 URL(网格/卡片/封面) + · preview(v,{max=2048}) → 原生缩放后的 dataURL(编辑/预览/导入) + · original(v) → 原图(仅导出/保存,显式调用) + · budget() → 当前解码账本(active/peak/total) +- 原生解码闸门(storage-file):原图解码并发 ≤2,>1024px 的 ≤1,所有缩略图只在**这一处**排队。 +- ctx.canvas.getNodeImage(id, {max}) 支持封顶;插件只要"看/预览"时拿到的是缩过的图。 +- 插件全部跟进(各自 bump 版本):album 1.2.4(网格走 thumb、大图走 preview 2048)、 + promptlib 1.14.10(封面入库前原生缩到 512)、batch-export 1.2.3(预览 64px)、 + image-annotate 1.20.9(入口 4096)、image-vectorize 0.6.2 / white-bg-800 1.2.2 / xianyi-cutout 1.0.4(入口 2048)。 +- check-plugins-sync 增加图片策略守卫(warn):插件里出现 dataUrl 取原图、getNodeImage 不带 max、 + createImageBitmap 直接解码时报警,下一版转 error。 + ## v3.26.123(2026-11-30)根治:缩略图改由原生生成,webview 不再解码 4K 原图 mac 实测 WebContent 进程 1GB+(整棵进程树 1.5~1.9GB),而画布只有 100 多张图、我们自己的结构只占 9~37MB。根因链: diff --git a/app.js b/app.js index 9151001..a61a52f 100644 --- a/app.js +++ b/app.js @@ -4986,6 +4986,88 @@ const tbCenterSeed = (type, refs) => { } // 缩略图队列(v3.25.67):并发上限 + 空闲启动,逻辑在 src/image-display-thumb.js + // ── v3.26.124 图片策略 v2:L0 thumb / L1 preview / L2 original ── + // 目的:把所有"取整幅原图再自己缩放"的路径收回到宿主,统一走原生 tier。 + function wRefPartsOf (v) { + if (!v || typeof v !== 'object') return null + if (typeof v.__blob === 'string' && v.__blob) return { kind: 'blob', key: v.__blob } + if (typeof v.__file === 'string' && v.__file) return { kind: 'file', key: v.__file } + return null + } + // 确保存在指定长边的 tier,返回 { url, h16, max } + async function wEnsureTier (v, max) { + try { + const F = wStoreMod() + if (!F || typeof F.makeDisplayThumb !== 'function') return null + const p = wRefPartsOf(v) + if (!p) return null + const m = Math.max(64, Number(max) || 320) + const refKey = 'ref:' + (p.kind === 'blob' ? 'b:' : 'f:') + p.key + const h16 = String(await wSha256(refKey)).slice(0, 16) + const r = p.kind === 'blob' + ? await F.makeDisplayThumb(h16, p.key, m) + : (typeof F.makeDisplayThumbFromPath === 'function' ? await F.makeDisplayThumbFromPath(h16, p.key, m) : null) + if (!r || !r.url) return null + return { url: r.url, h16: h16, max: m } + } catch (e) { return null } + } + // L0:小图 URL(网格/卡片/封面) + async function wPluginThumb (v, opts) { + const max = Math.max(64, Number(opts && opts.max) || 320) + try { + if (typeof v === 'string') { + if (/^https?:/i.test(v)) return v + const F = wStoreMod() + if (F && typeof F.resizeDataUrl === 'function' && v.indexOf('data:') === 0 && v.length > 200 * 1024) { + const small = await F.resizeDataUrl(v, max) + if (small) return small + } + return v + } + const t = await wEnsureTier(v, max) + return (t && t.url) || '' + } catch (e) { return '' } + } + // L1:预览(dataURL,封顶 max 长边);拿不到就退回原图,保证功能不降级 + async function wPluginPreview (v, opts) { + const max = Math.max(256, Number(opts && opts.max) || 2048) + try { + if (typeof v === 'string') { + const F0 = wStoreMod() + if (F0 && typeof F0.resizeDataUrl === 'function' && v.indexOf('data:') === 0) { + const small = await F0.resizeDataUrl(v, max) + if (small) return { ok: true, dataUrl: small, max: max } + } + return { ok: !!v, dataUrl: v, max: 0 } + } + const t = await wEnsureTier(v, max) + const F = wStoreMod() + if (t && F && typeof F.readDisplayThumb === 'function') { + const d = await F.readDisplayThumb(t.h16, max) + if (d) return { ok: true, dataUrl: d, max: max } + } + const raw = await wImgRefDataUrl(v) + return { ok: !!raw, dataUrl: raw || '', max: 0 } + } catch (e) { return { ok: false, dataUrl: '', max: 0 } } + } + // L2:原图(显式、单张) + async function wPluginOriginal (v) { + try { + const d = await wImgRefDataUrl(v) + return { ok: !!d, dataUrl: d || '' } + } catch (e) { return { ok: false, dataUrl: '' } } + } + function wPluginDecodeBudget () { + try { const F = wStoreMod(); return (F && typeof F.decodeStats === 'function') ? F.decodeStats() : { active: 0, peak: 0, total: 0 } } catch (e) { return { active: 0, peak: 0, total: 0 } } + } + // 给"只拿到 dataURL"的调用方用:原生缩一档(避免 WebKit 整幅解码) + async function wShrinkDataUrl (dataUrl, max) { + try { + const F = wStoreMod() + if (F && typeof F.resizeDataUrl === 'function') return await F.resizeDataUrl(dataUrl, max) + } catch (e) { /* ignore */ } + return '' + } // v3.26.123:原生缩略图入口(桌面端)。key 形如 ref:b: ⇒ 让 Rust 按目标尺寸 // 解码/缩放/编码,返回 objectURL;失败返回 ''(调用方退回 JS 解码路径)。 function wNativeThumb (key, max) { @@ -5966,10 +6048,19 @@ const tbCenterSeed = (type, refs) => { return n ? { id: n.id, type: n.type, name: String((n.data && (n.data.name || n.data.title)) || '') } : null }, // v3.26.61:逻辑在 src/gallery-sync.js 的 nodeImageOf(补 localRef 候选 + 远程按需落地) - getNodeImage: async (id) => { + getNodeImage: async (id, opts) => { const n = refs.nodes.find((x) => x.id === id) if (!n || !wGallerySync) return null - return wGallerySync.nodeImageOf(n, { writeRef: (src) => wImgRefWrite(src), onLocalRef: (ref) => updateNode(n.id, { localRef: ref }) }) + const img = await wGallerySync.nodeImageOf(n, { writeRef: (src) => wImgRefWrite(src), onLocalRef: (ref) => updateNode(n.id, { localRef: ref }) }) + // v3.26.124:插件只要能看/能预览时,给**缩过的**图,别让它整幅解码原图 + const max = Math.max(64, Number(opts && opts.max) || 0) + if (!img || !max) return img + try { + const src = img.localRef || img.url || img.dataUrl + if (src && typeof src === 'object') { const t = await wEnsureTier(src, max); if (t) { const d = await wPluginPreview(src, { max }); if (d && d.ok) return Object.assign({}, img, { dataUrl: d.dataUrl, capped: max }) } } + else if (typeof src === 'string' && src.indexOf('data:') === 0) { const s2 = await wShrinkDataUrl(src, max); if (s2) return Object.assign({}, img, { dataUrl: s2, capped: max }) } + } catch (e) { /* ignore */ } + return img }, createNode: (type, x, y, data) => { // 插件节点传的是变体 id(web/style…):不在 NODE_TYPES 里,交给 addPluginNode 拼出 plugin:<插件>:<变体> @@ -8271,6 +8362,10 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) => importDataUrl: (url, opts) => new Promise((resolve) => { try { wImportImage(url, opts || {}, (p) => resolve(p || { ok: false, error: 'import-failed' })) } catch (e) { resolve({ ok: false, error: 'import-threw' }) } }), readDataUrl: (v) => (typeof v === 'string' ? Promise.resolve(v) : wImgRefDataUrl(v)), thumbUrlOf: (v) => wDisplayThumbOf(v), + thumbFor: (v, o) => wPluginThumb(v, o), + previewFor: (v, o) => wPluginPreview(v, o), + originalFor: (v) => wPluginOriginal(v), + budgetOf: () => wPluginDecodeBudget(), })(ctx) }, files: (ctx) => { @@ -8413,7 +8508,7 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) => getNodes: () => (wPluginCanvasBridge ? wPluginCanvasBridge.getNodes() : []), listNodeTypes: () => [...NODE_TYPES.keys()], getSelectedNode: () => (wPluginCanvasBridge ? wPluginCanvasBridge.getSelectedNode() : null), - getNodeImage: async (id) => (wPluginCanvasBridge ? wPluginCanvasBridge.getNodeImage(id) : null), + getNodeImage: async (id, opts) => (wPluginCanvasBridge ? wPluginCanvasBridge.getNodeImage(id, opts) : null), // 只读坐标换算:插件拖拽插入时把「松手点」换成世界坐标(不给插件 viewport,避免它自己算偏) screenToWorld: (clientX, clientY) => (wPluginCanvasBridge ? wPluginCanvasBridge.screenToWorld(clientX, clientY) : null), }), diff --git a/package.json b/package.json index 60e9342..a9047fb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "image-workbench-web", "private": true, - "version": "3.26.123", + "version": "3.26.124", "description": "Browser-based node canvas image workbench", "scripts": { "build": "node scripts/build.js && node scripts/copy-plugins.js", diff --git a/plugins/album/main.js b/plugins/album/main.js index 9cf4574..5161c87 100644 --- a/plugins/album/main.js +++ b/plugins/album/main.js @@ -669,11 +669,23 @@ } } } catch (e) { /* 没缓存就现做 */ } - let dataUrl = '' + // v1.2.4(图片策略 v2):先让**宿主原生**出 320 小图 —— 不再把整幅原图读成 dataURL + // 再在 webview 里 canvas 缩放(那是 WebKit 上每次 21MB 的整幅解码,一夹 100 张就是 GB 级)。 const src = it && it.src try { - if (fileRefOf(src)) dataUrl = await ctx.images.dataUrl(src) - else if (typeof src === 'string') dataUrl = src + if (ctx.images && typeof ctx.images.thumb === 'function') { + const u = await ctx.images.thumb(src, { max: 320 }) + if (u && typeof u === 'string' && u.length < 512 * 1024) { + st.thumbs.set(key, u) + if (st.forceThumbs) st.forceThumbs.delete(key) + // 不再往 plugin-data 另存一份:宿主已在 thumbs/ 里按 hash 落了 tier,重复存等于两份 + return u + } + } + } catch (e) { /* 回退到本地缩放 */ } + let dataUrl = '' + try { + if (typeof src === 'string' && src.indexOf('data:') === 0) dataUrl = src else dataUrl = await ctx.images.dataUrl(src) } catch (e) { dataUrl = '' } if (!dataUrl) return '' @@ -742,7 +754,11 @@ const flight = pending.get(key) if (flight) return flight if (!ctx.images || typeof ctx.images.dataUrl !== 'function') return Promise.resolve('') - const p = Promise.resolve(ctx.images.dataUrl(it && it.src)).then((u) => { + // v1.2.4:大图也只取 2048 长边的预览(原生缩放),避免整幅解码 4K 原图 + const ask = (ctx.images && typeof ctx.images.preview === 'function') + ? Promise.resolve(ctx.images.preview(it && it.src, { max: 2048 })).then((r) => (r && r.dataUrl) || '') + : Promise.resolve(ctx.images.dataUrl(it && it.src)) + const p = ask.then((u) => { pending.delete(key) if (!u) return '' cache.set(key, u) diff --git a/plugins/album/manifest.json b/plugins/album/manifest.json index 59336f0..7037710 100644 --- a/plugins/album/manifest.json +++ b/plugins/album/manifest.json @@ -1,7 +1,7 @@ { "id": "album", "name": "相册", - "version": "1.2.3", + "version": "1.2.4", "apiVersion": 1, "main": "main.js", "style": "styles.css", @@ -14,7 +14,7 @@ "出图" ], "files": { - "main.js": "4b5987a4a39451214b356b0b08add9b0d4ad5517930a6314b077f543bd2a0def", + "main.js": "79ac746f0065103a32b083e2d79f16fb09fcf9032709952ba256887db1bedb4a", "styles.css": "40623c831620d0082dde956300461b29b9256aab98da86c7ab5e3495214f8d49" } } diff --git a/plugins/index.json b/plugins/index.json index 787311b..39065e5 100644 --- a/plugins/index.json +++ b/plugins/index.json @@ -4,17 +4,17 @@ "dir": "album", "main": "main.js", "style": "styles.css", - "version": "1.2.3", + "version": "1.2.4", "files": { - "main.js": "4b5987a4a39451214b356b0b08add9b0d4ad5517930a6314b077f543bd2a0def", + "main.js": "79ac746f0065103a32b083e2d79f16fb09fcf9032709952ba256887db1bedb4a", "styles.css": "40623c831620d0082dde956300461b29b9256aab98da86c7ab5e3495214f8d49" } }, { "id": "batch-export", - "file": "plugin-batch-export.44c13ac8e6d9.js", - "version": "1.2.2", - "integrity": "sha256-RME6yObZW4tKMZmfwtaIX4iODMFlo57baLn/y0GZbfU=" + "file": "plugin-batch-export.e0152de8d928.js", + "version": "1.2.3", + "integrity": "sha256-4BUt6NkoAWOoCk1omq3EcE3CZdexZyEqs9qeYqtnwb0=" }, { "id": "blank-canvas", @@ -55,23 +55,23 @@ "dir": "promptlib", "main": "main.js", "style": "styles.css", - "version": "1.14.9", + "version": "1.14.10", "files": { - "main.js": "33dc53a4bb293bb9464be4ade48ce3099a740f75687d54be0244883774c9d461", + "main.js": "a05ce22ad8ab2e079e313dd31d8fe34f2e2e07e017bfb782afcd08047e0b485b", "styles.css": "86f73b4d1c7b827f97ec1135c9130c7da6898537b2ed2ab815e282618232f56b" } }, { "id": "image-annotate", - "file": "plugin-image-annotate.d822acf4cfae.js", - "version": "1.20.8", - "integrity": "sha256-2CKs9M+uirlLOMFYJ0dMnGnTvFkwdw2m0SzeNZ++3A8=" + "file": "plugin-image-annotate.fda7cfeefb9b.js", + "version": "1.20.9", + "integrity": "sha256-/afP7vub8Psm2dGzos2M4fcE/GxmS2I9Jvx523xCZr0=" }, { "id": "image-vectorize", - "file": "plugin-image-vectorize.0e5a7fa018b2.js", - "version": "0.6.1", - "integrity": "sha256-Dlp/oBiy6d5wX5ESJyrQLc8Ye1NhaW9/wKEqk8TyDU4=" + "file": "plugin-image-vectorize.2d9e6c796bf6.js", + "version": "0.6.2", + "integrity": "sha256-LZ5seWv25KomL7q8Wa5VGALyj16HWk805YbLyUEDgPs=" }, { "id": "sticky", @@ -87,14 +87,14 @@ }, { "id": "white-bg-800", - "file": "plugin-white-bg-800.a5a92f045c0d.js", - "version": "1.2.1", - "integrity": "sha256-pakvBFwNQEayBVYgGbKmNUr9FKqSN1t4VZ1I8s6bdO4=" + "file": "plugin-white-bg-800.988d72d6f09d.js", + "version": "1.2.2", + "integrity": "sha256-mI1y1vCd5IF8UTGdOzggB4OnCpQrQkmCclL6y9w86IE=" }, { "id": "xianyi-cutout", - "file": "plugin-xianyi-cutout.79c8c47dfc61.js", - "version": "1.0.3", - "integrity": "sha256-ecjEffxhh7ivJoh2Pn9jA0uyaGfM778TXtdtN0XW4k0=" + "file": "plugin-xianyi-cutout.84907706bb31.js", + "version": "1.0.4", + "integrity": "sha256-hJB3Brsx1krtgrhXhEg7kTNTAaitQeDkSBOiud/he0c=" } ] diff --git a/plugins/promptlib/main.js b/plugins/promptlib/main.js index 0f4b30e..198fe7f 100644 --- a/plugins/promptlib/main.js +++ b/plugins/promptlib/main.js @@ -607,13 +607,25 @@ // dataURL → { img: 文件名 };失败原样返回(宁可留着内联 base64,也不丢封面) function ensureCoverFile (cover) { if (!FILEAPI || typeof cover !== 'string' || cover.indexOf('data:') !== 0) return Promise.resolve(cover) - var name = coverFileName(cover) - pendingCoverNames[name] = 1 - return Promise.resolve(FILEAPI.put(name, cover)).then(function (r) { - if (r && r.ok) return { img: name } - delete pendingCoverNames[name] - return cover - }).catch(function () { delete pendingCoverNames[name]; return cover }) + // v: 封面入库前先在**原生侧**缩到 512 —— 大封面会永久占 plugin-data, + // 而且每次显示都要整幅解码(WebKit 上 4K 封面 = 21MB/次)。 + var prepped = Promise.resolve(cover) + try { + if (PLP_CTX && PLP_CTX.images && typeof PLP_CTX.images.preview === 'function') { + prepped = Promise.resolve(PLP_CTX.images.preview(cover, { max: 512 })) + .then(function (r) { return (r && r.ok && r.dataUrl) ? r.dataUrl : cover }) + .catch(function () { return cover }) + } + } catch (e) { /* 退回未压缩 */ } + return prepped.then(function (small) { + var name = coverFileName(small) + pendingCoverNames[name] = 1 + return Promise.resolve(FILEAPI.put(name, small)).then(function (r) { + if (r && r.ok) return { img: name } + delete pendingCoverNames[name] + return small + }).catch(function () { delete pendingCoverNames[name]; return small }) + }) } // 回收未被任何词条引用的封面文件(判定在模块级 plpGcCoverFiles,设置页复用同一套)。 // 返回 { removed, bytes, skipped } diff --git a/plugins/promptlib/manifest.json b/plugins/promptlib/manifest.json index b0541df..f8c5e98 100644 --- a/plugins/promptlib/manifest.json +++ b/plugins/promptlib/manifest.json @@ -1,7 +1,7 @@ { "id": "promptlib", "name": "提示词库", - "version": "1.14.9", + "version": "1.14.10", "apiVersion": 1, "main": "main.js", "style": "styles.css", @@ -13,7 +13,7 @@ "提示词" ], "files": { - "main.js": "33dc53a4bb293bb9464be4ade48ce3099a740f75687d54be0244883774c9d461", + "main.js": "a05ce22ad8ab2e079e313dd31d8fe34f2e2e07e017bfb782afcd08047e0b485b", "styles.css": "86f73b4d1c7b827f97ec1135c9130c7da6898537b2ed2ab815e282618232f56b" } } diff --git a/plugins/runtime/plugin-batch-export.44c13ac8e6d9.js b/plugins/runtime/plugin-batch-export.e0152de8d928.js similarity index 99% rename from plugins/runtime/plugin-batch-export.44c13ac8e6d9.js rename to plugins/runtime/plugin-batch-export.e0152de8d928.js index 7ce7d8a..6eabf0a 100644 --- a/plugins/runtime/plugin-batch-export.44c13ac8e6d9.js +++ b/plugins/runtime/plugin-batch-export.e0152de8d928.js @@ -577,7 +577,7 @@ async function makeSrcThumb(nodeId) { if (!ctx.canvas || typeof ctx.canvas.getNodeImage !== 'function') return null - var image = await ctx.canvas.getNodeImage(nodeId) + var image = await ctx.canvas.getNodeImage(nodeId, { max: 64 }) // v: 缩略预览只要 64px,别整幅解码 var src = image && (image.dataUrl || image.url) if (!src) return null var img = await loadImageEl(src) diff --git a/plugins/runtime/plugin-image-annotate.d822acf4cfae.js b/plugins/runtime/plugin-image-annotate.fda7cfeefb9b.js similarity index 99% rename from plugins/runtime/plugin-image-annotate.d822acf4cfae.js rename to plugins/runtime/plugin-image-annotate.fda7cfeefb9b.js index c4fdd55..954e953 100644 --- a/plugins/runtime/plugin-image-annotate.d822acf4cfae.js +++ b/plugins/runtime/plugin-image-annotate.fda7cfeefb9b.js @@ -9214,7 +9214,8 @@ var __IWB_ANNOTATE_MANIFEST__ = { if (!editor || typeof editor.open !== "function") { return Promise.reject(new Error("标注编辑器未加载")); } - return Promise.resolve(ctx.canvas.getNodeImage(id)).then(function(image) { + // v: 编辑器入口封顶 4096 长边(原生缩),避免 8K 原图整幅进 canvas + return Promise.resolve(ctx.canvas.getNodeImage(id, { max: 4096 })).then(function(image) { if (!image || !image.dataUrl) { throw new Error(image && image.url ? "该节点是远程图片,暂不支持标注(请先转存为本地位图)" : "该节点没有可编辑的图片"); } diff --git a/plugins/runtime/plugin-image-vectorize.0e5a7fa018b2.js b/plugins/runtime/plugin-image-vectorize.2d9e6c796bf6.js similarity index 99% rename from plugins/runtime/plugin-image-vectorize.0e5a7fa018b2.js rename to plugins/runtime/plugin-image-vectorize.2d9e6c796bf6.js index 13a3d93..a6fc2c6 100644 --- a/plugins/runtime/plugin-image-vectorize.0e5a7fa018b2.js +++ b/plugins/runtime/plugin-image-vectorize.2d9e6c796bf6.js @@ -3691,7 +3691,7 @@ var __IWB_VECTORIZE_MANIFEST__ = { }); } async function resolveImage(nodeId) { - const image = await ctx.canvas.getNodeImage(nodeId); + const image = await ctx.canvas.getNodeImage(nodeId, { max: 2048 }); // v: 预览/矢量化封顶 2048 if (!image) throw new Error("该节点没有可描摹的图片"); if (image.dataUrl) return image.dataUrl; if (!image.url) throw new Error("该节点没有可描摹的图片"); diff --git a/plugins/runtime/plugin-white-bg-800.a5a92f045c0d.js b/plugins/runtime/plugin-white-bg-800.988d72d6f09d.js similarity index 99% rename from plugins/runtime/plugin-white-bg-800.a5a92f045c0d.js rename to plugins/runtime/plugin-white-bg-800.988d72d6f09d.js index 1fcd8a0..b59a0bb 100644 --- a/plugins/runtime/plugin-white-bg-800.a5a92f045c0d.js +++ b/plugins/runtime/plugin-white-bg-800.988d72d6f09d.js @@ -166,7 +166,7 @@ // 绝不对 data: URL 发网络请求 —— 宿主 CSP connect-src 不含 data:,必被拦(姊妹插件 image-annotate 踩过并修掉的坑)。 // 远程 url 才走 fetch,转成 blob: URL 再解码(blob: 同样不污染画布)。 async function resolveImage(nodeId) { - const image = await ctx.canvas.getNodeImage(nodeId) + const image = await ctx.canvas.getNodeImage(nodeId, { max: 2048 }) // v: 白底处理封顶 2048 if (!image) throw new Error(NO_IMAGE) if (image.dataUrl) return loadImage(image.dataUrl) diff --git a/plugins/runtime/plugin-xianyi-cutout.79c8c47dfc61.js b/plugins/runtime/plugin-xianyi-cutout.84907706bb31.js similarity index 97% rename from plugins/runtime/plugin-xianyi-cutout.79c8c47dfc61.js rename to plugins/runtime/plugin-xianyi-cutout.84907706bb31.js index 724cc8c..f40658e 100644 --- a/plugins/runtime/plugin-xianyi-cutout.79c8c47dfc61.js +++ b/plugins/runtime/plugin-xianyi-cutout.84907706bb31.js @@ -47,7 +47,7 @@ }, }, function (ctx) { async function cutoutOne(nodeId) { - const image = await ctx.canvas.getNodeImage(nodeId) + const image = await ctx.canvas.getNodeImage(nodeId, { max: 2048 }) // v: 抠图封顶 2048 if (!image || !image.dataUrl) throw new Error('该节点没有可编码的图片(请选中带图的参考图或输出节点)') const apiUrl = trimUrl(ctx.settings.get('apiUrl')) diff --git a/scripts/check-plugins-sync.js b/scripts/check-plugins-sync.js index c7be806..156208e 100644 --- a/scripts/check-plugins-sync.js +++ b/scripts/check-plugins-sync.js @@ -67,6 +67,26 @@ for (const id of fs.readdirSync(srcRoot)) { } } +// ── 图片策略守卫(v3.26.124,warn 阶段)───────────────────────────────── +// 插件里出现「取整幅原图再自己缩放」的写法,在 WebKit 上每次都是 21MB 级解码, +// 正是 mac 端 GB 级占用的来源。这里只报警不阻塞(下一版转 error)。 +const IMG_WARN = [ + { re: /ctx\.images\.dataUrl\(/g, why: 'ctx.images.dataUrl 会取整幅原图;预览请用 ctx.images.preview({max}),网格请用 thumb({max})' }, + { re: /getNodeImage\(\s*[A-Za-z0-9_.$]+\s*\)/g, why: 'getNodeImage 未传 {max}:插件只需看/预览时请传 {max:2048}(导出才拿原图)' }, + { re: /createImageBitmap\(/g, why: '插件里 createImageBitmap 会整幅解码;改用 ctx.images.thumb/preview 让宿主原生缩放' }, +] +try { + for (const id of fs.readdirSync(srcRoot)) { + const entry = path.join(srcRoot, id, 'src', 'index.js') + if (!fs.existsSync(entry)) continue + const code = fs.readFileSync(entry, 'utf8') + for (const rule of IMG_WARN) { + const hits = (code.match(rule.re) || []).length + if (hits) console.log('[img-policy][warn] ' + id + ' ×' + hits + ':' + rule.why) + } + } +} catch (e) { /* 源码仓不可读时跳过 */ } + if (problems.length) { console.error('[check-plugins] 两仓不一致(' + problems.length + ' 项):') for (const p of problems) console.error(' - ' + p) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index fd0a426..df44713 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2014,7 +2014,7 @@ dependencies = [ [[package]] name = "image-workbench" -version = "3.26.123" +version = "3.26.124" dependencies = [ "arboard", "base64 0.22.1", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 85f9346..2c2c44d 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "image-workbench" -version = "3.26.123" +version = "3.26.124" description = "无限画布 · 生图工作台桌面版" authors = ["malongan"] license = "" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 27d93d5..413c9eb 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "../node_modules/@tauri-apps/cli/config.schema.json", "productName": "image-workbench", - "version": "3.26.123", + "version": "3.26.124", "identifier": "com.malongan.imageworkbench", "build": { "frontendDist": "../dist", diff --git a/src/plugin-api-images.js b/src/plugin-api-images.js index 455927e..1854d54 100644 --- a/src/plugin-api-images.js +++ b/src/plugin-api-images.js @@ -29,6 +29,11 @@ const importDataUrl = need('importDataUrl') const readDataUrl = need('readDataUrl') const thumbUrlOf = need('thumbUrlOf') + // v3.26.124 图片策略 v2:L0 thumb / L1 preview / L2 original + const thumbFor = (typeof D.thumbFor === 'function') ? D.thumbFor : null + const previewFor = (typeof D.previewFor === 'function') ? D.previewFor : null + const originalFor = (typeof D.originalFor === 'function') ? D.originalFor : null + const budgetOf = (typeof D.budgetOf === 'function') ? D.budgetOf : null const str = (v) => (v == null ? '' : String(v)) const IMAGE_EXT = ['png', 'jpg', 'jpeg', 'webp', 'gif', 'avif', 'bmp', 'svg'] @@ -52,8 +57,17 @@ stat: (path) => fileRef.stat(path), // 读字节(base64 dataURL):只给「需要预览/上传」的显式场景 dataUrl: (v) => readDataUrl(v), - // 缩略图地址(同步;宿主内部 768 缓存,不解析原图字节) - thumb: (v) => thumbUrlOf(v), + // 缩略图地址(同步;宿主内部缓存,不解析原图字节) + thumbSync: (v) => thumbUrlOf(v), + // L0 小图(异步、原生生成):给网格/卡片/封面。默认 320 长边。 + thumb: (v, opts) => (thumbFor ? thumbFor(v, opts) : Promise.resolve('')), + // L1 预览(异步、原生缩放后返回 dataURL):给编辑/预览/导入。默认封顶 2048 长边。 + // 插件**不应该**再用 dataUrl 拿整幅原图去自己缩放。 + preview: (v, opts) => (previewFor ? previewFor(v, opts) : readDataUrl(v).then((u) => ({ ok: !!u, dataUrl: u || '' }))), + // L2 原图(显式、单张、带租约):只给导出/保存这类必须完整字节的场景。 + original: (v) => (originalFor ? originalFor(v) : readDataUrl(v).then((u) => ({ ok: !!u, dataUrl: u || '' }))), + // 宿主解码账本(active/peak/total):插件面板可用来显示"当前解码数" + budget: () => (budgetOf ? budgetOf() : { active: 0, peak: 0, total: 0 }), // 把本地文件字节写进宿主存储(拖拽=复制、以及「固化」按钮) importPath: async (path, opts) => { if (!isImagePath(path)) return { ok: false, error: 'not-image' } diff --git a/src/storage-file.js b/src/storage-file.js index 3064504..a63a315 100644 --- a/src/storage-file.js +++ b/src/storage-file.js @@ -1108,10 +1108,10 @@ } return null } - async function readDisplayThumb(hash) { + async function readDisplayThumb(hash, max) { try { if (!enabled() || !dir || !hash) return '' - const f = await thumbFileOf(hash) + const f = await thumbFileOf(hash, max) if (!f) return '' const u8 = await fs().readFile(f.path) if (!u8 || !u8.length) return '' @@ -1208,21 +1208,32 @@ // v3.26.123:原生缩略图。有就返回,没有就让 Rust 按目标尺寸解码/缩放/编码后写盘,再读成 objectURL。 // 关键:整条链里 webview 只接触几十 KB 的小图,4K 原图永远不会被 WebKit 解码(mac 端 GB 级占用的根源)。 - async function makeDisplayThumb (hash, srcKey, max) { + // 原生解码闸门(全局唯一收口):原图解码并发 ≤2,>4096px 的 ≤1。 + // 所有插件/画布的缩略图都经这里 ⇒ 宿主第一次真正掌握"此刻有几张原图在被解码"。 + const dec = { active: 0, big: 0, peak: 0, total: 0 } + // 真正的闸门:并发上限 2;>1024px 的(走原图整幅解码)上限 1。 + const runWithSlot = async (big, fn) => { + const cap = () => (big ? 1 : 2) + while (dec.active >= cap()) await new Promise((r) => setTimeout(r, 40)) + dec.active++; dec.total++; if (big) dec.big++ + if (dec.active > dec.peak) dec.peak = dec.active + try { return await fn() } finally { dec.active--; if (big) dec.big-- } + } + function decodeStats () { return { active: dec.active, peak: dec.peak, total: dec.total, big: dec.big, queued: 0 } } + + // 按**绝对路径**生成 tier(相册的 {__file} 引用走这条) + async function makeDisplayThumbFromPath (hash, absPath, max) { try { - if (!enabled() || !dir || !hash || !srcKey) return null + if (!enabled() || !dir || !hash || !absPath) return null const m = Math.max(64, Number(max) || 768) const have = await thumbFileOf(hash, m) if (have) return { url: await readDisplayThumbUrl(hash, m), path: have.path, cached: true } - const a = assets[String(srcKey)] - if (!a) return null - const src = await join(dir, a.f) const base = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_' + m) try { await fs().mkdir(await join(dir, IMG_DIR, 'thumbs'), { recursive: true }) } catch (e) { /* ignore */ } const T = (typeof window !== 'undefined') ? window.__TAURI__ : null const inv = T && T.core && T.core.invoke if (typeof inv !== 'function') return null - const r = await inv('img_thumbs', { src, jobs: [{ base, max: m }] }) + const r = await runWithSlot(m > 1024, () => inv('img_thumbs', { src: String(absPath), jobs: [{ base, max: m }] })) if (!r || !r.ok || !Array.isArray(r.files) || !r.files.length) return null const url = await readDisplayThumbUrl(hash, m) if (!url) return null @@ -1230,6 +1241,54 @@ } catch (e) { return null } } + // 把一段 dataURL 在**原生侧**缩到 max 长边(不经过 webview 解码)。 + // 用途:插件拿到的是 dataURL(封面、节点图)时,也能避免 WebKit 整幅解码大图。 + let tmpSeq = 0 + async function resizeDataUrl (dataUrl, max) { + try { + if (!enabled() || !dir) return '' + const s = String(dataUrl || '') + const m = /^data:([^;,]+)?(;base64)?,(.*)$/.exec(s) + if (!m || !m[2]) return '' + const mime = String(m[1] || 'image/png').toLowerCase() + const ext = mime.indexOf('jpeg') >= 0 ? 'jpg' : (mime.indexOf('webp') >= 0 ? 'webp' : (mime.indexOf('gif') >= 0 ? 'gif' : 'png')) + const bin = atob(m[3]) + const u8 = new Uint8Array(bin.length) + for (let i = 0; i < bin.length; i++) u8[i] = bin.charCodeAt(i) + const cfg = await join(dir, 'cache') + try { await fs().mkdir(cfg, { recursive: true }) } catch (e) { /* ignore */ } + tmpSeq = (tmpSeq + 1) % 100000 + const tag = 'rt' + Date.now().toString(36) + tmpSeq + const src = await join(cfg, tag + '.' + ext) + const base = await join(cfg, tag + '_out') + await fs().writeFile(src, u8) + try { + const t = (typeof window !== 'undefined') ? window.__TAURI__ : null + const inv = t && t.core && t.core.invoke + if (typeof inv !== 'function') return '' + const r = await runWithSlot(true, () => inv('img_thumbs', { src, jobs: [{ base, max: Math.max(64, Number(max) || 512) }] })) + if (!r || !r.ok || !Array.isArray(r.files) || !r.files.length) return '' + const out = await fs().readFile(r.files[0].path) + if (!out || !out.length) return '' + const durl = bytesToDataUrl(sniffMime(out) || 'image/jpeg', out) + try { await fs().remove(r.files[0].path) } catch (e) { /* ignore */ } + return durl + } finally { + try { await fs().remove(src) } catch (e) { /* ignore */ } + } + } catch (e) { return '' } + } + + async function makeDisplayThumb (hash, srcKey, max) { + try { + if (!enabled() || !dir || !hash || !srcKey) return null + const a = assets[String(srcKey)] + if (!a) return null + const src = await join(dir, a.f) + return await makeDisplayThumbFromPath(hash, src, max) + } catch (e) { return null } + } + async function writeDisplayThumbBlob(hash, blob) { try { if (!enabled() || !dir || !hash || !blob) return false @@ -1448,7 +1507,7 @@ enabled, ready: init, flush, // 供其它模块把诊断写进 _boot.log(v3.26.109) logLine: (m) => blog(m), - runtime, pickDir, switchTo, migrateTo, deleteImageBytes, deleteGalleryBytesFor, galleryAudit, gcImages, rotateTrash, galleryThumb, readDisplayThumb, readDisplayThumbUrl, makeDisplayThumb, writeDisplayThumb, writeDisplayThumbBlob, pruneThumbs, cleanupLegacy, maintain, + runtime, pickDir, switchTo, migrateTo, deleteImageBytes, deleteGalleryBytesFor, galleryAudit, gcImages, rotateTrash, galleryThumb, readDisplayThumb, readDisplayThumbUrl, makeDisplayThumb, makeDisplayThumbFromPath, resizeDataUrl, decodeStats, writeDisplayThumb, writeDisplayThumbBlob, pruneThumbs, cleanupLegacy, maintain, plugins: { manifest: pluginManifest, read: pluginRead, write: pluginWrite, remove: pluginRemove, dir: pluginDir, listLocal: pluginListLocal, listDirs: pluginListDirs, readFile: pluginReadFile, writeFile: pluginWriteFile, writeBinary: pluginWriteBinary, mkdir: pluginMkdir, removeDir: pluginRemoveDir, renameDir: pluginRenameDir, existsDir: pluginExistsDir }, pluginData: { read: pluginDataRead, write: pluginDataWrite, remove: pluginDataRemove, list: pluginDataList }, pluginFiles: { put: pluginFilePut, get: pluginFileGet, list: pluginFileList, remove: pluginFileRemove, stat: pluginFileStat, dir: pluginFilesDir, readBytes: pluginFileReadBytes, listAll: pluginFilesListAll },