logComponent.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import * as React from "react";
  2. import { GlobalState } from '../../globalState';
  3. import * as ReactDOM from 'react-dom';
  4. require("./log.scss");
  5. interface ILogComponentProps {
  6. globalState: GlobalState;
  7. }
  8. export class LogComponent extends React.Component<ILogComponentProps, { logs: string[] }> {
  9. constructor(props: ILogComponentProps) {
  10. super(props);
  11. this.state = { logs: [] };
  12. }
  13. componentWillMount() {
  14. this.props.globalState.onLogRequiredObservable.add(log => {
  15. let currentLogs = this.state.logs;
  16. currentLogs.push(...log.split("\r\n"));
  17. this.setState({ logs: currentLogs });
  18. });
  19. }
  20. componentDidUpdate() {
  21. const logConsole = ReactDOM.findDOMNode(this.refs["log-console"]) as HTMLElement;
  22. if (!logConsole) {
  23. return;
  24. }
  25. logConsole.scrollTop = logConsole.scrollHeight;
  26. }
  27. render() {
  28. return (
  29. <div id="log-console" ref={"log-console"} >
  30. {
  31. this.state.logs.map((l, i) => {
  32. return (
  33. <div key={i} className="log">
  34. {l}
  35. </div>
  36. )
  37. })
  38. }
  39. </div>
  40. );
  41. }
  42. }