feat: edit post images (upload/remove) + fix captcha input in dark mode
- PostDetail edit modal: show existing images with delete button, upload new images (multi-file, 5MB limit), save replaces image list - API PUT /posts/:id accepts images array; omitting keeps original - lib/api updatePost passes images - NewPost/ReplyList captcha answer input: use CSS vars (was hardcoded #e2e8f0 border with white bg — glaring in dark mode)
This commit is contained in:
1 parent
ebc5201569
commit
4a30e79269
5 files changed
+60
-12
No files matched your search
@@ -1,8 +1,8 @@
|
||||
'use client';
|
||||
|
||||
// 帖子详情页
|
||||
import { useEffect, useState, useCallback } from 'react';
|
||||
import { API } from '@/lib/api';
|
||||
import { useEffect, useState, useCallback, useRef } from 'react';
|
||||
import { API, uploadImage } from '@/lib/api';
|
||||
import { useApp } from './AppProvider';
|
||||
import ReplyList from './ReplyList';
|
||||
import { Icon } from './Icons';
|
||||
@@ -21,6 +21,9 @@ export default function PostDetail({ postId }) {
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [editTitle, setEditTitle] = useState('');
|
||||
const [editContent, setEditContent] = useState('');
|
||||
const [editImages, setEditImages] = useState([]);
|
||||
const [editUploading, setEditUploading] = useState(false);
|
||||
const editImageInputRef = useRef(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setError(null);
|
||||
@@ -73,13 +76,14 @@ export default function PostDetail({ postId }) {
|
||||
const handleEdit = () => {
|
||||
setEditTitle(post.title || '');
|
||||
setEditContent(post.content || '');
|
||||
setEditImages(post.images || []);
|
||||
setEditing(true);
|
||||
};
|
||||
|
||||
const handleSaveEdit = async () => {
|
||||
if (!editContent.trim()) { alert('请填写内容'); return; }
|
||||
try {
|
||||
await API.updatePost(postId, editTitle.trim() || '无标题', editContent.trim());
|
||||
await API.updatePost(postId, editTitle.trim() || '无标题', editContent.trim(), editImages);
|
||||
setEditing(false);
|
||||
load();
|
||||
} catch (err) {
|
||||
@@ -87,6 +91,27 @@ export default function PostDetail({ postId }) {
|
||||
}
|
||||
};
|
||||
|
||||
// 编辑时上传新图片
|
||||
const handleEditUpload = async (file) => {
|
||||
if (!file) return;
|
||||
if (!file.type.startsWith('image/')) { alert('请选择图片文件'); return; }
|
||||
if (file.size > 5 * 1024 * 1024) { alert('图片不能超过 5MB'); return; }
|
||||
setEditUploading(true);
|
||||
try {
|
||||
const url = await uploadImage(file);
|
||||
setEditImages((prev) => [...prev, url]);
|
||||
} catch (err) {
|
||||
alert('上传失败:' + err.message);
|
||||
} finally {
|
||||
setEditUploading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 编辑时删除图片
|
||||
const handleEditRemoveImage = (url) => {
|
||||
setEditImages((prev) => prev.filter((u) => u !== url));
|
||||
};
|
||||
|
||||
return (
|
||||
<div id="pagePost" className="page-slide active">
|
||||
<div className="page-header" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
|
||||
@@ -169,7 +194,29 @@ export default function PostDetail({ postId }) {
|
||||
onChange={(e) => setEditContent(e.target.value)}
|
||||
style={{ width: '100%', padding: 12, border: '1px solid var(--border)', borderRadius: 6, fontSize: 15, fontFamily: 'inherit', resize: 'vertical', background: 'var(--bg)', color: 'var(--text)' }}
|
||||
/>
|
||||
<button className="btn-primary" style={{ padding: '10px 32px', fontSize: 15, marginTop: 8 }} onClick={handleSaveEdit}>保存</button>
|
||||
{/* 图片管理:查看/删除已有图片 + 上传新图片 */}
|
||||
{editImages.length > 0 && (
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginTop: 10 }}>
|
||||
{editImages.map((img, i) => (
|
||||
<div key={img + i} style={{ position: 'relative' }}>
|
||||
<img src={img} alt="" style={{ width: 72, height: 72, objectFit: 'cover', borderRadius: 6, border: '1px solid var(--border)' }} />
|
||||
<button
|
||||
type="button"
|
||||
title="删除图片"
|
||||
onClick={() => handleEditRemoveImage(img)}
|
||||
style={{ position: 'absolute', top: -6, right: -6, width: 20, height: 20, borderRadius: '50%', background: '#ef4444', color: '#fff', border: 'none', cursor: 'pointer', fontSize: 12, lineHeight: 1, display: 'flex', alignItems: 'center', justifyContent: 'center' }}
|
||||
>×</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div style={{ display: 'flex', gap: 8, marginTop: 12 }}>
|
||||
<button type="button" className="btn-secondary" style={{ padding: '8px 16px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={() => editImageInputRef.current?.click()}>
|
||||
{editUploading ? '上传中...' : '📷 上传图片'}
|
||||
</button>
|
||||
<input type="file" ref={editImageInputRef} accept="image/*" multiple style={{ display: 'none' }} onChange={(e) => { const files = e.target.files; if (files && files.length > 0) { [...files].forEach((f) => handleEditUpload(f)); e.target.value = ''; } }} />
|
||||
</div>
|
||||
<button className="btn-primary" style={{ padding: '10px 32px', fontSize: 15, marginTop: 12, width: '100%' }} onClick={handleSaveEdit}>保存</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user