-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathRepl.vue
168 lines (153 loc) · 3.88 KB
/
Repl.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<script setup lang="ts">
import SplitPane from './SplitPane.vue'
import Output from './output/Output.vue'
import { type Store, useStore } from './store'
import { computed, provide, toRefs, useTemplateRef, watchEffect } from 'vue'
import {
type ConsoleComponentType,
type EditorComponentType,
injectKeyPreviewRef,
injectKeyProps,
} from './types'
import EditorContainer from './editor/EditorContainer.vue'
import type * as monaco from 'monaco-editor-core'
export interface Props {
theme?: 'dark' | 'light'
previewTheme?: boolean
editor: EditorComponentType
console?: ConsoleComponentType
store?: Store
autoResize?: boolean
showCompileOutput?: boolean
showImportMap?: boolean
showTsConfig?: boolean
showConsole?: boolean
clearConsole?: boolean
layout?: 'horizontal' | 'vertical'
layoutReverse?: boolean
ssr?: boolean
previewOptions?: {
headHTML?: string
bodyHTML?: string
placeholderHTML?: string
customCode?: {
importCode?: string
useCode?: string
}
showRuntimeError?: boolean
showRuntimeWarning?: boolean
}
editorOptions?: {
showErrorText?: string | false
autoSaveText?: string | false
monacoOptions?: monaco.editor.IStandaloneEditorConstructionOptions
}
splitPaneOptions?: {
codeTogglerText?: string
outputTogglerText?: string
}
}
const autoSave = defineModel<boolean>({ default: true })
const props = withDefaults(defineProps<Props>(), {
theme: 'light',
previewTheme: false,
store: () => useStore(),
autoResize: true,
showCompileOutput: true,
showImportMap: true,
showTsConfig: true,
showConsole: false,
clearConsole: true,
layoutReverse: false,
ssr: false,
layout: 'horizontal',
previewOptions: () => ({}),
editorOptions: () => ({}),
splitPaneOptions: () => ({}),
})
if (!props.editor) {
throw new Error('The "editor" prop is now required.')
}
watchEffect(() => {
if (!!props.showConsole && !props.console)
throw new Error(
'If you want to enable a console "console" prop is required.',
)
})
const consoleWrapper = computed<ConsoleComponentType>(
() => props.console ?? (() => ({})),
)
const outputRef = useTemplateRef('output')
props.store.init()
const editorSlotName = computed(() => (props.layoutReverse ? 'right' : 'left'))
const outputSlotName = computed(() => (props.layoutReverse ? 'left' : 'right'))
provide(injectKeyProps, {
...toRefs(props),
console: consoleWrapper,
autoSave,
})
provide(
injectKeyPreviewRef,
computed(() => outputRef.value?.previewRef?.container ?? null),
)
/**
* Reload the preview iframe
*/
function reload() {
outputRef.value?.reload()
}
defineExpose({ reload })
</script>
<template>
<div class="vue-repl">
<SplitPane :layout="layout">
<template #[editorSlotName]>
<EditorContainer :editor-component="editor" />
</template>
<template #[outputSlotName]>
<Output
ref="output"
:editor-component="editor"
:console-component="consoleWrapper"
:show-compile-output="props.showCompileOutput"
:ssr="!!props.ssr"
/>
</template>
</SplitPane>
</div>
</template>
<style>
.vue-repl {
--bg: #fff;
--bg-soft: #f8f8f8;
--border: #ddd;
--text-light: #888;
--font-code: Menlo, Monaco, Consolas, 'Courier New', monospace;
--color-branding: #42b883;
--color-branding-dark: #416f9c;
--header-height: 38px;
height: 100%;
margin: 0;
overflow: hidden;
font-size: 13px;
font-family:
-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-color: var(--bg-soft);
}
.dark .vue-repl {
--bg: #1a1a1a;
--bg-soft: #242424;
--border: #383838;
--text-light: #aaa;
--color-branding: #42d392;
--color-branding-dark: #89ddff;
}
.vue-repl button {
border: none;
outline: none;
cursor: pointer;
margin: 0;
background-color: transparent;
}
</style>