Skip to content

Commit 273618e

Browse files
committed
initial commit
1 parent 91ed3fa commit 273618e

5 files changed

Lines changed: 80 additions & 6 deletions

File tree

js/panes/ImageComparePane.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@
77
*
88
*/
99

10-
import React from 'react';
10+
import React, { useEffect, useRef } from 'react';
1111

1212
import Pane from './Pane';
13+
import { typesetMathJax } from './utils/mathjaxHelpers';
1314

1415
function ImageComparePane(props) {
1516
const { content, title, id } = props;
17+
const containerRef = useRef();
18+
19+
useEffect(() => {
20+
let cancelled = false;
21+
typesetMathJax(contentRef.current, () => cancelled);
22+
return () => {
23+
cancelled = true;
24+
};
25+
}, [content]);
1626

1727
// If content isn't an array, fallback
1828
if (!Array.isArray(content)) {
@@ -38,6 +48,7 @@ function ImageComparePane(props) {
3848
return (
3949
<Pane {...props} handleDownload={handleDownload}>
4050
<div
51+
ref={containerRef}
4152
style={{
4253
display: 'flex',
4354
flexDirection: 'row',

js/panes/ImagePane.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import React, { useContext, useEffect, useRef, useState } from 'react';
1212
import ApiContext from '../api/ApiContext';
1313
import EventSystem from '../EventSystem';
1414
import Pane from './Pane';
15+
import { typesetMathJax } from './utils/mathjaxHelpers';
1516

1617
const DEFAULT_HEIGHT = 400;
1718
const DEFAULT_WIDTH = 300;
@@ -25,6 +26,7 @@ function ImagePane(props) {
2526
// --------------
2627
const paneRef = useRef();
2728
const imgRef = useRef();
29+
const captionRef = useRef();
2830
const mouseLocationRef = useRef({ x: null, y: null });
2931
const [view, setView] = useState({ scale: 1, tx: 0, ty: 0 });
3032
const [imgDim, setImgDim] = useState({ width: null, height: 0 });
@@ -147,7 +149,11 @@ function ImagePane(props) {
147149
const commitSliderSelection = (value) => {
148150
const idx = parseInt(value, 10);
149151
if (Number.isNaN(idx)) return;
150-
sendPaneMessage({ event_type: 'SliderMoved', index: idx, pane_data: false }, id, envID);
152+
sendPaneMessage(
153+
{ event_type: 'SliderMoved', index: idx, pane_data: false },
154+
id,
155+
envID
156+
);
151157
};
152158

153159
const finalizeSlider = (evt) => {
@@ -307,12 +313,20 @@ function ImagePane(props) {
307313
content = content[actualSelected];
308314
}
309315

316+
useEffect(() => {
317+
let cancelled = false;
318+
typesetMathJax(captionRef.current, () => cancelled);
319+
return () => {
320+
cancelled = true;
321+
};
322+
}, [content.caption]);
323+
310324
// add caption as widget
311325
if (content.caption) {
312326
widgets.splice(
313327
0,
314328
0,
315-
<span className="widget" key="img_caption">
329+
<span className="widget" key="img_caption" ref={captionRef}>
316330
{content.caption}
317331
</span>
318332
);

js/panes/PlotPane.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import React, { useContext, useEffect, useRef, useState } from 'react';
1111
const { usePrevious } = require('../util');
12+
import { typesetMathJax } from './utils/mathjaxHelpers';
1213
import ApiContext from '../api/ApiContext';
1314
import Pane from './Pane';
1415
const { sgg } = require('ml-savitzky-golay-generalized');
@@ -20,6 +21,7 @@ var PlotPane = (props) => {
2021
// state variables
2122
// --------------
2223
const plotlyRef = useRef();
24+
const captionRef = useRef();
2325
const maxsmoothvalue = 100;
2426
const [smoothWidgetActive, setSmoothWidgetActive] = useState(false);
2527
const [smoothvalue, setSmoothValue] = useState(1);
@@ -41,6 +43,14 @@ var PlotPane = (props) => {
4143
}
4244
}, [selected]);
4345

46+
useEffect(() => {
47+
let cancelled = false;
48+
typesetMathJax(captionRef.current, () => cancelled);
49+
return () => {
50+
cancelled = true;
51+
};
52+
}, [content && content.caption]);
53+
4454
// private events
4555
// -------------
4656
const toggleSmoothWidget = () => {
@@ -327,7 +337,7 @@ var PlotPane = (props) => {
327337
var caption_widget = '';
328338
if (content && content.caption) {
329339
caption_widget = (
330-
<div className="widget plot-caption" key="plot_caption">
340+
<div className="widget plot-caption" key="plot_caption" ref={captionRef}>
331341
{content.caption}
332342
</div>
333343
);

js/panes/TextPane.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
*
88
*/
99

10-
import React, { useContext, useEffect } from 'react';
10+
import React, { useContext, useEffect, useRef } from 'react';
1111
import ScrollToBottom from 'react-scroll-to-bottom';
1212

1313
import ApiContext from '../api/ApiContext';
1414
import EventSystem from '../EventSystem';
1515
import Pane from './Pane';
16+
import { typesetMathJax } from './utils/mathjaxHelpers';
1617

1718
function TextPane(props) {
1819
const { sendPaneMessage } = useContext(ApiContext);
1920
const { envID, id, content, isFocused } = props;
21+
const contentRef = useRef();
2022

2123
// private events
2224
// --------------
@@ -70,6 +72,14 @@ function TextPane(props) {
7072
};
7173
});
7274

75+
useEffect(() => {
76+
let cancelled = false;
77+
typesetMathJax(contentRef.current, () => cancelled);
78+
return () => {
79+
cancelled = true;
80+
};
81+
}, [content]);
82+
7383
// rendering
7484
// ---------
7585

@@ -88,7 +98,7 @@ function TextPane(props) {
8898
initialScrollBehavior={initialScrollBehavior}
8999
mode="bottom"
90100
>
91-
<div dangerouslySetInnerHTML={{ __html: content }} />
101+
<div ref={contentRef} dangerouslySetInnerHTML={{ __html: content }} />
92102
</ScrollToBottom>
93103
</Pane>
94104
);

js/panes/utils/mathjaxHelpers.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2017-present, The Visdom Authors
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
8+
*/
9+
10+
function typesetMathJax(element, isCancelled) {
11+
if (!element) return;
12+
if (
13+
!window.MathJax ||
14+
!window.MathJax.startup ||
15+
!window.MathJax.startup.promise
16+
) {
17+
return;
18+
}
19+
window.MathJax.startup.promise = window.MathJax.startup.promise
20+
.then(() => {
21+
if (isCancelled && isCancelled()) return;
22+
return window.MathJax.typesetPromise([element]);
23+
})
24+
.catch((err) => {
25+
console.error('MathJax typeset error:', err);
26+
});
27+
}
28+
29+
export { typesetMathJax };

0 commit comments

Comments
 (0)