1
1
import useThemeContext from "@theme/hooks/useThemeContext" ;
2
2
import clsx from "clsx" ;
3
+ import { Console as ConsoleFeed } from "console-feed" ;
3
4
import React , { useCallback , useContext , useEffect , useMemo , useRef , useState } from "react" ;
4
5
import JSONTree from "react-json-tree" ;
5
6
import MonacoEditor from "react-monaco-editor" ;
6
7
import { version as tstlVersion } from "typescript-to-lua/package.json" ;
7
8
import { version as tsVersion } from "typescript/package.json" ;
8
- import FengariWorker from "worker-loader?name=fengari.worker.js!./fengari.worker" ;
9
9
import { debounce } from "../../utils" ;
10
10
import { getInitialCode , updateCodeHistory } from "./code" ;
11
- import type { LuaMessage } from "./fengari.worker " ;
11
+ import { ConsoleMessage , executeLua } from "./execute " ;
12
12
import { monaco , useMonacoTheme } from "./monaco" ;
13
13
import styles from "./styles.module.scss" ;
14
+ import { consoleFeedTheme , jsonTreeTheme } from "./themes" ;
14
15
import type { CustomTypeScriptWorker } from "./ts.worker" ;
15
16
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
-
33
17
interface EditorState {
34
18
source : string ;
35
19
lua : string ;
36
20
sourceMap : string ;
37
21
ast : object ;
38
- results : LuaMessage [ ] ;
22
+ results : ConsoleMessage [ ] ;
39
23
}
40
24
41
25
const EditorContext = React . createContext < EditorContext > ( null ! ) ;
@@ -67,7 +51,7 @@ const commonMonacoOptions: monaco.editor.IEditorConstructionOptions = {
67
51
68
52
function InputPane ( ) {
69
53
const theme = useMonacoTheme ( ) ;
70
- const ref = useRef < MonacoEditor > ( null ! ) ;
54
+ const ref = useRef < MonacoEditor > ( null ) ;
71
55
const { updateModel } = useContext ( EditorContext ) ;
72
56
73
57
useEffect ( ( ) => {
@@ -96,35 +80,15 @@ function InputPane() {
96
80
) ;
97
81
}
98
82
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
-
120
83
const LuaSyntaxKind = __LUA_SYNTAX_KIND__ ;
121
84
function LuaAST ( { ast } : { ast : object } ) {
122
85
const { isDarkTheme } = useThemeContext ( ) ;
86
+
123
87
return (
124
88
< JSONTree
125
89
data = { ast }
126
90
hideRoot = { true }
127
- theme = { astTheme }
91
+ theme = { jsonTreeTheme }
128
92
invertTheme = { ! isDarkTheme }
129
93
valueRenderer = { ( raw , value , lastKey ) => {
130
94
if ( lastKey === "kind" ) {
@@ -137,9 +101,28 @@ function LuaAST({ ast }: { ast: object }) {
137
101
) ;
138
102
}
139
103
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
+
140
123
function OutputPane ( ) {
141
124
const theme = useMonacoTheme ( ) ;
142
- const { source, lua, sourceMap, ast, results } = useContext ( EditorContext ) ;
125
+ const { source, lua, sourceMap, ast } = useContext ( EditorContext ) ;
143
126
const [ isAstView , setAstView ] = useState ( false ) ;
144
127
const toggleAstView = useCallback ( ( ) => setAstView ( ( x ) => ! x ) , [ ] ) ;
145
128
const sourceMapUrl = useMemo ( ( ) => {
@@ -183,14 +166,7 @@ function OutputPane() {
183
166
</ div >
184
167
</ div >
185
168
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 />
194
170
</ div >
195
171
) ;
196
172
}
@@ -204,7 +180,7 @@ export default function Playground() {
204
180
< a
205
181
href = "https://github.com/TypeScriptToLua/TypeScriptToLua/blob/master/CHANGELOG.md"
206
182
target = "_blank"
207
- rel = "noopener noreferrer "
183
+ rel = "noopener"
208
184
>
209
185
< b > v{ tstlVersion } </ b >
210
186
</ a >
0 commit comments