Skip to content

Commit

Permalink
feat: add autoSave option (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
btea authored May 24, 2024
1 parent 1f3c54d commit d47eca5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Repl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface Props {
editor: EditorComponentType
store?: Store
autoResize?: boolean
autoSave?: boolean // auto save and compile, default to true, if false, user need to press ctrl + s to save and compile
showCompileOutput?: boolean
showImportMap?: boolean
showTsConfig?: boolean
Expand All @@ -35,6 +36,7 @@ const props = withDefaults(defineProps<Props>(), {
previewTheme: false,
store: () => useStore(),
autoResize: true,
autoSave: true,
showCompileOutput: true,
showImportMap: true,
showTsConfig: true,
Expand Down Expand Up @@ -66,6 +68,7 @@ const outputSlotName = computed(() => (props.layoutReverse ? 'left' : 'right'))
provide(injectKeyStore, props.store)
provide('autoresize', props.autoResize)
provide('autosave', props.autoSave)
provide('import-map', toRef(props, 'showImportMap'))
provide('tsconfig', toRef(props, 'showTsConfig'))
provide('clear-console', toRef(props, 'clearConsole'))
Expand Down
16 changes: 13 additions & 3 deletions src/monaco/Monaco.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const containerRef = ref<HTMLDivElement>()
const ready = ref(false)
const editor = shallowRef<monaco.editor.IStandaloneCodeEditor>()
const store = inject(injectKeyStore)!
const autoSave = inject('autosave')!
initMonaco(store)
Expand Down Expand Up @@ -140,9 +141,18 @@ onMounted(async () => {
// ignore save event
})
editorInstance.onDidChangeModelContent(() => {
emit('change', editorInstance.getValue())
})
if (autoSave) {
editorInstance.onDidChangeModelContent(() => {
emit('change', editorInstance.getValue())
})
} else {
containerRef.value.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.ctrlKey && e.key === 's') {
e.preventDefault()
emit('change', editorInstance.getValue())
}
})
}
// update theme
watch(replTheme, (n) => {
Expand Down

0 comments on commit d47eca5

Please sign in to comment.