|
|
@ -1,10 +1,9 @@ |
|
|
|
<script setup lang="ts"> |
|
|
|
<script setup lang="ts"> |
|
|
|
import * as monaco from 'monaco-editor' |
|
|
|
|
|
|
|
import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker' |
|
|
|
import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker' |
|
|
|
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker' |
|
|
|
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker' |
|
|
|
import TypescriptWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker' |
|
|
|
import TypescriptWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker' |
|
|
|
import { onMounted } from '#imports' |
|
|
|
import type { editor as MonacoEditor } from 'monaco-editor' |
|
|
|
import { deepCompare } from '~/utils' |
|
|
|
import { deepCompare, onMounted, ref, watch } from '#imports' |
|
|
|
|
|
|
|
|
|
|
|
interface Props { |
|
|
|
interface Props { |
|
|
|
modelValue: string | Record<string, any> |
|
|
|
modelValue: string | Record<string, any> |
|
|
@ -60,7 +59,8 @@ self.MonacoEnvironment = { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const root = ref<HTMLDivElement>() |
|
|
|
const root = ref<HTMLDivElement>() |
|
|
|
let editor: monaco.editor.IStandaloneCodeEditor |
|
|
|
|
|
|
|
|
|
|
|
let editor: MonacoEditor.IStandaloneCodeEditor |
|
|
|
|
|
|
|
|
|
|
|
const format = () => { |
|
|
|
const format = () => { |
|
|
|
editor.setValue(JSON.stringify(JSON.parse(editor?.getValue() as string), null, 2)) |
|
|
|
editor.setValue(JSON.stringify(JSON.parse(editor?.getValue() as string), null, 2)) |
|
|
@ -71,18 +71,20 @@ defineExpose({ |
|
|
|
isValid, |
|
|
|
isValid, |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
onMounted(() => { |
|
|
|
onMounted(async () => { |
|
|
|
|
|
|
|
const { editor: monacoEditor, languages } = await import('monaco-editor') |
|
|
|
|
|
|
|
|
|
|
|
if (root.value && lang) { |
|
|
|
if (root.value && lang) { |
|
|
|
const model = monaco.editor.createModel(vModel, lang) |
|
|
|
const model = monacoEditor.createModel(vModel, lang) |
|
|
|
|
|
|
|
|
|
|
|
if (lang === 'json') { |
|
|
|
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({ |
|
|
|
languages.json.jsonDefaults.setDiagnosticsOptions({ |
|
|
|
validate: validate as boolean, |
|
|
|
validate: validate as boolean, |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
editor = monaco.editor.create(root.value, { |
|
|
|
editor = monacoEditor.create(root.value, { |
|
|
|
model, |
|
|
|
model, |
|
|
|
theme: 'vs', |
|
|
|
theme: 'vs', |
|
|
|
foldingStrategy: 'indentation', |
|
|
|
foldingStrategy: 'indentation', |
|
|
@ -107,6 +109,7 @@ onMounted(() => { |
|
|
|
vModel = editor.getValue() |
|
|
|
vModel = editor.getValue() |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
const obj = JSON.parse(editor.getValue()) |
|
|
|
const obj = JSON.parse(editor.getValue()) |
|
|
|
|
|
|
|
|
|
|
|
if (!obj || !deepCompare(vModel, obj)) vModel = obj |
|
|
|
if (!obj || !deepCompare(vModel, obj)) vModel = obj |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|