Commit 853bde9275584fb3ad2cceb03e59f981b8ebbd9a
1 parent
5af45275
fix: fixed prop `mode` of `CodeEditor`
Showing
5 changed files
with
30 additions
and
13 deletions
src/components/CodeEditor/index.ts
src/components/CodeEditor/src/CodeEditor.vue
... | ... | @@ -12,11 +12,18 @@ |
12 | 12 | import { computed } from 'vue'; |
13 | 13 | import CodeMirrorEditor from './codemirror/CodeMirror.vue'; |
14 | 14 | import { isString } from '/@/utils/is'; |
15 | - import type { MODE } from './typing'; | |
15 | + import { MODE } from './typing'; | |
16 | 16 | |
17 | 17 | const props = defineProps({ |
18 | 18 | value: { type: [Object, String] as PropType<Record<string, any> | string> }, |
19 | - mode: { type: String, default: MODE.JSON }, | |
19 | + mode: { | |
20 | + type: String as PropType<MODE>, | |
21 | + default: MODE.JSON, | |
22 | + validator(value: any) { | |
23 | + // 这个值必须匹配下列字符串中的一个 | |
24 | + return Object.values(MODE).includes(value); | |
25 | + }, | |
26 | + }, | |
20 | 27 | readonly: { type: Boolean }, |
21 | 28 | autoFormat: { type: Boolean, default: true }, |
22 | 29 | }); | ... | ... |
src/components/CodeEditor/src/codemirror/CodeMirror.vue
... | ... | @@ -8,6 +8,7 @@ |
8 | 8 | import { useAppStore } from '/@/store/modules/app'; |
9 | 9 | import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn'; |
10 | 10 | import CodeMirror from 'codemirror'; |
11 | + import { MODE } from './../typing'; | |
11 | 12 | // css |
12 | 13 | import './codemirror.css'; |
13 | 14 | import 'codemirror/theme/idea.css'; |
... | ... | @@ -18,7 +19,14 @@ |
18 | 19 | import 'codemirror/mode/htmlmixed/htmlmixed'; |
19 | 20 | |
20 | 21 | const props = defineProps({ |
21 | - mode: { type: String, default: 'application/json' }, | |
22 | + mode: { | |
23 | + type: String as PropType<MODE>, | |
24 | + default: MODE.JSON, | |
25 | + validator(value: any) { | |
26 | + // 这个值必须匹配下列字符串中的一个 | |
27 | + return Object.values(MODE).includes(value); | |
28 | + }, | |
29 | + }, | |
22 | 30 | value: { type: String, default: '' }, |
23 | 31 | readonly: { type: Boolean, default: false }, |
24 | 32 | }); | ... | ... |
src/components/CodeEditor/src/typing.ts
src/views/demo/editor/json/index.vue
... | ... | @@ -15,7 +15,7 @@ |
15 | 15 | </template> |
16 | 16 | <script lang="ts"> |
17 | 17 | import { defineComponent, ref, unref, h } from 'vue'; |
18 | - import { CodeEditor, JsonPreview } from '/@/components/CodeEditor'; | |
18 | + import { CodeEditor, JsonPreview, MODE } from '/@/components/CodeEditor'; | |
19 | 19 | import { PageWrapper } from '/@/components/Page'; |
20 | 20 | import { Radio, Space, Modal } from 'ant-design-vue'; |
21 | 21 | |
... | ... | @@ -62,20 +62,20 @@ |
62 | 62 | ASpace: Space, |
63 | 63 | }, |
64 | 64 | setup() { |
65 | - const modeValue = ref('application/json'); | |
65 | + const modeValue = ref<MODE>(MODE.JSON); | |
66 | 66 | const value = ref(jsonData); |
67 | 67 | |
68 | 68 | function handleModeChange(e: ChangeEvent) { |
69 | 69 | const mode = e.target.value; |
70 | - if (mode === 'application/json') { | |
70 | + if (mode === MODE.JSON) { | |
71 | 71 | value.value = jsonData; |
72 | 72 | return; |
73 | 73 | } |
74 | - if (mode === 'htmlmixed') { | |
74 | + if (mode === MODE.HTML) { | |
75 | 75 | value.value = htmlData; |
76 | 76 | return; |
77 | 77 | } |
78 | - if (mode === 'javascript') { | |
78 | + if (mode === MODE.JS) { | |
79 | 79 | value.value = jsData; |
80 | 80 | return; |
81 | 81 | } | ... | ... |