-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
33,643 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<script lang="ts"> | ||
import { setupMonacoEnv } from './utils'; | ||
setupMonacoEnv(); | ||
</script> | ||
<script lang="ts" setup> | ||
import { onMounted, onBeforeUnmount, ref, shallowRef, nextTick, watchEffect } from 'vue'; | ||
import { loadGrammars } from './grammars'; | ||
import * as monaco from 'monaco-editor'; | ||
import { setupThemePromise } from './utils'; | ||
interface Props { | ||
value?: string | ||
language?: string; | ||
readonly?: boolean | ||
} | ||
const props = withDefaults(defineProps<Props>(), { | ||
value: '', | ||
readonly: false | ||
}) | ||
const emits = defineEmits<{ | ||
(e: 'change', value: string): void, | ||
(e: 'save', value: string): void | ||
}>() | ||
const containerRef = ref<HTMLDivElement | null>(); | ||
const ready = ref(false); | ||
const editor = shallowRef<monaco.editor.IStandaloneCodeEditor | undefined>(undefined); | ||
onMounted(async () => { | ||
const theme = await setupThemePromise; | ||
ready.value = true; | ||
await nextTick(); | ||
if (!containerRef.value) { | ||
throw new Error("Cannot find containerRef"); | ||
} | ||
const editorInstance = monaco.editor.create(containerRef.value, { | ||
theme, | ||
value: props.value, | ||
language: props.language, | ||
readOnly: props.readonly, | ||
automaticLayout: true, | ||
scrollBeyondLastLine: false, | ||
minimap: { | ||
enabled: false, | ||
}, | ||
inlineSuggest: { | ||
enabled: false, | ||
}, | ||
}); | ||
editor.value = editorInstance | ||
await loadGrammars(editorInstance); | ||
editorInstance.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => { | ||
emits('save', editorInstance.getValue()); | ||
}); | ||
editorInstance.onDidChangeModelContent(() => { | ||
emits('change', editorInstance.getValue()); | ||
}); | ||
}); | ||
watchEffect(() => { | ||
if (editor.value && editor.value.getValue() !== props.value) { | ||
editor.value.setValue(props.value); | ||
} | ||
}) | ||
onBeforeUnmount(() => { | ||
editor.value?.dispose(); | ||
}); | ||
</script> | ||
<template> | ||
<div class="editor" ref="containerRef"></div> | ||
</template> | ||
<style> | ||
.editor { | ||
position: relative; | ||
height: 100%; | ||
width: 100%; | ||
overflow: hidden; | ||
} | ||
</style> |
Oops, something went wrong.