@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 81 lines 3.58 KB
Raw
1 import { useGT } from "gt-react";
2 import { lazy, memo, Suspense, useLayoutEffect, useRef, useState } from "react";
3
4 import { observeResize } from "../../lib/observeResize";
5
6 import { RenderErrorBoundary } from "../RenderErrorBoundary";
7
8 const MathTypeset = lazy(() => import("./MathTypeset"));
9
10 /**
11 * One formula from `remark-math`. Shows its TeX until KaTeX loads, and
12 * keeps showing it when KaTeX cannot parse it, such as mid-stream. Copying
13 * any part of a formula copies its whole TeX source.
14 */
15 export const MarkdownMath = memo(function MarkdownMath(props: {
16 tex: string;
17 display: boolean;
18 focusable?: boolean;
19 }) {
20 const gt = useGT();
21 const viewportRef = useRef<HTMLSpanElement>(null);
22 const contentRef = useRef<HTMLSpanElement>(null);
23 const [overflows, setOverflows] = useState(false);
24 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- observes formula DOM width to make overflowing equations keyboard scrollable
25 useLayoutEffect(() => {
26 const viewport = viewportRef.current;
27 const content = contentRef.current;
28 if (!viewport || !content || props.focusable === false) return;
29 const measure = () => setOverflows(viewport.scrollWidth > viewport.clientWidth);
30 measure();
31 // Typesetting and streamed TeX can change content width without resizing the viewport.
32 return observeResize([viewport, content], measure);
33 }, [props.focusable]);
34 const scrollable = props.focusable !== false && overflows;
35 const tex = props.tex.trim();
36 const source = props.display ? `\\[\n${tex}\n\\]` : `\\(${tex}\\)`;
37 const fallback = <span className="whitespace-pre-wrap">{source}</span>;
38 return (
39 <span
40 className={
41 props.display
42 ? "chat-markdown-math-display my-[0.65rem] block in-[.chat-markdown-table-container]:max-w-none in-[.chat-markdown-table-container]:overflow-visible"
43 : "inline-block max-w-full align-middle whitespace-nowrap wrap-normal break-normal in-[.chat-markdown-table-container]:max-w-none in-[.chat-markdown-table-container]:overflow-visible"
44 }
45 data-markdown-math={props.display ? "display" : "inline"}
46 data-markdown-copy={props.display ? `${source}\n\n` : source}
47 role={props.focusable === false ? undefined : "group"}
48 aria-label={
49 props.focusable === false
50 ? undefined
51 : props.display
52 ? gt("Display equation")
53 : gt("Inline equation")
54 }
55 >
56 <span
57 ref={viewportRef}
58 className="block max-w-full overflow-x-auto overflow-y-hidden in-[.chat-markdown-table-container]:max-w-none in-[.chat-markdown-table-container]:overflow-visible px-0.5 py-0.75 focus-visible:outline-1 focus-visible:outline-solid focus-visible:-outline-offset-1 focus-visible:outline-border [&::-webkit-scrollbar]:h-2 [&::-webkit-scrollbar-thumb]:rounded [&::-webkit-scrollbar-thumb]:border [&::-webkit-scrollbar-thumb]:border-transparent [&::-webkit-scrollbar-thumb]:bg-clip-content"
59 tabIndex={scrollable ? 0 : -1}
60 aria-label={
61 !scrollable
62 ? undefined
63 : props.display
64 ? gt("Scroll display equation")
65 : gt("Scroll inline equation")
66 }
67 >
68 <span
69 ref={contentRef}
70 className={props.display ? "inline-block w-max min-w-full" : "inline-block w-max"}
71 >
72 <RenderErrorBoundary resetKeys={[tex]} fallback={fallback}>
73 <Suspense fallback={fallback}>
74 <MathTypeset tex={tex} display={props.display} fallback={fallback} />
75 </Suspense>
76 </RenderErrorBoundary>
77 </span>
78 </span>
79 </span>
80 );
81 });