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.
137 lines
3.2 KiB
137 lines
3.2 KiB
2 years ago
|
<script setup lang="ts">
|
||
|
import * as monaco from 'monaco-editor'
|
||
2 years ago
|
import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'
|
||
|
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'
|
||
2 years ago
|
import TypescriptWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker'
|
||
2 years ago
|
import { onMounted } from '#imports'
|
||
2 years ago
|
import { deepCompare } from '~/utils'
|
||
2 years ago
|
|
||
2 years ago
|
interface Props {
|
||
2 years ago
|
modelValue: string | Record<string, any>
|
||
2 years ago
|
hideMinimap?: boolean
|
||
2 years ago
|
lang?: string
|
||
|
validate?: boolean
|
||
|
disableDeepCompare?: boolean
|
||
2 years ago
|
readOnly?: boolean
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const { hideMinimap, lang = 'json', validate = true, disableDeepCompare = false, modelValue, readOnly } = defineProps<Props>()
|
||
2 years ago
|
|
||
2 years ago
|
const emits = defineEmits(['update:modelValue'])
|
||
2 years ago
|
|
||
2 years ago
|
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)
|
||
|
}
|
||
|
},
|
||
|
})
|
||
2 years ago
|
|
||
2 years ago
|
const isValid = ref(true)
|
||
2 years ago
|
|
||
2 years ago
|
/**
|
||
|
* Adding monaco editor to Vite
|
||
|
*
|
||
|
* @ts-expect-error */
|
||
|
self.MonacoEnvironment = {
|
||
|
getWorker(_: any, label: string) {
|
||
2 years ago
|
switch (label) {
|
||
|
case 'json':
|
||
|
return new JsonWorker()
|
||
|
case 'typescript':
|
||
|
return new TypescriptWorker()
|
||
|
default:
|
||
|
return new EditorWorker()
|
||
2 years ago
|
}
|
||
|
},
|
||
|
}
|
||
|
|
||
2 years ago
|
const root = ref<HTMLDivElement>()
|
||
2 years ago
|
let editor: monaco.editor.IStandaloneCodeEditor
|
||
2 years ago
|
|
||
2 years ago
|
const format = () => {
|
||
|
editor.setValue(JSON.stringify(JSON.parse(editor?.getValue() as string), null, 2))
|
||
|
}
|
||
|
|
||
|
defineExpose({
|
||
|
format,
|
||
2 years ago
|
isValid,
|
||
2 years ago
|
})
|
||
|
|
||
2 years ago
|
onMounted(() => {
|
||
2 years ago
|
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,
|
||
|
})
|
||
|
}
|
||
2 years ago
|
|
||
|
editor = monaco.editor.create(root.value, {
|
||
|
model,
|
||
2 years ago
|
theme: 'vs',
|
||
|
foldingStrategy: 'indentation',
|
||
|
selectOnLineNumbers: true,
|
||
|
scrollbar: {
|
||
|
verticalScrollbarSize: 8,
|
||
|
horizontalScrollbarSize: 8,
|
||
|
},
|
||
|
tabSize: 2,
|
||
|
automaticLayout: true,
|
||
2 years ago
|
readOnly,
|
||
2 years ago
|
minimap: {
|
||
|
enabled: !hideMinimap,
|
||
|
},
|
||
2 years ago
|
})
|
||
|
|
||
2 years ago
|
editor.onDidChangeModelContent(async () => {
|
||
2 years ago
|
try {
|
||
2 years ago
|
isValid.value = true
|
||
2 years ago
|
|
||
|
if (disableDeepCompare) {
|
||
|
vModel = editor.getValue()
|
||
|
} else {
|
||
|
const obj = JSON.parse(editor.getValue())
|
||
|
if (!obj || !deepCompare(vModel, obj)) vModel = obj
|
||
|
}
|
||
2 years ago
|
} catch (e) {
|
||
2 years ago
|
isValid.value = false
|
||
2 years ago
|
console.log(e)
|
||
|
}
|
||
2 years ago
|
})
|
||
|
}
|
||
|
})
|
||
|
|
||
2 years ago
|
watch($$(vModel), (v) => {
|
||
|
if (!editor || !v) return
|
||
|
|
||
|
const editorValue = editor?.getValue()
|
||
|
if (!disableDeepCompare) {
|
||
|
if (!editorValue || !deepCompare(JSON.parse(v), JSON.parse(editorValue))) {
|
||
|
editor.setValue(v)
|
||
2 years ago
|
}
|
||
2 years ago
|
} else {
|
||
|
if (editorValue !== v) editor.setValue(v)
|
||
|
}
|
||
|
})
|
||
2 years ago
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<div ref="root"></div>
|
||
|
</template>
|