From c82da2252ef2a466e31a2747b582ef5eb9ebfe61 Mon Sep 17 00:00:00 2001 From: shuai Date: Tue, 18 Mar 2025 14:34:50 +0800 Subject: [PATCH] fix: fix the number of lines displayed in the list information, and click on the list to jump to an exception; footnote link to jump to an exception --- ui/src/components/QuestionList/index.tsx | 14 +++-- ui/src/pages/Questions/Detail/index.tsx | 71 ++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/ui/src/components/QuestionList/index.tsx b/ui/src/components/QuestionList/index.tsx index 3ab9f45df..fcb0994f2 100644 --- a/ui/src/components/QuestionList/index.tsx +++ b/ui/src/components/QuestionList/index.tsx @@ -173,17 +173,21 @@ const QuestionList: FC = ({
e.stopPropagation()} to={pathFactory.questionLanding(li.id, li.url_title)}> {li.title} {li.status === 2 ? ` [${t('closed')}]` : ''}
{viewType === 'card' && ( - +
+ e.stopPropagation()} + /> +
)}
diff --git a/ui/src/pages/Questions/Detail/index.tsx b/ui/src/pages/Questions/Detail/index.tsx index 8c8921b88..95ae52347 100644 --- a/ui/src/pages/Questions/Detail/index.tsx +++ b/ui/src/pages/Questions/Detail/index.tsx @@ -242,6 +242,77 @@ const Index = () => { } } + useEffect(() => { + // handle footnote links + const fixFootnoteLinks = () => { + const footnoteLinks = document.querySelectorAll( + 'a[href^="#"]:not([data-footnote-fixed])', + ); + + footnoteLinks.forEach((link) => { + link.setAttribute('data-footnote-fixed', 'true'); + const href = link.getAttribute('href'); + link.addEventListener('click', (e) => { + e.preventDefault(); + const targetId = href?.substring(1) || ''; + const targetElement = document.getElementById(targetId); + + if (targetElement) { + window.history.pushState(null, '', `${location.pathname}${href}`); + + scrollToElementTop(targetElement); + } + }); + }); + + // 检查当前URL是否包含锚点,如果有,自动滚动到正确位置 + if (window.location.hash) { + const { hash } = window.location; + const targetElement = document.getElementById(hash.substring(1)); + + if (targetElement) { + // 给浏览器一点时间来完成渲染 + setTimeout(() => { + scrollToElementTop(targetElement); + }, 100); + } + } + }; + fixFootnoteLinks(); + + const observer = new MutationObserver(() => { + fixFootnoteLinks(); + }); + + observer.observe(document.body, { + childList: true, + subtree: true, + attributes: true, + attributeFilter: ['id', 'href'], + }); + + // 监听 URL hash 变化 + const handleHashChange = () => { + if (window.location.hash) { + const { hash } = window.location; + const targetElement = document.getElementById(hash.substring(1)); + + if (targetElement) { + setTimeout(() => { + scrollToElementTop(targetElement); + }, 100); + } + } + }; + + window.addEventListener('hashchange', handleHashChange); + + return () => { + observer.disconnect(); + window.removeEventListener('hashchange', handleHashChange); + }; + }, [location.pathname]); + return (