返回 slidev
monaco-run.md
根目录 / docs / features / monaco-run.md
1 ---
2 depends:
3 - features/monaco-editor
4 - guide/animations
5 relates:
6 - Custom Code Runners: /custom/config-code-runners
7 since: v0.48.0
8 tags: [codeblock, editor]
9 description: |
10 Run code directly in the editor and see the result.
11 ---
12
13 # Monaco Runner
14
15 Slidev also provides the Monaco Runner Editor, which allows you to run the code directly in the editor and see the result. Use `{monaco-run}` to turn the block into a Monaco Runner Editor.
16
17 ````md
18 ```ts {monaco-run}
19 function distance(x: number, y: number) {
20 return Math.sqrt(x ** 2 + y ** 2)
21 }
22 console.log(distance(3, 4))
23 ```
24 ````
25
26 It provides the editor with a "Run" button, and shows the result of the code execution right below the code block. You may also modify the code and the result will be re-evaluated on the fly.
27
28 By default it will automatically run the code when the slide is loaded; if you want to instead explicitly trigger the run, you can set `{autorun:false}`.
29
30 ````md
31 ```ts {monaco-run} {autorun:false}
32 console.log('Click the play button to run me')
33 ```
34 ````
35
36 If you want to only show the output in certain clicks, you can use the `showOutputAt` prop. The value is the same as `v-click`.
37
38 ````md
39 ```ts {monaco-run} {showOutputAt:'+1'}
40 console.log('Shown after 1 click')
41 ```
42 ````
43
44 ## Font Size
45
46 When you customize the editor's [`fontSize` option](/custom/config-monaco#configure-the-editor), the runner output uses the same font size. This works both with per-instance `editorOptions` and with the global Monaco setup:
47
48 ````md
49 ```ts {monaco-run} {editorOptions: {fontSize:16}}
50 console.log('Both the editor and the output are 16px')
51 ```
52 ````
53
54 You can also set the output font size independently with `outputFontSize`. It accepts a number (in pixels) or any CSS length:
55
56 ````md
57 ```ts {monaco-run} {outputFontSize:18}
58 console.log('Only the output is 18px')
59 ```
60 ````
61
62 Currently, Slidev supports running JavaScript and TypeScript code out-of-box. Refer to [Custom Code Runners](/custom/config-code-runners) for custom language support.
63
63 lines MARKDOWN