api-client/components/smart/CodeMirror.vue

84 lines
1.5 KiB
Vue
Raw Normal View History

2021-08-30 18:33:07 +00:00
<template>
<div ref="editor" class="w-full block"></div>
</template>
2021-08-31 05:17:00 +00:00
<script setup lang="ts">
2021-08-30 18:33:07 +00:00
import Codemirror from "codemirror"
import "codemirror/lib/codemirror.css"
2021-08-31 05:17:00 +00:00
2021-08-30 18:33:07 +00:00
import "codemirror/theme/juejin.css"
2021-08-31 05:17:00 +00:00
import "codemirror/addon/display/autorefresh"
import "codemirror/addon/selection/active-line"
import "codemirror/addon/edit/closebrackets"
import "codemirror/addon/display/placeholder"
2021-08-30 18:33:07 +00:00
2021-08-31 05:17:00 +00:00
import { onMounted, ref, watch } from "@nuxtjs/composition-api"
2021-08-30 18:33:07 +00:00
const DEFAULT_THEME = "juejin"
2021-08-31 05:17:00 +00:00
const props = defineProps<{
value: string
mode: string
placeholder: string
wrap: boolean
2021-08-31 05:17:00 +00:00
}>()
const emit = defineEmits<{
(e: "input", value: string): void
}>()
watch(
() => props.value,
(value) => {
editor.setValue(value)
}
)
watch(
() => props.mode,
(mode) => {
editor.setOption("mode", mode)
}
)
const editor = ref<any | null>(null)
const cm = ref<Codemirror.Editor | null>(null)
onMounted(() => {
cm.value = Codemirror(editor.value, {
value: props.value,
mode: props.mode,
lineWrapping: props.wrap,
placeholder: props.placeholder,
2021-08-31 05:17:00 +00:00
autoRefresh: true,
lineNumbers: true,
styleActiveLine: true,
autoCloseBrackets: true,
2021-08-31 05:17:00 +00:00
theme: DEFAULT_THEME,
gutters: ["CodeMirror-linenumbers"],
2021-08-31 05:17:00 +00:00
})
cm.value?.on("change", (cm) => {
const val = cm.getValue()
2021-08-31 05:17:00 +00:00
emit("input", val)
})
})
2021-08-30 18:33:07 +00:00
</script>
2021-08-31 05:17:00 +00:00
<style lang="scss" scoped>
2021-08-30 18:33:07 +00:00
.CodeMirror {
@apply block;
@apply border-b;
@apply border-dividerLight;
@apply w-full;
2021-08-31 05:17:00 +00:00
@apply h-auto;
@apply font-mono;
2021-08-31 05:17:00 +00:00
}
.CodeMirror-scroll {
@apply min-h-32;
2021-08-30 18:33:07 +00:00
}
</style>