mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.3 KiB
53 lines
1.3 KiB
2 years ago
|
<script setup lang="ts">
|
||
|
import * as monaco from 'monaco-editor'
|
||
|
import { onMounted } from '#imports'
|
||
2 years ago
|
import { deepCompare } from '~/utils/deepCompare'
|
||
2 years ago
|
|
||
|
const { modelValue } = defineProps<{ modelValue: any }>()
|
||
|
const emit = defineEmits(['update:modelValue'])
|
||
|
|
||
|
const root = ref<HTMLDivElement>()
|
||
2 years ago
|
let editor: monaco.editor.IStandaloneCodeEditor
|
||
2 years ago
|
|
||
|
onMounted(() => {
|
||
|
if (root.value) {
|
||
2 years ago
|
const model = monaco.editor.createModel(JSON.stringify(modelValue, null, 2), 'json')
|
||
|
|
||
|
// configure the JSON language support with schemas and schema associations
|
||
|
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
|
||
|
validate: true,
|
||
|
})
|
||
|
|
||
|
editor = monaco.editor.create(root.value, {
|
||
|
model,
|
||
|
theme: 'dark',
|
||
2 years ago
|
})
|
||
|
|
||
2 years ago
|
editor.onDidChangeModelContent(async (e) => {
|
||
2 years ago
|
try {
|
||
2 years ago
|
// console.log(e)
|
||
2 years ago
|
const obj = JSON.parse(editor.getValue())
|
||
|
if (!deepCompare(modelValue, obj)) emit('update:modelValue', obj)
|
||
2 years ago
|
} catch (e) {
|
||
|
console.log(e)
|
||
|
}
|
||
2 years ago
|
})
|
||
|
}
|
||
|
})
|
||
|
|
||
2 years ago
|
watch(
|
||
|
() => modelValue,
|
||
|
(v) => {
|
||
|
if (editor && v && !deepCompare(v, JSON.parse(editor?.getValue() as string))) {
|
||
|
editor.setValue(JSON.stringify(v, null, 2))
|
||
|
}
|
||
|
},
|
||
|
)
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<div ref="root"></div>
|
||
|
</template>
|
||
|
|
||
|
<style scoped></style>
|