|
|
@ -7,22 +7,37 @@ import { onMounted } from '#imports' |
|
|
|
import { deepCompare } from '~/utils' |
|
|
|
import { deepCompare } from '~/utils' |
|
|
|
|
|
|
|
|
|
|
|
interface Props { |
|
|
|
interface Props { |
|
|
|
modelValue: any |
|
|
|
modelValue: string | Record<string, any> |
|
|
|
hideMinimap?: boolean |
|
|
|
hideMinimap?: boolean |
|
|
|
|
|
|
|
lang?: string |
|
|
|
|
|
|
|
validate?: boolean |
|
|
|
|
|
|
|
disableDeepCompare?: boolean |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
interface Emits { |
|
|
|
const { hideMinimap, lang = 'json', validate = true, disableDeepCompare = false, modelValue } = defineProps<Props>() |
|
|
|
(event: 'update:modelValue', model: any): void |
|
|
|
|
|
|
|
(event: 'validationError', error: any): void |
|
|
|
|
|
|
|
(event: 'textChanged'): void |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const props = defineProps<Props>() |
|
|
|
|
|
|
|
const emits = defineEmits<Emits>() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { hideMinimap } = props |
|
|
|
const emits = defineEmits(['update:modelValue']) |
|
|
|
|
|
|
|
|
|
|
|
let vModel = $(useVModel(props, 'modelValue', emits)) |
|
|
|
let vModel = $computed<string>({ |
|
|
|
|
|
|
|
get: () => { |
|
|
|
|
|
|
|
if (typeof modelValue === 'object') { |
|
|
|
|
|
|
|
return JSON.stringify(modelValue, null, 2) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return modelValue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
set: (newVal: string | Record<string, any>) => { |
|
|
|
|
|
|
|
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) |
|
|
|
const isValid = ref(true) |
|
|
|
|
|
|
|
|
|
|
@ -56,13 +71,15 @@ defineExpose({ |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
onMounted(() => { |
|
|
|
onMounted(() => { |
|
|
|
if (root.value) { |
|
|
|
if (root.value && lang) { |
|
|
|
const model = monaco.editor.createModel(JSON.stringify(vModel, null, 2), 'json') |
|
|
|
const model = monaco.editor.createModel(vModel, lang) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (lang === 'json') { |
|
|
|
// configure the JSON language support with schemas and schema associations |
|
|
|
// configure the JSON language support with schemas and schema associations |
|
|
|
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ |
|
|
|
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ |
|
|
|
validate: true, |
|
|
|
validate: validate as boolean, |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
editor = monaco.editor.create(root.value, { |
|
|
|
editor = monaco.editor.create(root.value, { |
|
|
|
model, |
|
|
|
model, |
|
|
@ -80,15 +97,18 @@ onMounted(() => { |
|
|
|
}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
editor.onDidChangeModelContent(async (e) => { |
|
|
|
editor.onDidChangeModelContent(async () => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
isValid.value = true |
|
|
|
isValid.value = true |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (disableDeepCompare) { |
|
|
|
|
|
|
|
vModel = editor.getValue() |
|
|
|
|
|
|
|
} else { |
|
|
|
const obj = JSON.parse(editor.getValue()) |
|
|
|
const obj = JSON.parse(editor.getValue()) |
|
|
|
emits('textChanged') |
|
|
|
if (!obj || !deepCompare(vModel, obj)) vModel = obj |
|
|
|
if (!deepCompare(vModel, obj)) vModel = obj |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
isValid.value = false |
|
|
|
isValid.value = false |
|
|
|
emits('validationError', e) |
|
|
|
|
|
|
|
console.log(e) |
|
|
|
console.log(e) |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
@ -98,17 +118,15 @@ onMounted(() => { |
|
|
|
watch( |
|
|
|
watch( |
|
|
|
() => vModel, |
|
|
|
() => vModel, |
|
|
|
(v) => { |
|
|
|
(v) => { |
|
|
|
if (!editor || !v) { |
|
|
|
if (!editor || !v) return |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
const editorValue = editor?.getValue() |
|
|
|
if (!deepCompare(v, JSON.parse(editor?.getValue() as string))) { |
|
|
|
if (!disableDeepCompare) { |
|
|
|
editor.setValue(JSON.stringify(v, null, 2)) |
|
|
|
if (!editorValue || !deepCompare(JSON.parse(v), JSON.parse(editorValue))) { |
|
|
|
|
|
|
|
editor.setValue(v) |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
} else { |
|
|
|
console.log(e) |
|
|
|
if (editorValue !== v) editor.setValue(v) |
|
|
|
editor.setValue(JSON.stringify(v, null, 2)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
) |
|
|
|
) |
|
|
|