@@ -13,32 +13,31 @@ import * as tstl from "typescript-to-lua/dist/LuaAST";
1313const renderjson = require ( "renderjson" ) ;
1414const tstlPackageJson = require ( "typescript-to-lua/package.json" ) ;
1515
16- document . addEventListener ( "DOMContentLoaded" , ( ) => {
17- const container = document . getElementById ( "editor-ts" ) ;
18- const outputTerminalHeader = document . getElementById ( "editor-output-terminal-header" ) ;
19- const outputTerminalContent = document . getElementById ( "editor-output-terminal-content" ) ;
20- const exampleLua = document . getElementById ( "editor-lua" ) ;
21- const astLua = document . getElementById ( "editor-lua-ast" ) ;
22-
23- // Set tstl version
24- outputTerminalHeader ! . textContent = `TypescriptToLua version ${ tstlPackageJson . version } ` ;
25-
26- // Layout stuff
27- const luaTabText = document . getElementById ( "lua-tab-text" ) as HTMLDivElement | null ;
28- const luaTabAst = document . getElementById ( "lua-tab-ast" ) as HTMLDivElement | null ;
29- if ( luaTabText && luaTabAst && exampleLua && astLua ) {
30- const tabOnclick = ( ) => {
31- luaTabText . classList . toggle ( "lua-tab-active" ) ;
32- luaTabAst . classList . toggle ( "lua-tab-active" ) ;
33- exampleLua . classList . toggle ( "editor-lua-active" ) ;
34- astLua . classList . toggle ( "editor-lua-active" ) ;
35- } ;
36- luaTabText . onclick = tabOnclick ;
37- luaTabAst . onclick = tabOnclick ;
38- }
16+ const container = document . getElementById ( "editor-ts" ) ;
17+ const outputTerminalHeader = document . getElementById ( "editor-output-terminal-header" ) ;
18+ const outputTerminalContent = document . getElementById ( "editor-output-terminal-content" ) ;
19+ const exampleLua = document . getElementById ( "editor-lua" ) ;
20+ const astLua = document . getElementById ( "editor-lua-ast" ) ;
21+
22+ // Set tstl version
23+ outputTerminalHeader ! . textContent = `TypescriptToLua version ${ tstlPackageJson . version } ` ;
24+
25+ // Layout stuff
26+ const luaTabText = document . getElementById ( "lua-tab-text" ) as HTMLDivElement | null ;
27+ const luaTabAst = document . getElementById ( "lua-tab-ast" ) as HTMLDivElement | null ;
28+ if ( luaTabText && luaTabAst && exampleLua && astLua ) {
29+ const tabOnclick = ( ) => {
30+ luaTabText . classList . toggle ( "lua-tab-active" ) ;
31+ luaTabAst . classList . toggle ( "lua-tab-active" ) ;
32+ exampleLua . classList . toggle ( "editor-lua-active" ) ;
33+ astLua . classList . toggle ( "editor-lua-active" ) ;
34+ } ;
35+ luaTabText . onclick = tabOnclick ;
36+ luaTabAst . onclick = tabOnclick ;
37+ }
3938
40- // Actual editor and transpilation
41- let example = `/** @noSelfInFile */
39+ // Actual editor and transpilation
40+ let example = `/** @noSelfInFile */
4241
4342// Declare exposed API
4443type Vector = [number, number, number];
@@ -68,81 +67,80 @@ function onSpellStart(event: OnSpellStartEvent): void {
6867 }
6968}` ;
7069
71- var queryStringSrcStart = window . location . hash . indexOf ( "#src=" ) ;
72- if ( queryStringSrcStart == 0 ) {
73- var encoded = window . location . hash . substring ( "#src=" . length ) ;
74- example = decodeURIComponent ( encoded ) ;
75- }
70+ var queryStringSrcStart = window . location . hash . indexOf ( "#src=" ) ;
71+ if ( queryStringSrcStart == 0 ) {
72+ var encoded = window . location . hash . substring ( "#src=" . length ) ;
73+ example = decodeURIComponent ( encoded ) ;
74+ }
7675
77- if ( container && exampleLua && astLua ) {
78- let tsEditor = editor . create ( container , {
79- value : example ,
80- language : "typescript" ,
81- minimap : { enabled : false } ,
82- theme : "vs-dark" ,
83- } ) ;
84-
85- let luaEditor = editor . create ( exampleLua , {
86- value : "" ,
87- language : "lua" ,
88- minimap : { enabled : false } ,
89- theme : "vs-dark" ,
90- readOnly : true ,
91- } ) ;
92-
93- window . onresize = ( ) => {
94- tsEditor . layout ( ) ;
95- luaEditor . layout ( ) ;
96- } ;
97-
98- const tstlWorker = new ( TSTLWorker as any ) ( ) ;
99- tstlWorker . postMessage ( { tsStr : tsEditor . getValue ( ) } ) ;
100-
101- let timerVar : any ;
102- let ignoreHashChange = false ;
103-
104- tsEditor . onDidChangeModelContent ( e => {
105- clearInterval ( timerVar ) ;
106- // wait one second before submitting work
107- timerVar = setTimeout ( ( ) => {
108- tstlWorker . postMessage ( { tsStr : tsEditor . getValue ( ) } ) ;
109- window . location . replace ( "#src=" + encodeURIComponent ( tsEditor . getValue ( ) ) ) ;
110- ignoreHashChange = true ;
111- } , 500 ) ;
112- } ) ;
113-
114- window . onhashchange = ( ) => {
115- if ( ignoreHashChange ) {
116- ignoreHashChange = false ;
117- return ;
118- }
119- } ;
120-
121- const fengariWorker = new ( FengariWorker as any ) ( ) ;
122-
123- tstlWorker . onmessage = ( event : MessageEvent ) => {
124- if ( event . data . luaStr ) {
125- luaEditor . setValue ( event . data . luaStr ) ;
126-
127- astLua . innerText = "" ;
128- astLua . appendChild (
129- renderjson . set_show_to_level ( 1 ) . set_replacer ( ( name : string , val : any ) => {
130- if ( name === "kind" ) {
131- return tstl . SyntaxKind [ val ] ;
132- }
133- return val ;
134- } ) ( event . data . luaAST ) ,
135- ) ;
136- fengariWorker . postMessage ( { luaStr : event . data . luaStr } ) ;
137- } else {
138- luaEditor . setValue ( event . data . diagnostics ) ;
139- }
140- } ;
141-
142- fengariWorker . onmessage = ( event : MessageEvent ) => {
143- if ( outputTerminalContent ) {
144- outputTerminalContent . innerText = event . data . luaPrint ;
145- }
146- } ;
147- }
148- } ) ;
76+ if ( container && exampleLua && astLua ) {
77+ let tsEditor = editor . create ( container , {
78+ value : example ,
79+ language : "typescript" ,
80+ minimap : { enabled : false } ,
81+ theme : "vs-dark" ,
82+ } ) ;
83+
84+ let luaEditor = editor . create ( exampleLua , {
85+ value : "" ,
86+ language : "lua" ,
87+ minimap : { enabled : false } ,
88+ theme : "vs-dark" ,
89+ readOnly : true ,
90+ } ) ;
91+
92+ window . onresize = ( ) => {
93+ tsEditor . layout ( ) ;
94+ luaEditor . layout ( ) ;
95+ } ;
96+
97+ const tstlWorker = new ( TSTLWorker as any ) ( ) ;
98+ tstlWorker . postMessage ( { tsStr : tsEditor . getValue ( ) } ) ;
99+
100+ let timerVar : any ;
101+ let ignoreHashChange = false ;
102+
103+ tsEditor . onDidChangeModelContent ( e => {
104+ clearInterval ( timerVar ) ;
105+ // wait one second before submitting work
106+ timerVar = setTimeout ( ( ) => {
107+ tstlWorker . postMessage ( { tsStr : tsEditor . getValue ( ) } ) ;
108+ window . location . replace ( "#src=" + encodeURIComponent ( tsEditor . getValue ( ) ) ) ;
109+ ignoreHashChange = true ;
110+ } , 500 ) ;
111+ } ) ;
112+
113+ window . onhashchange = ( ) => {
114+ if ( ignoreHashChange ) {
115+ ignoreHashChange = false ;
116+ return ;
117+ }
118+ } ;
119+
120+ const fengariWorker = new ( FengariWorker as any ) ( ) ;
121+
122+ tstlWorker . onmessage = ( event : MessageEvent ) => {
123+ if ( event . data . luaStr ) {
124+ luaEditor . setValue ( event . data . luaStr ) ;
125+
126+ astLua . innerText = "" ;
127+ astLua . appendChild (
128+ renderjson . set_show_to_level ( 1 ) . set_replacer ( ( name : string , val : any ) => {
129+ if ( name === "kind" ) {
130+ return tstl . SyntaxKind [ val ] ;
131+ }
132+ return val ;
133+ } ) ( event . data . luaAST ) ,
134+ ) ;
135+ fengariWorker . postMessage ( { luaStr : event . data . luaStr } ) ;
136+ } else {
137+ luaEditor . setValue ( event . data . diagnostics ) ;
138+ }
139+ } ;
140+
141+ fengariWorker . onmessage = ( event : MessageEvent ) => {
142+ if ( outputTerminalContent ) {
143+ outputTerminalContent . innerText = event . data . luaPrint ;
144+ }
145+ } ;
146+ }
0 commit comments