Files
Trilium/apps/client/src/widgets/react/RawHtml.tsx

39 lines
930 B
TypeScript
Raw Normal View History

2025-08-07 20:08:51 +03:00
import { CSSProperties } from "preact/compat";
type HTMLElementLike = string | HTMLElement | JQuery<HTMLElement>;
2025-08-05 20:35:53 +03:00
interface RawHtmlProps {
2025-08-06 18:38:52 +03:00
className?: string;
2025-08-07 20:08:51 +03:00
html: HTMLElementLike;
style?: CSSProperties;
2025-08-05 20:35:53 +03:00
}
2025-08-07 20:08:51 +03:00
export default function RawHtml({ className, html, style }: RawHtmlProps) {
2025-08-06 18:38:52 +03:00
return <span
className={className}
dangerouslySetInnerHTML={getHtml(html)}
2025-08-07 20:08:51 +03:00
style={style}
2025-08-06 18:38:52 +03:00
/>;
}
2025-08-07 20:08:51 +03:00
export function RawHtmlBlock({ className, html, style }: RawHtmlProps) {
2025-08-06 18:38:52 +03:00
return <div
className={className}
dangerouslySetInnerHTML={getHtml(html)}
2025-08-07 20:08:51 +03:00
style={style}
2025-08-06 18:38:52 +03:00
/>
}
2025-08-07 20:08:51 +03:00
function getHtml(html: string | HTMLElement | JQuery<HTMLElement>) {
if (typeof html === "object" && "length" in html) {
html = html[0];
}
if (typeof html === "object" && "outerHTML" in html) {
2025-08-06 18:38:52 +03:00
html = html.outerHTML;
}
return {
2025-08-07 20:08:51 +03:00
__html: html as string
2025-08-06 18:38:52 +03:00
};
2025-08-05 20:35:53 +03:00
}