-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathMathJaxProvider.tsx
More file actions
80 lines (71 loc) · 1.83 KB
/
MathJaxProvider.tsx
File metadata and controls
80 lines (71 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React, { useEffect, useState } from 'react';
import { DEFAULT_CONFIG, getMathJax, loadMathJax } from 'mathjaxjs';
declare global {
interface Window {
MathJax: any;
}
}
interface MathJaxConfig {
/** 自定义 MathJax 脚本地址,不传则使用默认 CDN */
scriptURL?: string;
tex?: {
inlineMath?: string[][];
displayMath?: string[][];
packages?: string[];
processEscapes?: boolean;
processEnvironments?: boolean;
};
chtml?: {
fontURL?: string;
linebreaks?: { automatic: boolean; width: string };
};
}
interface MathJaxProviderProps {
children: any;
config?: MathJaxConfig;
loadingComponent?: any;
className?: string;
}
function MathJaxProvider({
children,
config,
loadingComponent,
className = ""
}: MathJaxProviderProps) {
const [mathJaxLoaded, setMathJaxLoaded] = useState(false);
const finalConfig = {
...DEFAULT_CONFIG,
...config,
tex: {
...DEFAULT_CONFIG.tex,
...config?.tex,
packages: config?.tex?.packages || DEFAULT_CONFIG.tex.packages
},
chtml: { ...DEFAULT_CONFIG.chtml, ...config?.chtml }
};
useEffect(() => {
if (typeof window !== 'undefined') {
if (getMathJax()) {
setMathJaxLoaded(true);
} else {
loadMathJax(() => {
setMathJaxLoaded(true);
}, finalConfig);
}
}
}, []);
useEffect(() => {
if (mathJaxLoaded && getMathJax()) {
const mathJax = getMathJax();
if (mathJax && mathJax.typesetPromise) {
mathJax.typesetPromise().then(() => {
console.log('MathJax typesetting complete');
});
}
}
}, [mathJaxLoaded]);
return React.createElement('div', { className },
mathJaxLoaded ? children : (loadingComponent || React.createElement('div', null, 'Loading MathJax...'))
);
}
export default MathJaxProvider;