|
1 | 1 | /* eslint-disable no-console */ |
2 | 2 | // eslint-disable-next-line import/no-unresolved |
3 | | -import * as monaco from 'monaco-editor'; |
| 3 | +import * as m from 'monaco-editor'; |
4 | 4 | import { h, createApp } from 'vue'; |
5 | 5 | import { transform } from '@babel/core'; |
6 | | -import babelPluginJSx from '../../babel-plugin-jsx/src'; |
| 6 | +import babelPluginJsx from '../../babel-plugin-jsx/src'; |
7 | 7 | import './index.css'; |
8 | | -// or import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; |
9 | | -// if shipping only a subset of the features & languages is desired |
10 | 8 |
|
11 | | -createApp( |
12 | | - () => h('h1', null, 'Vue JSX Explorer'), |
13 | | -).mount('#header'); |
14 | | - |
15 | | -// @ts-ignore |
16 | | -if (module.hot) { |
17 | | - // @ts-ignore |
18 | | - module.hot.accept('../../babel-plugin-jsx/src', () => { |
19 | | - compile(); |
20 | | - }); |
| 9 | +declare global { |
| 10 | + interface Window { |
| 11 | + monaco: typeof m |
| 12 | + init: () => void |
| 13 | + } |
21 | 14 | } |
22 | 15 |
|
23 | | -const sharedEditorOptions: monaco.editor.IStandaloneEditorConstructionOptions = { |
24 | | - theme: 'vs-dark', |
25 | | - fontSize: 14, |
26 | | - wordWrap: 'on', |
27 | | - scrollBeyondLastLine: false, |
28 | | - renderWhitespace: 'selection', |
29 | | - contextmenu: false, |
30 | | - minimap: { |
31 | | - enabled: false, |
32 | | - }, |
33 | | -}; |
| 16 | +window.init = () => { |
| 17 | + const { monaco } = window; |
| 18 | + createApp( |
| 19 | + () => h('h1', null, 'Vue JSX Explorer'), |
| 20 | + ).mount('#header'); |
| 21 | + |
| 22 | + // @ts-ignore |
| 23 | + if (module.hot) { |
| 24 | + // @ts-ignore |
| 25 | + module.hot.accept('../../babel-plugin-jsx/src', () => { |
| 26 | + compile(); |
| 27 | + }); |
| 28 | + } |
34 | 29 |
|
35 | | -monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ |
36 | | - allowJs: true, |
37 | | - allowNonTsExtensions: true, |
38 | | - lib: [], |
39 | | - jsx: monaco.languages.typescript.JsxEmit.React, |
40 | | - target: monaco.languages.typescript.ScriptTarget.Latest, |
41 | | - typeRoots: ['node_modules/@types'], |
42 | | -}); |
| 30 | + const sharedEditorOptions: m.editor.IStandaloneEditorConstructionOptions = { |
| 31 | + theme: 'vs-dark', |
| 32 | + fontSize: 14, |
| 33 | + wordWrap: 'on', |
| 34 | + scrollBeyondLastLine: false, |
| 35 | + renderWhitespace: 'selection', |
| 36 | + contextmenu: false, |
| 37 | + minimap: { |
| 38 | + enabled: false, |
| 39 | + }, |
| 40 | + }; |
43 | 41 |
|
44 | | -const editor = monaco.editor.create(document.getElementById('source')!, { |
45 | | - value: decodeURIComponent(window.location.hash.slice(1)) || localStorage.getItem('state') || 'const App = () => <div>Hello World</div>', |
46 | | - language: 'javascript', |
47 | | - tabSize: 2, |
48 | | - ...sharedEditorOptions, |
49 | | -}); |
| 42 | + monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ |
| 43 | + allowJs: true, |
| 44 | + allowNonTsExtensions: true, |
| 45 | + lib: [], |
| 46 | + jsx: monaco.languages.typescript.JsxEmit.React, |
| 47 | + target: monaco.languages.typescript.ScriptTarget.Latest, |
| 48 | + typeRoots: ['node_modules/@types'], |
| 49 | + }); |
50 | 50 |
|
51 | | -const output = monaco.editor.create(document.getElementById('output')!, { |
52 | | - value: '', |
53 | | - language: 'javascript', |
54 | | - readOnly: true, |
55 | | - tabSize: 2, |
56 | | - ...sharedEditorOptions, |
57 | | -}); |
| 51 | + const editor = monaco.editor.create(document.getElementById('source')!, { |
| 52 | + value: decodeURIComponent(window.location.hash.slice(1)) || localStorage.getItem('state') || 'const App = () => <div>Hello World</div>', |
| 53 | + language: 'javascript', |
| 54 | + tabSize: 2, |
| 55 | + ...sharedEditorOptions, |
| 56 | + }); |
58 | 57 |
|
59 | | -const compile = () => { |
60 | | - const src = editor.getValue(); |
61 | | - localStorage.setItem('state', src); |
62 | | - window.location.hash = encodeURIComponent(src); |
63 | | - console.clear(); |
64 | | - transform(src, { |
65 | | - babelrc: false, |
66 | | - plugins: [[babelPluginJSx, { transformOn: true, optimize: true }]], |
67 | | - ast: true, |
68 | | - }, (err, result = {}) => { |
69 | | - const res = result!; |
70 | | - if (!err) { |
71 | | - console.log('AST', res.ast!); |
72 | | - output.setValue(res.code!); |
73 | | - } else { |
74 | | - output.setValue(err.message!); |
75 | | - } |
| 58 | + const output = monaco.editor.create(document.getElementById('output')!, { |
| 59 | + value: '', |
| 60 | + language: 'javascript', |
| 61 | + readOnly: true, |
| 62 | + tabSize: 2, |
| 63 | + ...sharedEditorOptions, |
76 | 64 | }); |
77 | | -}; |
78 | 65 |
|
79 | | -// handle resize |
80 | | -window.addEventListener('resize', () => { |
81 | | - editor.layout(); |
82 | | - output.layout(); |
83 | | -}); |
| 66 | + const compile = () => { |
| 67 | + const src = editor.getValue(); |
| 68 | + localStorage.setItem('state', src); |
| 69 | + window.location.hash = encodeURIComponent(src); |
| 70 | + console.clear(); |
| 71 | + transform(src, { |
| 72 | + babelrc: false, |
| 73 | + plugins: [[babelPluginJsx, { transformOn: true, optimize: true }]], |
| 74 | + ast: true, |
| 75 | + }, (err, result = {}) => { |
| 76 | + const res = result!; |
| 77 | + if (!err) { |
| 78 | + console.log('AST', res.ast!); |
| 79 | + output.setValue(res.code!); |
| 80 | + } else { |
| 81 | + output.setValue(err.message!); |
| 82 | + } |
| 83 | + }); |
| 84 | + }; |
84 | 85 |
|
85 | | -compile(); |
| 86 | + // handle resize |
| 87 | + window.addEventListener('resize', () => { |
| 88 | + editor.layout(); |
| 89 | + output.layout(); |
| 90 | + }); |
| 91 | + |
| 92 | + compile(); |
86 | 93 |
|
87 | | -// update compile output when input changes |
88 | | -editor.onDidChangeModelContent(debounce(compile)); |
| 94 | + // update compile output when input changes |
| 95 | + editor.onDidChangeModelContent(debounce(compile)); |
| 96 | +}; |
89 | 97 |
|
90 | 98 | function debounce<T extends(...args: any[]) => any>( |
91 | 99 | fn: T, |
|
0 commit comments