playground.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import * as React from "react";
  2. import * as ReactDOM from "react-dom";
  3. import { MonacoComponent } from "./components/monacoComponent";
  4. import { RenderingComponent } from "./components/rendererComponent";
  5. import { GlobalState, EditionMode, RuntimeMode } from "./globalState";
  6. import { FooterComponent } from "./components/footerComponent";
  7. import { HeaderComponent } from "./components/headerComponent";
  8. import { SaveManager } from "./tools/saveManager";
  9. import { LoadManager } from "./tools/loadManager";
  10. import { WaitRingComponent } from "./components/waitRingComponent";
  11. import { MetadataComponent } from "./components/metadataComponent";
  12. import { HamburgerMenuComponent } from "./components/hamburgerMenu";
  13. import { Utilities } from "./tools/utilities";
  14. import { ShortcutManager } from "./tools/shortcutManager";
  15. import { ErrorDisplayComponent } from "./components/errorDisplayComponent";
  16. import { ExamplesComponent } from "./components/examplesComponent";
  17. import { QRCodeComponent } from "./components/qrCodeComponent";
  18. require("./scss/main.scss");
  19. const Split = require("split.js").default;
  20. interface IPlaygroundProps {
  21. runtimeMode: RuntimeMode;
  22. }
  23. export class Playground extends React.Component<IPlaygroundProps, { errorMessage: string; mode: EditionMode }> {
  24. private splitRef: React.RefObject<HTMLDivElement>;
  25. private monacoRef: React.RefObject<HTMLDivElement>;
  26. private renderingRef: React.RefObject<HTMLDivElement>;
  27. private _globalState: GlobalState;
  28. private _splitInstance: any;
  29. public saveManager: SaveManager;
  30. public loadManager: LoadManager;
  31. public shortcutManager: ShortcutManager;
  32. public constructor(props: IPlaygroundProps) {
  33. super(props);
  34. this._globalState = new GlobalState();
  35. this._globalState.runtimeMode = props.runtimeMode || RuntimeMode.Editor;
  36. this.splitRef = React.createRef();
  37. this.monacoRef = React.createRef();
  38. this.renderingRef = React.createRef();
  39. let defaultDesktop = Utilities.ReadBoolFromStore("editor", true) ? EditionMode.Desktop : EditionMode.RenderingOnly;
  40. this.state = { errorMessage: "", mode: window.innerWidth < this._globalState.MobileSizeTrigger ? this._globalState.mobileDefaultMode : defaultDesktop };
  41. window.addEventListener("resize", () => {
  42. let defaultDesktop = Utilities.ReadBoolFromStore("editor", true) ? EditionMode.Desktop : EditionMode.RenderingOnly;
  43. this.setState({ mode: window.innerWidth < this._globalState.MobileSizeTrigger ? this._globalState.mobileDefaultMode : defaultDesktop });
  44. });
  45. this._globalState.onMobileDefaultModeChangedObservable.add(() => {
  46. this.setState({ mode: this._globalState.mobileDefaultMode });
  47. });
  48. this._globalState.onEditorDisplayChangedObservable.add((value) => {
  49. this.setState({ mode: value ? EditionMode.Desktop : EditionMode.RenderingOnly });
  50. });
  51. // Managers
  52. this.saveManager = new SaveManager(this._globalState);
  53. this.loadManager = new LoadManager(this._globalState);
  54. this.shortcutManager = new ShortcutManager(this._globalState);
  55. }
  56. componentDidMount() {
  57. this.checkSize();
  58. }
  59. componentDidUpdate() {
  60. this.checkSize();
  61. }
  62. checkSize() {
  63. if (this._globalState.runtimeMode !== RuntimeMode.Editor) {
  64. return;
  65. }
  66. switch (this.state.mode) {
  67. case EditionMode.CodeOnly:
  68. this._splitInstance?.destroy();
  69. this._splitInstance = null;
  70. this.renderingRef.current!.classList.add("hidden");
  71. this.monacoRef.current!.classList.remove("hidden");
  72. this.monacoRef.current!.style.width = "100%";
  73. break;
  74. case EditionMode.RenderingOnly:
  75. this._splitInstance?.destroy();
  76. this._splitInstance = null;
  77. this.monacoRef.current!.classList.add("hidden");
  78. this.renderingRef.current!.classList.remove("hidden");
  79. this.renderingRef.current!.style.width = "100%";
  80. break;
  81. case EditionMode.Desktop:
  82. if (this._splitInstance) {
  83. return;
  84. }
  85. this.renderingRef.current!.classList.remove("hidden");
  86. this.monacoRef.current!.classList.remove("hidden");
  87. this._splitInstance = Split([this.monacoRef.current, this.renderingRef.current], {
  88. direction: "horizontal",
  89. minSize: [200, 200],
  90. gutterSize: 4,
  91. });
  92. break;
  93. }
  94. }
  95. public render() {
  96. if (this._globalState.runtimeMode === RuntimeMode.Full) {
  97. return (
  98. <div className="canvasZone" id="pg-root-full">
  99. <RenderingComponent globalState={this._globalState} />
  100. <ErrorDisplayComponent globalState={this._globalState} />
  101. <WaitRingComponent globalState={this._globalState} />
  102. </div>
  103. );
  104. }
  105. if (this._globalState.runtimeMode === RuntimeMode.Frame) {
  106. return (
  107. <div className="canvasZone" id="pg-root-frame">
  108. <RenderingComponent globalState={this._globalState} />
  109. <FooterComponent globalState={this._globalState} />
  110. <ErrorDisplayComponent globalState={this._globalState} />
  111. <WaitRingComponent globalState={this._globalState} />
  112. </div>
  113. );
  114. }
  115. return (
  116. <div id="pg-root">
  117. <HeaderComponent globalState={this._globalState} />
  118. <div ref={this.splitRef} id="pg-split">
  119. <MonacoComponent globalState={this._globalState} className="pg-split-part" refObject={this.monacoRef} />
  120. <div ref={this.renderingRef} id="canvasZone" className="pg-split-part canvasZone">
  121. <RenderingComponent globalState={this._globalState} />
  122. </div>
  123. </div>
  124. {window.innerWidth < 1080 && <HamburgerMenuComponent globalState={this._globalState} />}
  125. <ExamplesComponent globalState={this._globalState} />
  126. <FooterComponent globalState={this._globalState} />
  127. <QRCodeComponent globalState={this._globalState} />
  128. <ErrorDisplayComponent globalState={this._globalState} />
  129. <WaitRingComponent globalState={this._globalState} />
  130. <MetadataComponent globalState={this._globalState} />
  131. </div>
  132. );
  133. }
  134. public static Show(hostElement: HTMLElement, mode: RuntimeMode) {
  135. const playground = React.createElement(Playground, { runtimeMode: mode });
  136. ReactDOM.render(playground, hostElement);
  137. }
  138. }