fix: fixed prop mode of CodeEditor

This commit is contained in:
无木 2021-09-18 20:18:41 +08:00
parent 5af452754b
commit 853bde9275
5 changed files with 30 additions and 13 deletions

View File

@ -4,3 +4,5 @@ import jsonPreview from './src/json-preview/JsonPreview.vue';
export const CodeEditor = withInstall(codeEditor); export const CodeEditor = withInstall(codeEditor);
export const JsonPreview = withInstall(jsonPreview); export const JsonPreview = withInstall(jsonPreview);
export * from './src/typing';

View File

@ -12,11 +12,18 @@
import { computed } from 'vue'; import { computed } from 'vue';
import CodeMirrorEditor from './codemirror/CodeMirror.vue'; import CodeMirrorEditor from './codemirror/CodeMirror.vue';
import { isString } from '/@/utils/is'; import { isString } from '/@/utils/is';
import type { MODE } from './typing'; import { MODE } from './typing';
const props = defineProps({ const props = defineProps({
value: { type: [Object, String] as PropType<Record<string, any> | string> }, 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 }, readonly: { type: Boolean },
autoFormat: { type: Boolean, default: true }, autoFormat: { type: Boolean, default: true },
}); });

View File

@ -8,6 +8,7 @@
import { useAppStore } from '/@/store/modules/app'; import { useAppStore } from '/@/store/modules/app';
import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn'; import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
import CodeMirror from 'codemirror'; import CodeMirror from 'codemirror';
import { MODE } from './../typing';
// css // css
import './codemirror.css'; import './codemirror.css';
import 'codemirror/theme/idea.css'; import 'codemirror/theme/idea.css';
@ -18,7 +19,14 @@
import 'codemirror/mode/htmlmixed/htmlmixed'; import 'codemirror/mode/htmlmixed/htmlmixed';
const props = defineProps({ 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: '' }, value: { type: String, default: '' },
readonly: { type: Boolean, default: false }, readonly: { type: Boolean, default: false },
}); });

View File

@ -1,5 +1,5 @@
export const MODE = { export enum MODE {
JSON: 'application/json', JSON = 'application/json',
html: 'htmlmixed', HTML = 'htmlmixed',
js: 'javascript', JS = 'javascript',
}; }

View File

@ -15,7 +15,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent, ref, unref, h } from 'vue'; 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 { PageWrapper } from '/@/components/Page';
import { Radio, Space, Modal } from 'ant-design-vue'; import { Radio, Space, Modal } from 'ant-design-vue';
@ -62,20 +62,20 @@
ASpace: Space, ASpace: Space,
}, },
setup() { setup() {
const modeValue = ref('application/json'); const modeValue = ref<MODE>(MODE.JSON);
const value = ref(jsonData); const value = ref(jsonData);
function handleModeChange(e: ChangeEvent) { function handleModeChange(e: ChangeEvent) {
const mode = e.target.value; const mode = e.target.value;
if (mode === 'application/json') { if (mode === MODE.JSON) {
value.value = jsonData; value.value = jsonData;
return; return;
} }
if (mode === 'htmlmixed') { if (mode === MODE.HTML) {
value.value = htmlData; value.value = htmlData;
return; return;
} }
if (mode === 'javascript') { if (mode === MODE.JS) {
value.value = jsData; value.value = jsData;
return; return;
} }