mx / dengtaishan-src /src /app /CoverPanel.tsx
Ethscriptions's picture
登泰山记:渲染引擎、九个篇章场景、程序化配乐音效、MP4 导出、发布文案与封面(初版)
6fff8e3 verified
Raw History Blame Contribute Delete
6.38 kB
// 封面:4:3 / 3:4 / 16:9 多款可多选,单张 PNG 或打包 ZIP 下载
import { useEffect, useMemo, useState } from 'react'
import { zipSync } from 'fflate'
import { COVER_DEFS, RATIO_SIZE, Ratio, renderCover } from '../video/covers'
import { cx, Chip, toast } from './ui'
interface Item { key: string; ratio: Ratio; def: (typeof COVER_DEFS)[number] }
const RATIOS: Ratio[] = ['16:9', '4:3', '3:4']
const ITEMS: Item[] = RATIOS.flatMap((ratio) => COVER_DEFS.map((def) => ({ key: `${def.id}-${ratio}`, ratio, def })))
const fname = (it: Item) => `登泰山记_封面_${it.def.name.replace(/[??]/g, '')}_${it.ratio.replace(':', 'x')}.png`
function toBlob(c: HTMLCanvasElement): Promise<Blob> {
return new Promise((res, rej) => c.toBlob((b) => (b ? res(b) : rej(new Error('toBlob failed'))), 'image/png'))
}
function download(blob: Blob, name: string) {
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = name
document.body.appendChild(a)
a.click()
a.remove()
setTimeout(() => URL.revokeObjectURL(url), 4000)
}
export default function CoverPanel({ ready }: { ready: boolean }) {
const [filter, setFilter] = useState<Ratio | 'all'>('all')
const [sel, setSel] = useState<Set<string>>(new Set())
const [thumbs, setThumbs] = useState<Record<string, string>>({})
const [busy, setBusy] = useState(false)
const list = useMemo(() => ITEMS.filter((it) => filter === 'all' || it.ratio === filter), [filter])
useEffect(() => {
if (!ready) return
let cancel = false
let i = 0
const next = () => {
if (cancel || i >= ITEMS.length) return
const it = ITEMS[i++]
const c = renderCover(it.def, it.ratio, 0.34)
const url = c.toDataURL('image/jpeg', 0.86)
setThumbs((t) => ({ ...t, [it.key]: url }))
setTimeout(next, 30)
}
const id = setTimeout(next, 600)
return () => {
cancel = true
clearTimeout(id)
}
}, [ready])
const toggle = (k: string) => setSel((s) => {
const n = new Set(s)
if (n.has(k)) n.delete(k)
else n.add(k)
return n
})
const one = async (it: Item) => {
const b = await toBlob(renderCover(it.def, it.ratio, 1))
download(b, fname(it))
}
const zipSel = async () => {
const items = ITEMS.filter((it) => sel.has(it.key))
if (!items.length) return toast('请先选择封面')
setBusy(true)
try {
if (items.length === 1) {
await one(items[0])
} else {
const files: Record<string, Uint8Array> = {}
for (const it of items) {
const b = await toBlob(renderCover(it.def, it.ratio, 1))
files[fname(it)] = new Uint8Array(await b.arrayBuffer())
}
const z = zipSync(files, { level: 0 })
download(new Blob([z as BlobPart], { type: 'application/zip' }), `登泰山记_封面_${items.length}张.zip`)
}
} finally {
setBusy(false)
}
}
return (
<div className="flex flex-col gap-5">
<div className="flex flex-wrap items-center justify-between gap-3">
<div className="flex flex-wrap gap-2">
<Chip active={filter === 'all'} onClick={() => setFilter('all')}>全部比例</Chip>
{RATIOS.map((r) => (
<Chip key={r} active={filter === r} onClick={() => setFilter(r)}>{r} · {RATIO_SIZE[r][0]}×{RATIO_SIZE[r][1]}</Chip>
))}
</div>
<div className="flex items-center gap-2">
<button onClick={() => setSel(new Set(list.map((i) => i.key)))} className="h-9 px-4 rounded-full border border-white/15 text-[13px] text-[#ddd6c9] hover:border-[#c9a86a]">全选当前</button>
<button onClick={() => setSel(new Set())} className="h-9 px-4 rounded-full border border-white/15 text-[13px] text-[#ddd6c9] hover:border-[#c9a86a]">清空</button>
<button onClick={zipSel} disabled={busy} className="h-9 px-5 rounded-full bg-[#c9a86a] text-[13px] font-semibold text-[#16130e] hover:bg-[#e7cf98] disabled:opacity-60">
{busy ? '打包中…' : `下载所选(${sel.size})`}
</button>
</div>
</div>
{RATIOS.filter((r) => filter === 'all' || filter === r).map((r) => (
<div key={r} className="flex flex-col gap-3">
<div className="text-[13px] tracking-widest text-[#8f897e]">{r} 封面 · {r === '3:4' ? '小红书 / 视频号竖版' : r === '4:3' ? '小红书 / B站动态' : 'B站 / 抖音横版 / 视频号横版'}</div>
<div className={cx('grid gap-4', r === '3:4' ? 'grid-cols-2 sm:grid-cols-4' : 'grid-cols-1 sm:grid-cols-2 lg:grid-cols-4')}>
{list.filter((it) => it.ratio === r).map((it) => {
const on = sel.has(it.key)
return (
<div key={it.key} className={cx('group relative rounded-xl overflow-hidden ring-2 transition-all cursor-pointer', on ? 'ring-[#c9a86a]' : 'ring-transparent hover:ring-white/20')} onClick={() => toggle(it.key)}>
<div style={{ aspectRatio: `${RATIO_SIZE[r][0]} / ${RATIO_SIZE[r][1]}` }} className="bg-white/5">
{thumbs[it.key] ? <img src={thumbs[it.key]} alt={it.def.name} className="h-full w-full object-cover" /> : <div className="h-full w-full animate-pulse bg-white/[0.04]" />}
</div>
<div className={cx('absolute left-2 top-2 h-6 w-6 rounded-full border-2 flex items-center justify-center', on ? 'bg-[#c9a86a] border-[#c9a86a]' : 'bg-black/40 border-white/70')}>
{on && <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="#16130e" strokeWidth="3"><path d="M5 12l5 5 9-10" /></svg>}
</div>
<div className="flex items-center justify-between gap-2 bg-[#121214] px-3 py-2">
<div className="min-w-0">
<div className="truncate text-[13px] text-[#f3eee4]">{it.def.name}</div>
<div className="truncate text-[11px] text-[#8f897e]">{it.def.desc}</div>
</div>
<button onClick={(e) => { e.stopPropagation(); one(it) }} className="shrink-0 rounded-full border border-white/15 px-3 py-1 text-[12px] text-[#ddd6c9] hover:border-[#c9a86a]">PNG</button>
</div>
</div>
)
})}
</div>
</div>
))}
</div>
)
}