feat(widget): 新增评论组件核心功能及开发环境配置

新增评论组件核心功能,包括评论列表、分页、回复、表单验证等功能
添加开发环境配置,包括Vite构建配置、样式变量和工具函数
实现管理员验证功能,支持本地存储加密
添加组件基础类及常用组件如加载状态、分页、模态框等
配置文档站点构建流程,支持widget独立构建和集成
This commit is contained in:
anghunk
2026-01-22 09:01:11 +08:00
parent f221245c44
commit 6555907156
28 changed files with 7 additions and 7 deletions

View File

@@ -0,0 +1,496 @@
/**
* CWDComments 主类
* 使用 Shadow DOM 隔离样式
*/
import { createApiClient } from './api.js';
import { createCommentStore } from './store.js';
import { CommentForm } from '@/components/CommentForm.js';
import { CommentList } from '@/components/CommentList.js';
import styles from '@/styles/main.css?inline';
/**
* CWDComments 评论组件主类
*/
export class CWDComments {
/**
* @param {Object} config - 配置对象
* @param {string|HTMLElement} config.el - 挂载元素选择器或 DOM 元素
* @param {string} config.apiBaseUrl - API 基础地址
* @param {'light'|'dark'} [config.theme] - 主题(可选)
* @param {number} [config.pageSize] - 每页评论数(可选,默认 20
*
* 以下字段由组件自动推导或从后端读取,无需通过 config 传入:
* - postSlugwindow.location.origin + window.location.pathname
* - postTitledocument.title 或 postSlug
* - postUrlwindow.location.href
* - avatarPrefix/adminEmail/adminBadge通过 /api/config/comments 接口获取
*/
constructor(config) {
this.config = { ...config };
if (typeof window !== 'undefined') {
this.config.postSlug = window.location.origin + window.location.pathname;
}
if (typeof document !== 'undefined') {
this.config.postTitle = document.title || this.config.postSlug;
}
if (typeof window !== 'undefined') {
this.config.postUrl = window.location.href;
}
this.hostElement = this._resolveElement(config.el);
this.shadowRoot = null;
this.mountPoint = null;
this.commentForm = null;
this.commentList = null;
this.store = null;
this.unsubscribe = null;
// 初始加载标志
this._mounted = false;
}
/**
* 解析挂载元素
* @private
*/
_resolveElement(el) {
if (typeof el === 'string') {
const element = document.querySelector(el);
if (!element) {
throw new Error(`元素未找到:${el}`);
}
if (!(element instanceof HTMLElement)) {
throw new Error(`目标不是 HTMLElement: ${el}`);
}
return element;
}
return el;
}
async _loadServerConfig() {
try {
const base = this.config.apiBaseUrl;
if (!base) {
return {};
}
const apiBaseUrl = base.replace(/\/$/, '');
const res = await fetch(`${apiBaseUrl}/api/config/comments`);
if (!res.ok) {
return {};
}
const data = await res.json();
return {
adminEmail: data.adminEmail || '',
adminBadge: data.adminBadge || '',
adminEnabled: !!data.adminEnabled,
avatarPrefix: data.avatarPrefix || '',
allowedDomains: Array.isArray(data.allowedDomains) ? data.allowedDomains : [],
};
} catch (e) {
return {};
}
}
/**
* 挂载组件
*/
mount() {
if (this._mounted) {
return;
}
// 创建 Shadow DOM
this.shadowRoot = this.hostElement.attachShadow({ mode: 'open' });
// 注入样式
const styleElement = document.createElement('style');
if (typeof styles === 'string') {
styleElement.textContent = styles;
} else if (styles && typeof styles === 'object' && 'default' in styles) {
styleElement.textContent = styles.default;
}
this.shadowRoot.appendChild(styleElement);
// 创建容器
this.mountPoint = document.createElement('div');
this.mountPoint.className = 'cwd-comments-container';
this.shadowRoot.appendChild(this.mountPoint);
// 设置主题
if (this.config.theme) {
this.mountPoint.setAttribute('data-theme', this.config.theme);
}
(async () => {
const serverConfig = await this._loadServerConfig();
if (!this._mounted) {
return;
}
// 检查域名限制
if (
serverConfig.allowedDomains &&
serverConfig.allowedDomains.length > 0 &&
typeof window !== 'undefined'
) {
const currentHostname = window.location.hostname;
const isAllowed = serverConfig.allowedDomains.some((domain) => {
return currentHostname === domain || currentHostname.endsWith('.' + domain);
});
if (!isAllowed) {
this.mountPoint.innerHTML = `
<div style="padding: 20px; text-align: center; color: #666; font-size: 14px; border: 1px solid #eee; border-radius: 8px; background: #f9f9f9;">
当前域名 (${currentHostname}) 未获得评论组件调用授权
</div>
`;
return;
}
}
if (serverConfig.avatarPrefix) {
this.config.avatarPrefix = serverConfig.avatarPrefix;
}
if (serverConfig.adminEmail) {
this.config.adminEmail = serverConfig.adminEmail;
}
if (serverConfig.adminEnabled && serverConfig.adminBadge) {
this.config.adminBadge = serverConfig.adminBadge;
}
this.config.requireReview = !!serverConfig.requireReview;
const api = createApiClient(this.config);
this.api = api;
this.store = createCommentStore(this.config, api.fetchComments.bind(api), api.submitComment.bind(api));
this.unsubscribe = this.store.store.subscribe((state) => {
this._onStateChange(state);
});
this._render();
this.store.loadComments();
if (this.api && typeof this.api.trackVisit === 'function') {
this.api.trackVisit();
}
})();
this._mounted = true;
}
/**
* 卸载组件
*/
unmount() {
if (!this._mounted) {
return;
}
// 销毁组件
if (this.commentForm) {
this.commentForm.destroy();
this.commentForm = null;
}
if (this.commentList) {
this.commentList.destroy();
this.commentList = null;
}
// 取消订阅
if (this.unsubscribe) {
this.unsubscribe();
this.unsubscribe = null;
}
// 移除 Shadow DOM - 通过替换所有子节点
if (this.hostElement) {
// Shadow DOM 会在清空子节点时自动移除
while (this.hostElement.firstChild) {
this.hostElement.removeChild(this.hostElement.firstChild);
}
}
this.shadowRoot = null;
this.mountPoint = null;
this.store = null;
this._mounted = false;
}
/**
* 渲染组件
* @private
*/
_render() {
if (!this.mountPoint) {
return;
}
const state = this.store.store.getState();
// 创建评论表单
if (!this.commentForm) {
this.commentForm = new CommentForm(this.mountPoint, {
form: state.form,
formErrors: state.formErrors,
submitting: state.submitting,
onSubmit: () => this._handleSubmit(),
onFieldChange: (field, value) => this.store.updateFormField(field, value),
adminEmail: this.config.adminEmail,
onVerifyAdmin: (key) => this.api.verifyAdminKey(key)
});
this.commentForm.render();
}
// 创建错误提示
const existingError = this.mountPoint.querySelector('.cwd-error-inline');
if (state.error) {
if (!existingError) {
const errorEl = document.createElement('div');
errorEl.className = 'cwd-error-inline';
errorEl.innerHTML = `
<span>${state.error}</span>
<button type="button" class="cwd-error-close" data-action="clear-error">✕</button>
`;
errorEl.querySelector('[data-action="clear-error"]').addEventListener('click', () => {
this.store.clearError();
});
this.mountPoint.insertBefore(errorEl, this.mountPoint.firstChild);
}
} else if (existingError) {
existingError.remove();
}
const existingSuccess = this.mountPoint.querySelector('.cwd-success-inline');
if (state.successMessage) {
if (!existingSuccess) {
const successEl = document.createElement('div');
successEl.className = 'cwd-success-inline';
successEl.innerHTML = `
<span>${state.successMessage}</span>
<button type="button" class="cwd-error-close" data-action="clear-success">✕</button>
`;
successEl.querySelector('[data-action="clear-success"]').addEventListener('click', () => {
this.store.clearSuccess();
});
this.mountPoint.insertBefore(successEl, this.mountPoint.firstChild);
} else {
const span = existingSuccess.querySelector('span');
if (span) {
span.textContent = state.successMessage;
}
}
} else if (existingSuccess) {
existingSuccess.remove();
}
// 创建头部统计
let header = this.mountPoint.querySelector('.cwd-comments-header');
if (!header) {
header = document.createElement('div');
header.className = 'cwd-comments-header';
header.innerHTML = `
<h3 class="cwd-comments-count">
共 <span class="cwd-comments-count-number">0</span> 条评论
</h3>
`;
this.mountPoint.appendChild(header);
}
const countEl = header.querySelector('.cwd-comments-count-number');
if (countEl) {
countEl.textContent = state.pagination.totalCount;
}
// 创建评论列表
if (!this.commentList) {
const listContainer = document.createElement('div');
this.mountPoint.appendChild(listContainer);
this.commentList = new CommentList(listContainer, {
comments: state.comments,
loading: state.loading,
error: null,
currentPage: state.pagination.page,
totalPages: this.store.getTotalPages(),
replyingTo: state.replyingTo,
replyContent: state.replyContent,
replyError: state.replyError,
submitting: state.submitting,
adminEmail: this.config.adminEmail,
adminBadge: this.config.adminBadge,
onRetry: () => this.store.loadComments(),
onReply: (commentId) => this.store.startReply(commentId),
onSubmitReply: (commentId) => this.store.submitReply(commentId),
onCancelReply: () => this.store.cancelReply(),
onUpdateReplyContent: (content) => this.store.updateReplyContent(content),
onClearReplyError: () => this.store.clearReplyError(),
onPrevPage: () => this.store.goToPage(state.pagination.page - 1),
onNextPage: () => this.store.goToPage(state.pagination.page + 1),
onGoToPage: (page) => this.store.goToPage(page),
});
this.commentList.render();
}
}
/**
* 状态变化处理
* @private
*/
_onStateChange(state, prevState) {
if (!this._mounted) {
return;
}
// 根据回复状态显示/隐藏主评论表单
if (this.commentForm?.elements?.root) {
const formRoot = this.commentForm.elements.root;
if (state.replyingTo !== null) {
formRoot.style.display = 'none';
} else {
formRoot.style.display = '';
}
}
// 更新评论表单
if (this.commentForm) {
this.commentForm.setProps({
form: state.form,
formErrors: state.formErrors,
submitting: state.submitting,
adminEmail: this.config.adminEmail
});
}
// 更新错误提示
const existingError = this.mountPoint?.querySelector('.cwd-error-inline');
if (state.error) {
if (!existingError) {
const errorEl = document.createElement('div');
errorEl.className = 'cwd-error-inline';
errorEl.innerHTML = `
<span>${state.error}</span>
<button type="button" class="cwd-error-close" data-action="clear-error">✕</button>
`;
errorEl.querySelector('[data-action="clear-error"]').addEventListener('click', () => {
this.store.clearError();
});
this.mountPoint?.insertBefore(errorEl, this.mountPoint.firstChild);
}
} else if (existingError) {
existingError.remove();
}
const existingSuccess = this.mountPoint?.querySelector('.cwd-success-inline');
if (state.successMessage) {
if (!existingSuccess) {
const successEl = document.createElement('div');
successEl.className = 'cwd-success-inline';
successEl.innerHTML = `
<span>${state.successMessage}</span>
<button type="button" class="cwd-error-close" data-action="clear-success">✕</button>
`;
successEl.querySelector('[data-action="clear-success"]').addEventListener('click', () => {
this.store.clearSuccess();
});
this.mountPoint?.insertBefore(successEl, this.mountPoint.firstChild);
} else {
const span = existingSuccess.querySelector('span');
if (span) {
span.textContent = state.successMessage;
}
}
} else if (existingSuccess) {
existingSuccess.remove();
}
// 更新头部统计
const header = this.mountPoint?.querySelector('.cwd-comments-header');
const countEl = header?.querySelector('.cwd-comments-count-number');
if (countEl) {
countEl.textContent = state.pagination.totalCount;
}
// 更新评论列表
if (this.commentList) {
this.commentList.setProps({
comments: state.comments,
loading: state.loading,
currentPage: state.pagination.page,
totalPages: this.store.getTotalPages(),
replyingTo: state.replyingTo,
replyContent: state.replyContent,
replyError: state.replyError,
submitting: state.submitting,
});
}
}
/**
* 处理评论提交
* @private
*/
async _handleSubmit() {
const success = await this.store.submitNewComment();
if (success) {
// 表单内容已在 store 中清空
// 更新表单组件
if (this.commentForm) {
this.commentForm.state.localForm = { ...this.store.store.getState().form };
}
}
}
/**
* 更新配置
* @param {Object} newConfig - 新配置
*/
updateConfig(newConfig) {
const prevConfig = { ...this.config };
Object.assign(this.config, newConfig);
if (typeof window !== 'undefined') {
this.config.postSlug = window.location.origin + window.location.pathname;
}
if (typeof document !== 'undefined') {
this.config.postTitle = document.title || this.config.postSlug;
}
if (typeof window !== 'undefined') {
this.config.postUrl = window.location.href;
}
// 更新主题
if (newConfig.theme && this.mountPoint) {
this.mountPoint.setAttribute('data-theme', newConfig.theme);
}
const shouldReload =
this.config.apiBaseUrl !== prevConfig.apiBaseUrl ||
this.config.pageSize !== prevConfig.pageSize ||
this.config.postSlug !== prevConfig.postSlug;
if (shouldReload) {
const api = createApiClient(this.config);
this.api = api;
if (this.unsubscribe) {
this.unsubscribe();
}
this.store = createCommentStore(this.config, api.fetchComments.bind(api), api.submitComment.bind(api));
this.unsubscribe = this.store.store.subscribe((state) => {
this._onStateChange(state);
});
this.store.loadComments();
}
}
/**
* 获取当前配置
* @returns {Object}
*/
getConfig() {
return { ...this.config };
}
}

123
docs/widget/src/core/api.js Normal file
View File

@@ -0,0 +1,123 @@
/**
* API 请求封装
*/
/**
* 创建 API 客户端
* @param {Object} config - 配置对象
* @param {string} config.apiBaseUrl - API 基础地址
* @param {string} config.postSlug - 文章标识符
* @param {string} config.postTitle - 文章标题(可选)
* @param {string} config.postUrl - 文章 URL可选
* @returns {Object}
*/
export function createApiClient(config) {
const baseUrl = config.apiBaseUrl.replace(/\/$/, '');
/**
* 获取评论列表
* @param {number} page - 页码
* @param {number} limit - 每页数量
* @returns {Promise<Object>}
*/
async function fetchComments(page = 1, limit = 20) {
const params = new URLSearchParams({
post_slug: config.postSlug,
page: page.toString(),
limit: limit.toString(),
nested: 'true',
});
if (config.avatarPrefix) {
params.set('avatar_prefix', config.avatarPrefix);
}
const response = await fetch(`${baseUrl}/api/comments?${params}`);
if (!response.ok) {
throw new Error(`获取评论失败:${response.status} ${response.statusText}`);
}
return response.json();
}
/**
* 提交评论
* @param {Object} data - 评论数据
* @param {string} data.name - 昵称
* @param {string} data.email - 邮箱
* @param {string} data.url - 网址(可选)
* @param {string} data.content - 评论内容
* @param {number} data.parentId - 父评论 ID可选用于回复
* @returns {Promise<Object>}
*/
async function submitComment(data) {
const response = await fetch(`${baseUrl}/api/comments`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
post_slug: config.postSlug,
post_title: config.postTitle,
post_url: config.postUrl,
name: data.name,
email: data.email,
url: data.url || undefined,
content: data.content,
parent_id: data.parentId,
adminToken: data.adminToken
}),
});
if (!response.ok) {
// Try to parse error message
let msg = response.statusText;
try {
const json = await response.json();
if (json.message) msg = json.message;
} catch (e) {}
throw new Error(msg);
}
return response.json();
}
async function verifyAdminKey(key) {
const response = await fetch(`${baseUrl}/api/verify-admin`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ adminToken: key })
});
if (!response.ok) {
let msg = response.statusText;
try {
const json = await response.json();
if (json.message) msg = json.message;
} catch (e) {}
throw new Error(msg);
}
return response.json();
}
async function trackVisit() {
try {
await fetch(`${baseUrl}/api/analytics/visit`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
postSlug: config.postSlug,
postTitle: config.postTitle,
postUrl: config.postUrl
})
});
} catch (e) {
}
}
return {
fetchComments,
submitComment,
verifyAdminKey,
trackVisit
};
}

View File

@@ -0,0 +1,388 @@
/**
* 状态管理 - 使用发布-订阅模式
*/
import { auth } from '../utils/auth.js';
// localStorage 键名
const STORAGE_KEY = 'cwd_user_info';
/**
* 从 localStorage 读取用户信息
* @returns {Object}
*/
function loadUserInfo() {
try {
const data = localStorage.getItem(STORAGE_KEY);
if (data) {
const parsed = JSON.parse(data);
return {
name: parsed.name || '',
email: parsed.email || '',
url: parsed.url || '',
};
}
} catch (e) {}
return { name: '', email: '', url: '' };
}
/**
* 保存用户信息到 localStorage
* @param {string} name - 昵称
* @param {string} email - 邮箱
* @param {string} url - 网址
*/
function saveUserInfo(name, email, url) {
try {
const data = { name, email, url };
localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
} catch (e) {}
}
/**
* 简单的 Store 类
*/
class Store {
constructor(initialState) {
this.state = { ...initialState };
this.listeners = [];
}
/**
* 获取当前状态
* @returns {Object}
*/
getState() {
return { ...this.state };
}
/**
* 更新状态
* @param {Object} updates - 要更新的属性
*/
setState(updates) {
const prevState = { ...this.state };
this.state = { ...this.state, ...updates };
// 通知所有监听器
this.listeners.forEach((listener) => {
listener(this.state, prevState);
});
}
/**
* 订阅状态变化
* @param {Function} listener - 监听器函数
* @returns {Function} - 取消订阅的函数
*/
subscribe(listener) {
this.listeners.push(listener);
// 返回取消订阅的函数
return () => {
this.listeners = this.listeners.filter((l) => l !== listener);
};
}
}
/**
* 创建评论状态管理 Store
* @param {Object} config - 配置对象
* @param {Function} fetchComments - 获取评论的函数
* @param {Function} submitComment - 提交评论的函数
* @returns {Object}
*/
export function createCommentStore(config, fetchComments, submitComment) {
// 从 localStorage 加载用户信息
const savedInfo = loadUserInfo();
// 创建 store 实例
const store = new Store({
// 评论数据
comments: [],
loading: true,
error: null,
successMessage: '',
// 分页
pagination: {
page: 1,
limit: config.pageSize || 20,
total: 0,
totalCount: 0,
},
// 表单数据
form: {
name: savedInfo.name || '',
email: savedInfo.email || '',
url: savedInfo.url || '',
content: '',
},
formErrors: {},
submitting: false,
// 回复状态
replyingTo: null,
replyContent: '',
replyError: null,
});
// 监听用户信息变化,自动保存到 localStorage
store.subscribe((state) => {
if (state.form.name || state.form.email || state.form.url) {
saveUserInfo(state.form.name, state.form.email, state.form.url);
}
});
/**
* 加载评论列表
* @param {number} page - 页码
*/
async function loadComments(page = 1) {
store.setState({
loading: true,
error: null,
});
try {
const response = await fetchComments(page, store.getState().pagination.limit);
store.setState({
comments: response.data,
pagination: {
page: response.pagination.page,
limit: response.pagination.limit,
total: response.pagination.total,
totalCount: response.pagination.totalCount,
},
loading: false,
});
} catch (e) {
store.setState({
error: e instanceof Error ? e.message : '加载评论失败',
loading: false,
});
}
}
/**
* 提交评论
*/
async function submitNewComment() {
const state = store.getState();
const form = state.form;
// 验证表单
const { validateCommentForm } = await import('@/utils/validator.js');
const validation = validateCommentForm(form);
if (!validation.valid) {
store.setState({
formErrors: validation.errors,
});
return false;
}
// 清空错误
store.setState({
formErrors: {},
submitting: true,
error: null,
successMessage: '',
});
try {
const result = await submitComment({
name: form.name,
email: form.email,
url: form.url,
content: form.content,
adminToken: auth.getToken() // Add token if exists
});
const successMessage =
result && typeof result.message === 'string'
? result.message
: config.requireReview
? '已提交评论,待管理员审核后显示'
: '评论已提交';
store.setState({
form: { ...form, content: '' },
submitting: false,
successMessage,
});
// 重新加载评论
await loadComments(state.pagination.page);
return true;
} catch (e) {
store.setState({
error: e instanceof Error ? e.message : '提交评论失败',
submitting: false,
successMessage: '',
});
return false;
}
}
/**
* 提交回复
* @param {number} parentId - 父评论 ID
*/
async function submitReply(parentId) {
const state = store.getState();
// 验证回复内容
if (!state.replyContent.trim()) {
return false;
}
// 验证用户信息
const { validateReplyUserInfo } = await import('@/utils/validator.js');
const validation = validateReplyUserInfo(state.form);
if (!validation.valid) {
const errorMessages = Object.values(validation.errors).join('');
store.setState({
replyError: errorMessages,
});
return false;
}
store.setState({
formErrors: {},
submitting: true,
replyError: null,
});
try {
await submitComment({
name: state.form.name,
email: state.form.email,
url: state.form.url,
content: state.replyContent,
parentId,
adminToken: auth.getToken()
});
// 清空回复内容并关闭回复框
store.setState({
replyContent: '',
replyingTo: null,
submitting: false,
});
// 重新加载评论
await loadComments(state.pagination.page);
return true;
} catch (e) {
store.setState({
error: e instanceof Error ? e.message : '提交回复失败',
submitting: false,
});
return false;
}
}
/**
* 开始回复
* @param {number} commentId - 评论 ID
*/
function startReply(commentId) {
store.setState({
replyingTo: commentId,
replyContent: '',
replyError: null,
});
}
/**
* 取消回复
*/
function cancelReply() {
store.setState({
replyingTo: null,
replyContent: '',
replyError: null,
});
}
/**
* 更新表单字段
* @param {string} field - 字段名
* @param {string} value - 值
*/
function updateFormField(field, value) {
const form = { ...store.getState().form };
form[field] = value;
store.setState({ form });
}
/**
* 更新回复内容
* @param {string} content - 回复内容
*/
function updateReplyContent(content) {
store.setState({
replyContent: content,
});
}
/**
* 清除回复错误
*/
function clearReplyError() {
store.setState({
replyError: null,
});
}
/**
* 清除错误
*/
function clearError() {
store.setState({
error: null,
});
}
function clearSuccess() {
store.setState({
successMessage: '',
});
}
/**
* 切换页码
* @param {number} page - 页码
*/
function goToPage(page) {
const totalPages = store.getState().pagination.total;
if (page >= 1 && page <= totalPages) {
loadComments(page);
}
}
return {
// Store 实例
store,
// 计算属性方法
getTotalPages: () => {
const state = store.getState();
return state.pagination.total;
},
// 操作方法
loadComments,
submitNewComment,
submitReply,
startReply,
cancelReply,
updateFormField,
updateReplyContent,
clearReplyError,
clearError,
clearSuccess,
goToPage,
};
}