@@ -5,14 +5,16 @@ import {
5
5
watch ,
6
6
onBeforeUnmount ,
7
7
defineProps ,
8
- defineEmits
8
+ defineEmits ,
9
+ onMounted
9
10
} from " vue" ;
10
11
import { useLocalStorage } from " @vueuse/core" ;
11
12
import " highlight.js/styles/github.css" ;
12
13
import hljs from " highlight.js/lib/core" ;
13
14
import javascript from " highlight.js/lib/languages/javascript" ;
14
15
import typescript from " highlight.js/lib/languages/typescript" ;
15
16
import hljsVuePlugin from " @highlightjs/vue-plugin" ;
17
+ import { initialize , transform } from " esbuild-wasm" ;
16
18
17
19
import { getImportsFromOption } from " ./utils/codegen" ;
18
20
@@ -56,37 +58,76 @@ watch(
56
58
}
57
59
58
60
setTimeout (() => {
61
+ if (initializing .value ) {
62
+ return ;
63
+ }
59
64
source .value ? .focus ();
60
65
});
61
66
}
62
67
);
63
68
69
+ const initializing = ref (true );
64
70
const optionCode = ref (" " );
65
- const importCode = computed (() => {
66
- if (optionCode .value .trim () === " " ) {
67
- return " /* Paste your option code first */" ;
68
- }
71
+ const transformedCode = ref (" " );
72
+ const transformErrors = ref ([]);
73
+
74
+ onMounted (async () => {
75
+ await initialize ({
76
+ wasmURL: " https://cdn.jsdelivr.net/npm/[email protected] /esbuild.wasm"
77
+ });
78
+
79
+ initializing .value = false ;
80
+ source .value ? .focus ();
81
+ });
69
82
70
- let option = null ;
83
+ watch (optionCode, async val => {
71
84
try {
72
- option = JSON .parse (optionCode .value );
85
+ transformedCode .value = await transform (` (${ val} )` , { loader: " ts" });
86
+ transformErrors .value = [];
73
87
} catch (e) {
74
- try {
75
- option = eval (` (${ optionCode .value } )` );
76
- } catch (e) {
77
- return ` /* Unable to parse \` option\` code */
78
- // ${ e .message }
88
+ transformErrors .value = e .errors ;
89
+ }
90
+ });
91
+
92
+ function formatError (errors ) {
93
+ return errors
94
+ .map (({ text, location: { lineText, line, column, length } }) => {
95
+ const digit = Math .ceil (Math .log10 (line)) || 1 ;
96
+ lineText = line === 1 ? lineText .slice (1 ) : lineText;
97
+ lineText =
98
+ line === optionCode .value .split (" \n " ).length
99
+ ? lineText .slice (0 , - 1 )
100
+ : lineText;
101
+ column = line === 1 ? column - 1 : column;
102
+
103
+ return ` /* ${ text} */
104
+
105
+ // ${ line} | ${ lineText}
106
+ // ${ " " .repeat (digit)} | ${ " " .repeat (column)}${ " ~" .repeat (length)}
79
107
` ;
80
- }
108
+ })
109
+ .join (" \n\n " );
110
+ }
111
+
112
+ const importCode = computed (() => {
113
+ if (optionCode .value .trim () === " " ) {
114
+ return " // Paste your option code first" ;
115
+ }
116
+
117
+ if (transformErrors .value .length ) {
118
+ return formatError (transformErrors .value );
81
119
}
82
120
83
121
try {
84
- return getImportsFromOption (option , {
122
+ return getImportsFromOption (eval ( transformedCode . value . code ) , {
85
123
renderer: renderer .value ,
86
124
... codegenOptions .value
87
125
});
88
126
} catch (e) {
89
- return ` /* Invalid ECharts option */` ;
127
+ return ` /* Invalid ECharts option */
128
+
129
+ // ${ e .message }
130
+ ` ;
90
131
}
91
132
});
92
133
@@ -169,7 +210,12 @@ onBeforeUnmount(() => {
169
210
ref= " source"
170
211
class = " option-code"
171
212
v- model= " optionCode"
172
- placeholder= " Paste your option code here..."
213
+ : placeholder= "
214
+ initializing
215
+ ? 'Initializing...'
216
+ : 'Paste your option code (TS/JS literal) here...'
217
+ "
218
+ : disabled= " initializing"
173
219
autofocus
174
220
>< / textarea>
175
221
< code- highlight
0 commit comments