diff --git a/packages/nc-gui-v2/components/cell/Json.vue b/packages/nc-gui-v2/components/cell/Json.vue index fad7d4248e..17b64deea7 100644 --- a/packages/nc-gui-v2/components/cell/Json.vue +++ b/packages/nc-gui-v2/components/cell/Json.vue @@ -7,11 +7,11 @@ import { inject, onMounted } from '#imports' import { EditModeInj } from '~/context' interface Props { - modelValue: any + modelValue: string } interface Emits { - (event: 'update:modelValue', model: any): void + (event: 'update:modelValue', model: string): void } const props = defineProps() @@ -22,40 +22,44 @@ let editEnabled = $(inject(EditModeInj)) let vModel = $(useVModel(props, 'modelValue', emits)) -let localValue = $ref({}) +let localValue = $ref('{}') let error = $ref(undefined) let isExpanded = $ref(false) -const onError = (e: any) => { - error = e -} - const clear = () => { error = undefined isExpanded = false editEnabled = false - localValue = JSON.parse(vModel) + localValue = vModel } const onSave = () => { - vModel = JSON.stringify(localValue) + vModel = localValue isExpanded = false } -const resetError = () => { - error = undefined -} - onMounted(() => { - localValue = JSON.parse(vModel) + localValue = vModel }) +watch( + () => localValue, + (val) => { + try { + JSON.parse(val) + error = undefined + } catch (e: any) { + error = e + } + }, +) + watch( () => editEnabled, () => { isExpanded = false - localValue = JSON.parse(vModel) + localValue = vModel }, ) @@ -70,7 +74,9 @@ watch(
Cancel
-
Save
+
Save
{{ error.toString() }} diff --git a/packages/nc-gui-v2/components/monaco/Editor.vue b/packages/nc-gui-v2/components/monaco/Editor.vue index bbf99fabbb..d6c5a912e8 100644 --- a/packages/nc-gui-v2/components/monaco/Editor.vue +++ b/packages/nc-gui-v2/components/monaco/Editor.vue @@ -7,22 +7,37 @@ import { onMounted } from '#imports' import { deepCompare } from '~/utils' interface Props { - modelValue: any + modelValue: string | Record hideMinimap?: boolean + lang?: string + validate?: boolean + disableDeepCompare?: boolean } -interface Emits { - (event: 'update:modelValue', model: any): void - (event: 'validationError', error: any): void - (event: 'textChanged'): void -} - -const props = defineProps() -const emits = defineEmits() +const { hideMinimap, lang = 'json', validate = true, disableDeepCompare = false, modelValue } = defineProps() -const { hideMinimap } = props +const emits = defineEmits(['update:modelValue']) -let vModel = $(useVModel(props, 'modelValue', emits)) +let vModel = $computed({ + get: () => { + if (typeof modelValue === 'object') { + return JSON.stringify(modelValue, null, 2) + } else { + return modelValue + } + }, + set: (newVal: string | Record) => { + if (typeof modelValue === 'object') { + try { + emits('update:modelValue', typeof newVal === 'object' ? newVal : JSON.parse(newVal)) + } catch (e) { + console.error(e) + } + } else { + emits('update:modelValue', newVal) + } + }, +}) const isValid = ref(true) @@ -56,13 +71,15 @@ defineExpose({ }) onMounted(() => { - if (root.value) { - const model = monaco.editor.createModel(JSON.stringify(vModel, null, 2), 'json') - - // configure the JSON language support with schemas and schema associations - monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ - validate: true, - }) + if (root.value && lang) { + const model = monaco.editor.createModel(vModel, lang) + + if (lang === 'json') { + // configure the JSON language support with schemas and schema associations + monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ + validate: validate as boolean, + }) + } editor = monaco.editor.create(root.value, { model, @@ -80,15 +97,18 @@ onMounted(() => { }, }) - editor.onDidChangeModelContent(async (e) => { + editor.onDidChangeModelContent(async () => { try { isValid.value = true - const obj = JSON.parse(editor.getValue()) - emits('textChanged') - if (!deepCompare(vModel, obj)) vModel = obj + + if (disableDeepCompare) { + vModel = editor.getValue() + } else { + const obj = JSON.parse(editor.getValue()) + if (!obj || !deepCompare(vModel, obj)) vModel = obj + } } catch (e) { isValid.value = false - emits('validationError', e) console.log(e) } }) @@ -98,17 +118,15 @@ onMounted(() => { watch( () => vModel, (v) => { - if (!editor || !v) { - return - } + if (!editor || !v) return - try { - if (!deepCompare(v, JSON.parse(editor?.getValue() as string))) { - editor.setValue(JSON.stringify(v, null, 2)) + const editorValue = editor?.getValue() + if (!disableDeepCompare) { + if (!editorValue || !deepCompare(JSON.parse(v), JSON.parse(editorValue))) { + editor.setValue(v) } - } catch (e) { - console.log(e) - editor.setValue(JSON.stringify(v, null, 2)) + } else { + if (editorValue !== v) editor.setValue(v) } }, )