fix(code-editor): fixed formatting error

修复JSON编辑器在格式化无效JSON文本时会抛出异常的问题
This commit is contained in:
无木 2021-08-18 20:52:34 +08:00
parent 93812f734e
commit e7c96363a1
2 changed files with 15 additions and 6 deletions

View File

@ -4,6 +4,7 @@
### 🐛 Bug Fixes ### 🐛 Bug Fixes
- **CodeEditor** 修复 JSON 编辑器在格式化无效 JSON 文本时会抛出异常的问题
- **其它** - **其它**
- 修复部分封装组件在使用插槽时报错的问题 - 修复部分封装组件在使用插槽时报错的问题
- 修复`useECharts`的`theme`参数不起作用的问题 - 修复`useECharts`的`theme`参数不起作用的问题

View File

@ -25,18 +25,26 @@
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, default: MODE.JSON },
readonly: { type: Boolean }, readonly: { type: Boolean },
autoFormat: { type: Boolean, default: true },
}); });
const emit = defineEmits(['change', 'update:value']); const emit = defineEmits(['change', 'update:value', 'format-error']);
const getValue = computed(() => { const getValue = computed(() => {
const { value, mode } = props; const { value, mode, autoFormat } = props;
if (mode !== MODE.JSON) { if (!autoFormat || mode !== MODE.JSON) {
return value as string; return value as string;
} }
return isString(value) let result = value;
? JSON.stringify(JSON.parse(value), null, 2) if (isString(value)) {
: JSON.stringify(value, null, 2); try {
result = JSON.parse(value);
} catch (e) {
emit('format-error', value);
return value as string;
}
}
return JSON.stringify(result, null, 2);
}); });
function handleValueChange(v) { function handleValueChange(v) {