mirror of
https://github.com/imdap/ruoyi-plus-vben5.git
synced 2026-05-07 18:51:46 +08:00
feat: codemirror6
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, ref, useTemplateRef, watch } from 'vue';
|
||||
import CodeMirror from 'vue-codemirror6';
|
||||
|
||||
import { usePreferences } from '@vben/preferences';
|
||||
|
||||
import { javascript } from '@codemirror/lang-javascript';
|
||||
import { oneDark } from '@codemirror/theme-one-dark';
|
||||
|
||||
import { type LanguageSupport, languageSupportMap } from './data';
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
/**
|
||||
* 语言
|
||||
*/
|
||||
language: LanguageSupport;
|
||||
/**
|
||||
* 只读
|
||||
*/
|
||||
readonly: boolean;
|
||||
}>(),
|
||||
{
|
||||
language: 'js',
|
||||
readonly: false,
|
||||
},
|
||||
);
|
||||
|
||||
const codeMirrorRef =
|
||||
useTemplateRef<InstanceType<typeof CodeMirror>>('codeMirrorRef');
|
||||
|
||||
const { isDark } = usePreferences();
|
||||
|
||||
const modelValue = defineModel({ default: '', type: String });
|
||||
|
||||
const lang = computed(() => languageSupportMap[props.language] ?? javascript());
|
||||
|
||||
// 通过v-if 卸载挂载达到更新语言的目的
|
||||
const langChanged = ref(true);
|
||||
watch(
|
||||
() => props.language,
|
||||
() => {
|
||||
langChanged.value = false;
|
||||
nextTick(() => (langChanged.value = true));
|
||||
},
|
||||
);
|
||||
/** 插件 */
|
||||
const extensions = [oneDark];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<CodeMirror
|
||||
v-if="langChanged"
|
||||
v-bind="$attrs"
|
||||
ref="codeMirrorRef"
|
||||
v-model="modelValue"
|
||||
:dark="isDark"
|
||||
:extensions="extensions"
|
||||
:lang="lang"
|
||||
:readonly="props.readonly"
|
||||
basic
|
||||
wrap
|
||||
>
|
||||
<template v-for="slotName in Object.keys($slots)" #[slotName]="scope">
|
||||
<slot :name="slotName" v-bind="scope ?? {}"></slot>
|
||||
</template>
|
||||
</CodeMirror>
|
||||
</template>
|
||||
Reference in New Issue
Block a user