|
| 1 | +<script setup lang="ts"> |
| 2 | +import * as monacoEditor from 'monaco-editor'; |
| 3 | +import type { MonacoEditor } from '@guolao/vue-monaco-editor'; |
| 4 | +import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'; |
| 5 | +import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'; |
| 6 | +import CssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker'; |
| 7 | +import HtmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker'; |
| 8 | +import TsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker'; |
| 9 | +import { useStyleStore } from '@/stores/style.store'; |
| 10 | +
|
| 11 | +const props = withDefaults(defineProps<EditorProps>(), { |
| 12 | + theme: 'vs', |
| 13 | + options: () => ({}), |
| 14 | + overrideServices: () => ({}), |
| 15 | + saveViewState: true, |
| 16 | + width: '100%', |
| 17 | + height: '100%', |
| 18 | +}); |
| 19 | +
|
| 20 | +const emits = defineEmits<{ |
| 21 | + (e: 'update:value', value: string | undefined): void |
| 22 | + (e: 'beforeMount', monaco: MonacoEditor): void |
| 23 | + (e: 'mount', editor: monacoEditor.editor.IStandaloneCodeEditor, monaco: MonacoEditor): void |
| 24 | + (e: 'change', value: string | undefined, event: monacoEditor.editor.IModelContentChangedEvent): void |
| 25 | + (e: 'validate', markers: monacoEditor.editor.IMarker[]): void |
| 26 | +}>(); |
| 27 | +
|
| 28 | +interface MonacoEnvironment { |
| 29 | + getWorker(_: any, label: string): Worker |
| 30 | +} |
| 31 | +// eslint-disable-next-line @typescript-eslint/no-namespace |
| 32 | +declare module globalThis { |
| 33 | + let MonacoEnvironment: MonacoEnvironment; |
| 34 | +} |
| 35 | +
|
| 36 | +const value = useVModel(props, 'value', emits); |
| 37 | +
|
| 38 | +globalThis.MonacoEnvironment = { |
| 39 | + getWorker(_: any, label: string) { |
| 40 | + if (label === 'json') { |
| 41 | + return new JsonWorker(); |
| 42 | + } |
| 43 | + if (label === 'css' || label === 'scss' || label === 'less') { |
| 44 | + return new CssWorker(); |
| 45 | + } |
| 46 | + if (label === 'html' || label === 'handlebars' || label === 'razor') { |
| 47 | + return new HtmlWorker(); |
| 48 | + } |
| 49 | + if (label === 'typescript' || label === 'javascript') { |
| 50 | + return new TsWorker(); |
| 51 | + } |
| 52 | + return new EditorWorker(); |
| 53 | + }, |
| 54 | +}; |
| 55 | +
|
| 56 | +export interface EditorProps { |
| 57 | + defaultValue?: string |
| 58 | + defaultPath?: string |
| 59 | + defaultLanguage?: string |
| 60 | + value?: string |
| 61 | + language?: string |
| 62 | + path?: string |
| 63 | +
|
| 64 | + /* === */ |
| 65 | +
|
| 66 | + theme: 'vs' | string |
| 67 | + line?: number |
| 68 | + options?: monacoEditor.editor.IStandaloneEditorConstructionOptions |
| 69 | + overrideServices?: monacoEditor.editor.IEditorOverrideServices |
| 70 | + saveViewState?: boolean |
| 71 | +
|
| 72 | + /* === */ |
| 73 | +
|
| 74 | + width?: number | string |
| 75 | + height?: number | string |
| 76 | + className?: string |
| 77 | +} |
| 78 | +
|
| 79 | +monacoEditor.editor.defineTheme('it-tools-dark', { |
| 80 | + base: 'vs-dark', |
| 81 | + inherit: true, |
| 82 | + rules: [], |
| 83 | + colors: { |
| 84 | + 'editor.background': '#00000000', |
| 85 | + }, |
| 86 | +}); |
| 87 | +
|
| 88 | +monacoEditor.editor.defineTheme('it-tools-light', { |
| 89 | + base: 'vs', |
| 90 | + inherit: true, |
| 91 | + rules: [], |
| 92 | + colors: { |
| 93 | + 'editor.background': '#00000000', |
| 94 | + }, |
| 95 | +}); |
| 96 | +
|
| 97 | +const styleStore = useStyleStore(); |
| 98 | +
|
| 99 | +watch( |
| 100 | + () => styleStore.isDarkTheme, |
| 101 | + isDarkTheme => monacoEditor.editor.setTheme(isDarkTheme ? 'it-tools-dark' : 'it-tools-light'), |
| 102 | + { immediate: true }, |
| 103 | +); |
| 104 | +
|
| 105 | +const attrs = useAttrs(); |
| 106 | +const inheritedAttrs = { ...attrs, ...props }; |
| 107 | +</script> |
| 108 | + |
| 109 | +<script lang="ts"> |
| 110 | +export default { |
| 111 | + inheritAttrs: false, |
| 112 | +}; |
| 113 | +</script> |
| 114 | + |
| 115 | +<template> |
| 116 | + <vue-monaco-editor |
| 117 | + v-bind="inheritedAttrs" |
| 118 | + v-model:value="value" |
| 119 | + @before-mount="(monaco: MonacoEditor) => emits('beforeMount', monaco)" |
| 120 | + @mount="(editor: monacoEditor.editor.IStandaloneCodeEditor, monaco: MonacoEditor) => emits('mount', editor, monaco)" |
| 121 | + @change="(value: string | undefined, event: monacoEditor.editor.IModelContentChangedEvent) => emits('change', value, event)" |
| 122 | + @validate="(markers: monacoEditor.editor.IMarker[]) => emits('validate', markers)" |
| 123 | + /> |
| 124 | +</template> |
0 commit comments