import { For, Show, createSignal, createEffect, on } from 'solid-js' import { clsx } from 'clsx' import { DEFAULT_HEADER_OFFSET } from '../../stores/router' import { useLocalize } from '../../context/localize' import debounce from 'debounce' import { Icon } from '../_shared/Icon' import styles from './TableOfContents.module.scss' interface Props { variant: 'article' | 'editor' parentSelector: string body: string } const scrollToHeader = (element) => { window.scrollTo({ behavior: 'smooth', top: element.getBoundingClientRect().top - document.body.getBoundingClientRect().top - DEFAULT_HEADER_OFFSET }) } export const TableOfContents = (props: Props) => { const { t } = useLocalize() const [headings, setHeadings] = createSignal([]) const [areHeadingsLoaded, setAreHeadingsLoaded] = createSignal(false) const [isVisible, setIsVisible] = createSignal(props.variant === 'article') const toggleIsVisible = () => { setIsVisible((visible) => !visible) } const updateHeadings = () => { const { parentSelector } = props // eslint-disable-next-line unicorn/prefer-spread setHeadings(Array.from(document.querySelector(parentSelector).querySelectorAll('h2, h3, h4'))) setAreHeadingsLoaded(true) } const debouncedUpdateHeadings = debounce(updateHeadings, 500) createEffect( on( () => props.body, () => debouncedUpdateHeadings() ) ) return ( 2 : headings().length > 1) } >

{t('contents')}

    {(h) => (
  • )}
) }