Skip to content

Commit

Permalink
Remove label/math/text from graph on umount (Doenet#1982)
Browse files Browse the repository at this point in the history
  • Loading branch information
dqnykamp authored Mar 8, 2023
1 parent cef9377 commit f2d878d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
26 changes: 21 additions & 5 deletions src/Viewer/renderers/label.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useRef } from 'react';
import React, { useContext, useEffect, useRef } from 'react';
import { BoardContext } from './graph';
import useDoenetRender from './useDoenetRenderer';
import { MathJax } from "better-react-mathjax";
Expand Down Expand Up @@ -26,6 +26,20 @@ export default React.memo(function Label(props) {
let previousPositionFromAnchor = useRef(null);


useEffect(() => {
//On unmount
return () => {
if (labelJXG.current !== null) {
labelJXG.current.off('drag');
labelJXG.current.off('down');
labelJXG.current.off('up');
board?.removeObject(labelJXG.current);
labelJXG.current = null;
}

}
}, [])

function createLabelJXG() {

let fixed = !SVs.draggable || SVs.fixed;
Expand Down Expand Up @@ -206,10 +220,12 @@ export default React.memo(function Label(props) {
if (SVs.hasLatex) {
setTimeout(() => {

labelJXG.current.needsUpdate = true;
labelJXG.current.setText(SVs.value)
labelJXG.current.update();
board.updateRenderer();
if (labelJXG.current) {
labelJXG.current.needsUpdate = true;
labelJXG.current.setText(SVs.value)
labelJXG.current.update();
board?.updateRenderer();
}

}, 1000)
}
Expand Down
26 changes: 21 additions & 5 deletions src/Viewer/renderers/math.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useRef } from 'react';
import React, { useContext, useEffect, useRef } from 'react';
import { BoardContext } from './graph';
import useDoenetRender from './useDoenetRenderer';
import { MathJax } from "better-react-mathjax";
Expand Down Expand Up @@ -26,6 +26,20 @@ export default React.memo(function MathComponent(props) {
let lastPositionFromCore = useRef(null);
let previousPositionFromAnchor = useRef(null);

useEffect(() => {
//On unmount
return () => {
if (mathJXG.current !== null) {
mathJXG.current.off('drag');
mathJXG.current.off('down');
mathJXG.current.off('up');
board?.removeObject(mathJXG.current);
mathJXG.current = null;
}

}
}, [])


function createMathJXG() {

Expand Down Expand Up @@ -225,10 +239,12 @@ export default React.memo(function MathComponent(props) {
// TODO: can we trigger this on MathJax being finished rather than wait 1 second?
setTimeout(() => {

mathJXG.current.needsUpdate = true;
mathJXG.current.setText(beginDelim + SVs.latex + endDelim)
mathJXG.current.update();
board.updateRenderer();
if (mathJXG.current) {
mathJXG.current.needsUpdate = true;
mathJXG.current.setText(beginDelim + SVs.latex + endDelim)
mathJXG.current.update();
board.updateRenderer();
}

}, 1000)
}
Expand Down
16 changes: 15 additions & 1 deletion src/Viewer/renderers/text.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useRef } from 'react';
import React, { useContext, useEffect, useRef } from 'react';
import { BoardContext } from './graph';
import useDoenetRender from './useDoenetRenderer';
import me from 'math-expressions';
Expand Down Expand Up @@ -26,6 +26,20 @@ export default React.memo(function Text(props) {
let previousPositionFromAnchor = useRef(null);


useEffect(() => {
//On unmount
return () => {
if (textJXG.current !== null) {
textJXG.current.off('drag');
textJXG.current.off('down');
textJXG.current.off('up');
board?.removeObject(textJXG.current);
textJXG.current = null;
}

}
}, [])

function createTextJXG() {

let fixed = !SVs.draggable || SVs.fixed;
Expand Down

0 comments on commit f2d878d

Please sign in to comment.