fix(image-annotate): 1.1.2 输出预设收口(JPG 自动白底、边长/体积控件、maxKB 只属电商预设、下载与还原后摘要刷新)

This commit is contained in:
qiqi committed 2026-11-04 18:09:06 +08:00
1 parent 9a0f5bab69
commit a4e89f5f8b
9 files changed
+312 -47

No files matched your search

+109 -21
View File
@@ -9,7 +9,7 @@
var __IWB_ANNOTATE_MANIFEST__ = {
id: 'image-annotate',
name: '标注 / 修图',
version: '1.1.1',
version: '1.1.2',
apiVersion: 1,
capabilities: ['commands', 'nodeActions', 'canvas', 'canvasWrite'],
settings: {
@@ -1648,7 +1648,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
<div class="iwb-editor-export-settings">
<div class="iwb-editor-export-row"><span>预设</span>
<button data-export-key="preset" data-export-value="original">原尺寸</button>
<button data-export-key="preset" data-export-value="ecom800">电商 800² ≤500KB</button>
<button data-export-key="preset" data-export-value="ecom800">电商方图</button>
</div>
<div class="iwb-editor-export-row"><span>格式</span>
<button data-export-key="format" data-export-value="png">PNG</button>
@@ -1659,6 +1659,18 @@ var __IWB_ANNOTATE_MANIFEST__ = {
<button data-export-key="bg" data-export-value="transparent">透明</button>
<button data-export-key="bg" data-export-value="white">白底</button>
</div>
<div class="iwb-editor-export-row" data-export-when="ecom800"><span>边长</span>
<button data-export-key="size" data-export-value="750">750</button>
<button data-export-key="size" data-export-value="800">800</button>
<button data-export-key="size" data-export-value="1000">1000</button>
<button data-export-key="size" data-export-value="1200">1200</button>
</div>
<div class="iwb-editor-export-row" data-export-when="ecom800" title="商品图体积上限(仅对 JPG / WebP 生效,PNG 无法降质)"><span>≤KB</span>
<button data-export-key="maxKB" data-export-value="0">不限</button>
<button data-export-key="maxKB" data-export-value="300">300</button>
<button data-export-key="maxKB" data-export-value="500">500</button>
<button data-export-key="maxKB" data-export-value="800">800</button>
</div>
<div class="iwb-editor-export-note">一次只输出一个结果;设置会记住</div>
</div>
</div>
@@ -5302,6 +5314,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
S.redoStack = [];
clearAllEditState();
updateAllUI();
if (S.renderAll) S.renderAll();
toast("已还原为原图(裁剪后的画布尺寸已复位)", "success");
return;
}
@@ -5403,6 +5416,15 @@ var __IWB_ANNOTATE_MANIFEST__ = {
function settings() {
return S.callbacks && S.callbacks.settings;
}
function effectiveBg(o) {
o = o || S.exportOptions || DEFAULT_EXPORT;
return o.bg === "white" || o.format === "jpeg" ? "white" : "transparent";
}
function exportSignature(o) {
o = o || S.exportOptions || DEFAULT_EXPORT;
const ecom = o.preset === "ecom800";
return [o.preset, o.format, ecom ? o.size : "orig", effectiveBg(o), ecom ? o.maxKB : 0].join(":");
}
function loadExportOptions() {
const st = settings();
const get = (k, d) => {
@@ -5416,27 +5438,58 @@ var __IWB_ANNOTATE_MANIFEST__ = {
}
return v;
};
const preset = get("preset", DEFAULT_EXPORT.preset) === "ecom800" ? "ecom800" : "original";
const format = ["png", "jpeg", "webp"].indexOf(get("format", DEFAULT_EXPORT.format)) >= 0 ? get("format", DEFAULT_EXPORT.format) : "png";
const rawMax = get("maxKB", null);
const rawSize = get("size", null);
S.exportOptions = {
preset: get("preset", DEFAULT_EXPORT.preset) === "ecom800" ? "ecom800" : "original",
format: ["png", "jpeg", "webp"].indexOf(get("format", DEFAULT_EXPORT.format)) >= 0 ? get("format", DEFAULT_EXPORT.format) : "png",
bg: get("bg", DEFAULT_EXPORT.bg) === "white" ? "white" : "transparent",
maxKB: Math.max(0, Number(get("maxKB", 0)) || 0),
size: Math.max(64, Number(get("size", 800)) || 800)
preset,
format,
// JPG 下背景直接收敛到白底,状态本身自洽,不靠 UI 撒谎
bg: format === "jpeg" ? "white" : get("bg", DEFAULT_EXPORT.bg) === "white" ? "white" : "transparent",
maxKB: preset === "ecom800" ? rawMax === null ? 500 : Math.max(0, Number(rawMax) || 0) : 0,
size: Math.max(64, Number(rawSize === null ? 800 : rawSize) || 800)
};
if (S.exportOptions.preset === "ecom800" && !S.exportOptions.maxKB) S.exportOptions.maxKB = 500;
renderExportUI();
}
function setExportOption(key, value) {
if (!S.exportOptions) loadExportOptions();
S.exportOptions[key] = value;
if (key === "preset" && value === "ecom800") S.exportOptions.maxKB = 500;
if (key === "preset" && value === "original") S.exportOptions.maxKB = 0;
const o = S.exportOptions;
const st = settings();
if (st && typeof st.set === "function") {
try {
st.set(SETTINGS_KEY[key], value);
} catch (e) {
const persist = (k, v) => {
if (st && typeof st.set === "function") {
try {
st.set(SETTINGS_KEY[k], v);
} catch (e) {
}
}
};
if (key === "preset") {
o.preset = value === "ecom800" ? "ecom800" : "original";
o.maxKB = o.preset === "ecom800" ? 500 : 0;
persist("preset", o.preset);
persist("maxKB", o.maxKB);
} else if (key === "format") {
o.format = ["png", "jpeg", "webp"].indexOf(value) >= 0 ? value : "png";
persist("format", o.format);
if (o.format === "jpeg" && o.bg !== "white") {
o.bg = "white";
persist("bg", "white");
toast("JPG 不支持透明背景,已切换为白底", "info");
}
} else if (key === "bg") {
if (o.format === "jpeg") {
renderExportUI();
return;
}
o.bg = value === "white" ? "white" : "transparent";
persist("bg", o.bg);
} else if (key === "maxKB") {
o.maxKB = Math.max(0, Number(value) || 0);
persist("maxKB", o.maxKB);
} else if (key === "size") {
o.size = Math.max(64, Number(value) || 800);
persist("size", o.size);
}
renderExportUI();
}
@@ -5444,13 +5497,35 @@ var __IWB_ANNOTATE_MANIFEST__ = {
const o = S.exportOptions || DEFAULT_EXPORT;
const root = runtime.el;
if (!root) return;
const ecom = o.preset === "ecom800";
const jpg = o.format === "jpeg";
root.querySelectorAll("[data-export-key]").forEach((btn) => {
const on = String(o[btn.dataset.exportKey]) === String(btn.dataset.exportValue);
btn.classList.toggle("active", on);
btn.setAttribute("aria-pressed", String(on));
});
root.querySelectorAll('[data-export-key="bg"]').forEach((btn) => {
const blocked = jpg && btn.dataset.exportValue === "transparent";
btn.disabled = blocked;
if (blocked) btn.title = "JPG 不支持透明背景,已自动使用白底";
else btn.removeAttribute("title");
});
root.querySelectorAll('[data-export-when="ecom800"]').forEach((row) => {
row.hidden = !ecom;
});
const summary = root.querySelector(".iwb-editor-export-summary");
if (summary) summary.textContent = o.preset === "ecom800" ? o.size + "×" + o.size + " · " + o.format.toUpperCase() + (o.maxKB ? " · ≤" + o.maxKB + "KB" : "") : "原尺寸 " + S.imgW + "×" + S.imgH + " · " + o.format.toUpperCase();
if (summary) {
const size = ecom ? o.size + "×" + o.size : "原尺寸 " + S.imgW + "×" + S.imgH;
const limit = ecom && o.maxKB ? " · ≤" + o.maxKB + "KB" : "";
const bg = effectiveBg(o) === "white" ? "白底" : "透明";
const fmt = o.format === "jpeg" ? "JPG" : o.format === "webp" ? "WebP" : "PNG";
summary.textContent = size + " · " + fmt + " · " + bg + limit;
}
const note = root.querySelector(".iwb-editor-export-note");
if (note) {
const le = S.lastExport;
note.textContent = le && le.signature === exportSignature(o) ? "上次输出 " + le.width + "×" + le.height + " · " + Math.max(1, Math.round(le.bytes / 1024)) + "KB" : "一次只输出一个结果;设置会记住";
}
}
function bindExportUI() {
const root = runtime.el;
@@ -5481,6 +5556,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
}
function buildOutputCanvas() {
const o = S.exportOptions || DEFAULT_EXPORT;
const white = effectiveBg(o) === "white";
const src = getCompositeCanvas();
let out = src;
if (o.preset === "ecom800") {
@@ -5489,7 +5565,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
c.width = size;
c.height = size;
const cx = c.getContext("2d");
if (o.bg === "white" || o.format === "jpeg") {
if (white) {
cx.fillStyle = "#ffffff";
cx.fillRect(0, 0, size, size);
}
@@ -5500,7 +5576,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
src.width = 0;
src.height = 0;
out = c;
} else if (o.bg === "white" || o.format === "jpeg") {
} else if (white) {
const c = document.createElement("canvas");
c.width = S.imgW;
c.height = S.imgH;
@@ -5547,7 +5623,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
async function encodeOutput(canvas) {
const o = S.exportOptions || DEFAULT_EXPORT;
const mime = o.format === "jpeg" ? "image/jpeg" : o.format === "webp" ? "image/webp" : "image/png";
const limit = o.maxKB > 0 ? o.maxKB * 1024 : 0;
const limit = o.preset === "ecom800" && o.maxKB > 0 ? o.maxKB * 1024 : 0;
let blob = await toBlob(canvas, mime, 0.92);
if (limit && blob.size > limit) {
if (o.format === "png") {
@@ -5567,7 +5643,18 @@ var __IWB_ANNOTATE_MANIFEST__ = {
const canvas = buildOutputCanvas();
try {
const blob = await encodeOutput(canvas);
const info = { bytes: blob.size, width: canvas.width, height: canvas.height, format: S.exportOptions.format, preset: S.exportOptions.preset };
const o = S.exportOptions || DEFAULT_EXPORT;
const info = {
bytes: blob.size,
width: canvas.width,
height: canvas.height,
format: o.format,
bg: effectiveBg(o),
preset: o.preset,
maxKB: o.preset === "ecom800" ? o.maxKB : 0,
// 记下产出时用的设置指纹,UI 才能判断「上次输出」是否还代表当前设置
signature: exportSignature(o)
};
S.lastExport = info;
return { blob, info };
} finally {
@@ -5781,6 +5868,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
const { blob } = await prepareOutput();
if (mode === "download") {
downloadBlob(blob);
if (S.renderAll) S.renderAll();
return;
}
if (S.callbacks && S.callbacks.onResult) S.callbacks.onResult(blob, mode);
@@ -5837,7 +5925,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
};
// image-annotate/plugin/editor.css
var editor_default = '/* IWB 鲜艺抠图节点 - 注入样式 */\n\n/* 抠图按钮 */\n.iwb-cutout-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n\n.iwb-cutout-btn:hover {\n opacity: 1;\n transform: scale(1.15);\n}\n\n.iwb-cutout-btn svg {\n width: 14px;\n height: 14px;\n display: block;\n}\n\n/* 正在处理中的按钮状态 */\n.iwb-cutout-btn.processing {\n animation: iwb-cutout-pulse 1s infinite;\n pointer-events: none;\n}\n\n@keyframes iwb-cutout-pulse {\n 0%, 100% { opacity: 0.4; }\n 50% { opacity: 1; }\n}\n\n/* 全屏遮罩 */\n.iwb-cutout-overlay {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.25);\n display: none;\n align-items: center;\n justify-content: center;\n flex-direction: column;\n gap: 12px;\n z-index: 999999;\n pointer-events: all;\n backdrop-filter: blur(2px);\n}\n\n/* 自定义按钮提示(挂在 body,最高层级,不会被图片遮挡) */\n#iwb-cutout-tip {\n position: fixed;\n z-index: 2147483647;\n background: rgba(30, 30, 46, 0.95);\n color: #fff;\n padding: 5px 10px;\n border-radius: 6px;\n font-size: 12px;\n line-height: 1.5;\n white-space: nowrap;\n pointer-events: none;\n display: none;\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);\n border: 1px solid rgba(255, 255, 255, 0.14);\n font-family: system-ui, -apple-system, "Segoe UI", sans-serif;\n max-width: 320px;\n white-space: normal;\n word-break: break-all;\n}\n\n/* 加载动画 */\n.iwb-cutout-spinner {\n width: 48px;\n height: 48px;\n border: 4px solid rgba(255, 255, 255, 0.3);\n border-top-color: #e8a735;\n border-radius: 50%;\n animation: iwb-cutout-spin 0.8s linear infinite;\n}\n\n@keyframes iwb-cutout-spin {\n to { transform: rotate(360deg); }\n}\n\n/* 遮罩文字 */\n.iwb-cutout-text {\n color: #fff;\n font-size: 14px;\n font-weight: 500;\n text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);\n background: rgba(0, 0, 0, 0.6);\n padding: 8px 16px;\n border-radius: 6px;\n}\n\n/* Toast 提示 */\n.iwb-cutout-toast {\n position: fixed;\n bottom: 24px;\n left: 50%;\n transform: translateX(-50%);\n padding: 10px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-weight: 500;\n z-index: 999999;\n transition: opacity 0.3s;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);\n display: none;\n max-width: 400px;\n}\n\n.iwb-cutout-toast-success {\n background: #1a7f37;\n color: #fff;\n}\n\n.iwb-cutout-toast-error {\n background: #cf222e;\n color: #fff;\n}\n\n.iwb-cutout-toast-info {\n background: #1f6feb;\n color: #fff;\n}\n\n/* 白底按钮 */\n.iwb-whitebg-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-whitebg-btn:hover { opacity: 1; transform: scale(1.15); }\n.iwb-whitebg-btn svg { width: 14px; height: 14px; display: block; }\n.iwb-whitebg-btn.processing { animation: iwb-cutout-pulse 1s infinite; pointer-events: none; }\n\n/* 编辑按钮 */\n.iwb-editor-btn-icon {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-editor-btn-icon:hover { opacity: 1; transform: scale(1.15); }\n.iwb-editor-btn-icon svg { width: 14px; height: 14px; display: block; }\n\n/* ==================== 图片编辑器 ==================== */\n\n.iwb-editor-overlay {\n position: fixed;\n top: 0; left: 0;\n width: 100%; height: 100%;\n background: rgba(0, 0, 0, 0.6);\n display: none;\n align-items: center;\n justify-content: center;\n z-index: 999998;\n backdrop-filter: blur(3px);\n}\n\n.iwb-editor-modal {\n background: #1e1e2e;\n border-radius: 0;\n box-shadow: none;\n display: flex;\n flex-direction: column;\n width: 100vw;\n height: 100vh;\n max-width: none;\n max-height: none;\n overflow: visible;\n}\n\n/* 新增:主区域(画布 + 图层面板)flex 布局 */\n.iwb-editor-main {\n display: flex;\n flex: 1 1 auto;\n min-height: 0;\n overflow: visible;\n}\n\n.iwb-editor-header {\n display: flex;\n align-items: center;\n gap: 12px;\n padding: 10px 16px;\n background: #181825;\n border-bottom: 1px solid #313244;\n flex-wrap: nowrap;\n}\n.iwb-editor-header > .iwb-editor-tools {\n position: absolute;\n left: 12px;\n top: 68px;\n width: 44px;\n flex-direction: column;\n padding: 6px;\n background: #181825;\n border: 1px solid #313244;\n border-radius: 8px;\n z-index: 20;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action { position: static; }\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="redo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="reset"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="restore"] {\n position: absolute;\n top: -54px;\n left: auto;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"] { right: 154px; }\n.iwb-editor-header > .iwb-edLine truncated
var editor_default = '/* IWB 鲜艺抠图节点 - 注入样式 */\n\n/* 抠图按钮 */\n.iwb-cutout-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n\n.iwb-cutout-btn:hover {\n opacity: 1;\n transform: scale(1.15);\n}\n\n.iwb-cutout-btn svg {\n width: 14px;\n height: 14px;\n display: block;\n}\n\n/* 正在处理中的按钮状态 */\n.iwb-cutout-btn.processing {\n animation: iwb-cutout-pulse 1s infinite;\n pointer-events: none;\n}\n\n@keyframes iwb-cutout-pulse {\n 0%, 100% { opacity: 0.4; }\n 50% { opacity: 1; }\n}\n\n/* 全屏遮罩 */\n.iwb-cutout-overlay {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.25);\n display: none;\n align-items: center;\n justify-content: center;\n flex-direction: column;\n gap: 12px;\n z-index: 999999;\n pointer-events: all;\n backdrop-filter: blur(2px);\n}\n\n/* 自定义按钮提示(挂在 body,最高层级,不会被图片遮挡) */\n#iwb-cutout-tip {\n position: fixed;\n z-index: 2147483647;\n background: rgba(30, 30, 46, 0.95);\n color: #fff;\n padding: 5px 10px;\n border-radius: 6px;\n font-size: 12px;\n line-height: 1.5;\n white-space: nowrap;\n pointer-events: none;\n display: none;\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);\n border: 1px solid rgba(255, 255, 255, 0.14);\n font-family: system-ui, -apple-system, "Segoe UI", sans-serif;\n max-width: 320px;\n white-space: normal;\n word-break: break-all;\n}\n\n/* 加载动画 */\n.iwb-cutout-spinner {\n width: 48px;\n height: 48px;\n border: 4px solid rgba(255, 255, 255, 0.3);\n border-top-color: #e8a735;\n border-radius: 50%;\n animation: iwb-cutout-spin 0.8s linear infinite;\n}\n\n@keyframes iwb-cutout-spin {\n to { transform: rotate(360deg); }\n}\n\n/* 遮罩文字 */\n.iwb-cutout-text {\n color: #fff;\n font-size: 14px;\n font-weight: 500;\n text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);\n background: rgba(0, 0, 0, 0.6);\n padding: 8px 16px;\n border-radius: 6px;\n}\n\n/* Toast 提示 */\n.iwb-cutout-toast {\n position: fixed;\n bottom: 24px;\n left: 50%;\n transform: translateX(-50%);\n padding: 10px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-weight: 500;\n z-index: 999999;\n transition: opacity 0.3s;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);\n display: none;\n max-width: 400px;\n}\n\n.iwb-cutout-toast-success {\n background: #1a7f37;\n color: #fff;\n}\n\n.iwb-cutout-toast-error {\n background: #cf222e;\n color: #fff;\n}\n\n.iwb-cutout-toast-info {\n background: #1f6feb;\n color: #fff;\n}\n\n/* 白底按钮 */\n.iwb-whitebg-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-whitebg-btn:hover { opacity: 1; transform: scale(1.15); }\n.iwb-whitebg-btn svg { width: 14px; height: 14px; display: block; }\n.iwb-whitebg-btn.processing { animation: iwb-cutout-pulse 1s infinite; pointer-events: none; }\n\n/* 编辑按钮 */\n.iwb-editor-btn-icon {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-editor-btn-icon:hover { opacity: 1; transform: scale(1.15); }\n.iwb-editor-btn-icon svg { width: 14px; height: 14px; display: block; }\n\n/* ==================== 图片编辑器 ==================== */\n\n.iwb-editor-overlay {\n position: fixed;\n top: 0; left: 0;\n width: 100%; height: 100%;\n background: rgba(0, 0, 0, 0.6);\n display: none;\n align-items: center;\n justify-content: center;\n z-index: 999998;\n backdrop-filter: blur(3px);\n}\n\n.iwb-editor-modal {\n background: #1e1e2e;\n border-radius: 0;\n box-shadow: none;\n display: flex;\n flex-direction: column;\n width: 100vw;\n height: 100vh;\n max-width: none;\n max-height: none;\n overflow: visible;\n}\n\n/* 新增:主区域(画布 + 图层面板)flex 布局 */\n.iwb-editor-main {\n display: flex;\n flex: 1 1 auto;\n min-height: 0;\n overflow: visible;\n}\n\n.iwb-editor-header {\n display: flex;\n align-items: center;\n gap: 12px;\n padding: 10px 16px;\n background: #181825;\n border-bottom: 1px solid #313244;\n flex-wrap: nowrap;\n}\n.iwb-editor-header > .iwb-editor-tools {\n position: absolute;\n left: 12px;\n top: 68px;\n width: 44px;\n flex-direction: column;\n padding: 6px;\n background: #181825;\n border: 1px solid #313244;\n border-radius: 8px;\n z-index: 20;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action { position: static; }\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="redo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="reset"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="restore"] {\n position: absolute;\n top: -54px;\n left: auto;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"] { right: 154px; }\n.iwb-editor-header > .iwb-edLine truncated
// image-annotate/plugin/glue.js
function createGlue(ctx, css) {