diff --git a/src/ConsolePanel.res b/src/ConsolePanel.res
index 2777aea2b..04f11d9b8 100644
--- a/src/ConsolePanel.res
+++ b/src/ConsolePanel.res
@@ -5,9 +5,7 @@ type logLevel = [
]
@react.component
-let make = (~compilerState, ~runOutput) => {
- let (logs, setLogs) = React.useState(_ => [])
-
+let make = (~logs, ~setLogs) => {
React.useEffect(() => {
let cb = e => {
let data = e["data"]
@@ -22,23 +20,13 @@ let make = (~compilerState, ~runOutput) => {
Some(() => Webapi.Window.removeEventListener("message", cb))
}, [])
- React.useEffect(() => {
- if runOutput {
- switch compilerState {
- | CompilerManagerHook.Ready({result: Comp(Success({js_code}))}) =>
- setLogs(_ => [])
- let ast = AcornParse.parse(js_code)
- let transpiled = AcornParse.removeImportsAndExports(ast)
- EvalIFrame.sendOutput(transpiled)
- | _ => ()
- }
- }
- None
- }, (compilerState, runOutput))
-
-
+
+
{React.string("Console")}
{switch logs {
- | [] => React.null
+ | [] =>
+ React.string(
+ "Add some 'Console.log' to your code and enable 'Auto-run' to see your logs here.",
+ )
| logs =>
let content =
logs
@@ -56,8 +44,7 @@ let make = (~compilerState, ~runOutput) => {
})
->React.array
-
content
+
content
}}
-
}
diff --git a/src/OutputPanel.res b/src/OutputPanel.res
new file mode 100644
index 000000000..17d07ad90
--- /dev/null
+++ b/src/OutputPanel.res
@@ -0,0 +1,8 @@
+@react.component
+let make = (~runOutput, ~compilerState, ~logs, ~setLogs) => {
+
+ setLogs(_ => [])} />
+
+
+
+}
diff --git a/src/Playground.res b/src/Playground.res
index c42eb3be5..288a80197 100644
--- a/src/Playground.res
+++ b/src/Playground.res
@@ -22,7 +22,7 @@ open CompilerManagerHook
module Api = RescriptCompilerApi
type layout = Column | Row
-type tab = JavaScript | Problems | Settings | Console
+type tab = JavaScript | Output | Problems | Settings
let breakingPoint = 1024
module DropdownSelect = {
@@ -1204,67 +1204,6 @@ let locMsgToCmError = (~kind: CodeMirror.Error.kind, locMsg: Api.LocMsg.t): Code
}
}
-// module RenderOutput = {
-// @react.component
-// let make = (~compilerState: CompilerManagerHook.state) => {
-// React.useEffect(() => {
-// let code = switch compilerState {
-// | Ready(ready) =>
-// switch ready.result {
-// | Comp(Success(_)) => ControlPanel.codeFromResult(ready.result)->Some
-// | _ => None
-// }
-// | _ => None
-// }
-
-// let _valid = switch code {
-// | Some(code) =>
-// switch RenderOutputManager.renderOutput(code) {
-// | Ok(_) => true
-// | Error(_) => false
-// }
-// | None => false
-// }
-// None
-// }, [compilerState])
-
-//
-//
-//
-
-// // switch code {
-// // | Some(code) =>
-// // switch RenderOutputManager.renderOutput(code) {
-// // | Ok() =>
-// //
-// // | Error() =>
-// // let code = `module App = {
-// // @react.component
-// // let make = () => {
-// //
-// // }
-// // }`
-// //
-// //
{React.string("To render element create a module App")}
-// //
{HighlightJs.renderHLJS(~code, ~darkmode=true, ~lang="rescript", ())}
-// //
-// // }
-
-// // | _ => React.null
-// // }
-// }
-// }
-
module OutputPanel = {
@react.component
let make = (
@@ -1381,8 +1320,10 @@ module OutputPanel = {
prevSelected.current = selected
+ let (logs, setLogs) = React.useState(_ => [])
+
let tabs = [
- (Console,
),
+ (Output,
),
(JavaScript, output),
(Problems, errorPane),
(Settings, settingsPane),
@@ -1763,12 +1704,11 @@ let make = (~versions: array
) => {
"flex-1 items-center p-4 border-t-4 border-transparent " ++ activeClass
}
- let tabs = [JavaScript, Console, Problems, Settings]
+ let tabs = [JavaScript, Output, Problems, Settings]
let headers = Array.mapWithIndex(tabs, (tab, i) => {
let title = switch tab {
- // | RenderOutput => "Render Output"
- | Console => "Console"
+ | Output => "Output"
| JavaScript => "JavaScript"
| Problems => "Problems"
| Settings => "Settings"
diff --git a/src/RenderPanel.res b/src/RenderPanel.res
new file mode 100644
index 000000000..c8cea5cf0
--- /dev/null
+++ b/src/RenderPanel.res
@@ -0,0 +1,39 @@
+let wrapReactApp = code =>
+ `(function () {
+ ${code}
+ window.reactRoot.render(React.createElement(App.make, {}));
+})();`
+
+@react.component
+let make = (~compilerState: CompilerManagerHook.state, ~clearLogs, ~runOutput) => {
+ let (validReact, setValidReact) = React.useState(() => false)
+ React.useEffect(() => {
+ if runOutput {
+ switch compilerState {
+ | CompilerManagerHook.Ready({result: Comp(Success({js_code}))}) =>
+ clearLogs()
+ let ast = AcornParse.parse(js_code)
+ let transpiled = AcornParse.removeImportsAndExports(ast)
+ let isValidReact = AcornParse.hasEntryPoint(ast)
+ isValidReact
+ ? transpiled->wrapReactApp->EvalIFrame.sendOutput
+ : EvalIFrame.sendOutput(transpiled)
+ setValidReact(_ => isValidReact)
+ | _ => ()
+ }
+ }
+ None
+ }, (compilerState, runOutput))
+
+
+
{React.string("React")}
+ {validReact
+ ? React.null
+ : React.string(
+ "Create a React component called 'App' if you want to render it here, then enable 'Auto-run'.",
+ )}
+
+
+
+
+}
diff --git a/src/common/EvalIFrame.res b/src/common/EvalIFrame.res
index 5ba690705..3228b9c00 100644
--- a/src/common/EvalIFrame.res
+++ b/src/common/EvalIFrame.res
@@ -4,6 +4,8 @@ let css = `body {
color-scheme: light dark;
}`
+let reactVersion = "18.2.0"
+
let srcDoc = `
@@ -13,22 +15,49 @@ let srcDoc = `
+
+