-
Notifications
You must be signed in to change notification settings - Fork 154
/
monaco.ts
130 lines (113 loc) · 3.63 KB
/
monaco.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { watch, Ref, unref, ref, watchEffect } from 'vue3'
import type { editor as Editor } from 'monaco-editor'
import * as monaco from 'monaco-editor'
import { createSingletonPromise } from '@antfu/utils'
import { isDark } from '../composables/dark'
const setupMonaco = createSingletonPromise(async () => {
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
...monaco.languages.typescript.typescriptDefaults.getCompilerOptions(),
noUnusedLocals: false,
noUnusedParameters: false,
allowUnreachableCode: true,
allowUnusedLabels: true,
strict: true
})
await Promise.all([
// load workers
(async () => {
const [
{ default: EditorWorker },
{ default: JsonWorker },
{ default: CssWorker },
{ default: HtmlWorker },
{ default: TsWorker }
] = await Promise.all([
// @ts-expect-error
import('monaco-editor/esm/vs/editor/editor.worker?worker'),
// @ts-expect-error
import('monaco-editor/esm/vs/language/json/json.worker?worker'),
// @ts-expect-error
import('monaco-editor/esm/vs/language/css/css.worker?worker'),
// @ts-expect-error
import('monaco-editor/esm/vs/language/html/html.worker?worker'),
// @ts-expect-error
import('monaco-editor/esm/vs/language/typescript/ts.worker?worker')
])
// @ts-expect-error
window.MonacoEnvironment = {
getWorker(_: any, label: string) {
if (label === 'json') return new JsonWorker()
if (label === 'css' || label === 'scss' || label === 'less') return new CssWorker()
if (label === 'html' || label === 'handlebars' || label === 'razor' || label === 'vue')
return new HtmlWorker()
if (label === 'typescript' || label === 'javascript') return new TsWorker()
return new EditorWorker()
}
}
watchEffect(() => {
monaco.editor.setTheme(isDark.value ? 'vs-dark' : 'vs')
})
})()
])
return { monaco }
})
export function useMonaco(
target: Ref,
options: { code: string; language: string; onChanged?: (content: string) => void }
) {
const isSetup = ref(false)
let editor: Editor.IStandaloneCodeEditor
const setContent = (content: string) => {
if (!isSetup.value) return
if (editor) editor.setValue(content)
}
const init = async () => {
const { monaco } = await setupMonaco()
watch(
target,
() => {
const el = unref(target)
if (!el) return
const extension = () => {
if (options.language === 'typescript') return 'ts'
else if (options.language === 'javascript') return 'js'
else if (options.language === 'html') return 'html'
}
const model = monaco.editor.createModel(
options.code,
options.language,
monaco.Uri.parse(`file:///root/${Date.now()}.${extension()}`)
)
editor = monaco.editor.create(el, {
model,
tabSize: 2,
wordWrap: 'on',
insertSpaces: true,
autoClosingQuotes: 'always',
detectIndentation: false,
folding: false,
glyphMargin: false,
lineNumbersMinChars: 3,
overviewRulerLanes: 0,
automaticLayout: true,
theme: isDark.value ? 'vs-dark' : 'vs',
minimap: {
enabled: false
}
})
isSetup.value = true
editor.getModel()?.onDidChangeContent(() => {
options.onChanged?.(editor.getValue())
})
},
{
flush: 'post',
immediate: true
}
)
}
init()
return {
setContent
}
}