11import useThemeContext from "@theme/hooks/useThemeContext" ;
22import clsx from "clsx" ;
3+ import { Console as ConsoleFeed } from "console-feed" ;
34import React , { useCallback , useContext , useEffect , useMemo , useRef , useState } from "react" ;
45import JSONTree from "react-json-tree" ;
56import MonacoEditor from "react-monaco-editor" ;
67import { version as tstlVersion } from "typescript-to-lua/package.json" ;
78import { version as tsVersion } from "typescript/package.json" ;
8- import FengariWorker from "worker-loader?name=fengari.worker.js!./fengari.worker" ;
99import { debounce } from "../../utils" ;
1010import { getInitialCode , updateCodeHistory } from "./code" ;
11- import type { LuaMessage } from "./fengari.worker " ;
11+ import { ConsoleMessage , executeLua } from "./execute " ;
1212import { monaco , useMonacoTheme } from "./monaco" ;
1313import styles from "./styles.module.scss" ;
14+ import { consoleFeedTheme , jsonTreeTheme } from "./themes" ;
1415import type { CustomTypeScriptWorker } from "./ts.worker" ;
1516
16- let fengariWorker = new FengariWorker ( ) ;
17- async function executeLua ( code : string ) {
18- return new Promise < LuaMessage [ ] > ( ( resolve ) => {
19- const timeout = setTimeout ( ( ) => {
20- resolve ( [ { type : "print" , text : "Lua code execution timed out" } ] ) ;
21- fengariWorker . terminate ( ) ;
22- fengariWorker = new FengariWorker ( ) ;
23- } , 2500 ) ;
24-
25- fengariWorker . postMessage ( { code } ) ;
26- fengariWorker . addEventListener ( "message" , ( event ) => {
27- clearTimeout ( timeout ) ;
28- resolve ( event . data . messages ) ;
29- } ) ;
30- } ) ;
31- }
32-
3317interface EditorState {
3418 source : string ;
3519 lua : string ;
3620 sourceMap : string ;
3721 ast : object ;
38- results : LuaMessage [ ] ;
22+ results : ConsoleMessage [ ] ;
3923}
4024
4125const EditorContext = React . createContext < EditorContext > ( null ! ) ;
@@ -67,7 +51,7 @@ const commonMonacoOptions: monaco.editor.IEditorConstructionOptions = {
6751
6852function InputPane ( ) {
6953 const theme = useMonacoTheme ( ) ;
70- const ref = useRef < MonacoEditor > ( null ! ) ;
54+ const ref = useRef < MonacoEditor > ( null ) ;
7155 const { updateModel } = useContext ( EditorContext ) ;
7256
7357 useEffect ( ( ) => {
@@ -96,35 +80,15 @@ function InputPane() {
9680 ) ;
9781}
9882
99- const astTheme = {
100- scheme : "monokai" ,
101- author : "wimer hazenberg (http://www.monokai.nl)" ,
102- base00 : "#1e1e1e" ,
103- base01 : "#383830" ,
104- base02 : "#49483e" ,
105- base03 : "#75715e" ,
106- base04 : "#a59f85" ,
107- base05 : "#f8f8f2" ,
108- base06 : "#f5f4f1" ,
109- base07 : "#f9f8f5" ,
110- base08 : "#f92672" ,
111- base09 : "#fd971f" ,
112- base0A : "#f4bf75" ,
113- base0B : "#a6e22e" ,
114- base0C : "#a1efe4" ,
115- base0D : "#66d9ef" ,
116- base0E : "#ae81ff" ,
117- base0F : "#cc6633" ,
118- } ;
119-
12083const LuaSyntaxKind = __LUA_SYNTAX_KIND__ ;
12184function LuaAST ( { ast } : { ast : object } ) {
12285 const { isDarkTheme } = useThemeContext ( ) ;
86+
12387 return (
12488 < JSONTree
12589 data = { ast }
12690 hideRoot = { true }
127- theme = { astTheme }
91+ theme = { jsonTreeTheme }
12892 invertTheme = { ! isDarkTheme }
12993 valueRenderer = { ( raw , value , lastKey ) => {
13094 if ( lastKey === "kind" ) {
@@ -137,9 +101,28 @@ function LuaAST({ ast }: { ast: object }) {
137101 ) ;
138102}
139103
104+ function LuaOutput ( ) {
105+ const { isDarkTheme } = useThemeContext ( ) ;
106+ const { results } = useContext ( EditorContext ) ;
107+
108+ return (
109+ < div className = { styles . editorOutput } >
110+ < div className = { styles . editorOutputLineNumbers } > { ">_" } </ div >
111+ < div className = { styles . editorOutputTerminal } >
112+ < ConsoleFeed
113+ key = { isDarkTheme } // It does not update styles without re-mount
114+ logs = { results as any }
115+ variant = { isDarkTheme ? "dark" : "light" }
116+ styles = { consoleFeedTheme ( isDarkTheme ) }
117+ />
118+ </ div >
119+ </ div >
120+ ) ;
121+ }
122+
140123function OutputPane ( ) {
141124 const theme = useMonacoTheme ( ) ;
142- const { source, lua, sourceMap, ast, results } = useContext ( EditorContext ) ;
125+ const { source, lua, sourceMap, ast } = useContext ( EditorContext ) ;
143126 const [ isAstView , setAstView ] = useState ( false ) ;
144127 const toggleAstView = useCallback ( ( ) => setAstView ( ( x ) => ! x ) , [ ] ) ;
145128 const sourceMapUrl = useMemo ( ( ) => {
@@ -183,14 +166,7 @@ function OutputPane() {
183166 </ div >
184167 </ div >
185168
186- < div className = { styles . editorOutput } >
187- < div className = { styles . editorOutputLineNumbers } > > _ </ div >
188- < div className = { styles . editorOutputTerminal } >
189- { results . map ( ( message , idx ) => (
190- < div key = { idx } > { message . text } </ div >
191- ) ) }
192- </ div >
193- </ div >
169+ < LuaOutput />
194170 </ div >
195171 ) ;
196172}
@@ -204,7 +180,7 @@ export default function Playground() {
204180 < a
205181 href = "https://github.com/TypeScriptToLua/TypeScriptToLua/blob/master/CHANGELOG.md"
206182 target = "_blank"
207- rel = "noopener noreferrer "
183+ rel = "noopener"
208184 >
209185 < b > v{ tstlVersion } </ b >
210186 </ a >
0 commit comments