6160a23117
同步基准:总仓库 a2ca13cb(= 本仓库初始导入 d8f3aee2) 范围:a2ca13cb..bfdfb339 中触及 web/ 的提交(不含 web/.claude/settings.local.json) - 8490ac02 feat(aliyun): integrate Wan 3 and Qwen Image models - 2aff08ec perf(analytics): 运营看板概览改读汇总表,首屏从 19s 降到 0.4s - dc69335e feat(experience): 体验中心图像/视频创作升级(能力 schema、估价、新创作台) - b059c657 fix(org): 下线 /org/create 自助加入企业页与 POST /api/org/join - 4fcf47e6 fix(i18n): 修复中/英/韩/繁体多语言错乱,补齐日/越/泰译文 - e4c301d5 fix: merge enterprise task record date filters - c9369b95 feat: expose enterprise image and video task records - a410fee5 feat(claude): 无断点请求自动注入 cache_control 断点(方案 2) - 521b3376 feat(claude): 缓存命中率优化——上游粘性路由 + 缓存时长强制改写 - 73b615dd feat(pricing): 企业成员不论登录身份均按所属企业等级计价与扣费 - f73f55c3 feat(client): 新增 WeyAi 版本说明管理与公开读取接口 - 8cdf1994 fix(topup): reset amount when switching from PayPal - 93a4ac34 feat(baidu): configure Seedance liveness header by channel - cb1818c5 fix(weyai): 按评审修正内容清理竞态、默认适用端与中文退回(M21) - 04214d83 feat(weyai): WeyAi 内容管理与客户端内容接口(M21 第二部分) - e3c4a57c feat(weyai): 设置页新增 WeyAi 页签与内容专用 COS 桶(M21 第一部分) - b5ee8e70 fix(auth): 会话角色/状态以 DB 为准,修复提升管理员后管理接口报权限不足 - e4618fc2 fix: 修复企业看板日志聚合死循环导致 CPU 打满,并降低看板查询开销 - 584aad52 style(web): 个人中心侧边栏按 prettier 换行 - b8d87064 fix: 小程序教程隐藏充值后保留原步骤编号,企业充值判断直接用 isMiniProgramWebView - eee7f5b2 fix: 小程序里企业首页不落到充值页,隐藏钱包入口与教程充值步骤 - 8b254d49 fix: 小程序里隐藏企业充值入口与页面 - a67661b4 fix(web): 小程序里个人中心默认页跳过充值和发票,避免来回跳转 - cff6c543 feat(web): WeyAi 小程序里打开个人中心时不出现充值和开票 - c6c99eb6 feat(billing): Claude 全部缓存写入统一按 1h 倍率计费 - 0699d1bf fix(web): 小程序 web-view 里注册页同样只显示站内方式 - 04853331 feat(web): WeyAi 小程序 web-view 里登录页只显示站内登录方式
643 lines
25 KiB
React
643 lines
25 KiB
React
/*
|
||
Copyright (C) 2025 QuantumNous
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Affero General Public License as
|
||
published by the Free Software Foundation, either version 3 of the
|
||
License, or (at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Affero General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Affero General Public License
|
||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
import React, { useEffect, useState, useCallback, useRef } from 'react';
|
||
import { API, showError } from '../../../helpers';
|
||
import { useTranslation } from 'react-i18next';
|
||
import MarkdownRenderer from '../markdown/MarkdownRenderer';
|
||
import {
|
||
Shield,
|
||
ScrollText,
|
||
ExternalLink,
|
||
ChevronUp,
|
||
ArrowLeft,
|
||
BookOpen,
|
||
Scale,
|
||
Printer,
|
||
} from 'lucide-react';
|
||
import { useNavigate, useLocation } from 'react-router-dom';
|
||
|
||
// 检查是否为 URL
|
||
const isUrl = (content) => {
|
||
try {
|
||
new URL(content.trim());
|
||
return true;
|
||
} catch {
|
||
return false;
|
||
}
|
||
};
|
||
|
||
// 检查是否为 HTML 内容
|
||
const isHtmlContent = (content) => {
|
||
if (!content || typeof content !== 'string') return false;
|
||
const htmlTagRegex = /<\/?[a-z][\s\S]*>/i;
|
||
return htmlTagRegex.test(content);
|
||
};
|
||
|
||
/**
|
||
* 抽取 HTML body 内容与 style 标签。
|
||
* 注意:此函数 NOT 是真正的 XSS 消毒,仅做结构整理;
|
||
* 信任来源:内容由后台管理员设置(需登录管理后台),如需在不受信场景使用应替换为 DOMPurify。
|
||
*/
|
||
const extractHtml = (html) => {
|
||
const tempDiv = document.createElement('div');
|
||
tempDiv.innerHTML = html;
|
||
const styles = Array.from(tempDiv.querySelectorAll('style'))
|
||
.map((style) => style.innerHTML)
|
||
.join('\n');
|
||
// 主动剔除 script 标签,降低风险
|
||
tempDiv.querySelectorAll('script').forEach((el) => el.remove());
|
||
const bodyContent = tempDiv.querySelector('body');
|
||
const content = bodyContent ? bodyContent.innerHTML : tempDiv.innerHTML;
|
||
return { content, styles };
|
||
};
|
||
|
||
// 根据 type / 标题获取对应的图标
|
||
const getIcon = ({ type, title }) => {
|
||
if (type === 'privacy') return Shield;
|
||
if (type === 'agreement') return Scale;
|
||
// 兜底(i18n 友好度有限):按标题关键字推断
|
||
if (title?.includes('隐私') || title?.toLowerCase().includes('privacy')) {
|
||
return Shield;
|
||
}
|
||
if (
|
||
title?.includes('服务') ||
|
||
title?.includes('用户') ||
|
||
title?.toLowerCase().includes('terms') ||
|
||
title?.toLowerCase().includes('service')
|
||
) {
|
||
return Scale;
|
||
}
|
||
return ScrollText;
|
||
};
|
||
|
||
/**
|
||
* 智能返回:
|
||
* 1) 有真实历史(length>1)→ navigate(-1)
|
||
* 2) 尝试 window.close()(脚本打开的标签页会成功,否则被忽略)
|
||
* 3) 兜底跳首页
|
||
*
|
||
* 说明:因 LoginForm 的协议链接使用了 rel="noopener noreferrer",
|
||
* 新标签里的 window.opener 为 null、document.referrer 为空,无法用 referrer 判同源。
|
||
*/
|
||
const useSmartBack = () => {
|
||
const navigate = useNavigate();
|
||
return useCallback(() => {
|
||
if (window.history.length > 1) {
|
||
navigate(-1);
|
||
return;
|
||
}
|
||
try {
|
||
window.close();
|
||
} catch (e) {
|
||
// 浏览器拒绝则继续走兜底
|
||
}
|
||
// 短延时给 close() 一个机会
|
||
setTimeout(() => {
|
||
if (!window.closed) navigate('/');
|
||
}, 50);
|
||
}, [navigate]);
|
||
};
|
||
|
||
/**
|
||
* 通用文档渲染组件
|
||
* @param {string} apiEndpoint API 接口地址
|
||
* @param {string} title 文档标题
|
||
* @param {string} cacheKey 本地存储缓存键
|
||
* @param {string} emptyMessage 空内容时的提示消息
|
||
* @param {'page'|'embedded'} mode page=独立页面(默认),embedded=嵌入式(无背景/头部)
|
||
* @param {'agreement'|'privacy'} [docType] 文档类型(用于图标识别,比标题更可靠)
|
||
* @param {function} [onProgressChange] 进度变化回调(嵌入模式用于宿主显示进度条)
|
||
*/
|
||
const DocumentRenderer = ({
|
||
apiEndpoint,
|
||
title,
|
||
cacheKey,
|
||
emptyMessage,
|
||
mode = 'page',
|
||
docType,
|
||
onProgressChange,
|
||
}) => {
|
||
const { t } = useTranslation();
|
||
const smartBack = useSmartBack();
|
||
const location = useLocation();
|
||
const [content, setContent] = useState('');
|
||
const [loading, setLoading] = useState(true);
|
||
const [htmlStyles, setHtmlStyles] = useState('');
|
||
const [processedHtmlContent, setProcessedHtmlContent] = useState('');
|
||
const [showBackToTop, setShowBackToTop] = useState(false);
|
||
const [readProgress, setReadProgress] = useState(0);
|
||
const scrollContainerRef = useRef(null);
|
||
const rootRef = useRef(null);
|
||
const isMountedRef = useRef(true);
|
||
const abortControllerRef = useRef(null);
|
||
|
||
const isEmbedded = mode === 'embedded';
|
||
const IconComponent = getIcon({ type: docType, title });
|
||
|
||
const processContent = useCallback((rawContent) => {
|
||
if (isHtmlContent(rawContent)) {
|
||
const { content: htmlContent, styles } = extractHtml(rawContent);
|
||
setProcessedHtmlContent(htmlContent);
|
||
setHtmlStyles(styles);
|
||
} else {
|
||
setProcessedHtmlContent('');
|
||
setHtmlStyles('');
|
||
}
|
||
}, []);
|
||
|
||
// 加载内容(含取消保护,避免在卸载后 setState)
|
||
useEffect(() => {
|
||
isMountedRef.current = true;
|
||
const controller = new AbortController();
|
||
abortControllerRef.current = controller;
|
||
|
||
const load = async () => {
|
||
const cachedContent = localStorage.getItem(cacheKey) || '';
|
||
if (cachedContent) {
|
||
setContent(cachedContent);
|
||
processContent(cachedContent);
|
||
setLoading(false);
|
||
}
|
||
|
||
try {
|
||
const res = await API.get(apiEndpoint, { signal: controller.signal });
|
||
if (!isMountedRef.current) return;
|
||
const { success, message, data } = res.data;
|
||
if (success && data) {
|
||
setContent(data);
|
||
processContent(data);
|
||
localStorage.setItem(cacheKey, data);
|
||
} else if (!cachedContent) {
|
||
showError(message || emptyMessage);
|
||
setContent('');
|
||
}
|
||
} catch (error) {
|
||
if (controller.signal.aborted) return; // 用户离开页面
|
||
if (!isMountedRef.current) return;
|
||
const cached = localStorage.getItem(cacheKey) || '';
|
||
if (!cached) {
|
||
showError(emptyMessage);
|
||
setContent('');
|
||
}
|
||
} finally {
|
||
if (isMountedRef.current) setLoading(false);
|
||
}
|
||
};
|
||
|
||
load();
|
||
|
||
return () => {
|
||
isMountedRef.current = false;
|
||
controller.abort();
|
||
};
|
||
}, [apiEndpoint, cacheKey, emptyMessage, processContent]);
|
||
|
||
// 滚动监听 + 阅读进度计算(page 模式监听 window,embedded 模式监听容器)
|
||
const handleScroll = useCallback(
|
||
(e) => {
|
||
let scrollTop;
|
||
let scrollHeight;
|
||
let clientHeight;
|
||
|
||
if (
|
||
e?.target &&
|
||
e.target !== window &&
|
||
e.target.scrollHeight !== undefined
|
||
) {
|
||
scrollTop = e.target.scrollTop;
|
||
scrollHeight = e.target.scrollHeight;
|
||
clientHeight = e.target.clientHeight;
|
||
} else {
|
||
scrollTop = window.scrollY;
|
||
scrollHeight = document.documentElement.scrollHeight;
|
||
clientHeight = window.innerHeight;
|
||
}
|
||
|
||
setShowBackToTop(scrollTop > 300);
|
||
|
||
const denom = scrollHeight - clientHeight;
|
||
const progress =
|
||
denom > 0 ? Math.min(100, Math.max(0, (scrollTop / denom) * 100)) : 0;
|
||
setReadProgress(progress);
|
||
if (isEmbedded && onProgressChange) {
|
||
onProgressChange(progress);
|
||
}
|
||
},
|
||
[isEmbedded, onProgressChange],
|
||
);
|
||
|
||
const scrollToTop = () => {
|
||
const target =
|
||
(isEmbedded && scrollContainerRef.current) || pageScrollElRef.current;
|
||
if (target) {
|
||
target.scrollTo({ top: 0, behavior: 'smooth' });
|
||
} else {
|
||
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||
}
|
||
};
|
||
|
||
// page 模式的实际滚动容器不是 window:PageLayout 外层是 height:100vh + overflow:hidden,
|
||
// 真正滚动的是内层 Semi Layout(一个普通 div)。元素的 scroll 事件不冒泡到 window,
|
||
// 所以原来只监听 window 时 showBackToTop 永远为 false、阅读进度条恒为 0。
|
||
// 这里向上找到真正可滚动的祖先来监听;找不到再回退 window(独立页/未来结构变化)。
|
||
const pageScrollElRef = useRef(null);
|
||
|
||
useEffect(() => {
|
||
if (isEmbedded) return;
|
||
|
||
const findScrollParent = (node) => {
|
||
let el = node?.parentElement;
|
||
while (el && el !== document.body) {
|
||
const overflowY = window.getComputedStyle(el).overflowY;
|
||
if (
|
||
(overflowY === 'auto' || overflowY === 'scroll') &&
|
||
el.scrollHeight > el.clientHeight
|
||
) {
|
||
return el;
|
||
}
|
||
el = el.parentElement;
|
||
}
|
||
return null;
|
||
};
|
||
|
||
const target = findScrollParent(rootRef.current) || window;
|
||
pageScrollElRef.current = target === window ? null : target;
|
||
|
||
const onScroll = (e) => {
|
||
if (target === window) {
|
||
handleScroll();
|
||
return;
|
||
}
|
||
handleScroll({ target });
|
||
};
|
||
|
||
target.addEventListener('scroll', onScroll, { passive: true });
|
||
return () => target.removeEventListener('scroll', onScroll);
|
||
}, [isEmbedded, handleScroll]);
|
||
|
||
// 处理 HTML 样式注入
|
||
useEffect(() => {
|
||
const styleId = `document-renderer-styles-${cacheKey}-${mode}`;
|
||
|
||
if (htmlStyles) {
|
||
let styleEl = document.getElementById(styleId);
|
||
if (!styleEl) {
|
||
styleEl = document.createElement('style');
|
||
styleEl.id = styleId;
|
||
styleEl.type = 'text/css';
|
||
document.head.appendChild(styleEl);
|
||
}
|
||
styleEl.innerHTML = htmlStyles;
|
||
} else {
|
||
const el = document.getElementById(styleId);
|
||
if (el) el.remove();
|
||
}
|
||
|
||
return () => {
|
||
const el = document.getElementById(styleId);
|
||
if (el) el.remove();
|
||
};
|
||
}, [htmlStyles, cacheKey, mode]);
|
||
|
||
// URL hash 自动滚动到对应 h2/h3 anchor(仅 page 模式 + 内容加载完成后)
|
||
useEffect(() => {
|
||
if (isEmbedded || loading || !content || !location.hash) return;
|
||
const id = decodeURIComponent(location.hash.slice(1));
|
||
if (!id) return;
|
||
const t = setTimeout(() => {
|
||
const el = document.getElementById(id);
|
||
if (el) el.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
||
}, 200);
|
||
return () => clearTimeout(t);
|
||
}, [isEmbedded, loading, content, location.hash]);
|
||
|
||
// 为 h2/h3 注入 id + 锚点链接(page 模式 + Markdown 已渲染 DOM 后)
|
||
const contentRef = useRef(null);
|
||
useEffect(() => {
|
||
if (isEmbedded || loading || !contentRef.current) return;
|
||
const root = contentRef.current;
|
||
const slugify = (text) =>
|
||
String(text || '')
|
||
.trim()
|
||
.toLowerCase()
|
||
.replace(/\s+/g, '-')
|
||
.replace(/[^\w\-一-龥]+/g, '')
|
||
.slice(0, 80);
|
||
|
||
const seen = new Map();
|
||
const headings = root.querySelectorAll('h2, h3');
|
||
headings.forEach((h) => {
|
||
const existing = h.querySelector('.doc-anchor');
|
||
if (existing) {
|
||
// 已处理;切换语言时只刷新读屏文案
|
||
existing.setAttribute('aria-label', t('复制段落链接'));
|
||
return;
|
||
}
|
||
let base = h.id || slugify(h.textContent);
|
||
if (!base) return;
|
||
let id = base;
|
||
const n = seen.get(base) || 0;
|
||
if (n > 0) id = `${base}-${n}`;
|
||
seen.set(base, n + 1);
|
||
h.id = id;
|
||
|
||
const a = document.createElement('a');
|
||
a.href = `#${id}`;
|
||
a.className = 'doc-anchor';
|
||
a.setAttribute('aria-label', t('复制段落链接'));
|
||
a.textContent = '#';
|
||
a.addEventListener('click', (e) => {
|
||
e.preventDefault();
|
||
const url = `${window.location.origin}${window.location.pathname}#${id}`;
|
||
if (navigator.clipboard) {
|
||
navigator.clipboard.writeText(url).catch(() => {});
|
||
}
|
||
history.replaceState(null, '', `#${id}`);
|
||
});
|
||
h.prepend(a);
|
||
});
|
||
}, [isEmbedded, loading, content, processedHtmlContent, t]);
|
||
|
||
// 文档内容样式(prose)—— 同时兼顾浅色与深色
|
||
const proseClass = `prose prose-base dark:prose-invert max-w-none
|
||
prose-headings:font-semibold prose-headings:text-gray-900 dark:prose-headings:text-gray-50 prose-headings:scroll-mt-24 prose-headings:tracking-tight
|
||
prose-h1:text-2xl prose-h1:mb-4 prose-h1:mt-0
|
||
prose-h2:text-lg prose-h2:mt-8 prose-h2:mb-3 prose-h2:pb-2 prose-h2:border-b prose-h2:border-gray-200 dark:prose-h2:border-gray-800
|
||
prose-h3:text-base prose-h3:mt-5 prose-h3:mb-2
|
||
prose-p:text-gray-700 dark:prose-p:text-gray-300 prose-p:leading-[1.75] prose-p:my-3
|
||
prose-a:text-brand-deep dark:prose-a:text-brand-from prose-a:no-underline prose-a:font-medium hover:prose-a:underline hover:prose-a:underline-offset-4
|
||
prose-strong:text-gray-900 dark:prose-strong:text-white prose-strong:font-semibold
|
||
prose-code:bg-brand/10 dark:prose-code:bg-brand/15 prose-code:text-brand-deep dark:prose-code:text-brand-from prose-code:px-1.5 prose-code:py-0.5 prose-code:rounded prose-code:text-[0.875em] prose-code:font-mono prose-code:before:content-none prose-code:after:content-none prose-code:border prose-code:border-brand/15 dark:prose-code:border-brand/20
|
||
prose-pre:bg-gray-50 dark:prose-pre:bg-gray-900/60 prose-pre:border prose-pre:border-gray-200 dark:prose-pre:border-gray-800 prose-pre:rounded-lg
|
||
prose-ul:my-3 prose-ol:my-3 prose-ul:space-y-1 prose-ol:space-y-1
|
||
prose-li:text-gray-700 dark:prose-li:text-gray-300 prose-li:my-0.5 prose-li:marker:text-brand
|
||
prose-blockquote:border-l-4 prose-blockquote:border-brand prose-blockquote:bg-brand/5 dark:prose-blockquote:bg-brand/10 prose-blockquote:py-2 prose-blockquote:px-4 prose-blockquote:rounded-r prose-blockquote:not-italic prose-blockquote:text-gray-700 dark:prose-blockquote:text-gray-300
|
||
prose-hr:border-gray-200 dark:prose-hr:border-gray-800 prose-hr:my-6
|
||
prose-table:rounded-lg prose-table:overflow-hidden
|
||
prose-th:bg-gray-50 dark:prose-th:bg-gray-800/50 prose-th:text-gray-900 dark:prose-th:text-gray-100
|
||
prose-td:border-gray-200 dark:prose-td:border-gray-800
|
||
prose-img:rounded-lg prose-img:border prose-img:border-gray-200 dark:prose-img:border-gray-800`;
|
||
|
||
// ============ Skeleton(短暂加载也更稳) ============
|
||
const renderLoading = () => (
|
||
<div className='doc-fade-in' aria-busy='true' aria-live='polite'>
|
||
<div className='space-y-3 max-w-2xl'>
|
||
<div className='h-7 w-2/3 rounded-md bg-gray-200/70 dark:bg-gray-800/70 animate-pulse' />
|
||
<div className='h-4 w-1/3 rounded-md bg-gray-200/60 dark:bg-gray-800/60 animate-pulse' />
|
||
<div className='h-px bg-gray-200 dark:bg-gray-800 my-5' />
|
||
<div className='space-y-2.5'>
|
||
<div className='h-4 w-full rounded bg-gray-200/60 dark:bg-gray-800/60 animate-pulse' />
|
||
<div className='h-4 w-[92%] rounded bg-gray-200/60 dark:bg-gray-800/60 animate-pulse' />
|
||
<div className='h-4 w-[85%] rounded bg-gray-200/60 dark:bg-gray-800/60 animate-pulse' />
|
||
</div>
|
||
<div className='h-6 w-1/2 rounded bg-gray-200/70 dark:bg-gray-800/70 animate-pulse mt-6' />
|
||
<div className='space-y-2.5'>
|
||
<div className='h-4 w-full rounded bg-gray-200/60 dark:bg-gray-800/60 animate-pulse' />
|
||
<div className='h-4 w-[88%] rounded bg-gray-200/60 dark:bg-gray-800/60 animate-pulse' />
|
||
</div>
|
||
</div>
|
||
<span className='sr-only'>{t('加载中...')}</span>
|
||
</div>
|
||
);
|
||
|
||
// ============ 空状态 ============
|
||
const renderEmpty = () => (
|
||
<div className='flex flex-col items-center justify-center py-20 px-6 text-center doc-fade-in'>
|
||
<div className='relative mb-5'>
|
||
<div className='absolute inset-0 bg-gradient-to-br from-brand-from/20 to-brand-to/20 dark:from-brand-from/10 dark:to-brand-to/10 blur-2xl rounded-full' />
|
||
<div className='relative w-16 h-16 bg-gray-100 dark:bg-gray-800/80 border border-gray-200 dark:border-gray-700/60 rounded-2xl flex items-center justify-center'>
|
||
<IconComponent
|
||
size={28}
|
||
className='text-gray-400 dark:text-gray-500'
|
||
/>
|
||
</div>
|
||
</div>
|
||
<h3 className='text-base font-semibold text-gray-800 dark:text-gray-100 mb-1.5'>
|
||
{t('内容正在准备中')}
|
||
</h3>
|
||
<p className='text-sm text-gray-500 dark:text-gray-400'>
|
||
{t('管理员尚未设置{{title}}内容', { title })}
|
||
</p>
|
||
</div>
|
||
);
|
||
|
||
// ============ URL 卡片 ============
|
||
const renderUrlCard = () => (
|
||
<div className='flex flex-col items-center justify-center py-16 px-6 text-center doc-fade-in'>
|
||
<div className='relative mb-5'>
|
||
<div className='absolute -inset-4 bg-gradient-to-br from-brand-from/30 to-brand-to/30 dark:from-brand-from/20 dark:to-brand-to/20 blur-2xl rounded-full' />
|
||
<div className='relative w-16 h-16 bg-gradient-to-br from-brand-from to-brand-to rounded-2xl flex items-center justify-center shadow-lg shadow-brand/30 dark:shadow-brand/20'>
|
||
<IconComponent size={28} className='text-white' />
|
||
</div>
|
||
</div>
|
||
<h3 className='text-lg font-semibold text-gray-900 dark:text-white mb-2'>
|
||
{title}
|
||
</h3>
|
||
<p className='text-sm text-gray-500 dark:text-gray-400 mb-6 max-w-md'>
|
||
{t('管理员设置了外部链接,点击下方按钮访问完整文档')}
|
||
</p>
|
||
<a
|
||
href={content.trim()}
|
||
target='_blank'
|
||
rel='noopener noreferrer'
|
||
title={content.trim()}
|
||
className='group inline-flex items-center gap-2 px-6 py-2.5 bg-gradient-to-r from-brand-from to-brand-to hover:shadow-lg hover:shadow-brand/30 dark:hover:shadow-brand/20 text-white text-sm font-medium rounded-lg shadow-sm transition-all hover:-translate-y-0.5'
|
||
>
|
||
<BookOpen size={16} />
|
||
{t('访问{{title}}', { title })}
|
||
<ExternalLink
|
||
size={14}
|
||
className='transition-transform group-hover:translate-x-0.5'
|
||
/>
|
||
</a>
|
||
</div>
|
||
);
|
||
|
||
// ============ HTML / Markdown 主体 ============
|
||
const renderDocumentBody = () => {
|
||
if (isHtmlContent(content)) {
|
||
return (
|
||
<div
|
||
ref={contentRef}
|
||
className={`${proseClass} doc-content`}
|
||
dangerouslySetInnerHTML={{ __html: processedHtmlContent }}
|
||
/>
|
||
);
|
||
}
|
||
return (
|
||
<div ref={contentRef} className={`${proseClass} doc-content`}>
|
||
<MarkdownRenderer content={content} />
|
||
</div>
|
||
);
|
||
};
|
||
|
||
// ============ 主体决策 ============
|
||
const renderBody = () => {
|
||
if (loading) return renderLoading();
|
||
if (!content || content.trim() === '') return renderEmpty();
|
||
if (isUrl(content)) return renderUrlCard();
|
||
return renderDocumentBody();
|
||
};
|
||
|
||
// ============ 嵌入模式 ============
|
||
if (isEmbedded) {
|
||
return (
|
||
<div
|
||
ref={scrollContainerRef}
|
||
onScroll={handleScroll}
|
||
className='relative h-full overflow-y-auto px-5 sm:px-8 py-6 doc-fade-in scroll-smooth doc-scrollbar'
|
||
>
|
||
{renderBody()}
|
||
|
||
<button
|
||
onClick={scrollToTop}
|
||
className={`sticky bottom-3 ml-auto block p-2 bg-white/95 dark:bg-gray-800/95 backdrop-blur-md rounded-full shadow-lg border border-gray-200 dark:border-gray-700 hover:shadow-xl hover:-translate-y-0.5 hover:bg-white dark:hover:bg-gray-800 transition-all duration-200 ${
|
||
showBackToTop ? 'opacity-100' : 'opacity-0 pointer-events-none'
|
||
}`}
|
||
aria-label={t('返回顶部')}
|
||
>
|
||
<ChevronUp size={16} className='text-gray-600 dark:text-gray-300' />
|
||
</button>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
// ============ 页面模式 ============
|
||
const hasRealContent =
|
||
!loading && content && content.trim() !== '' && !isUrl(content);
|
||
|
||
return (
|
||
<div
|
||
ref={rootRef}
|
||
className='relative min-h-screen bg-gray-50 dark:bg-gray-950 doc-fade-in doc-page-root'
|
||
>
|
||
{/* 阅读进度条 */}
|
||
<div
|
||
className='fixed top-0 left-0 right-0 z-50 h-[2px] bg-gradient-to-r from-brand-from via-brand to-brand-to origin-left transition-transform duration-150 ease-out doc-no-print'
|
||
style={{ transform: `scaleX(${readProgress / 100})` }}
|
||
aria-hidden
|
||
/>
|
||
|
||
{/* 顶部环境光 */}
|
||
<div
|
||
className='pointer-events-none absolute inset-x-0 top-0 h-[420px] overflow-hidden doc-no-print'
|
||
aria-hidden
|
||
>
|
||
<div className='absolute -top-40 left-1/2 -translate-x-1/2 w-[900px] h-[500px] bg-gradient-to-b from-brand-from/[0.08] via-brand-via/[0.04] to-transparent dark:from-brand-from/[0.12] dark:via-brand-via/[0.06] blur-3xl rounded-full' />
|
||
</div>
|
||
|
||
{/* Sticky Header */}
|
||
<header className='sticky top-0 z-40 bg-white/85 dark:bg-gray-950/85 backdrop-blur-xl border-b border-gray-200/70 dark:border-gray-800/70 doc-no-print'>
|
||
<div className='max-w-3xl mx-auto px-4 sm:px-6 h-14 flex items-center justify-between gap-2'>
|
||
<button
|
||
onClick={smartBack}
|
||
className='group inline-flex items-center gap-1.5 px-2.5 py-1.5 -ml-2 text-sm font-medium text-gray-600 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white hover:bg-gray-100 dark:hover:bg-gray-800/80 rounded-md transition-colors shrink-0'
|
||
>
|
||
<ArrowLeft
|
||
size={15}
|
||
className='transition-transform group-hover:-translate-x-0.5'
|
||
/>
|
||
<span>{t('返回')}</span>
|
||
</button>
|
||
|
||
<div className='flex items-center gap-2 text-gray-700 dark:text-gray-200 min-w-0'>
|
||
<div className='w-6 h-6 rounded-md bg-brand/10 dark:bg-brand/20 flex items-center justify-center shrink-0'>
|
||
<IconComponent
|
||
size={13}
|
||
className='text-brand-deep dark:text-brand-from'
|
||
/>
|
||
</div>
|
||
<span className='text-sm font-semibold tracking-tight truncate'>
|
||
{title}
|
||
</span>
|
||
</div>
|
||
|
||
{/* 打印按钮 */}
|
||
<button
|
||
onClick={() => window.print()}
|
||
className='inline-flex items-center justify-center w-8 h-8 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white hover:bg-gray-100 dark:hover:bg-gray-800/80 rounded-md transition-colors shrink-0'
|
||
aria-label={t('打印')}
|
||
title={t('打印')}
|
||
>
|
||
<Printer size={15} />
|
||
</button>
|
||
</div>
|
||
</header>
|
||
|
||
{/* 文档主体 */}
|
||
<main className='relative max-w-3xl mx-auto px-4 sm:px-6 py-8 sm:py-12'>
|
||
{/* 文档标题区 */}
|
||
{hasRealContent && (
|
||
<div className='mb-6 doc-slide-up'>
|
||
<div className='flex items-center gap-2 mb-3 doc-no-print'>
|
||
<div className='w-1 h-5 bg-gradient-to-b from-brand-from to-brand-to rounded-full' />
|
||
<span className='text-xs font-medium text-brand-deep dark:text-brand-from uppercase tracking-wider'>
|
||
{t('法律文档')}
|
||
</span>
|
||
</div>
|
||
<h1 className='text-2xl sm:text-3xl font-bold text-gray-900 dark:text-white mb-2 tracking-tight'>
|
||
{title}
|
||
</h1>
|
||
<p className='text-xs text-gray-500 dark:text-gray-400'>
|
||
{t('最后更新')}:{new Date().toLocaleDateString()}
|
||
</p>
|
||
</div>
|
||
)}
|
||
|
||
{/* 文档卡片 */}
|
||
<article className='doc-card relative bg-white dark:bg-gray-900 rounded-xl border border-gray-200/80 dark:border-gray-800/80 shadow-[0_1px_2px_rgba(0,0,0,0.04),0_4px_24px_-12px_rgba(0,0,0,0.08)] dark:shadow-[0_1px_2px_rgba(0,0,0,0.3),0_8px_32px_-12px_rgba(0,0,0,0.4)] p-6 sm:p-10 doc-slide-up'>
|
||
{renderBody()}
|
||
</article>
|
||
|
||
{/* 底部返回入口 */}
|
||
{!loading && (
|
||
<div className='mt-8 flex justify-center doc-no-print'>
|
||
<button
|
||
onClick={smartBack}
|
||
className='group inline-flex items-center gap-2 px-5 py-2 text-sm bg-white dark:bg-gray-900 hover:bg-gray-50 dark:hover:bg-gray-800/80 border border-gray-200 dark:border-gray-800 hover:border-gray-300 dark:hover:border-gray-700 rounded-lg text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-white shadow-sm transition-all'
|
||
>
|
||
<ArrowLeft
|
||
size={14}
|
||
className='transition-transform group-hover:-translate-x-0.5'
|
||
/>
|
||
{t('返回')}
|
||
</button>
|
||
</div>
|
||
)}
|
||
</main>
|
||
|
||
{/* 返回顶部 FAB */}
|
||
<button
|
||
onClick={scrollToTop}
|
||
className={`fixed bottom-6 right-6 z-50 p-2.5 bg-white/95 dark:bg-gray-800/95 backdrop-blur-md rounded-full shadow-lg border border-gray-200 dark:border-gray-700 hover:shadow-xl hover:-translate-y-0.5 hover:bg-white dark:hover:bg-gray-800 transition-all duration-200 doc-no-print ${
|
||
showBackToTop
|
||
? 'opacity-100 translate-y-0'
|
||
: 'opacity-0 translate-y-2 pointer-events-none'
|
||
}`}
|
||
aria-label={t('返回顶部')}
|
||
>
|
||
<ChevronUp size={18} className='text-gray-600 dark:text-gray-300' />
|
||
</button>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default DocumentRenderer;
|