import React, { useState, useEffect, useRef, lazy, Suspense } from "react"; import { createRoot } from "react-dom/client"; import { mountViewer, configure } from "./viewers"; import "./style.css"; const SkinEditor = lazy(() => import("./editor/SkinEditor").then((m) => ({ default: m.SkinEditor })), ); const done = (x) => !!(x.best || x.rejection); const modelOf = (x) => x.model ?? x.folder; const rejectionLabel = (x) => x === "edited_unsatisfied" ? "edited 不满意" : "结果都不满意"; const annotationTypes = { all: "全部已标注", perfect: "有完美标注", best: "有最佳标注(含完美)", ordinary: "仅最佳标注(无完美)", result_unsatisfied: "结果都不满意", edited_unsatisfied: "edited 不满意", }; const matchesAnnotation = (item, type) => type === "all" || (type === "perfect" && !!item.perfect) || (type === "best" && !!item.best) || (type === "ordinary" && !!item.best && !item.perfect) || item.rejection === type; const label = (r) => (r.version === "original" ? "原版" : r.version); async function api(path, body) { const res = await fetch( path, body === undefined ? {} : { method: "POST", headers: { "Content-Type": "application/json", "X-Annotation-Client": "ddj-review", }, body: JSON.stringify(body), }, ); const data = await res.json(); if (!res.ok) throw Error(data.error || "请求失败"); return data; } function Preview({ result }) { const ref = useRef(null); useEffect(() => mountViewer(ref.current, result), [result.url]); return (
正在加载 3D…
); } function App() { const [items, setItems] = useState([]), [key, setKey] = useState(null), [filter, setFilter] = useState("pending"), [annotationType, setAnnotationType] = useState("all"), [folder, setFolder] = useState(""), [search, setSearch] = useState(""); const [skipped, setSkipped] = useState(new Set()), [history, setHistory] = useState([]), [busy, setBusy] = useState(false), [notice, setNotice] = useState(null); const [camera, setCamera] = useState("angle"), [outer, setOuter] = useState(true), [uv, setUv] = useState(false), [image, setImage] = useState(null), [editor, setEditor] = useState(null); const busyRef = useRef(false), dialog = useRef(null), editorDialog = useRef(null); const item = items.find((x) => x.key === key); const matches = ( x, f = filter, fo = folder, q = search, type = annotationType, ) => (!fo || modelOf(x) === fo) && x.id.toLowerCase().includes(q.trim().toLowerCase()) && (f === "all" || (f === "pending" && x.results.length > 0 && !done(x)) || (f === "done" && done(x) && matchesAnnotation(x, type)) || (f === "rejected" && !!x.rejection) || (f === "no_results" && !x.results.length)); const rows = items.filter((x) => matches(x)); const jumpRows = item && !rows.includes(item) ? [item, ...rows] : rows; const toast = (message, error = false) => setNotice({ message, error }); const show = (k, remember = true) => { if (busyRef.current) return; if (remember && key && key !== k) setHistory((h) => [...h, key]); setKey(k); }; const first = (f = filter, fo = folder, q = search, skip = skipped) => items.find((x) => matches(x, f, fo, q) && !skip.has(x.key))?.key || null; const next = (list = items, skip = skipped) => { const pool = list.filter( (x) => matches(x) && !skip.has(x.key) && x.key !== key, ); const index = list.findIndex((x) => x.key === key); show((pool.find((x) => list.indexOf(x) > index) || pool[0])?.key || null); }; async function load(refresh = false) { if (busyRef.current) return; busyRef.current = true; setBusy(true); try { const data = await api( refresh ? "/api/refresh" : "/api/state", refresh ? {} : undefined, ); setItems(data.items); setKey( data.items.find((x) => x.key === key && matches(x))?.key || data.items.find((x) => matches(x) && !skipped.has(x.key))?.key || null, ); if (refresh) toast("目录已刷新"); } catch (e) { toast(e.message, true); } finally { busyRef.current = false; setBusy(false); } } async function save(kind, result, png) { if (busyRef.current || !item) return; busyRef.current = true; setBusy(true); try { const data = await api("/api/" + kind, { key: item.key, revision: item.revision, result: result?.name, reason: typeof result === "string" ? result : undefined, png, }); const updated = items.map((x) => (x.key === key ? data.item : x)); setItems(updated); const skip = new Set(skipped); skip.delete(key); setSkipped(skip); busyRef.current = false; setEditor(null); if (kind !== "clear") next(updated, skip); toast(kind === "clear" ? "已撤销标注" : "已保存 · " + item.id); } catch (e) { toast(e.message, true); } finally { busyRef.current = false; setBusy(false); } } function skip() { if (busyRef.current || !item) return; const s = new Set(skipped); s.add(key); setSkipped(s); next(items, s); } function previous() { if (!busyRef.current && history.length) { setKey(history.at(-1)); setHistory(history.slice(0, -1)); } } useEffect(() => { load(); }, []); useEffect(() => configure(camera, outer, uv), [camera, outer, uv, key]); useEffect(() => { if (!notice) return; const t = setTimeout(() => setNotice(null), notice.error ? 8000 : 3500); return () => clearTimeout(t); }, [notice]); useEffect(() => { if (image) dialog.current?.showModal(); }, [image]); useEffect(() => { if (editor) editorDialog.current?.showModal(); }, [editor]); useEffect(() => { const handler = (e) => { if ( busy || image || editor || e.ctrlKey || e.metaKey || e.altKey || e.target.closest("input,select,textarea,[contenteditable]") ) return; if (e.key.toLowerCase() === "s") { e.preventDefault(); skip(); } else if (e.key === "ArrowLeft") { e.preventDefault(); previous(); } else if (e.key === "ArrowRight") { e.preventDefault(); next(); } else if (/^[1-9]$/.test(e.key)) { const r = item?.results[+e.key - 1]; if (r) { e.preventDefault(); save("choose", r); } } }; document.addEventListener("keydown", handler); return () => document.removeEventListener("keydown", handler); }); const progressItems = items.filter((x) => !folder || modelOf(x) === folder); const progressDone = progressItems.filter(done).length; const progressTotal = progressDone + progressItems.filter((x) => x.results.length && !done(x)).length; const counts = { done: progressItems.filter(done).length, pending: progressItems.filter((x) => x.results.length && !done(x)).length, rejected: progressItems.filter((x) => x.rejection).length, all: progressItems.length, no_results: progressItems.filter((x) => !x.results.length).length, }; const changeFilter = (f) => { if (busyRef.current) return; setFilter(f); show(first(f)); }; return ( <>
D

DDJ 最佳结果标注

对照原图,选出最好的皮肤

标注进度 {progressDone} / {progressTotal}
{Object.entries({ pending: "待标注", done: "已标注", rejected: "不满意", all: "全部", no_results: "无结果", }).map(([f, l]) => ( ))}
{ setSearch(e.target.value); show(first(filter, folder, e.target.value)); }} />
{filter === "done" && (
可与数据目录、编号搜索组合筛选
)}
{item ? ( <>
{item.folder}

{item.id}

{item.results.length} 个结果版本 ·{" "} {item.annotation_conflict ? "标注冲突,请重新选择" : item.invalid_best || item.invalid_rejection ? "标注已失效,请重新选择" : item.perfect ? "已选最佳(完美)" : item.best ? "已保存最佳结果" : item.rejection ? rejectionLabel(item.rejection) : "请选择最佳结果、标记不满意,或暂时忽略"}

同步视角 {Object.entries({ angle: "斜前", front: "正面", back: "背面", head: "头部", }).map(([c, l]) => ( ))}
{item.results.map((r, i) => (
{label(r)} {item.best === r.name ? item.perfect ? "最佳 · 完美" : "已选最佳" : i + 1}
UV 贴图 点击放大
{r.name}
))} {!item.results.length && (
这个 source 暂无 result 图片
)}

拖动任一模型可同步旋转,滚轮缩放;点击 UV 贴图可放大。

) : (

当前筛选下没有更多条目

可以切换筛选,或重新查看暂时忽略的条目。

)}
{notice && (
{notice.message}
)} {image && ( setImage(null)} onClick={(e) => { if (e.target === e.currentTarget) setImage(null); }} >
{image.title}
放大预览
)} {editor && ( { e.preventDefault(); if (!busy) setEditor(null); }} >
修复 {editor.result.name} · 左键绘制,右键旋转
正在加载皮肤编辑器…

}> toast(m, true)} onSubmit={(png) => save("repair", editor.result, png)} />
{notice?.error &&

{notice.message}

}
)} ); } createRoot(document.getElementById("root")).render();