fix(image-annotate): 1.1.2 输出预设收口(JPG 自动白底、边长/体积控件、maxKB 只属电商预设、下载与还原后摘要刷新)
This commit is contained in:
1 parent
9a0f5bab69
commit
a4e89f5f8b
9 files changed
+312
-47
No files matched your search
@@ -1476,3 +1476,12 @@
|
||||
.iwb-editor-export-actions .iwb-editor-btn-cancel { display: none; }
|
||||
}
|
||||
|
||||
|
||||
/* 输出设置:按预设显示的行(尺寸/体积只属于电商方图)
|
||||
注意 `.iwb-editor-export-row` 是 display:flex,会盖掉 UA 的 [hidden]{display:none},
|
||||
必须显式 !important,否则 JS 设 hidden 后行还留在面板里。 */
|
||||
.iwb-editor-export-row[hidden] { display: none !important; }
|
||||
.iwb-editor-export-settings [data-export-key]:disabled {
|
||||
opacity: 0.32; cursor: not-allowed; text-decoration: line-through;
|
||||
}
|
||||
.iwb-editor-export-settings [data-export-key]:disabled:hover { background: #313244; }
|
||||
@@ -81,7 +81,7 @@ import { bindEvents } from './events.js'
|
||||
<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>
|
||||
@@ -92,6 +92,18 @@ import { bindEvents } from './events.js'
|
||||
<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>
|
||||
|
||||
@@ -27,6 +27,20 @@ import { toast } from './commands.js'
|
||||
|
||||
function settings() { return S.callbacks && S.callbacks.settings }
|
||||
|
||||
/** 实际输出背景:JPG 没有 alpha 通道,选「透明」也必须落白底。
|
||||
* 整条输出链(画布、摘要、状态)统一读这里,避免「按钮显示透明、文件是白底」。 */
|
||||
export 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(':')
|
||||
}
|
||||
|
||||
export function loadExportOptions() {
|
||||
const st = settings()
|
||||
const get = (k, d) => {
|
||||
@@ -36,43 +50,99 @@ import { toast } from './commands.js'
|
||||
}
|
||||
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'
|
||||
// 体积上限 / 边长只属于电商方图预设。原尺寸分支不继承上一次留下的限制,
|
||||
// 否则宿主里残留的 exportMaxKB 会让「原尺寸」也偷偷降质。
|
||||
// null 表示从未设置过(走默认 500),0 表示用户显式选了「不限」——两者必须区分。
|
||||
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: preset,
|
||||
format: 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()
|
||||
}
|
||||
|
||||
export 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) { /* ignore */ }
|
||||
const persist = (k, v) => {
|
||||
if (st && typeof st.set === 'function') { try { st.set(SETTINGS_KEY[k], v) } catch (e) { /* ignore */ } }
|
||||
}
|
||||
if (key === 'preset') {
|
||||
o.preset = value === 'ecom800' ? 'ecom800' : 'original'
|
||||
o.maxKB = o.preset === 'ecom800' ? 500 : 0
|
||||
persist('preset', o.preset)
|
||||
// maxKB 必须一起写回:只改内存的话,下次打开会被旧的 exportMaxKB 让限制「复活」
|
||||
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()
|
||||
}
|
||||
|
||||
/** 顶栏输出设置面板:按钮态 + 预设摘要 */
|
||||
/** 顶栏输出设置面板:按钮态 + 预设摘要(摘要必须反映「实际会输出什么」) */
|
||||
export function renderExportUI() {
|
||||
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))
|
||||
})
|
||||
// JPG 无 alpha 通道:透明按钮禁用并给出原因,而不是留着高亮却悄悄输出白底
|
||||
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' ? '白底' : '透明'
|
||||
// jpeg 的 MIME 名是 JPEG,但对用户就说 JPG
|
||||
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')
|
||||
: '一次只输出一个结果;设置会记住'
|
||||
}
|
||||
}
|
||||
|
||||
/** 事件委托:输出设置面板(下拉容器常驻 DOM,展开由 CSS class 控制) */
|
||||
@@ -107,6 +177,7 @@ import { toast } from './commands.js'
|
||||
/** 合成 → 尺寸/背景处理。返回结果画布(调用方负责释放) */
|
||||
export function buildOutputCanvas() {
|
||||
const o = S.exportOptions || DEFAULT_EXPORT
|
||||
const white = effectiveBg(o) === 'white'
|
||||
const src = getCompositeCanvas()
|
||||
let out = src
|
||||
if (o.preset === 'ecom800') {
|
||||
@@ -115,14 +186,14 @@ import { toast } from './commands.js'
|
||||
c.width = size
|
||||
c.height = size
|
||||
const cx = c.getContext('2d')
|
||||
if (o.bg === 'white' || o.format === 'jpeg') { cx.fillStyle = '#ffffff'; cx.fillRect(0, 0, size, size) }
|
||||
if (white) { cx.fillStyle = '#ffffff'; cx.fillRect(0, 0, size, size) }
|
||||
const long = Math.max(S.imgW, S.imgH) || 1
|
||||
const k = (size * 0.8) / long // 商品长边占比 80%(640px / 800px)
|
||||
const w = S.imgW * k, h = S.imgH * k
|
||||
cx.drawImage(src, (size - w) / 2, (size - h) / 2, w, h)
|
||||
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
|
||||
@@ -164,7 +235,8 @@ import { toast } from './commands.js'
|
||||
export 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
|
||||
// ≤KB 只在电商方图预设下约束;「原尺寸 + 残留 maxKB」不应偷偷降质
|
||||
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') {
|
||||
@@ -186,7 +258,18 @@ import { toast } from './commands.js'
|
||||
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 {
|
||||
|
||||
@@ -289,6 +289,9 @@ import { toast } from './commands.js'
|
||||
S.redoStack = []
|
||||
clearAllEditState()
|
||||
updateAllUI()
|
||||
// 尺寸变了就必须走统一重绘出口:输出摘要/状态栏读 S.imgW/imgH,
|
||||
// clearAllEditState 只清选区不停止蚂蚁线 rAF(唯一停止点是 renderAll/clearMarquee)
|
||||
if (S.renderAll) S.renderAll()
|
||||
toast('已还原为原图(裁剪后的画布尺寸已复位)', 'success')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -244,7 +244,12 @@ import { toast } from './commands.js'
|
||||
if (S.placingImage) cancelImagePlacement()
|
||||
try {
|
||||
const { blob } = await prepareOutput()
|
||||
if (mode === 'download') { downloadBlob(blob); return }
|
||||
if (mode === 'download') {
|
||||
downloadBlob(blob)
|
||||
// download 不关编辑器:必须走一次统一重绘,摘要的「上次输出 …KB」才会更新
|
||||
if (S.renderAll) S.renderAll()
|
||||
return
|
||||
}
|
||||
if (S.callbacks && S.callbacks.onResult) S.callbacks.onResult(blob, mode)
|
||||
close('done')
|
||||
} catch (e) {
|
||||
|
||||
Reference in new issue
Block a user