Skip to content

Commit d47eca5

Browse files
authored
feat: add autoSave option (#247)
1 parent 1f3c54d commit d47eca5

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/Repl.vue

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface Props {
1212
editor: EditorComponentType
1313
store?: Store
1414
autoResize?: boolean
15+
autoSave?: boolean // auto save and compile, default to true, if false, user need to press ctrl + s to save and compile
1516
showCompileOutput?: boolean
1617
showImportMap?: boolean
1718
showTsConfig?: boolean
@@ -35,6 +36,7 @@ const props = withDefaults(defineProps<Props>(), {
3536
previewTheme: false,
3637
store: () => useStore(),
3738
autoResize: true,
39+
autoSave: true,
3840
showCompileOutput: true,
3941
showImportMap: true,
4042
showTsConfig: true,
@@ -66,6 +68,7 @@ const outputSlotName = computed(() => (props.layoutReverse ? 'left' : 'right'))
6668
6769
provide(injectKeyStore, props.store)
6870
provide('autoresize', props.autoResize)
71+
provide('autosave', props.autoSave)
6972
provide('import-map', toRef(props, 'showImportMap'))
7073
provide('tsconfig', toRef(props, 'showTsConfig'))
7174
provide('clear-console', toRef(props, 'clearConsole'))

src/monaco/Monaco.vue

+13-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const containerRef = ref<HTMLDivElement>()
3737
const ready = ref(false)
3838
const editor = shallowRef<monaco.editor.IStandaloneCodeEditor>()
3939
const store = inject(injectKeyStore)!
40+
const autoSave = inject('autosave')!
4041
4142
initMonaco(store)
4243
@@ -140,9 +141,18 @@ onMounted(async () => {
140141
// ignore save event
141142
})
142143
143-
editorInstance.onDidChangeModelContent(() => {
144-
emit('change', editorInstance.getValue())
145-
})
144+
if (autoSave) {
145+
editorInstance.onDidChangeModelContent(() => {
146+
emit('change', editorInstance.getValue())
147+
})
148+
} else {
149+
containerRef.value.addEventListener('keydown', (e: KeyboardEvent) => {
150+
if (e.ctrlKey && e.key === 's') {
151+
e.preventDefault()
152+
emit('change', editorInstance.getValue())
153+
}
154+
})
155+
}
146156
147157
// update theme
148158
watch(replTheme, (n) => {

0 commit comments

Comments
 (0)