mirror of
https://github.com/zadam/trilium.git
synced 2026-05-07 03:27:20 +02:00
chore(sidebar): deduplicate math rendering
This commit is contained in:
@@ -15,6 +15,7 @@ import attributes from "../../services/attributes";
|
||||
import froca from "../../services/froca";
|
||||
import keyboard_actions from "../../services/keyboard_actions";
|
||||
import { ViewScope } from "../../services/link";
|
||||
import math from "../../services/math";
|
||||
import options, { type OptionValue } from "../../services/options";
|
||||
import protected_session_holder from "../../services/protected_session_holder";
|
||||
import server from "../../services/server";
|
||||
@@ -1435,3 +1436,25 @@ export function useColorScheme() {
|
||||
|
||||
return prefersDark ? "dark" : "light";
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders math equations within elements that have the `.math-tex` class.
|
||||
* Used by sidebar widgets like Table of Contents and Highlights list to display math content.
|
||||
*
|
||||
* @param containerRef - Ref to the container element that may contain math elements
|
||||
* @param deps - Dependencies that trigger re-rendering (e.g., text content)
|
||||
*/
|
||||
export function useMathRendering(containerRef: RefObject<HTMLElement>, deps: unknown[]) {
|
||||
useEffect(() => {
|
||||
if (!containerRef.current) return;
|
||||
const mathElements = containerRef.current.querySelectorAll(".math-tex");
|
||||
|
||||
for (const mathEl of mathElements) {
|
||||
try {
|
||||
math.render(mathEl.textContent || "", mathEl as HTMLElement);
|
||||
} catch (e) {
|
||||
console.warn("Failed to render math:", e);
|
||||
}
|
||||
}
|
||||
}, deps); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
}
|
||||
|
||||
@@ -3,9 +3,8 @@ import { createPortal } from "preact/compat";
|
||||
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
|
||||
|
||||
import { t } from "../../services/i18n";
|
||||
import math from "../../services/math";
|
||||
import { randomString } from "../../services/utils";
|
||||
import { useActiveNoteContext, useContentElement, useIsNoteReadOnly, useNoteProperty, useTextEditor, useTriliumOptionJson } from "../react/hooks";
|
||||
import { useActiveNoteContext, useContentElement, useIsNoteReadOnly, useMathRendering, useNoteProperty, useTextEditor, useTriliumOptionJson } from "../react/hooks";
|
||||
import Modal from "../react/Modal";
|
||||
import RawHtml from "../react/RawHtml";
|
||||
import { HighlightsListOptions } from "../type_widgets/options/text_notes";
|
||||
@@ -111,19 +110,7 @@ function HighlightItem<T extends RawHighlight>({ highlight, onClick }: {
|
||||
}) {
|
||||
const contentRef = useRef<HTMLElement>(null);
|
||||
|
||||
// Render math equations after component mounts/updates
|
||||
useEffect(() => {
|
||||
if (!contentRef.current) return;
|
||||
const mathElements = contentRef.current.querySelectorAll(".math-tex");
|
||||
|
||||
for (const mathEl of mathElements ?? []) {
|
||||
try {
|
||||
math.render(mathEl.textContent || "", mathEl as HTMLElement);
|
||||
} catch (e) {
|
||||
console.warn("Failed to render math in highlights:", e);
|
||||
}
|
||||
}
|
||||
}, [highlight.text]);
|
||||
useMathRendering(contentRef, [highlight.text]);
|
||||
|
||||
return (
|
||||
<li onClick={onClick}>
|
||||
|
||||
@@ -5,9 +5,8 @@ import clsx from "clsx";
|
||||
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
|
||||
|
||||
import { t } from "../../services/i18n";
|
||||
import math from "../../services/math";
|
||||
import { randomString } from "../../services/utils";
|
||||
import { useActiveNoteContext, useContentElement, useGetContextData, useIsNoteReadOnly, useNoteProperty, useTextEditor } from "../react/hooks";
|
||||
import { useActiveNoteContext, useContentElement, useGetContextData, useIsNoteReadOnly, useMathRendering, useNoteProperty, useTextEditor } from "../react/hooks";
|
||||
import Icon from "../react/Icon";
|
||||
import RawHtml from "../react/RawHtml";
|
||||
import RightPanelWidget from "./RightPanelWidget";
|
||||
@@ -84,19 +83,7 @@ function TableOfContentsHeading({ heading, scrollToHeading, activeHeadingId }: {
|
||||
const isActive = heading.id === activeHeadingId;
|
||||
const contentRef = useRef<HTMLElement>(null);
|
||||
|
||||
// Render math equations after component mounts/updates
|
||||
useEffect(() => {
|
||||
if (!contentRef.current) return;
|
||||
const mathElements = contentRef.current.querySelectorAll(".math-tex");
|
||||
|
||||
for (const mathEl of mathElements ?? []) {
|
||||
try {
|
||||
math.render(mathEl.textContent || "", mathEl as HTMLElement);
|
||||
} catch (e) {
|
||||
console.warn("Failed to render math in TOC:", e);
|
||||
}
|
||||
}
|
||||
}, [heading.text]);
|
||||
useMathRendering(contentRef, [heading.text]);
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
Reference in New Issue
Block a user