|
@@ -2,17 +2,8 @@
|
|
|
<div class="relative !h-full w-full overflow-hidden" ref="el"> </div>
|
|
|
</template>
|
|
|
|
|
|
-<script lang="ts">
|
|
|
- import {
|
|
|
- ref,
|
|
|
- onMounted,
|
|
|
- onUnmounted,
|
|
|
- watchEffect,
|
|
|
- watch,
|
|
|
- defineComponent,
|
|
|
- unref,
|
|
|
- nextTick,
|
|
|
- } from 'vue';
|
|
|
+<script lang="ts" setup>
|
|
|
+ import { ref, onMounted, onUnmounted, watchEffect, watch, unref, nextTick } from 'vue';
|
|
|
import { useDebounceFn } from '@vueuse/core';
|
|
|
import { useAppStore } from '/@/store/modules/app';
|
|
|
import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
|
|
@@ -26,95 +17,89 @@
|
|
|
import 'codemirror/mode/css/css';
|
|
|
import 'codemirror/mode/htmlmixed/htmlmixed';
|
|
|
|
|
|
- const props = {
|
|
|
+ const props = defineProps({
|
|
|
mode: { type: String, default: 'application/json' },
|
|
|
value: { type: String, default: '' },
|
|
|
readonly: { type: Boolean, default: false },
|
|
|
- };
|
|
|
+ });
|
|
|
|
|
|
- export default defineComponent({
|
|
|
- props,
|
|
|
- emits: ['change'],
|
|
|
- setup(props, { emit }) {
|
|
|
- const el = ref();
|
|
|
- let editor: Nullable<CodeMirror.Editor>;
|
|
|
+ const emit = defineEmits(['change']);
|
|
|
|
|
|
- const debounceRefresh = useDebounceFn(refresh, 100);
|
|
|
- const appStore = useAppStore();
|
|
|
+ const el = ref();
|
|
|
+ let editor: Nullable<CodeMirror.Editor>;
|
|
|
|
|
|
- watch(
|
|
|
- () => props.value,
|
|
|
- async (value) => {
|
|
|
- await nextTick();
|
|
|
- const oldValue = editor?.getValue();
|
|
|
- if (value !== oldValue) {
|
|
|
- editor?.setValue(value ? value : '');
|
|
|
- }
|
|
|
- },
|
|
|
- { flush: 'post' }
|
|
|
- );
|
|
|
+ const debounceRefresh = useDebounceFn(refresh, 100);
|
|
|
+ const appStore = useAppStore();
|
|
|
|
|
|
- watchEffect(() => {
|
|
|
- editor?.setOption('mode', props.mode);
|
|
|
- });
|
|
|
+ watch(
|
|
|
+ () => props.value,
|
|
|
+ async (value) => {
|
|
|
+ await nextTick();
|
|
|
+ const oldValue = editor?.getValue();
|
|
|
+ if (value !== oldValue) {
|
|
|
+ editor?.setValue(value ? value : '');
|
|
|
+ }
|
|
|
+ },
|
|
|
+ { flush: 'post' }
|
|
|
+ );
|
|
|
|
|
|
- watch(
|
|
|
- () => appStore.getDarkMode,
|
|
|
- async () => {
|
|
|
- setTheme();
|
|
|
- },
|
|
|
- {
|
|
|
- immediate: true,
|
|
|
- }
|
|
|
- );
|
|
|
+ watchEffect(() => {
|
|
|
+ editor?.setOption('mode', props.mode);
|
|
|
+ });
|
|
|
|
|
|
- function setTheme() {
|
|
|
- unref(editor)?.setOption(
|
|
|
- 'theme',
|
|
|
- appStore.getDarkMode === 'light' ? 'idea' : 'material-palenight'
|
|
|
- );
|
|
|
- }
|
|
|
+ watch(
|
|
|
+ () => appStore.getDarkMode,
|
|
|
+ async () => {
|
|
|
+ setTheme();
|
|
|
+ },
|
|
|
+ {
|
|
|
+ immediate: true,
|
|
|
+ }
|
|
|
+ );
|
|
|
|
|
|
- function refresh() {
|
|
|
- editor?.refresh();
|
|
|
- }
|
|
|
+ function setTheme() {
|
|
|
+ unref(editor)?.setOption(
|
|
|
+ 'theme',
|
|
|
+ appStore.getDarkMode === 'light' ? 'idea' : 'material-palenight'
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
- async function init() {
|
|
|
- const addonOptions = {
|
|
|
- autoCloseBrackets: true,
|
|
|
- autoCloseTags: true,
|
|
|
- foldGutter: true,
|
|
|
- gutters: ['CodeMirror-linenumbers'],
|
|
|
- };
|
|
|
+ function refresh() {
|
|
|
+ editor?.refresh();
|
|
|
+ }
|
|
|
|
|
|
- editor = CodeMirror(el.value!, {
|
|
|
- value: '',
|
|
|
- mode: props.mode,
|
|
|
- readOnly: props.readonly,
|
|
|
- tabSize: 2,
|
|
|
- theme: 'material-palenight',
|
|
|
- lineWrapping: true,
|
|
|
- lineNumbers: true,
|
|
|
- ...addonOptions,
|
|
|
- });
|
|
|
- editor?.setValue(props.value);
|
|
|
- setTheme();
|
|
|
- editor?.on('change', () => {
|
|
|
- emit('change', editor?.getValue());
|
|
|
- });
|
|
|
- }
|
|
|
+ async function init() {
|
|
|
+ const addonOptions = {
|
|
|
+ autoCloseBrackets: true,
|
|
|
+ autoCloseTags: true,
|
|
|
+ foldGutter: true,
|
|
|
+ gutters: ['CodeMirror-linenumbers'],
|
|
|
+ };
|
|
|
|
|
|
- onMounted(async () => {
|
|
|
- await nextTick();
|
|
|
- init();
|
|
|
- useWindowSizeFn(debounceRefresh);
|
|
|
- });
|
|
|
+ editor = CodeMirror(el.value!, {
|
|
|
+ value: '',
|
|
|
+ mode: props.mode,
|
|
|
+ readOnly: props.readonly,
|
|
|
+ tabSize: 2,
|
|
|
+ theme: 'material-palenight',
|
|
|
+ lineWrapping: true,
|
|
|
+ lineNumbers: true,
|
|
|
+ ...addonOptions,
|
|
|
+ });
|
|
|
+ editor?.setValue(props.value);
|
|
|
+ setTheme();
|
|
|
+ editor?.on('change', () => {
|
|
|
+ emit('change', editor?.getValue());
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- onUnmounted(() => {
|
|
|
- editor = null;
|
|
|
- });
|
|
|
+ onMounted(async () => {
|
|
|
+ await nextTick();
|
|
|
+ init();
|
|
|
+ useWindowSizeFn(debounceRefresh);
|
|
|
+ });
|
|
|
|
|
|
- return { el };
|
|
|
- },
|
|
|
+ onUnmounted(() => {
|
|
|
+ editor = null;
|
|
|
});
|
|
|
</script>
|