Files
Folo/apps/renderer/src/components/ui/markdown/HTML.tsx
T
3a85febb6e refactor(monorepo): migrate to monorepo (#1059)
* init

Signed-off-by: Innei <i@innei.in>

* chore: utils import

Signed-off-by: Innei <tukon479@gmail.com>

* remove stable router

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: components import

Signed-off-by: Innei <tukon479@gmail.com>

* chore: auto-fix linting and formatting issues

* minify

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* fix: test

Signed-off-by: Innei <tukon479@gmail.com>

* chore: ui component extract

Signed-off-by: Innei <tukon479@gmail.com>

* fix: import

Signed-off-by: Innei <tukon479@gmail.com>

* re-render

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: remove hooks

Signed-off-by: Innei <tukon479@gmail.com>

* fix

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* setting

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: remove icons

Signed-off-by: Innei <i@innei.in>

* typings

Signed-off-by: Innei <i@innei.in>

* feat: add horizontal scroll functionality to TimelineTabs (#1081)

Co-authored-by: xhuajin <xhuajin@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>

* fix: Wrong initial position of tab animation (#1087)

Co-authored-by: hamster1963 <hamster1963@users.noreply.github.com>

* fix: disable auto toggle readability in web

* fix: margin condition in EntryListHeader (#1103)

* chore(i18n): add daily reward system translation (#1102)

Co-authored-by: lc4t <lc4t@users.noreply.github.com>

* fix: deps

Signed-off-by: Innei <i@innei.in>

* fix: type

Signed-off-by: Innei <i@innei.in>

* update

Signed-off-by: Innei <i@innei.in>

* fix

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
Signed-off-by: Innei <tukon479@gmail.com>
Co-authored-by: Innei <Innei@users.noreply.github.com>
Co-authored-by: Huajin <114134574+xhuajin@users.noreply.github.com>
Co-authored-by: xhuajin <xhuajin@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: 仓鼠 <71394853+hamster1963@users.noreply.github.com>
Co-authored-by: hamster1963 <hamster1963@users.noreply.github.com>
Co-authored-by: Konv Suu <2583695112@qq.com>
Co-authored-by: lc4t <11334449+lc4t@users.noreply.github.com>
Co-authored-by: lc4t <lc4t@users.noreply.github.com>
2024-10-24 14:37:43 +08:00

91 lines
2.7 KiB
TypeScript

import { MemoedDangerousHTMLStyle } from "@follow/components/common/MemoedDangerousHTMLStyle.js"
import katexStyle from "katex/dist/katex.min.css?raw"
import { createElement, Fragment, memo, useEffect, useMemo, useRef, useState } from "react"
import { parseHtml } from "~/lib/parse-html"
import { useWrappedElementSize } from "~/providers/wrapped-element-provider"
import type { MediaInfoRecord } from "../media"
import { MediaContainerWidthProvider, MediaInfoRecordProvider } from "../media"
import { MarkdownRenderContainerRefContext } from "./context"
export type HTMLProps<A extends keyof JSX.IntrinsicElements = "div"> = {
children: string | null | undefined
as: A
accessory?: React.ReactNode
noMedia?: boolean
mediaInfo?: MediaInfoRecord
handleTranslate?: (html: HTMLElement | null) => void
} & JSX.IntrinsicElements[A] &
Partial<{
renderInlineStyle: boolean
}>
const HTMLImpl = <A extends keyof JSX.IntrinsicElements = "div">(props: HTMLProps<A>) => {
const {
children,
renderInlineStyle,
as = "div",
accessory,
noMedia,
mediaInfo,
handleTranslate: translate,
...rest
} = props
const [remarkOptions, setRemarkOptions] = useState({
renderInlineStyle,
noMedia,
})
const [shouldForceReMountKey, setShouldForceReMountKey] = useState(0)
useEffect(() => {
setRemarkOptions((options) => {
if (JSON.stringify(options) === JSON.stringify({ renderInlineStyle, noMedia })) {
return options
}
setShouldForceReMountKey((key) => key + 1)
return { ...options, renderInlineStyle, noMedia }
})
}, [renderInlineStyle, noMedia])
const [refElement, setRefElement] = useState<HTMLElement | null>(null)
const onceRef = useRef(false)
useEffect(() => {
if (onceRef.current || !refElement) {
return
}
translate?.(refElement)
onceRef.current = true
}, [translate, refElement])
const markdownElement = useMemo(
() =>
children &&
parseHtml(children, {
...remarkOptions,
}).toContent(),
[children, remarkOptions],
)
const { w: containerWidth } = useWrappedElementSize()
if (!markdownElement) return null
return (
<MarkdownRenderContainerRefContext.Provider value={refElement}>
<MediaContainerWidthProvider width={containerWidth}>
<MediaInfoRecordProvider mediaInfo={mediaInfo}>
<MemoedDangerousHTMLStyle>{katexStyle}</MemoedDangerousHTMLStyle>
{createElement(as, { ...rest, ref: setRefElement }, markdownElement)}
</MediaInfoRecordProvider>
</MediaContainerWidthProvider>
{!!accessory && <Fragment key={shouldForceReMountKey}>{accessory}</Fragment>}
</MarkdownRenderContainerRefContext.Provider>
)
}
export const HTML = memo(HTMLImpl)