import { createContext, useContext, useLayoutEffect, useMemo, useRef } from 'react'; import type { ReactNode, RefObject } from 'react'; import type { MarkdownFileMentions } from '@deepseek-ai/dsh-client-ui-primitives'; import { MarkdownText } from './markdown/MarkdownText.js'; import { readerPathImages } from './platform-media.js'; import { WORD_MOTION, WordTimeline } from './word-timeline.js'; import css from './Reader.module.css'; const WordScope = createContext({ enabled: false, generation: 0 }); const CODE_LABELS = { copyLabel: '复制代码', copiedLabel: '已复制' }; function useSourceReveal(element: RefObject, born: number | null, generation: number) { const scope = useContext(WordScope); const cancelled = useRef(false); useLayoutEffect(() => { const target = element.current; if (!target) return; target.dataset.wordState = 'settled'; if (!scope.enabled || scope.generation !== generation) { cancelled.current = true; return; } if (cancelled.current || born === null || document.hidden) return; const age = Number(document.timeline.currentTime ?? performance.now()) - born; if (age >= WORD_MOTION.duration || typeof target.animate !== 'function') return; // The reference opacity + blur frames, applied only to this new word. // No color interpolation, stylesheet mutation, or whole-paragraph wipe. const animation = target.animate([ { opacity: 0, filter: `blur(${WORD_MOTION.blur}px)` }, { opacity: 1, filter: 'blur(0px)' }, ], { duration: WORD_MOTION.duration, easing: WORD_MOTION.easing, fill: 'backwards' }); // One absolute clock prevents newly mounted/resegmented leaves from getting // a fresh delay or running ahead because their layout effects ran later. animation.startTime = born; target.dataset.wordState = 'resolving'; const finish = () => { animation.cancel(); target.dataset.wordState = 'settled'; document.removeEventListener('visibilitychange', hidden); }; animation.onfinish = finish; const hidden = () => { if (document.hidden) { cancelled.current = true; finish(); } }; document.addEventListener('visibilitychange', hidden); return () => { animation.cancel(); document.removeEventListener('visibilitychange', hidden); target.dataset.wordState = 'settled'; }; }, [element, born, generation, scope.enabled, scope.generation]); } function Word({ children, born, generation, offset, inline }: { children: string; born: number | null; generation: number; offset: number; inline: boolean }) { const element = useRef(null); useSourceReveal(element, born, generation); return {children}; } /** A native code block enters on the same clock, without rebuilding its text. */ function MotionAtom({ children, born, generation, offset }: { children: ReactNode; born: number | null; generation: number; offset: number }) { const element = useRef(null); useSourceReveal(element, born, generation); return
{children}
; } /** Native Think is literal text, not Markdown. Spans never alter its bytes. */ export function MotionPlainText({ text, enabled, revision }: { text: string; enabled: boolean; revision: number }) { const timeline = useRef(); timeline.current ??= new WordTimeline(); timeline.current.begin(text, enabled, revision, Number(document.timeline.currentTime ?? performance.now())); const current = timeline.current; const generation = current.generation; const scope = useMemo(() => ({ enabled, generation }), [enabled, generation]); return
{current.hasLiveText ? current.words(text, 0).map(word => word.text.trim() ? {word.text} : word.text) : text}
; } /** Native DSH Markdown semantics with a stable text-leaf animation hook. */ export function MotionMarkdown({ text, streaming, enabled, revision, fileMentions }: { text: string; streaming: boolean; enabled: boolean; revision: number; fileMentions?: MarkdownFileMentions; }) { const timeline = useRef(); timeline.current ??= new WordTimeline(); timeline.current.begin(text, enabled, revision, Number(document.timeline.currentTime ?? performance.now())); const generation = timeline.current.generation; const scope = useMemo(() => ({ enabled, generation }), [enabled, generation]); const renderText = useMemo(() => (value: string, offset: number, inline = false): ReactNode => { const current = timeline.current!; if (!current.hasLiveText) return value; return current.words(value, offset).map(word => word.text.trim() ? {word.text} : word.text); }, []); const renderAtom = useMemo(() => (children: ReactNode, offset: number): ReactNode => { const current = timeline.current!; return current.hasLiveText ? {children} : children; }, []); return ; }