Skip to content

Commit

Permalink
fix: optimize code editor
Browse files Browse the repository at this point in the history
  • Loading branch information
suyuan32 committed Dec 4, 2023
1 parent 5e272e2 commit 84d9a3d
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 727 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@
"@ant-design/icons-vue": "^6.1.0",
"@axolo/tree-array": "^0.1.0",
"@codemirror/lang-json": "^6.0.1",
"@codemirror/language": "^6.3.1",
"@codemirror/legacy-modes": "^6.3.1",
"@iconify/iconify": "^3.1.1",
"@logicflow/core": "^1.2.18",
"@logicflow/extension": "^1.2.19",
"@uiw/codemirror-theme-github": "^4.21.21",
"@uponu/vuedraggable": "^4.1.3",
"@vben/hooks": "workspace:*",
"@vue/shared": "^3.3.8",
Expand Down Expand Up @@ -102,7 +105,7 @@
"vditor": "^3.9.6",
"vue": "^3.3.8",
"vue-clipboard3": "^2.0.0",
"vue-codemirror6": "^1.1.30",
"vue-codemirror": "^6.1.1",
"vue-i18n": "^9.7.0",
"vue-json-pretty": "^2.2.4",
"vue-router": "^4.2.5",
Expand Down
71 changes: 51 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/components/CodeEditor/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { withInstall } from '/@/utils';
import codeEditor from './src/CodeEditor.vue';
import jsonPreview from './src/json-preview/JsonPreview.vue';
import { MODE } from './src/typing';

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

export * from './src/typing';
export const LangMode = MODE;
64 changes: 39 additions & 25 deletions src/components/CodeEditor/src/CodeEditor.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
<template>
<div class="h-full">
<CodeMirrorEditor
:value="getValue"
<Codemirror
v-model:model-value="inputValue"
@change="handleValueChange"
:mode="mode"
:readonly="readonly"
:bordered="bordered"
:extensions="extensions"
:autofocus="true"
:indent-with-tab="true"
:tab-size="2"
/>
</div>
</template>
<script lang="ts" setup>
import { computed } from 'vue';
import CodeMirrorEditor from './codemirror/CodeMirror.vue';
import { isString } from '/@/utils/is';
import { PropType, ref } from 'vue';
import { Codemirror } from 'vue-codemirror';
import { MODE } from './typing';
// import isString from 'lodash-es/isString';
import { useAppStore } from '/@/store/modules/app';
import { githubLight, githubDark } from '@uiw/codemirror-theme-github';
import { StreamLanguage } from '@codemirror/language';
import { yaml } from '@codemirror/legacy-modes/mode/yaml';
import { json } from '@codemirror/lang-json';
const props = defineProps({
value: { type: [Object, String] as PropType<Record<string, any> | string> },
Expand All @@ -25,31 +31,39 @@
return Object.values(MODE).includes(value);
},
},
readonly: { type: Boolean },
autoFormat: { type: Boolean, default: true },
bordered: { type: Boolean, default: false },
});
const inputValue = ref<string>('');
inputValue.value = props.value as string;
const emit = defineEmits(['change', 'update:value', 'format-error']);
const getValue = computed(() => {
const { value, mode, autoFormat } = props;
if (!autoFormat || mode !== MODE.JSON) {
return value as string;
const appStore = useAppStore();
const darkMode = appStore.getDarkMode;
let extensions: any = [];
if (darkMode === 'dark') {
extensions.push(githubDark);
} else {
extensions.push(githubLight);
}
switch (props.mode) {
case MODE.YAML: {
extensions.push(StreamLanguage.define(yaml));
break;
}
let result = value;
if (isString(value)) {
try {
result = JSON.parse(value);
} catch (e) {
emit('format-error', value);
return value as string;
}
case MODE.JSON: {
extensions.push(json());
}
return JSON.stringify(result, null, 2);
});
}
function handleValueChange(v) {
function handleValueChange(v: string) {
if (props.mode == MODE.JSON) {
v = v.replace(/(\r\n|\n|\r)/gm, '');
}
emit('update:value', v);
emit('change', v);
}
Expand Down
128 changes: 0 additions & 128 deletions src/components/CodeEditor/src/codemirror/CodeMirror.vue

This file was deleted.

Loading

0 comments on commit 84d9a3d

Please sign in to comment.