hedgedoc/src/components/markdown-renderer/hooks/use-post-toc-ast-on-change.ts
Tilman Vatteroth 0627e0f551
Replace document bar with sidebar (#937)
Signed-off-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de>
2021-01-24 21:39:47 +01:00

19 lines
619 B
TypeScript

/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import equal from 'fast-deep-equal'
import { TocAst } from 'markdown-it-toc-done-right'
import { RefObject, useEffect, useRef } from 'react'
export const usePostTocAstOnChange = (tocAst: RefObject<TocAst|undefined>, onTocChange?: (ast: TocAst) => void): void => {
const lastTocAst = useRef<TocAst>()
useEffect(() => {
if (onTocChange && tocAst.current && !equal(tocAst, lastTocAst.current)) {
lastTocAst.current = tocAst.current
onTocChange(tocAst.current)
}
})
}