From 1f15cba9a0d417fab40437a6ed64bbc43ffb0fe8 Mon Sep 17 00:00:00 2001 From: lyzno1 <92089059+lyzno1@users.noreply.github.com> Date: Wed, 6 Aug 2025 14:45:06 +0800 Subject: [PATCH] Enhance API documentation TOC with modern design and improved UX (#23490) Co-authored-by: crazywoola <427733928@qq.com> --- web/app/(commonLayout)/datasets/Doc.tsx | 116 ++++++++-- web/app/components/develop/doc.tsx | 211 ++++++++++++------ .../develop/template/template.ja.mdx | 2 +- .../develop/template/template.zh.mdx | 2 +- .../template/template_advanced_chat.en.mdx | 4 +- .../template/template_advanced_chat.ja.mdx | 4 +- .../template/template_advanced_chat.zh.mdx | 4 +- .../develop/template/template_chat.en.mdx | 4 +- .../develop/template/template_chat.ja.mdx | 4 +- .../develop/template/template_chat.zh.mdx | 4 +- web/i18n/de-DE/workflow.ts | 12 + web/i18n/es-ES/workflow.ts | 12 + web/i18n/fa-IR/workflow.ts | 12 + web/i18n/fr-FR/workflow.ts | 12 + web/i18n/hi-IN/workflow.ts | 12 + web/i18n/it-IT/workflow.ts | 12 + web/i18n/ja-JP/workflow.ts | 12 + web/i18n/ko-KR/workflow.ts | 12 + web/i18n/pl-PL/workflow.ts | 12 + web/i18n/pt-BR/workflow.ts | 12 + web/i18n/ro-RO/workflow.ts | 12 + web/i18n/ru-RU/workflow.ts | 12 + web/i18n/sl-SI/workflow.ts | 12 + web/i18n/th-TH/workflow.ts | 12 + web/i18n/tr-TR/workflow.ts | 12 + web/i18n/uk-UA/workflow.ts | 12 + web/i18n/vi-VN/workflow.ts | 12 + web/i18n/zh-Hant/workflow.ts | 12 + 28 files changed, 462 insertions(+), 109 deletions(-) diff --git a/web/app/(commonLayout)/datasets/Doc.tsx b/web/app/(commonLayout)/datasets/Doc.tsx index 78f767dbec..b31e0a4161 100644 --- a/web/app/(commonLayout)/datasets/Doc.tsx +++ b/web/app/(commonLayout)/datasets/Doc.tsx @@ -3,7 +3,7 @@ import { useEffect, useMemo, useState } from 'react' import { useContext } from 'use-context-selector' import { useTranslation } from 'react-i18next' -import { RiListUnordered } from '@remixicon/react' +import { RiCloseLine, RiListUnordered } from '@remixicon/react' import TemplateEn from './template/template.en.mdx' import TemplateZh from './template/template.zh.mdx' import TemplateJa from './template/template.ja.mdx' @@ -22,6 +22,7 @@ const Doc = ({ apiBaseUrl }: DocProps) => { const { t } = useTranslation() const [toc, setToc] = useState>([]) const [isTocExpanded, setIsTocExpanded] = useState(false) + const [activeSection, setActiveSection] = useState('') const { theme } = useTheme() // Set initial TOC expanded state based on screen width @@ -47,12 +48,47 @@ const Doc = ({ apiBaseUrl }: DocProps) => { return null }).filter((item): item is { href: string; text: string } => item !== null) setToc(tocItems) + // Set initial active section + if (tocItems.length > 0) + setActiveSection(tocItems[0].href.replace('#', '')) } } setTimeout(extractTOC, 0) }, [locale]) + // Track scroll position for active section highlighting + useEffect(() => { + const handleScroll = () => { + const scrollContainer = document.querySelector('.scroll-container') + if (!scrollContainer || toc.length === 0) + return + + // Find active section based on scroll position + let currentSection = '' + toc.forEach((item) => { + const targetId = item.href.replace('#', '') + const element = document.getElementById(targetId) + if (element) { + const rect = element.getBoundingClientRect() + // Consider section active if its top is above the middle of viewport + if (rect.top <= window.innerHeight / 2) + currentSection = targetId + } + }) + + if (currentSection && currentSection !== activeSection) + setActiveSection(currentSection) + } + + const scrollContainer = document.querySelector('.scroll-container') + if (scrollContainer) { + scrollContainer.addEventListener('scroll', handleScroll) + handleScroll() // Initial check + return () => scrollContainer.removeEventListener('scroll', handleScroll) + } + }, [toc, activeSection]) + // Handle TOC item click const handleTocClick = (e: React.MouseEvent, item: { href: string; text: string }) => { e.preventDefault() @@ -84,40 +120,76 @@ const Doc = ({ apiBaseUrl }: DocProps) => { return (
-
+
{isTocExpanded ? ( -