// Main editor screen with drag-to-select regions const REGION_COLORS = [ { solid: '#4c8dff', soft: 'rgba(76,141,255,0.25)', label: 'rgba(76,141,255,0.92)' }, { solid: '#ffb547', soft: 'rgba(255,181,71,0.25)', label: 'rgba(255,181,71,0.92)' }, { solid: '#3dd68c', soft: 'rgba(61,214,140,0.25)', label: 'rgba(61,214,140,0.92)' }, { solid: '#e371ff', soft: 'rgba(227,113,255,0.25)', label: 'rgba(227,113,255,0.92)' }, { solid: '#ff6a6a', soft: 'rgba(255,106,106,0.25)', label: 'rgba(255,106,106,0.92)' }, { solid: '#5eead4', soft: 'rgba(94,234,212,0.22)', label: 'rgba(94,234,212,0.92)' }, ]; const edStyles = { page: { minHeight: '100vh', background: 'var(--bg)', display: 'flex', flexDirection: 'column' }, topbar: { height: '56px', borderBottom: '1px solid var(--border)', display: 'flex', alignItems: 'center', padding: '0 20px', gap: '16px', background: 'var(--bg-2)', }, back: { display: 'flex', alignItems: 'center', gap: '6px', padding: '6px 12px', borderRadius: '8px', color: 'var(--text-dim)', fontSize: '13px', border: '1px solid var(--border)', }, crumbs: { fontSize: '13px', color: 'var(--text-mute)', fontFamily: 'JetBrains Mono, monospace', }, topRight: { marginLeft: 'auto', display: 'flex', alignItems: 'center', gap: '10px' }, stepPill: { fontSize: '11px', fontFamily: 'JetBrains Mono, monospace', letterSpacing: '0.08em', padding: '5px 10px', background: 'var(--accent-soft)', color: 'var(--accent-2)', border: '1px solid rgba(76,141,255,0.25)', borderRadius: '999px', }, body: { flex: 1, display: 'grid', gridTemplateColumns: 'minmax(0, 1fr) 420px', gap: 0, overflow: 'hidden', }, canvas: { position: 'relative', padding: '32px', display: 'flex', flexDirection: 'column', gap: '16px', background: 'repeating-linear-gradient(45deg, #0a1120 0, #0a1120 12px, #0b1426 12px, #0b1426 24px)', overflow: 'auto', }, canvasHead: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', color: 'var(--text-dim)', fontSize: '12px', fontFamily: 'JetBrains Mono, monospace', }, canvasHint: { display: 'flex', gap: '6px', alignItems: 'center', padding: '4px 10px', background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: '999px', }, bannerWrap: { margin: '0 auto', maxWidth: '100%', borderRadius: '12px', overflow: 'hidden', background: 'var(--surface)', border: '1px solid var(--border)', boxShadow: '0 20px 60px rgba(0,0,0,0.4)', position: 'relative', }, uploadZone: { width: '100%', maxWidth: '720px', margin: '40px auto', aspectRatio: '560 / 545', border: '2px dashed var(--border-strong)', borderRadius: '16px', background: 'var(--surface)', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: '14px', color: 'var(--text-dim)', cursor: 'pointer', transition: 'all 0.2s', }, uploadIcon: { width: '56px', height: '56px', borderRadius: '14px', background: 'var(--accent-soft)', border: '1px solid rgba(76,141,255,0.25)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--accent-2)', marginBottom: '4px', }, uploadTitle: { fontSize: '17px', fontWeight: 600, color: 'var(--text)' }, uploadSub: { fontSize: '13px', color: 'var(--text-mute)', textAlign: 'center', maxWidth: '320px' }, uploadMeta: { marginTop: '10px', fontSize: '11px', fontFamily: 'JetBrains Mono, monospace', color: 'var(--text-mute)', display: 'flex', gap: '14px', }, bannerImg: { width: '100%', display: 'block', userSelect: 'none', pointerEvents: 'none' }, overlay: { position: 'absolute', inset: 0, cursor: 'crosshair', }, regionBox: (c, active) => ({ position: 'absolute', border: `2px solid ${c.solid}`, background: c.soft, boxShadow: active ? `0 0 0 2px ${c.soft}, 0 8px 24px rgba(0,0,0,0.4)` : 'none', borderRadius: '4px', pointerEvents: 'auto', cursor: 'pointer', }), regionLabel: (c) => ({ position: 'absolute', top: '-1px', left: '-1px', padding: '3px 10px', background: c.label, color: '#0b1220', fontSize: '11px', fontWeight: 700, letterSpacing: '0.04em', fontFamily: 'JetBrains Mono, monospace', borderRadius: '4px 0 4px 0', }), drawingBox: (c) => ({ position: 'absolute', border: `2px dashed ${c.solid}`, background: c.soft, borderRadius: '4px', pointerEvents: 'none', }), sidebar: { background: 'var(--bg-2)', borderLeft: '1px solid var(--border)', display: 'flex', flexDirection: 'column', overflow: 'hidden', }, regionTabs: { padding: '16px 20px 12px', borderBottom: '1px solid var(--border)', display: 'flex', flexWrap: 'wrap', gap: '8px', minHeight: '72px', }, regionTab: (c, active) => ({ display: 'flex', alignItems: 'center', gap: '8px', padding: '6px 12px 6px 10px', borderRadius: '8px', fontSize: '12px', fontWeight: 600, fontFamily: 'JetBrains Mono, monospace', border: `1px solid ${active ? c.solid : 'var(--border)'}`, background: active ? c.soft : 'var(--surface)', color: active ? c.solid : 'var(--text-dim)', letterSpacing: '0.03em', transition: 'all 0.15s', }), tabDot: (c) => ({ width: '8px', height: '8px', borderRadius: '2px', background: c.solid, }), tabClose: { marginLeft: '2px', padding: '2px', borderRadius: '4px', color: 'var(--text-mute)', display: 'flex', opacity: 0.7, }, tabsEmpty: { fontSize: '12px', color: 'var(--text-mute)', fontFamily: 'JetBrains Mono, monospace', alignSelf: 'center', }, panel: { flex: 1, padding: '24px 24px 20px', overflow: 'auto', display: 'flex', flexDirection: 'column', gap: '20px', }, emptyPanel: { flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', textAlign: 'center', padding: '40px 28px', gap: '14px', }, emptyIcon: { width: '64px', height: '64px', borderRadius: '16px', background: 'var(--surface)', border: '1px dashed var(--border-strong)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--text-mute)', }, emptyTitle: { fontSize: '16px', fontWeight: 600, margin: 0 }, emptyText: { fontSize: '13px', color: 'var(--text-dim)', lineHeight: 1.6, maxWidth: '280px', margin: 0 }, stepsList: { marginTop: '16px', display: 'flex', flexDirection: 'column', gap: '10px', textAlign: 'left', alignSelf: 'stretch', }, stepItem: { display: 'flex', alignItems: 'flex-start', gap: '12px', padding: '12px 14px', background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: '10px', fontSize: '13px', color: 'var(--text-dim)', }, stepNum: { width: '22px', height: '22px', borderRadius: '6px', background: 'var(--accent-soft)', color: 'var(--accent-2)', fontSize: '11px', fontWeight: 700, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0, fontFamily: 'JetBrains Mono, monospace', }, panelHead: { display: 'flex', flexDirection: 'column', gap: '6px' }, panelEyebrow: { fontSize: '11px', fontFamily: 'JetBrains Mono, monospace', letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--text-mute)', }, panelTitle: { fontSize: '22px', fontWeight: 600, letterSpacing: '-0.015em', margin: 0, display: 'flex', alignItems: 'center', gap: '10px', }, panelBadge: (c) => ({ padding: '3px 10px', background: c.soft, color: c.solid, fontSize: '11px', fontWeight: 700, fontFamily: 'JetBrains Mono, monospace', borderRadius: '999px', border: `1px solid ${c.solid}55`, letterSpacing: '0.04em', }), fieldLabel: { fontSize: '12px', fontWeight: 500, color: 'var(--text-dim)', marginBottom: '8px', display: 'block', }, typeChoice: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '10px', }, typeOpt: (active) => ({ padding: '14px', background: active ? 'var(--accent-soft)' : 'var(--surface)', border: `1px solid ${active ? 'var(--accent)' : 'var(--border)'}`, borderRadius: '10px', display: 'flex', flexDirection: 'column', alignItems: 'flex-start', gap: '8px', textAlign: 'left', transition: 'all 0.15s', }), typeOptTop: { width: '32px', height: '32px', borderRadius: '8px', background: 'var(--surface-2)', border: '1px solid var(--border)', display: 'flex', alignItems: 'center', justifyContent: 'center', }, typeOptTitle: { fontSize: '14px', fontWeight: 600 }, typeOptDesc: { fontSize: '11px', color: 'var(--text-mute)', lineHeight: 1.4 }, artDrop: (hasFile) => ({ border: `1.5px dashed ${hasFile ? 'var(--accent)' : 'var(--border-strong)'}`, borderRadius: '10px', padding: hasFile ? '14px' : '28px 20px', background: 'var(--surface)', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '10px', cursor: 'pointer', textAlign: 'center', transition: 'all 0.15s', }), artThumb: { width: '100%', aspectRatio: '4 / 3', borderRadius: '8px', background: 'linear-gradient(135deg, #4c8dff, #7aa7ff)', display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#fff', fontSize: '12px', fontWeight: 600, marginBottom: '4px', }, artFileRow: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', width: '100%', fontSize: '12px', color: 'var(--text-dim)', fontFamily: 'JetBrains Mono, monospace', }, textInput: { width: '100%', padding: '12px 14px', background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: '10px', color: 'var(--text)', fontSize: '14px', outline: 'none', fontFamily: 'inherit', transition: 'border-color 0.15s', }, textArea: { width: '100%', padding: '12px 14px', background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: '10px', color: 'var(--text)', fontSize: '13px', outline: 'none', fontFamily: 'inherit', resize: 'vertical', minHeight: '80px', lineHeight: 1.5, }, helper: { fontSize: '11px', color: 'var(--text-mute)', marginTop: '6px', fontStyle: 'italic', }, actions: { marginTop: 'auto', paddingTop: '20px', borderTop: '1px dashed var(--border)', display: 'flex', gap: '10px', }, actionBar: { padding: '16px 24px', borderTop: '1px solid var(--border)', display: 'flex', gap: '10px', background: 'var(--bg-2)', }, btnGhost: { padding: '11px 18px', borderRadius: '10px', background: 'transparent', color: 'var(--text-dim)', border: '1px solid var(--border)', fontSize: '13px', fontWeight: 500, display: 'flex', alignItems: 'center', gap: '8px', }, btnSec: { padding: '11px 18px', borderRadius: '10px', background: 'var(--surface-2)', color: 'var(--text)', border: '1px solid var(--border-strong)', fontSize: '13px', fontWeight: 500, flex: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: '8px', }, btnPri: (disabled) => ({ padding: '11px 18px', borderRadius: '10px', background: disabled ? 'var(--surface-2)' : 'var(--accent)', color: disabled ? 'var(--text-mute)' : '#fff', fontSize: '13px', fontWeight: 600, flex: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: '8px', cursor: disabled ? 'not-allowed' : 'pointer', transition: 'all 0.15s', boxShadow: disabled ? 'none' : '0 4px 12px rgba(76,141,255,0.3)', }), }; function EditorScreen({ initialData, onBack, onDone }) { const [banner, setBanner] = React.useState(initialData?.banner || null); // {url, file, name} const [regions, setRegions] = React.useState(initialData?.regions || []); const [activeId, setActiveId] = React.useState(null); const [drawing, setDrawing] = React.useState(null); const [submitting, setSubmitting] = React.useState(false); const [submitError, setSubmitError] = React.useState(''); const [useWatermark, setUseWatermark] = React.useState(false); const [watermarkText, setWatermarkText] = React.useState('SOHAGAME'); const overlayRef = React.useRef(null); const fileInputRef = React.useRef(null); const artInputRef = React.useRef(null); const active = regions.find(r => r.id === activeId); const handleUploadBanner = () => { fileInputRef.current?.click(); }; const handleBannerFile = (e) => { const f = e.target.files?.[0]; if (!f) return; _processBannerFile(f); e.target.value = ''; }; const _processBannerFile = (f) => { if (!f.type.startsWith('image/')) { setSubmitError('Chỉ nhận file ảnh'); return; } if (f.size > 15 * 1024 * 1024) { setSubmitError('File quá lớn (max 15MB)'); return; } setSubmitError(''); setBanner({ url: URL.createObjectURL(f), file: f, name: f.name }); }; // Global paste listener cho banner upload React.useEffect(() => { const onPaste = (e) => { if (banner) return; // đã có banner rồi const items = e.clipboardData?.items; if (!items) return; for (const item of items) { if (item.type.startsWith('image/')) { e.preventDefault(); const f = item.getAsFile(); if (f) _processBannerFile(f); return; } } }; document.addEventListener('paste', onPaste); return () => document.removeEventListener('paste', onPaste); }, [banner]); // Drag to draw region const onPointerDown = (e) => { if (active && !active.committed) return; // don't start new while configuring const rect = overlayRef.current.getBoundingClientRect(); const x = (e.clientX - rect.left) / rect.width; const y = (e.clientY - rect.top) / rect.height; setDrawing({ x0: x, y0: y, x, y }); e.currentTarget.setPointerCapture(e.pointerId); }; const onPointerMove = (e) => { if (!drawing) return; const rect = overlayRef.current.getBoundingClientRect(); const x = Math.min(1, Math.max(0, (e.clientX - rect.left) / rect.width)); const y = Math.min(1, Math.max(0, (e.clientY - rect.top) / rect.height)); setDrawing({ ...drawing, x, y }); }; const onPointerUp = (e) => { if (!drawing) return; const { x0, y0, x, y } = drawing; const nx = Math.min(x0, x), ny = Math.min(y0, y); const nw = Math.abs(x - x0), nh = Math.abs(y - y0); setDrawing(null); if (nw < 0.03 || nh < 0.03) return; // too small const id = 'r' + Date.now(); const newRegion = { id, x: nx, y: ny, w: nw, h: nh, type: null, artUrl: null, artName: null, text: '', styleNote: '', committed: false, }; setRegions(prev => [...prev, newRegion]); setActiveId(id); }; const updateActive = (patch) => { setRegions(prev => prev.map(r => r.id === activeId ? { ...r, ...patch } : r)); }; const commitActive = () => { updateActive({ committed: true }); }; const continueToNext = () => { commitActive(); setActiveId(null); // deselect to allow drawing new region }; const finishAll = async () => { if (submitting) return; if (!banner?.file) { setSubmitError('Vui lòng upload banner trước'); return; } const allRegions = regions.map(r => r.id === activeId ? { ...r, committed: true } : r); const validRegions = allRegions.filter(r => r.committed && r.type); if (validRegions.length === 0) { setSubmitError('Vui lòng thêm ít nhất 1 vùng chỉnh sửa'); return; } setSubmitting(true); setSubmitError(''); try { // Bước 1: Upload art files trước, nhận key const regionsForAPI = await Promise.all(validRegions.map(async (r) => { const rd = { id: r.id, x: r.x, y: r.y, w: r.w, h: r.h, type: r.type, committed: true }; if (r.type === 'text') { rd.text = r.text || ''; rd.styleNote = r.styleNote || ''; } else if (r.type === 'art' && r.artFile) { const uploaded = await API.uploadFile(r.artFile); rd.art_key = uploaded.key; } return rd; })); // Bước 2: Tạo edit job const result = await API.createEditJob( banner.file, regionsForAPI, useWatermark ? watermarkText.trim() : '', ); onDone({ jobId: result.jobId, outputUrl: result.outputUrl, bannerUrl: banner.url, // ảnh gốc (local URL để show "before") regions: validRegions, watermarkText: useWatermark ? watermarkText.trim() : '', }); } catch (err) { setSubmitError(err.message || 'Có lỗi xảy ra. Vui lòng thử lại.'); } finally { setSubmitting(false); } }; const deleteRegion = (id) => { setRegions(prev => prev.filter(r => r.id !== id)); if (activeId === id) setActiveId(null); }; const selectTab = (id) => { setActiveId(id); }; // Art upload: lưu File object để upload lên server khi submit const handleArtUpload = (e) => { const f = e.target.files?.[0]; if (!f) return; _processArtFile(f); e.target.value = ''; }; const _processArtFile = (f) => { if (!f.type.startsWith('image/')) { setSubmitError('Chỉ nhận file ảnh'); return; } if (f.size > 15 * 1024 * 1024) { setSubmitError('File art quá lớn (max 15MB)'); return; } setSubmitError(''); const url = URL.createObjectURL(f); updateActive({ artUrl: url, artFile: f, artName: f.name, artKey: null }); }; const canProceed = active && ( (active.type === 'art' && active.artUrl) || (active.type === 'text') // text rỗng = xoá text khỏi vùng này ); // Confirmation modal state for "Xong" button const [showConfirm, setShowConfirm] = React.useState(false); // Indices for labels const regionIdx = (r) => regions.findIndex(x => x.id === r.id) + 1; return (
{!banner ? 'Upload banner mẫu ở bên trái để bắt đầu.' : regions.length === 0 ? 'Dùng chuột kéo trên banner để đánh dấu vùng đầu tiên bạn muốn chỉnh sửa.' : 'Bấm vào một tab phía trên, hoặc kéo chuột trên banner để tạo vùng mới.'}
{regions.length === 0 && banner && (Hệ thống sẽ bắt đầu xử lý {regions.filter(r => r.committed || r.id === activeId).length} vùng chỉnh sửa. Quá trình này mất khoảng 20 giây.