Skip to content

Commit fd0b06a

Browse files
committed
fix: split monaco out
1 parent 5ca6318 commit fd0b06a

File tree

9 files changed

+133
-33
lines changed

9 files changed

+133
-33
lines changed

src/Repl.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<script setup lang="ts">
22
import SplitPane from './SplitPane.vue'
3-
import Editor from './editor/Editor.vue'
43
import Output from './output/Output.vue'
54
import { Store, ReplStore, SFCOptions } from './store'
65
import { provide, toRef } from 'vue'
6+
import { EditorComponentType } from './types';
7+
import EditorContainer from './editor/EditorContainer.vue';
8+
import CodeMirrorEditor from './editor/CodeMirrorEditor.vue';
79
810
interface Props {
911
store?: Store
@@ -13,14 +15,16 @@ interface Props {
1315
clearConsole?: boolean
1416
sfcOptions?: SFCOptions
1517
layout?: string
18+
editor?: EditorComponentType
1619
}
1720
1821
const props = withDefaults(defineProps<Props>(), {
1922
store: () => new ReplStore(),
2023
autoResize: true,
2124
showCompileOutput: true,
2225
showImportMap: true,
23-
clearConsole: true
26+
clearConsole: true,
27+
editor: CodeMirrorEditor
2428
})
2529
2630
props.store.options = props.sfcOptions
@@ -35,10 +39,13 @@ provide('clear-console', toRef(props, 'clearConsole'))
3539
<div class="vue-repl">
3640
<SplitPane :layout="layout">
3741
<template #left>
38-
<Editor />
42+
<EditorContainer :editor-component="editor" />
3943
</template>
4044
<template #right>
41-
<Output :showCompileOutput="props.showCompileOutput" />
45+
<Output
46+
:showCompileOutput="props.showCompileOutput"
47+
:editor-component="editor"
48+
/>
4249
</template>
4350
</SplitPane>
4451
</div>

src/editor/CodeMirrorEditor.vue

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<script setup lang="ts">
2+
import CodeMirror from '../codemirror/CodeMirror.vue'
3+
import { computed } from 'vue'
4+
5+
const props = defineProps<{
6+
value: string;
7+
filename: string;
8+
readonly?: boolean
9+
}>()
10+
11+
const emits = defineEmits<{
12+
(e: 'change', code: string): void;
13+
}>()
14+
15+
const onChange = (code: string) => {
16+
emits('change', code)
17+
}
18+
19+
const activeMode = computed(() => {
20+
const filename = props.filename
21+
return filename.endsWith('.vue') || filename.endsWith('.html')
22+
? 'htmlmixed'
23+
: filename.endsWith('.css')
24+
? 'css'
25+
: 'javascript'
26+
})
27+
</script>
28+
29+
<template>
30+
<CodeMirror
31+
@change="onChange"
32+
:value="value"
33+
:mode="activeMode"
34+
/>
35+
</template>
36+

src/editor/Editor.vue renamed to src/editor/EditorContainer.vue

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
11
<script setup lang="ts">
22
import FileSelector from './FileSelector.vue'
3-
import Monaco from '../monaco/Monaco.vue'
43
import Message from '../Message.vue'
5-
import { computed, inject } from 'vue'
4+
import { debounce } from '../utils'
5+
import { inject, toRef } from 'vue'
66
import { Store } from '../store'
7+
import { EditorComponentType } from '../types'
8+
9+
const props = defineProps<{
10+
editorComponent: EditorComponentType
11+
}>()
12+
13+
const EditorComponent = toRef(props, 'editorComponent')
714
815
const store = inject('store') as Store
916
10-
const onChange = (code: string) => {
17+
const onChange = debounce((code: string) => {
1118
store.state.activeFile.code = code
12-
}
19+
}, 250)
1320
14-
const language = computed(() => {
15-
const { filename } = store.state.activeFile
16-
if (filename.endsWith('.vue')) {
17-
return 'vue'
18-
}
19-
if (filename.endsWith('.html')) {
20-
return 'html'
21-
}
22-
if (filename.endsWith('.css')) {
23-
return 'css'
24-
}
25-
if (filename.endsWith('.ts')) {
26-
return 'typescript'
27-
}
28-
return 'javascript'
29-
})
3021
</script>
3122

3223
<template>
3324
<FileSelector />
3425
<div class="editor-container">
35-
<Monaco
26+
<EditorComponent
27+
@change="onChange"
3628
:value="store.state.activeFile.code"
37-
:language="language"
38-
@save="onChange"
29+
:filename="store.state.activeFile.filename"
3930
/>
4031
<Message :err="store.state.errors[0]" />
4132
</div>

src/editor/MonacoEditor.vue

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<script setup lang="ts">
2+
import Monaco from '../monaco/Monaco.vue'
3+
import { computed } from 'vue'
4+
5+
const props = defineProps<{
6+
value: string;
7+
filename: string;
8+
readonly?: boolean
9+
}>()
10+
11+
const emits = defineEmits<{
12+
(e: 'change', code: string): void;
13+
}>()
14+
15+
const onChange = (code: string) => {
16+
emits('change', code)
17+
}
18+
19+
const language = computed(() => {
20+
const filename = props.filename;
21+
if (filename.endsWith('.vue')) {
22+
return 'vue'
23+
}
24+
if (filename.endsWith('.html')) {
25+
return 'html'
26+
}
27+
if (filename.endsWith('.css')) {
28+
return 'css'
29+
}
30+
if (filename.endsWith('.ts')) {
31+
return 'typescript'
32+
}
33+
return 'javascript'
34+
})
35+
</script>
36+
37+
<template>
38+
<Monaco
39+
:value="value"
40+
:language="language"
41+
@save="onChange"
42+
/>
43+
</template>

src/output/Output.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<script setup lang="ts">
22
import Preview from './Preview.vue'
3-
import Monaco from '../monaco/Monaco.vue'
43
import { Store } from '../store'
5-
import { inject, ref, computed } from 'vue'
4+
import { inject, ref, computed, toRef } from 'vue'
65
import type { OutputModes } from './types'
6+
import { EditorComponentType } from '../types'
77
88
const props = defineProps<{
99
showCompileOutput?: boolean
10+
editorComponent: EditorComponentType
1011
}>()
1112
13+
const EditorComponent = toRef(props, 'editorComponent')
14+
1215
const store = inject('store') as Store
1316
const modes = computed(() =>
1417
props.showCompileOutput
@@ -36,10 +39,10 @@ const mode = ref<OutputModes>(
3639

3740
<div class="output-container">
3841
<Preview :show="mode === 'preview'" />
39-
<Monaco
42+
<EditorComponent
4043
v-if="mode !== 'preview'"
4144
readonly
42-
:language="mode === 'css' ? 'css' : 'javascript'"
45+
:filename="mode === 'css' ? 'a.css' : 'a.js'"
4346
:value="store.state.activeFile.compiled[mode]"
4447
/>
4548
</div>

src/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Component } from "vue";
2+
3+
interface EditorProps {
4+
value: string;
5+
filename: string;
6+
readonly?: boolean
7+
}
8+
9+
interface EditorEmits {
10+
(e: 'change', code: string): void
11+
}
12+
13+
export type EditorComponentType = Component<EditorProps, EditorEmits>;

test/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createApp, h, watchEffect } from 'vue'
22
import { Repl, ReplStore } from '../src'
3+
// import MonacoEditor from '../src/editor/MonacoEditor.vue'
34

45
;(window as any).process = { env: {} }
56

@@ -35,7 +36,8 @@ const App = {
3536
return () =>
3637
h(Repl, {
3738
store,
38-
layout: 'vertical'
39+
layout: 'vertical',
40+
// editor: MonacoEditor
3941
// showCompileOutput: false,
4042
// showImportMap: false
4143
})

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"esModuleInterop": true,
1616
"removeComments": false,
1717
"lib": ["esnext", "dom"],
18+
"jsx": "preserve",
1819
"rootDir": "."
1920
},
2021
"include": ["src", "test", "vite.config.ts"]

vite.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ export default defineConfig({
2828
lib: {
2929
entry: './src/index.ts',
3030
formats: ['es'],
31-
fileName: () => 'vue-repl.js'
31+
fileName: () => '[name].js'
3232
},
3333
rollupOptions: {
34+
input: {
35+
'vue-repl': './src/index.ts',
36+
'vue-repl-monaco-editor': './src/editor/MonacoEditor.vue',
37+
},
3438
external: ['vue', 'vue/compiler-sfc']
3539
}
3640
}

0 commit comments

Comments
 (0)