From a49cdf682ec37d545c6affa0bc9c8f6f1ec8b4c6 Mon Sep 17 00:00:00 2001 From: pfqgauxfb <3521106529@qq.com> Date: Tue, 3 Jun 2025 10:04:02 +0800 Subject: [PATCH] feng --- src/components/Comment/index.js | 93 +++++++++++++++ src/components/Comment/styles.module.css | 132 +++++++++++++++++++++ src/theme/DocSidebarItem/index.js | 4 + src/theme/DocSidebarItem/styles.module.css | 32 ++++- 4 files changed, 256 insertions(+), 5 deletions(-) create mode 100644 src/components/Comment/index.js create mode 100644 src/components/Comment/styles.module.css diff --git a/src/components/Comment/index.js b/src/components/Comment/index.js new file mode 100644 index 0000000..0442a95 --- /dev/null +++ b/src/components/Comment/index.js @@ -0,0 +1,93 @@ +import React, { useState, useEffect } from 'react'; +import styles from './styles.module.css'; + +export default function Comment({ docId }) { + const [comments, setComments] = useState([]); + const [newComment, setNewComment] = useState(''); + const [isOpen, setIsOpen] = useState(false); + + useEffect(() => { + // 从localStorage中读取评论 + const savedComments = JSON.parse(localStorage.getItem(`docComments_${docId}`) || '[]'); + setComments(savedComments); + }, [docId]); + + const handleAddComment = () => { + if (!newComment.trim()) return; + + const comment = { + id: Date.now(), + text: newComment, + timestamp: new Date().toLocaleString(), + author: '用户' // 这里可以替换为实际的用户名 + }; + + const updatedComments = [...comments, comment]; + setComments(updatedComments); + localStorage.setItem(`docComments_${docId}`, JSON.stringify(updatedComments)); + setNewComment(''); + }; + + const handleDeleteComment = (commentId) => { + const updatedComments = comments.filter(comment => comment.id !== commentId); + setComments(updatedComments); + localStorage.setItem(`docComments_${docId}`, JSON.stringify(updatedComments)); + }; + + return ( +
+ + + {isOpen && ( +
+
+ {comments.map(comment => ( +
+
+ {comment.author} + {comment.timestamp} + +
+
{comment.text}
+
+ ))} +
+
+