Spaces:
Running
Running
Surf commited on
Commit ·
c2c4e21
1
Parent(s): f335a42
界面:封面面板升级为真人IP主题预览(推送 #22)
Browse files- README.md +1 -1
- src/film/CoverPanel.tsx +73 -59
README.md
CHANGED
|
@@ -64,7 +64,7 @@ pinned: false
|
|
| 64 |
|
| 65 |
- [x] **#20 计划**:已写入本节并同步,尚未改动封面实现。
|
| 66 |
- [x] **#21 封面引擎**:已改为香蕉黄、热带绿、炭黑金三套真人半身海报;采用原片 6.2 秒真实人物轮廓与曝光恢复,不生成新脸;新增真实字宽适配、完整末句标题、素材失败重试及单次解码缓存。浏览器视觉和下载验收在 #23 执行。
|
| 67 |
-
- [
|
| 68 |
- [ ] **#23 构建质检**:待实施。
|
| 69 |
- [ ] **#24 最终同步**:待实施。
|
| 70 |
|
|
|
|
| 64 |
|
| 65 |
- [x] **#20 计划**:已写入本节并同步,尚未改动封面实现。
|
| 66 |
- [x] **#21 封面引擎**:已改为香蕉黄、热带绿、炭黑金三套真人半身海报;采用原片 6.2 秒真实人物轮廓与曝光恢复,不生成新脸;新增真实字宽适配、完整末句标题、素材失败重试及单次解码缓存。浏览器视觉和下载验收在 #23 执行。
|
| 67 |
+
- [x] **#22 界面提示**:封面面板改为“真人半身 · 个人 IP 封面”,支持三套配色主题切换、当前配色预览、高清大图、错误重试与新的真人 IP 文件名。
|
| 68 |
- [ ] **#23 构建质检**:待实施。
|
| 69 |
- [ ] **#24 最终同步**:待实施。
|
| 70 |
|
src/film/CoverPanel.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import { useEffect, useRef, useState } from 'react'
|
| 2 |
-
import { Download, Loader2, Check } from 'lucide-react'
|
| 3 |
import JSZip from 'jszip'
|
| 4 |
import { DESIGNS, RATIOS, renderCover, type Design, type Ratio } from '@/film/core/covers'
|
| 5 |
import { downloadBlob } from '@/film/core/exporter'
|
|
@@ -12,94 +12,108 @@ const enqueue = <T,>(fn: () => Promise<T>) => { const p = queue.then(fn, fn); qu
|
|
| 12 |
|
| 13 |
export default function CoverPanel({ id }: { id: number }) {
|
| 14 |
const [ratios, setRatios] = useState<Ratio[]>(['16:9', '4:3', '3:4'])
|
|
|
|
| 15 |
const [imgs, setImgs] = useState<Record<string, string>>({})
|
| 16 |
const canv = useRef<Record<string, HTMLCanvasElement>>({})
|
| 17 |
const [picked, setPicked] = useState<Set<Key>>(new Set())
|
| 18 |
const [busy, setBusy] = useState(false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
useEffect(() => {
|
| 21 |
let stop = false
|
| 22 |
-
canv.current = {}
|
| 23 |
-
setImgs({})
|
| 24 |
-
setPicked(new Set())
|
| 25 |
;(async () => {
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
}
|
| 39 |
})()
|
| 40 |
return () => { stop = true }
|
| 41 |
-
}, [id])
|
| 42 |
|
| 43 |
-
const toggleRatio = (r: Ratio) => setRatios((a) =>
|
| 44 |
const togglePick = (k: Key) => setPicked((s) => { const n = new Set(s); if (n.has(k)) n.delete(k); else n.add(k); return n })
|
| 45 |
const visible = RATIOS.filter((r) => ratios.includes(r.key))
|
| 46 |
-
const
|
| 47 |
-
const
|
| 48 |
-
|
| 49 |
-
const downloadOne = async (k: string) => {
|
|
|
|
|
|
|
|
|
|
| 50 |
const downloadPicked = async () => {
|
| 51 |
-
|
| 52 |
-
if (!keys.length) return
|
| 53 |
setBusy(true)
|
| 54 |
try {
|
| 55 |
-
if (
|
| 56 |
const zip = new JSZip()
|
| 57 |
-
for (const k of
|
| 58 |
-
downloadBlob(await zip.generateAsync({ type: 'blob' }), `奥德彪
|
| 59 |
-
}
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
return (
|
| 63 |
<div className="space-y-3">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
<div>
|
| 65 |
-
<div className="mb-1.5 text-[12px] text-white/50">选择比例(可多选)</div>
|
| 66 |
<div className="flex flex-wrap gap-1.5">
|
| 67 |
-
{RATIOS.map((r) => (
|
| 68 |
-
<button key={r.key} onClick={() => toggleRatio(r.key)} className={`flex items-center gap-1 rounded-md px-3 py-1.5 text-[12px] ring-1 ${ratios.includes(r.key) ? 'bg-[#F5C518] text-black ring-[#F5C518]' : 'text-white/60 ring-white/15'}`}>
|
| 69 |
-
{ratios.includes(r.key) && <Check className="h-3 w-3" />}{r.key}
|
| 70 |
-
</button>
|
| 71 |
-
))}
|
| 72 |
</div>
|
| 73 |
</div>
|
| 74 |
-
{
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
<div className="mb-1.5 text-[11px] text-white/45">{r.label} · {r.w}×{r.h}</div>
|
| 77 |
-
<div className={`
|
| 78 |
-
{DESIGNS.
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
{imgs[k] ? <img src={imgs[k]} alt={d.name} className="h-full w-full cursor-pointer object-cover" onClick={() => togglePick(k)} /> : <div className="flex h-full items-center justify-center"><Loader2 className="h-4 w-4 animate-spin text-white/40" /></div>}
|
| 84 |
-
<div className="pointer-events-none absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/80 to-transparent px-1.5 pb-1 pt-4 text-[10px] text-white/80">{d.name}</div>
|
| 85 |
-
{imgs[k] && (
|
| 86 |
-
<>
|
| 87 |
-
<button onClick={() => togglePick(k)} className={`absolute left-1 top-1 flex h-5 w-5 items-center justify-center rounded-full ring-1 ${on ? 'bg-[#F5C518] text-black ring-[#F5C518]' : 'bg-black/50 text-transparent ring-white/50'}`}><Check className="h-3 w-3" /></button>
|
| 88 |
-
<button onClick={() => downloadOne(k)} className="absolute right-1 top-1 rounded bg-black/60 p-1 text-white/80 opacity-0 transition group-hover:opacity-100" aria-label="下载"><Download className="h-3 w-3" /></button>
|
| 89 |
-
</>
|
| 90 |
-
)}
|
| 91 |
-
</div>
|
| 92 |
-
)
|
| 93 |
-
})}
|
| 94 |
</div>
|
| 95 |
</div>
|
| 96 |
-
)
|
| 97 |
<div className="flex gap-2">
|
| 98 |
-
<button onClick={() => setPicked(new Set(visible.
|
| 99 |
-
<button onClick={downloadPicked} disabled={!
|
| 100 |
-
{busy ? <Loader2 className="h-4 w-4 animate-spin" /> : <Download className="h-4 w-4" />}下载已选 {picked.size ? `(${picked.size})` : ''}
|
| 101 |
-
</button>
|
| 102 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
</div>
|
| 104 |
)
|
| 105 |
}
|
|
|
|
| 1 |
import { useEffect, useRef, useState } from 'react'
|
| 2 |
+
import { Download, Loader2, Check, X } from 'lucide-react'
|
| 3 |
import JSZip from 'jszip'
|
| 4 |
import { DESIGNS, RATIOS, renderCover, type Design, type Ratio } from '@/film/core/covers'
|
| 5 |
import { downloadBlob } from '@/film/core/exporter'
|
|
|
|
| 12 |
|
| 13 |
export default function CoverPanel({ id }: { id: number }) {
|
| 14 |
const [ratios, setRatios] = useState<Ratio[]>(['16:9', '4:3', '3:4'])
|
| 15 |
+
const [theme, setTheme] = useState<Design>('ipHero')
|
| 16 |
const [imgs, setImgs] = useState<Record<string, string>>({})
|
| 17 |
const canv = useRef<Record<string, HTMLCanvasElement>>({})
|
| 18 |
const [picked, setPicked] = useState<Set<Key>>(new Set())
|
| 19 |
const [busy, setBusy] = useState(false)
|
| 20 |
+
const [error, setError] = useState('')
|
| 21 |
+
const [retry, setRetry] = useState(0)
|
| 22 |
+
const [preview, setPreview] = useState<{ url: string; name: string } | null>(null)
|
| 23 |
+
|
| 24 |
+
useEffect(() => {
|
| 25 |
+
if (!preview) return
|
| 26 |
+
const escape = (e: KeyboardEvent) => { if (e.key === 'Escape') setPreview(null) }
|
| 27 |
+
window.addEventListener('keydown', escape)
|
| 28 |
+
return () => window.removeEventListener('keydown', escape)
|
| 29 |
+
}, [preview])
|
| 30 |
|
| 31 |
useEffect(() => {
|
| 32 |
let stop = false
|
| 33 |
+
canv.current = {}; setImgs({}); setPicked(new Set()); setError(''); setPreview(null)
|
|
|
|
|
|
|
| 34 |
;(async () => {
|
| 35 |
+
try {
|
| 36 |
+
for (const r of RATIOS) for (const d of DESIGNS) {
|
| 37 |
+
if (stop) return
|
| 38 |
+
const c = await enqueue(() => renderCover(id, r.key, d.key))
|
| 39 |
+
if (stop) return
|
| 40 |
+
const k = `${r.key}|${d.key}`
|
| 41 |
+
canv.current[k] = c
|
| 42 |
+
const small = document.createElement('canvas'), scale = 640 / Math.max(c.width, c.height)
|
| 43 |
+
small.width = Math.round(c.width * scale); small.height = Math.round(c.height * scale)
|
| 44 |
+
small.getContext('2d')!.drawImage(c, 0, 0, small.width, small.height)
|
| 45 |
+
setImgs((m) => ({ ...m, [k]: small.toDataURL('image/jpeg', 0.92) }))
|
| 46 |
+
}
|
| 47 |
+
} catch (e) { if (!stop) setError((e as Error).message || '封面生成失败,请重试。') }
|
| 48 |
})()
|
| 49 |
return () => { stop = true }
|
| 50 |
+
}, [id, retry])
|
| 51 |
|
| 52 |
+
const toggleRatio = (r: Ratio) => setRatios((a) => a.includes(r) ? a.filter((x) => x !== r) : [...a, r])
|
| 53 |
const togglePick = (k: Key) => setPicked((s) => { const n = new Set(s); if (n.has(k)) n.delete(k); else n.add(k); return n })
|
| 54 |
const visible = RATIOS.filter((r) => ratios.includes(r.key))
|
| 55 |
+
const validKeys = [...picked].filter((k) => imgs[k] && ratios.includes(k.split('|')[0] as Ratio))
|
| 56 |
+
const fileName = (k: string) => { const [r, d] = k.split('|'); return `奥德彪_${pad2(id)}_${getCopy(id)?.chapter ?? ''}_真人IP封面_${r.replace(':', 'x')}_${DESIGNS.find((x) => x.key === d)?.name}.png` }
|
| 57 |
+
const toBlob = (c: HTMLCanvasElement) => new Promise<Blob>((resolve, reject) => c.toBlob((b) => b ? resolve(b) : reject(new Error('PNG 编码失败,请重试。')), 'image/png'))
|
| 58 |
+
const downloadOne = async (k: string) => {
|
| 59 |
+
try { if (canv.current[k]) downloadBlob(await toBlob(canv.current[k]), fileName(k)) }
|
| 60 |
+
catch (e) { setError((e as Error).message) }
|
| 61 |
+
}
|
| 62 |
const downloadPicked = async () => {
|
| 63 |
+
if (!validKeys.length) return
|
|
|
|
| 64 |
setBusy(true)
|
| 65 |
try {
|
| 66 |
+
if (validKeys.length === 1) { await downloadOne(validKeys[0]); return }
|
| 67 |
const zip = new JSZip()
|
| 68 |
+
for (const k of validKeys) zip.file(fileName(k), await toBlob(canv.current[k]))
|
| 69 |
+
downloadBlob(await zip.generateAsync({ type: 'blob' }), `奥德彪_${pad2(id)}_真人IP封面_${validKeys.length}张.zip`)
|
| 70 |
+
} catch (e) { setError((e as Error).message) }
|
| 71 |
+
finally { setBusy(false) }
|
| 72 |
}
|
| 73 |
|
| 74 |
return (
|
| 75 |
<div className="space-y-3">
|
| 76 |
+
<div className="rounded-lg bg-white/[0.03] p-3 ring-1 ring-white/10">
|
| 77 |
+
<div className="font-[OdbSerif] text-[17px] text-[#F5C518]">真人半身 · 个人 IP 封面</div>
|
| 78 |
+
<p className="mt-1 text-[12px] leading-relaxed text-white/60">原片真人定格,保留真实五官。统一人物 + 鲜明配色 + 大字金句,不再用远景充当封面。</p>
|
| 79 |
+
<p className="mt-1 text-[11px] text-white/40">图上保留“语录二创”标识;不表示本人发布或背书。</p>
|
| 80 |
+
</div>
|
| 81 |
+
<div className="grid grid-cols-3 gap-1.5" role="group" aria-label="封面配色主题">
|
| 82 |
+
{DESIGNS.map((d) => (
|
| 83 |
+
<button key={d.key} onClick={() => setTheme(d.key)} aria-pressed={theme === d.key} className={`rounded-md px-2 py-2 text-[12px] ring-1 ${theme === d.key ? 'text-[#F5C518] ring-[#F5C518]' : 'text-white/60 ring-white/15'}`}>
|
| 84 |
+
<span style={{ display: 'inline-block', width: 10, height: 10, borderRadius: 3, background: d.color, marginRight: 5, border: '1px solid #ffffff50' }} />{d.name.split(' · ')[0]}
|
| 85 |
+
</button>
|
| 86 |
+
))}
|
| 87 |
+
</div>
|
| 88 |
<div>
|
| 89 |
+
<div className="mb-1.5 text-[12px] text-white/50">选择比例(可多选)· 点击图片看高清大图</div>
|
| 90 |
<div className="flex flex-wrap gap-1.5">
|
| 91 |
+
{RATIOS.map((r) => <button key={r.key} onClick={() => toggleRatio(r.key)} aria-pressed={ratios.includes(r.key)} className={`flex items-center gap-1 rounded-md px-3 py-1.5 text-[12px] ring-1 ${ratios.includes(r.key) ? 'bg-[#F5C518] text-black ring-[#F5C518]' : 'text-white/60 ring-white/15'}`}>{ratios.includes(r.key) && <Check className="h-3 w-3" />}{r.key}</button>)}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
</div>
|
| 93 |
</div>
|
| 94 |
+
{error && <div role="alert" className="rounded-md bg-red-500/10 p-3 text-[12px] text-red-300">{error}<button className="ml-2 underline" onClick={() => setRetry((x) => x + 1)}>重新生成</button></div>}
|
| 95 |
+
{!visible.length && <p className="text-[12px] text-white/60">请至少选择一种封面比例。</p>}
|
| 96 |
+
{visible.map((r) => {
|
| 97 |
+
const k = `${r.key}|${theme}` as Key, on = picked.has(k)
|
| 98 |
+
return <div key={k}>
|
| 99 |
<div className="mb-1.5 text-[11px] text-white/45">{r.label} · {r.w}×{r.h}</div>
|
| 100 |
+
<div className={`relative overflow-hidden rounded-md bg-black ring-1 ${on ? 'ring-2 ring-[#F5C518]' : 'ring-white/10'}`} style={{ aspectRatio: `${r.w}/${r.h}` }}>
|
| 101 |
+
{imgs[k] ? <button className="block h-full w-full" aria-label={`查看${r.key}封面大图`} onClick={() => setPreview({ url: canv.current[k].toDataURL('image/png'), name: fileName(k) })}><img src={imgs[k]} alt={`${DESIGNS.find((x) => x.key === theme)?.name} · 奥德彪原片真人半身`} className="h-full w-full object-cover" /></button> : <div className="flex h-full items-center justify-center">{!error && <Loader2 className="h-5 w-5 animate-spin text-white/40" />}</div>}
|
| 102 |
+
{imgs[k] && <>
|
| 103 |
+
<button onClick={() => togglePick(k)} aria-label={`选择${r.key}封面`} aria-pressed={on} className={`absolute left-2 top-2 flex h-6 w-6 items-center justify-center rounded-full ring-1 ${on ? 'bg-[#F5C518] text-black ring-[#F5C518]' : 'bg-black/50 text-white/60 ring-white/50'}`}><Check className="h-3 w-3" /></button>
|
| 104 |
+
<button onClick={() => downloadOne(k)} className="absolute right-2 top-2 rounded bg-black/60 p-2 text-white" aria-label={`下载${r.key}封面`}><Download className="h-4 w-4" /></button>
|
| 105 |
+
</>}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
</div>
|
| 107 |
</div>
|
| 108 |
+
})}
|
| 109 |
<div className="flex gap-2">
|
| 110 |
+
<button onClick={() => setPicked(new Set(visible.map((r) => `${r.key}|${theme}` as Key).filter((k) => imgs[k])))} className="flex-1 rounded-md py-2 text-[12px] text-white/70 ring-1 ring-white/15">全选当前配色</button>
|
| 111 |
+
<button onClick={downloadPicked} disabled={!validKeys.length || busy} className="flex flex-[2] items-center justify-center gap-1.5 rounded-md bg-[#F5C518] py-2 text-[13px] font-bold text-black disabled:opacity-40">{busy ? <Loader2 className="h-4 w-4 animate-spin" /> : <Download className="h-4 w-4" />}下载已选{validKeys.length ? ` (${validKeys.length})` : ''}</button>
|
|
|
|
|
|
|
| 112 |
</div>
|
| 113 |
+
{preview && <div role="dialog" aria-modal="true" aria-label="封面高清预览" style={{ position: 'fixed', inset: 0, zIndex: 60, background: 'rgba(0,0,0,0.94)', display: 'flex', alignItems: 'center', justifyContent: 'center', padding: 28 }} onClick={() => setPreview(null)}>
|
| 114 |
+
<button autoFocus aria-label="关闭封面大图" onClick={() => setPreview(null)} style={{ position: 'absolute', right: 20, top: 16, padding: 12, background: '#242424', color: '#fff', borderRadius: 50 }}><X /></button>
|
| 115 |
+
<img src={preview.url} alt={preview.name} style={{ maxWidth: '92vw', maxHeight: '86vh', objectFit: 'contain' }} onClick={(e) => e.stopPropagation()} />
|
| 116 |
+
</div>}
|
| 117 |
</div>
|
| 118 |
)
|
| 119 |
}
|