Commit 853bde9275584fb3ad2cceb03e59f981b8ebbd9a

Authored by 无木
1 parent 5af45275

fix: fixed prop `mode` of `CodeEditor`

src/components/CodeEditor/index.ts
@@ -4,3 +4,5 @@ import jsonPreview from './src/json-preview/JsonPreview.vue'; @@ -4,3 +4,5 @@ import jsonPreview from './src/json-preview/JsonPreview.vue';
4 4
5 export const CodeEditor = withInstall(codeEditor); 5 export const CodeEditor = withInstall(codeEditor);
6 export const JsonPreview = withInstall(jsonPreview); 6 export const JsonPreview = withInstall(jsonPreview);
  7 +
  8 +export * from './src/typing';
src/components/CodeEditor/src/CodeEditor.vue
@@ -12,11 +12,18 @@ @@ -12,11 +12,18 @@
12 import { computed } from 'vue'; 12 import { computed } from 'vue';
13 import CodeMirrorEditor from './codemirror/CodeMirror.vue'; 13 import CodeMirrorEditor from './codemirror/CodeMirror.vue';
14 import { isString } from '/@/utils/is'; 14 import { isString } from '/@/utils/is';
15 - import type { MODE } from './typing'; 15 + import { MODE } from './typing';
16 16
17 const props = defineProps({ 17 const props = defineProps({
18 value: { type: [Object, String] as PropType<Record<string, any> | string> }, 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 readonly: { type: Boolean }, 27 readonly: { type: Boolean },
21 autoFormat: { type: Boolean, default: true }, 28 autoFormat: { type: Boolean, default: true },
22 }); 29 });
src/components/CodeEditor/src/codemirror/CodeMirror.vue
@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 import { useAppStore } from '/@/store/modules/app'; 8 import { useAppStore } from '/@/store/modules/app';
9 import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn'; 9 import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
10 import CodeMirror from 'codemirror'; 10 import CodeMirror from 'codemirror';
  11 + import { MODE } from './../typing';
11 // css 12 // css
12 import './codemirror.css'; 13 import './codemirror.css';
13 import 'codemirror/theme/idea.css'; 14 import 'codemirror/theme/idea.css';
@@ -18,7 +19,14 @@ @@ -18,7 +19,14 @@
18 import 'codemirror/mode/htmlmixed/htmlmixed'; 19 import 'codemirror/mode/htmlmixed/htmlmixed';
19 20
20 const props = defineProps({ 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 value: { type: String, default: '' }, 30 value: { type: String, default: '' },
23 readonly: { type: Boolean, default: false }, 31 readonly: { type: Boolean, default: false },
24 }); 32 });
src/components/CodeEditor/src/typing.ts
1 -export const MODE = {  
2 - JSON: 'application/json',  
3 - html: 'htmlmixed',  
4 - js: 'javascript',  
5 -}; 1 +export enum MODE {
  2 + JSON = 'application/json',
  3 + HTML = 'htmlmixed',
  4 + JS = 'javascript',
  5 +}
src/views/demo/editor/json/index.vue
@@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
15 </template> 15 </template>
16 <script lang="ts"> 16 <script lang="ts">
17 import { defineComponent, ref, unref, h } from 'vue'; 17 import { defineComponent, ref, unref, h } from 'vue';
18 - import { CodeEditor, JsonPreview } from '/@/components/CodeEditor'; 18 + import { CodeEditor, JsonPreview, MODE } from '/@/components/CodeEditor';
19 import { PageWrapper } from '/@/components/Page'; 19 import { PageWrapper } from '/@/components/Page';
20 import { Radio, Space, Modal } from 'ant-design-vue'; 20 import { Radio, Space, Modal } from 'ant-design-vue';
21 21
@@ -62,20 +62,20 @@ @@ -62,20 +62,20 @@
62 ASpace: Space, 62 ASpace: Space,
63 }, 63 },
64 setup() { 64 setup() {
65 - const modeValue = ref('application/json'); 65 + const modeValue = ref<MODE>(MODE.JSON);
66 const value = ref(jsonData); 66 const value = ref(jsonData);
67 67
68 function handleModeChange(e: ChangeEvent) { 68 function handleModeChange(e: ChangeEvent) {
69 const mode = e.target.value; 69 const mode = e.target.value;
70 - if (mode === 'application/json') { 70 + if (mode === MODE.JSON) {
71 value.value = jsonData; 71 value.value = jsonData;
72 return; 72 return;
73 } 73 }
74 - if (mode === 'htmlmixed') { 74 + if (mode === MODE.HTML) {
75 value.value = htmlData; 75 value.value = htmlData;
76 return; 76 return;
77 } 77 }
78 - if (mode === 'javascript') { 78 + if (mode === MODE.JS) {
79 value.value = jsData; 79 value.value = jsData;
80 return; 80 return;
81 } 81 }