Skip to content

Commit

Permalink
fix: toc depth
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Aug 12, 2022
1 parent f7f806a commit 2cf6c6d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
35 changes: 35 additions & 0 deletions src/components/app/ErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { Component } from 'react'

export class ErrorBoundary extends Component<{
children: React.ReactNode
[k: string]: any
}> {
state: any = {
error: null,
errorInfo: null,
}

componentDidCatch(error, errorInfo) {
console.error(error, 'render error')
console.error(errorInfo)

this.setState({
error,
errorInfo,
})
}

render() {
const { errorInfo } = this.state
const { children, ...restProps } = this.props

if (errorInfo) {
return <div>渲染报错</div>
}

// @ts-ignore
return React.cloneElement(children, {
...restProps,
})
}
}
1 change: 1 addition & 0 deletions src/components/layouts/BasicLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export const BasicLayout: FC = observer(({ children }) => {
</Suspense>

<div className="app-content">{children}</div>

<Suspense fallback={null}>
<Footer />
<MusicMiniPlayerStoreControlled />
Expand Down
10 changes: 6 additions & 4 deletions src/components/widgets/Toc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ export const Toc: FC<TocProps> = memo(
depth={heading.depth}
title={heading.title}
key={heading.title}
rootDepth={headings.reduce(
(d: number, cur) => Math.min(d, cur.depth),
headings[0].depth,
)}
rootDepth={
headings.reduce(
(d: number, cur) => Math.min(d, cur.depth),
headings[0].depth,
) as any as number
}
/>
</RightLeftTransitionView>
)
Expand Down
33 changes: 20 additions & 13 deletions src/components/widgets/Toc/item.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { clsx } from 'clsx'
import type { FC } from 'react'
import { memo, useEffect, useMemo } from 'react'
import { memo, useCallback, useEffect, useMemo } from 'react'

import { springScrollToElement } from '~/utils/spring'

Expand All @@ -24,25 +24,32 @@ export const TocItem: FC<{
const renderDepth = useMemo(() => {
const result = depth - rootDepth

return result == 0 ? 1 : result
return result
}, [depth, rootDepth])
return (
<a
data-index={index}
href={`#${title}`}
className={clsx(styles['toc-link'], active && styles['active'])}
style={{
paddingLeft: depth >= rootDepth ? `${1.2 * renderDepth}rem` : undefined,
}}
style={useMemo(
() => ({
paddingLeft:
depth >= rootDepth ? `${1.2 + renderDepth * 0.6}rem` : undefined,
}),
[depth, renderDepth, rootDepth],
)}
data-depth={depth}
onClick={(e) => {
e.preventDefault()
onClick(index)
const $el = document.getElementById(title)
if ($el) {
springScrollToElement($el, undefined, -100)
}
}}
onClick={useCallback(
(e) => {
e.preventDefault()
onClick(index)
const $el = document.getElementById(title)
if ($el) {
springScrollToElement($el, undefined, -100)
}
},
[index, title, onClick],
)}
>
<span className={styles['a-pointer']}>{title}</span>
</a>
Expand Down

0 comments on commit 2cf6c6d

Please sign in to comment.