Skip to content

Commit

Permalink
fix: fixed prop mode of CodeEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
mynetfan committed Sep 18, 2021
1 parent 5af4527 commit 853bde9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/components/CodeEditor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ import jsonPreview from './src/json-preview/JsonPreview.vue';

export const CodeEditor = withInstall(codeEditor);
export const JsonPreview = withInstall(jsonPreview);

export * from './src/typing';
11 changes: 9 additions & 2 deletions src/components/CodeEditor/src/CodeEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
import { computed } from 'vue';
import CodeMirrorEditor from './codemirror/CodeMirror.vue';
import { isString } from '/@/utils/is';
import type { MODE } from './typing';
import { MODE } from './typing';
const props = defineProps({
value: { type: [Object, String] as PropType<Record<string, any> | string> },
mode: { type: String, default: MODE.JSON },
mode: {
type: String as PropType<MODE>,
default: MODE.JSON,
validator(value: any) {
// 这个值必须匹配下列字符串中的一个
return Object.values(MODE).includes(value);
},
},
readonly: { type: Boolean },
autoFormat: { type: Boolean, default: true },
});
Expand Down
10 changes: 9 additions & 1 deletion src/components/CodeEditor/src/codemirror/CodeMirror.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { useAppStore } from '/@/store/modules/app';
import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
import CodeMirror from 'codemirror';
import { MODE } from './../typing';
// css
import './codemirror.css';
import 'codemirror/theme/idea.css';
Expand All @@ -18,7 +19,14 @@
import 'codemirror/mode/htmlmixed/htmlmixed';
const props = defineProps({
mode: { type: String, default: 'application/json' },
mode: {
type: String as PropType<MODE>,
default: MODE.JSON,
validator(value: any) {
// 这个值必须匹配下列字符串中的一个
return Object.values(MODE).includes(value);
},
},
value: { type: String, default: '' },
readonly: { type: Boolean, default: false },
});
Expand Down
10 changes: 5 additions & 5 deletions src/components/CodeEditor/src/typing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const MODE = {
JSON: 'application/json',
html: 'htmlmixed',
js: 'javascript',
};
export enum MODE {
JSON = 'application/json',
HTML = 'htmlmixed',
JS = 'javascript',
}
10 changes: 5 additions & 5 deletions src/views/demo/editor/json/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</template>
<script lang="ts">
import { defineComponent, ref, unref, h } from 'vue';
import { CodeEditor, JsonPreview } from '/@/components/CodeEditor';
import { CodeEditor, JsonPreview, MODE } from '/@/components/CodeEditor';
import { PageWrapper } from '/@/components/Page';
import { Radio, Space, Modal } from 'ant-design-vue';
Expand Down Expand Up @@ -62,20 +62,20 @@
ASpace: Space,
},
setup() {
const modeValue = ref('application/json');
const modeValue = ref<MODE>(MODE.JSON);
const value = ref(jsonData);
function handleModeChange(e: ChangeEvent) {
const mode = e.target.value;
if (mode === 'application/json') {
if (mode === MODE.JSON) {
value.value = jsonData;
return;
}
if (mode === 'htmlmixed') {
if (mode === MODE.HTML) {
value.value = htmlData;
return;
}
if (mode === 'javascript') {
if (mode === MODE.JS) {
value.value = jsData;
return;
}
Expand Down

0 comments on commit 853bde9

Please sign in to comment.