@@ -8,6 +8,7 @@ import { oneDarkTheme } from "@codemirror/theme-one-dark";
8
8
9
9
import { pyodideLoaded , loadedEnvironments , componentDetailsNavOpen , currentComponentDetails , mode } from '../stores' ;
10
10
import { addClasses } from '../utils' ;
11
+ import { BaseEvalElement } from './base' ;
11
12
12
13
// Premise used to connect to the first available pyodide interpreter
13
14
let pyodideReadyPromise ;
@@ -43,27 +44,15 @@ function createCmdHandler(el){
43
44
}
44
45
45
46
46
- export class PyRepl extends HTMLElement {
47
- shadow : ShadowRoot ;
48
- wrapper : HTMLElement ;
47
+ export class PyRepl extends BaseEvalElement {
49
48
editor : EditorView ;
50
49
editorNode : HTMLElement ;
51
50
code : string ;
52
- cm : any ;
53
- btnConfig : HTMLElement ;
54
- btnRun : HTMLElement ;
55
- editorOut : HTMLElement ; //HTMLTextAreaElement;
56
51
theme : string ;
57
- // editorState: EditorState;
58
52
59
53
constructor ( ) {
60
54
super ( ) ;
61
55
62
- // attach shadow so we can preserve the element original innerHtml content
63
- this . shadow = this . attachShadow ( { mode : 'open' } ) ;
64
-
65
- this . wrapper = document . createElement ( 'slot' ) ;
66
-
67
56
// add an extra div where we can attach the codemirror editor
68
57
this . editorNode = document . createElement ( 'div' ) ;
69
58
addClasses ( this . editorNode , [ "editor-box" ] )
@@ -172,21 +161,21 @@ export class PyRepl extends HTMLElement {
172
161
}
173
162
174
163
if ( this . hasAttribute ( 'target' ) ) {
175
- this . editorOut = document . getElementById ( this . getAttribute ( 'target' ) ) ;
164
+ this . outputElement = document . getElementById ( this . getAttribute ( 'target' ) ) ;
176
165
177
166
// in this case, the default output-mode is append, if hasn't been specified
178
167
if ( ! this . hasAttribute ( 'output-mode' ) ) {
179
168
this . setAttribute ( 'output-mode' , 'append' ) ;
180
169
}
181
170
} else {
182
171
// Editor Output Div
183
- this . editorOut = document . createElement ( 'div' ) ;
184
- this . editorOut . classList . add ( "output" ) ;
185
- this . editorOut . hidden = true ;
186
- this . editorOut . id = this . id + "-" + this . getAttribute ( "exec-id" ) ;
172
+ this . outputElement = document . createElement ( 'div' ) ;
173
+ this . outputElement . classList . add ( "output" ) ;
174
+ this . outputElement . hidden = true ;
175
+ this . outputElement . id = this . id + "-" + this . getAttribute ( "exec-id" ) ;
187
176
188
177
// add the output div id there's not target
189
- mainDiv . appendChild ( this . editorOut ) ;
178
+ mainDiv . appendChild ( this . outputElement ) ;
190
179
}
191
180
192
181
this . appendChild ( mainDiv ) ;
@@ -195,63 +184,35 @@ export class PyRepl extends HTMLElement {
195
184
}
196
185
197
186
addToOutput ( s : string ) {
198
- this . editorOut . innerHTML += "<div>" + s + "</div>" ;
199
- this . editorOut . hidden = false ;
187
+ this . outputElement . innerHTML += "<div>" + s + "</div>" ;
188
+ this . outputElement . hidden = false ;
200
189
}
201
190
202
- async evaluate ( ) {
203
- console . log ( 'evaluate' ) ;
204
- let pyodide = await pyodideReadyPromise ;
191
+ postEvaluate ( ) : void {
192
+ if ( this . hasAttribute ( 'auto-generate' ) ) {
193
+ let nextExecId = parseInt ( this . getAttribute ( 'exec-id' ) ) + 1 ;
194
+ const newPyRepl = document . createElement ( "py-repl" ) ;
195
+ newPyRepl . setAttribute ( 'root' , this . getAttribute ( 'root' ) ) ;
196
+ newPyRepl . id = this . getAttribute ( 'root' ) + "-" + nextExecId . toString ( ) ;
197
+ newPyRepl . setAttribute ( 'auto-generate' , null ) ;
198
+ if ( this . hasAttribute ( 'target' ) ) {
199
+ newPyRepl . setAttribute ( 'target' , this . getAttribute ( 'target' ) ) ;
200
+ }
201
+
202
+ newPyRepl . setAttribute ( 'exec-id' , nextExecId . toString ( ) ) ;
203
+ this . parentElement . appendChild ( newPyRepl ) ;
204
+ }
205
+ }
205
206
206
- try {
207
- // @ts -ignore
208
- const sourceStrings = [ `output_manager.change("` + this . editorOut . id + `")` ,
207
+ getSource ( ) : string {
208
+ const sourceStrings = [ `output_manager.change("` + this . outputElement . id + `")` ,
209
209
...this . editor . state . doc . toString ( ) . split ( "\n" ) ] ;
210
- const source = sourceStrings . join ( '\n' )
211
- let output ;
212
-
213
- if ( source . includes ( "asyncio" ) ) {
214
- output = await pyodide . runPythonAsync ( source ) ;
215
- await pyodide . runPythonAsync ( `output_manager.revert()` )
216
- } else {
217
- output = pyodide . runPython ( source ) ;
218
- pyodide . runPython ( `output_manager.revert()` )
219
- }
220
-
221
- if ( output !== undefined ) {
222
- let Element = pyodide . globals . get ( 'Element' ) ;
223
- let out = Element ( this . editorOut . id ) ;
224
-
225
- // @ts -ignore
226
- out . write . callKwargs ( output , { append : true } ) ;
227
-
228
- if ( ! this . hasAttribute ( 'target' ) ) {
229
- this . editorOut . hidden = false ;
230
- }
231
- }
210
+ return sourceStrings . join ( '\n' )
211
+ }
232
212
233
- if ( this . hasAttribute ( 'auto-generate' ) ) {
234
- let nextExecId = parseInt ( this . getAttribute ( 'exec-id' ) ) + 1 ;
235
- const newPyRepl = document . createElement ( "py-repl" ) ;
236
- newPyRepl . setAttribute ( 'root' , this . getAttribute ( 'root' ) ) ;
237
- newPyRepl . id = this . getAttribute ( 'root' ) + "-" + nextExecId . toString ( ) ;
238
- newPyRepl . setAttribute ( 'auto-generate' , null ) ;
239
- if ( this . hasAttribute ( 'target' ) ) {
240
- newPyRepl . setAttribute ( 'target' , this . getAttribute ( 'target' ) ) ;
241
- }
242
-
243
- newPyRepl . setAttribute ( 'exec-id' , nextExecId . toString ( ) ) ;
244
- this . parentElement . appendChild ( newPyRepl ) ;
245
- }
246
- } catch ( err ) {
247
- this . addToOutput ( err ) ;
248
- }
249
- }
250
-
251
213
render ( ) {
252
214
console . log ( 'rendered' ) ;
253
215
254
216
}
255
217
}
256
218
257
-
0 commit comments