1
1
import useThemeContext from "@theme/hooks/useThemeContext" ;
2
2
import clsx from "clsx" ;
3
- import React , { useCallback , useContext , useEffect , useRef , useState } from "react" ;
3
+ import React , { useCallback , useContext , useEffect , useRef , useState , useMemo } from "react" ;
4
4
import JSONTree from "react-json-tree" ;
5
5
import MonacoEditor from "react-monaco-editor" ;
6
6
import { version as tstlVersion } from "typescript-to-lua/package.json" ;
@@ -27,29 +27,33 @@ async function executeLua(code: string) {
27
27
} ) ;
28
28
}
29
29
30
- const EditorContext = React . createContext < EditorContext > ( null ! ) ;
31
- interface EditorContext {
32
- updateModel ( model : monaco . editor . ITextModel ) : void ;
33
- code : string ;
30
+ interface EditorState {
31
+ source : string ;
32
+ lua : string ;
33
+ sourceMap : string ;
34
34
ast : object ;
35
35
results : LuaMessage [ ] ;
36
36
}
37
37
38
+ const EditorContext = React . createContext < EditorContext > ( null ! ) ;
39
+ interface EditorContext extends EditorState {
40
+ updateModel ( model : monaco . editor . ITextModel ) : void ;
41
+ }
42
+
38
43
function EditorContextProvider ( { children } : { children : React . ReactNode } ) {
39
- const [ code , setCode ] = useState ( "" ) ;
40
- const [ ast , setAst ] = useState < object > ( { } ) ;
41
- const [ results , setResults ] = useState < LuaMessage [ ] > ( [ ] ) ;
44
+ const [ state , setState ] = useState < EditorState > ( { source : "" , lua : "" , ast : { } , sourceMap : "" , results : [ ] } ) ;
42
45
const updateModel = useCallback < EditorContext [ "updateModel" ] > ( async model => {
43
46
const getWorker = await monaco . languages . typescript . getTypeScriptWorker ( ) ;
44
47
const client = ( await getWorker ( model . uri ) ) as CustomTypeScriptWorker ;
48
+ const { lua, ast, sourceMap } = await client . getTranspileOutput ( model . uri . toString ( ) ) ;
49
+
50
+ const source = model . getValue ( ) ;
51
+ const results = await executeLua ( lua ) ;
45
52
46
- const { code, ast } = await client . getTranspileOutput ( model . uri . toString ( ) ) ;
47
- setCode ( code ) ;
48
- setAst ( ast ) ;
49
- setResults ( await executeLua ( code ) ) ;
53
+ setState ( { source, lua, ast, sourceMap, results } ) ;
50
54
} , [ ] ) ;
51
55
52
- return < EditorContext . Provider value = { { code , ast , results , updateModel } } > { children } </ EditorContext . Provider > ;
56
+ return < EditorContext . Provider value = { { updateModel , ... state } } > { children } </ EditorContext . Provider > ;
53
57
}
54
58
55
59
const commonMonacoOptions : monaco . editor . IEditorConstructionOptions = {
@@ -67,7 +71,6 @@ function InputPane() {
67
71
updateModel ( ref . current ! . editor ! . getModel ( ) ! ) ;
68
72
} , [ ] ) ;
69
73
70
- // TODO: Debounce
71
74
const onChange = useCallback (
72
75
debounce ( ( newValue : string ) => {
73
76
updateCodeHistory ( newValue ) ;
@@ -133,9 +136,16 @@ function LuaAST({ ast }: { ast: object }) {
133
136
134
137
function OutputPane ( ) {
135
138
const theme = useMonacoTheme ( ) ;
136
- const { code , ast, results } = useContext ( EditorContext ) ;
139
+ const { source , lua , sourceMap , ast, results } = useContext ( EditorContext ) ;
137
140
const [ isAstView , setAstView ] = useState ( false ) ;
138
141
const toggleAstView = useCallback ( ( ) => setAstView ( x => ! x ) , [ ] ) ;
142
+ const sourceMapUrl = useMemo ( ( ) => {
143
+ const inputs = [ lua , sourceMap , source ]
144
+ // Replace non-ASCII characters, because btoa not supports them
145
+ . map ( s => btoa ( s . replace ( / [ ^ \x00 - \x7F ] / g, "?" ) ) )
146
+ . join ( "," ) ;
147
+ return `https://sokra.github.io/source-map-visualization#base64,${ inputs } ` ;
148
+ } , [ lua , sourceMap , source ] ) ;
139
149
140
150
return (
141
151
< div className = { styles . contentPane } >
@@ -144,7 +154,7 @@ function OutputPane() {
144
154
< MonacoEditor
145
155
theme = { theme }
146
156
language = "lua"
147
- value = { code }
157
+ value = { lua }
148
158
options = { {
149
159
...commonMonacoOptions ,
150
160
scrollBeyondLastLine : false ,
@@ -157,16 +167,17 @@ function OutputPane() {
157
167
< LuaAST ast = { ast } />
158
168
</ div >
159
169
160
- < button
161
- className = { clsx (
162
- "button button--outline button--primary" ,
163
- styles . outputToggleButton ,
164
- ! isAstView && "button--active" ,
165
- ) }
166
- onClick = { toggleAstView }
167
- >
168
- { isAstView ? "Lua AST" : "TEXT" }
169
- </ button >
170
+ < div className = { styles . outputControls } >
171
+ < button
172
+ className = { clsx ( "button button--outline button--primary" , ! isAstView && "button--active" ) }
173
+ onClick = { toggleAstView }
174
+ >
175
+ { isAstView ? "Lua AST" : "TEXT" }
176
+ </ button >
177
+ < a className = "button button--success" href = { sourceMapUrl } target = "_blank" >
178
+ Source Map
179
+ </ a >
180
+ </ div >
170
181
</ div >
171
182
172
183
< div className = { styles . editorOutput } >
0 commit comments