import React, { useEffect, useRef, useState } from 'react'; import { Link } from 'react-router-dom'; /** * markdown 锁定块占位(未解锁时渲染,MD3 风格): * - login → 「登录后查看」+ 去登录按钮 * - reply → 「评论后可见」+ 去评论按钮(onGoComment 滚动到评论区) * - password → 密码输入 + 解锁按钮(onUnlock(index, password),失败显示错误态) * 键盘:密码框 Enter 提交、Esc 清空;Tab 自然可达。 */ export default function LockBlock({ index, type = 'password', onUnlock, onGoComment }) { const [pwd, setPwd] = useState(''); const [err, setErr] = useState(''); const [busy, setBusy] = useState(false); const pwdRef = useRef(null); // 弹窗/焦点管理不适用这里(块内内联控件),但复用 useDialog 的 Esc 习惯:直接监听 useEffect(() => { if (type === 'password') { const h = (e) => { if (e.key === 'Escape') { setPwd(''); setErr(''); } }; window.addEventListener('keydown', h); return () => window.removeEventListener('keydown', h); } return undefined; }, [type]); const tryUnlock = async () => { if (busy) return; if (!pwd) { setErr('请输入密码'); if (pwdRef.current) pwdRef.current.focus(); return; } setBusy(true); setErr(''); try { await onUnlock(index, pwd); // 成功后父级重渲染(unlocked/lockContent 更新),本组件被内容替换 } catch (e) { setErr((e && e.message) || '密码错误'); } setBusy(false); }; if (type === 'login') { return (