1+ import style from './style.scss' ;
12import tag from 'html-tag-js' ;
23
34class Python {
@@ -8,35 +9,31 @@ class Python {
89 #onRunError;
910 #cacheFile;
1011 #cacheFileUrl;
12+ #workerInitialized = false ;
1113 name = 'Python' ;
1214 baseUrl = '' ;
1315 pyodide = null ;
1416 $input = null ;
1517 $page = null ;
1618 $runBtn = null ;
19+ $style = null ;
1720
1821 async init ( $page , cacheFile , cacheFileUrl ) {
1922
2023 this . #cacheFileUrl = cacheFileUrl ;
2124
2225 $page . id = 'acode-plugin-python' ;
23- this . $refresh = tag ( 'span' , {
24- className : 'icon refresh' ,
25- attr : {
26- action : 'refresh' ,
27- } ,
28- onclick : async ( ) => {
29- await this . initWorker ( ) ;
30- this . run ( ) ;
31- } ,
32- } ) ;
3326 this . $page = $page ;
3427 this . $page . settitle ( 'Python' ) ;
35- this . $page . get ( 'header' ) . append ( this . $refresh ) ;
28+
3629 this . #cacheFile = cacheFile ;
30+
3731 const onhide = $page . onhide ;
3832 $page . onhide = ( ) => {
39- this . #cacheFile. writeFile ( '\0' ) ;
33+ if ( this . #workerInitialized) {
34+ this . #worker. terminate ( ) ;
35+ this . #workerInitialized = false ;
36+ }
4037 onhide ( ) ;
4138 }
4239
@@ -59,27 +56,25 @@ class Python {
5956 onclick : this . run . bind ( this ) ,
6057 } ) ;
6158
62- this . $input = tag ( 'textarea' , {
63- onkeydown : this . #onkeydown. bind ( this ) ,
64- style : {
65- backgroundColor : 'transparent' ,
66- color : 'inherit' ,
67- width : '100%' ,
68- border : 'none' ,
69- } ,
59+ this . $style = tag ( 'style' , {
60+ textContent : style ,
61+ } ) ;
62+
63+ this . $input = tag ( 'div' , {
64+ className : 'print input' ,
65+ child : tag ( 'textarea' , {
66+ onkeydown : this . #onkeydown. bind ( this ) ,
67+ } ) ,
7068 } ) ;
69+
7170 this . checkRunnable ( ) ;
7271 editorManager . on ( 'switch-file' , this . checkRunnable . bind ( this ) ) ;
7372 editorManager . on ( 'rename-file' , this . checkRunnable . bind ( this ) ) ;
74-
75- await this . initWorker ( ) ;
73+ document . head . append ( this . $style ) ;
7674 }
7775
7876 async initWorker ( ) {
7977 if ( this . #worker) this . #worker. terminate ( ) ;
80- if ( window . toast ) {
81- window . toast ( 'Python is loading...' ) ;
82- }
8378 this . #worker = new Worker ( this . baseUrl + 'worker.js' ) ;
8479 this . #worker. postMessage ( {
8580 action : 'init' ,
@@ -91,46 +86,56 @@ class Python {
9186 this . #onInitSuccess = resolve ;
9287 this . #onInitError = error ;
9388 } ) ;
94- if ( window . toast ) {
95- window . toast ( 'Python is loaded.' ) ;
96- }
89+ this . #workerInitialized = true ;
9790 }
9891
9992 async run ( ) {
100- await this . #cacheFile. writeFile ( '' ) ;
101- this . $page . get ( '.main' ) . innerHTML = '' ;
102- this . #append( this . $input ) ;
103-
93+ const $main = this . $page . get ( '.main' ) ;
10494 if ( ! this . $page . isConnected ) {
10595 this . $page . classList . remove ( 'hide' ) ;
10696 this . $page . show ( ) ;
10797 }
108- setTimeout ( async ( ) => {
109- const code = editorManager . editor . getValue ( ) ;
110- this . #worker. postMessage ( {
111- action : 'run' ,
112- code,
98+
99+ $main . innerHTML = '' ;
100+
101+ await this . #cacheFile. writeFile ( '' ) ;
102+ this . #append( this . $input ) ;
103+
104+ this . print ( 'Python initializing' ) ;
105+ try {
106+ await this . initWorker ( ) ;
107+ } catch ( error ) {
108+ this . print ( error , 'error' ) ;
109+ return ;
110+ }
111+
112+ const code = editorManager . editor . getValue ( ) ;
113+ this . #worker. postMessage ( {
114+ action : 'run' ,
115+ code,
116+ } ) ;
117+ try {
118+ const res = await new Promise ( ( resolve , error ) => {
119+ this . #onRunSuccess = resolve ;
120+ this . #onRunError = error ;
113121 } ) ;
114- try {
115- const res = await new Promise ( ( resolve , error ) => {
116- this . #onRunSuccess = resolve ;
117- this . #onRunError = error ;
118- } ) ;
119- this . print ( res , 'output' ) ;
120- } catch ( error ) {
121- this . print ( error , 'error' ) ;
122- }
123- } , 600 ) ;
122+ this . print ( res , 'output' ) ;
123+ } catch ( error ) {
124+ this . print ( error , 'error' ) ;
125+ }
124126 }
125127
126128 destroy ( ) {
127129 if ( this . $runBtn ) {
128130 this . $runBtn . onclick = null ;
129131 this . $runBtn . remove ( ) ;
130132 }
131- if ( this . #worker) this . #worker. terminate ( ) ;
133+
134+ if ( this . #workerInitialized) this . #worker. terminate ( ) ;
135+
132136 editorManager . off ( 'switch-file' , this . checkRunnable . bind ( this ) ) ;
133137 editorManager . off ( 'rename-file' , this . checkRunnable . bind ( this ) ) ;
138+ this . $style . remove ( ) ;
134139 }
135140
136141 checkRunnable ( ) {
@@ -150,14 +155,9 @@ class Python {
150155 print ( res , type ) {
151156 if ( ! this . $page . isConnected ) return ;
152157 const $output = tag ( 'div' , {
153- className : 'python-output' ,
154- } ) ;
155- $output . appendChild ( tag ( 'pre' , {
158+ className : `print ${ type || '' } ` ,
156159 textContent : res ,
157- style : {
158- color : type === 'error' ? 'orangered' : 'inherit' ,
159- }
160- } ) ) ;
160+ } ) ;
161161 this . #append( $output , this . $input ) ;
162162 }
163163
@@ -191,7 +191,7 @@ class Python {
191191 if ( action === 'input' ) {
192192 if ( text ) this . print ( text ) ;
193193 await this . #cacheFile. writeFile ( '' ) ;
194- this . $input . focus ( ) ;
194+ this . $input . get ( 'textarea' ) . focus ( ) ;
195195 }
196196 if ( action === 'stdout' ) {
197197 this . print ( text ) ;
@@ -204,8 +204,8 @@ class Python {
204204 #onkeydown( e ) {
205205 if ( e . key === 'Enter' ) {
206206 e . preventDefault ( ) ;
207- const value = this . $input . value ;
208- this . print ( value ) ;
207+ const value = e . target . value + '\0' ;
208+ this . print ( value , 'input' ) ;
209209 this . #cacheFile. writeFile ( value ) ;
210210 e . target . value = '' ;
211211 }
0 commit comments