Skip to content

Commit

Permalink
fix: toc range calcation
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Aug 31, 2024
1 parent d4581db commit 2ebcc58
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/renderer/src/components/ui/markdown/components/Toc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ import { getViewport } from "@renderer/atoms/hooks/viewport"
import { getElementTop } from "@renderer/lib/dom"
import { springScrollToElement } from "@renderer/lib/scroller"
import { cn } from "@renderer/lib/utils"
import { useGetWrappedElementPosition } from "@renderer/providers/wrapped-element-provider"
import {
useGetWrappedElementPosition,
useWrappedElementSize,
} from "@renderer/providers/wrapped-element-provider"
import { AnimatePresence, m } from "framer-motion"
import { throttle } from "lodash-es"
import { memo, useContext, useEffect, useMemo, useRef, useState } from "react"
import {
memo,
startTransition,
useContext,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react"
import { useEventCallback } from "usehooks-ts"

import { useScrollViewElement } from "../../scroll-area/hooks"
Expand Down Expand Up @@ -93,8 +105,10 @@ export const Toc: Component = ({ className }) => {
},
)

const { h } = useWrappedElementSize()
const [currentScrollRange, setCurrentScrollRange] = useState([-1, 0])
const titleBetweenPositionTopRangeMap = useMemo(() => {

const headingRangeParser = () => {
// calculate the range of data-container-top between each two headings
const titleBetweenPositionTopRangeMap = [] as [number, number][]
for (let i = 0; i < $headings.length - 1; i++) {
Expand All @@ -119,7 +133,16 @@ export const Toc: Component = ({ className }) => {
titleBetweenPositionTopRangeMap.push([headingTop, nextTop])
}
return titleBetweenPositionTopRangeMap
}, [$headings])
}

const [titleBetweenPositionTopRangeMap, setTitleBetweenPositionTopRangeMap] =
useState(headingRangeParser)

useLayoutEffect(() => {
startTransition(() => {
setTitleBetweenPositionTopRangeMap(headingRangeParser)
})
}, [$headings, h])

const throttleCallerRef = useRef<DebouncedFuncLeading<() => void>>()
const getWrappedElPos = useGetWrappedElementPosition()
Expand Down Expand Up @@ -152,10 +175,11 @@ export const Toc: Component = ({ className }) => {
setCurrentScrollRange([currentRangeIndex, precent])
} else {
const last = titleBetweenPositionTopRangeMap.at(-1) || [0, 0]
if (top > last[1]) {

if (top + winHeight > last[1]) {
setCurrentScrollRange([
titleBetweenPositionTopRangeMap.length - 1,
1,
titleBetweenPositionTopRangeMap.length,
1 - (last[1] - top) / winHeight,
])
} else {
setCurrentScrollRange([-1, 1])
Expand All @@ -168,6 +192,7 @@ export const Toc: Component = ({ className }) => {

return () => {
scrollContainerElement.removeEventListener("scroll", handler)
handler.cancel()
}
}, [
getWrappedElPos,
Expand Down

0 comments on commit 2ebcc58

Please sign in to comment.