|
| 1 | +import * as React from "react"; |
| 2 | +import AutorenewIcon from "@material-ui/icons/Autorenew"; |
| 3 | + |
| 4 | +import { LINKS } from "../../utils/constants"; |
| 5 | + |
| 6 | +interface IEnvVisualizerState { |
| 7 | + loading: boolean; |
| 8 | +} |
| 9 | + |
| 10 | +class EnvVisualizer extends React.Component<{}, IEnvVisualizerState> { |
| 11 | + private $parent: HTMLElement | null; |
| 12 | + |
| 13 | + constructor(props: any) { |
| 14 | + super(props); |
| 15 | + this.state = { loading: true }; |
| 16 | + this.$parent = null; |
| 17 | + } |
| 18 | + |
| 19 | + public componentDidMount() { |
| 20 | + this.tryToLoad(); |
| 21 | + } |
| 22 | + |
| 23 | + public render() { |
| 24 | + return ( |
| 25 | + <div ref={r => (this.$parent = r)}> |
| 26 | + <p> |
| 27 | + The environmental visualizer generates the environmental model diagram |
| 28 | + based on breakpoints set in the editor. |
| 29 | + <br /> |
| 30 | + <br /> |
| 31 | + It is activated by clicking on the gutter of the editor (where all the |
| 32 | + line numbers are, on the left) to set a breakpoint, and then running |
| 33 | + the program. |
| 34 | + <br /> |
| 35 | + <br /> |
| 36 | + The environment model diagram follows a notation introduced in{" "} |
| 37 | + <a href={LINKS.SOURCE_DOCS_CHAPTER_3_2} target="_blank"> |
| 38 | + <i> |
| 39 | + Structure and Interpretation of Computer Programs, JavaScript |
| 40 | + Adaptation, Chapter 3, Section 2 |
| 41 | + </i> |
| 42 | + </a> |
| 43 | + . |
| 44 | + </p> |
| 45 | + {this.state.loading && <AutorenewIcon />} |
| 46 | + </div> |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + private tryToLoad = () => { |
| 51 | + const element = (window as any).EnvVisualizer; |
| 52 | + if (this.$parent && element) { |
| 53 | + // Env Visualizer has been loaded into the DOM |
| 54 | + element.init(this.$parent); |
| 55 | + this.setState((state, props) => { |
| 56 | + return { loading: false }; |
| 57 | + }); |
| 58 | + } else { |
| 59 | + // Try again in 1 second |
| 60 | + window.setTimeout(this.tryToLoad, 1000); |
| 61 | + } |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +export default EnvVisualizer; |
0 commit comments