@@ -5,7 +5,9 @@ import { Node } from "../model/mod.ts";
5
5
export interface CodeExecutor {
6
6
// executes the source and returns an output string.
7
7
// exceptions in execution should be caught and returned as a string.
8
- execute ( source : string , options : ExecuteOptions ) : string ;
8
+ async execute ( source : string , options : ExecuteOptions ) : string ;
9
+
10
+ canExecute ( options : ExecuteOptions ) : boolean ;
9
11
}
10
12
11
13
export interface ExecuteOptions {
@@ -14,11 +16,18 @@ export interface ExecuteOptions {
14
16
15
17
// defaultExecutor can be replaced with an external service, etc
16
18
export let defaultExecutor : CodeExecutor = {
17
- execute : ( source : string , options : ExecuteOptions ) : string = > {
19
+ async execute ( source : string , options : ExecuteOptions ) : Promise < string > {
18
20
if ( options . language !== "javascript" ) {
19
21
return `Unsupported language: ${ options . language } ` ;
20
22
}
21
23
return JSON . stringify ( window . eval ( source ) ) ;
24
+ } ,
25
+
26
+ canExecute ( options : ExecuteOptions ) : boolean {
27
+ if ( options . language === "javascript" ) {
28
+ return true ;
29
+ }
30
+ return false ;
22
31
}
23
32
}
24
33
@@ -27,9 +36,11 @@ export let defaultExecutor: CodeExecutor = {
27
36
@component
28
37
export class CodeBlock {
29
38
code : string ;
39
+ language : string ;
30
40
31
41
constructor ( ) {
32
42
this . code = "" ;
43
+ this . language = "" ;
33
44
}
34
45
35
46
childrenView ( ) {
@@ -75,6 +86,7 @@ const CodeEditor = {
75
86
// let's do it by this hack.
76
87
editor . textContent = editor . textContent ;
77
88
window . hljs . highlightBlock ( editor ) ;
89
+ snippet . language = window . hljs . highlightAuto ( editor . textContent ) . language || "" ;
78
90
} ) ;
79
91
dom . jarEditor . updateCode ( snippet . code ) ;
80
92
dom . jarEditor . onUpdate ( code => {
0 commit comments