Browse Source

refactor(nc-gui): lazy load monaco editor

pull/3801/head
braks 2 years ago
parent
commit
344d6d71dd
  1. 6
      packages/nc-gui/components/cell/Json.vue
  2. 19
      packages/nc-gui/components/monaco/Editor.vue

6
packages/nc-gui/components/cell/Json.vue

@ -1,8 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { Modal as AModal } from 'ant-design-vue' import { Modal as AModal } from 'ant-design-vue'
import Editor from '~/components/monaco/Editor.vue' import { EditModeInj, IsFormInj, ReadonlyInj, computed, inject, ref, useVModel, watch } from '#imports'
import { ReadonlyInj, computed, inject, ref, useVModel, watch } from '#imports'
import { EditModeInj, IsFormInj } from '~/context'
interface Props { interface Props {
modelValue: string | Record<string, any> | undefined modelValue: string | Record<string, any> | undefined
@ -113,7 +111,7 @@ watch(editEnabled, () => {
</div> </div>
</div> </div>
<Editor <LazyMonacoEditor
:model-value="localValue" :model-value="localValue"
class="min-w-full w-80" class="min-w-full w-80"
:class="{ 'expanded-editor': isExpanded, 'editor': !isExpanded }" :class="{ 'expanded-editor': isExpanded, 'editor': !isExpanded }"

19
packages/nc-gui/components/monaco/Editor.vue

@ -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) {

Loading…
Cancel
Save