forked from aisuda/amis-editor-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadMonacoEditor.ts
128 lines (122 loc) · 8.34 KB
/
loadMonacoEditor.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
// 这是个特殊的方法,请看考 mod.js 里面的实现。
export function __mod__async__load(callback: (exports: any) => void) {
// @ts-ignore
const monacoLoader = __uri('monaco-editor/min/vs/loader.js');
// @ts-ignore
const script = amis.require.loadJs(filterUrl(monacoLoader));
script.onload = () => onLoad(window.require, callback);
}
function __uri(url: string) {
return url;
}
// 用于发布 sdk 版本的时候替换,因为不确定 sdk 版本怎么部署,而 worker 地址路径不可知。
// 所以会被 fis3 替换成取相对的代码。
function filterUrl(url: string) {
return url;
}
function onLoad(req: any, callback: (result: any) => void) {
const config = {
'vs/nls': {
availableLanguages: {
'*': 'zh-cn'
}
},
paths: {
vs: __uri('monaco-editor/min/vs/editor/editor.main.js').replace(/\/vs\/.*$/, ''),
'vs/base/worker/workerMain': __uri('monaco-editor/min/vs/base/worker/workerMain.js'),
'vs/basic-languages/apex/apex': __uri('monaco-editor/min/vs/basic-languages/apex/apex'),
'vs/basic-languages/azcli/azcli': __uri('monaco-editor/min/vs/basic-languages/azcli/azcli'),
'vs/basic-languages/clojure/clojure': __uri('monaco-editor/min/vs/basic-languages/clojure/clojure'),
'vs/basic-languages/bat/bat': __uri('monaco-editor/min/vs/basic-languages/bat/bat'),
'vs/basic-languages/coffee/coffee': __uri('monaco-editor/min/vs/basic-languages/coffee/coffee'),
'vs/basic-languages/cpp/cpp': __uri('monaco-editor/min/vs/basic-languages/cpp/cpp'),
'vs/basic-languages/csharp/csharp': __uri('monaco-editor/min/vs/basic-languages/csharp/csharp'),
'vs/basic-languages/css/css': __uri('monaco-editor/min/vs/basic-languages/css/css'),
'vs/basic-languages/dockerfile/dockerfile': __uri(
'monaco-editor/min/vs/basic-languages/dockerfile/dockerfile'
),
'vs/basic-languages/fsharp/fsharp': __uri('monaco-editor/min/vs/basic-languages/fsharp/fsharp'),
'vs/basic-languages/go/go': __uri('monaco-editor/min/vs/basic-languages/go/go'),
'vs/basic-languages/handlebars/handlebars': __uri(
'monaco-editor/min/vs/basic-languages/handlebars/handlebars'
),
'vs/basic-languages/html/html': __uri('monaco-editor/min/vs/basic-languages/html/html'),
'vs/basic-languages/ini/ini': __uri('monaco-editor/min/vs/basic-languages/ini/ini'),
'vs/basic-languages/java/java': __uri('monaco-editor/min/vs/basic-languages/java/java'),
'vs/basic-languages/javascript/javascript': __uri(
'monaco-editor/min/vs/basic-languages/javascript/javascript'
),
'vs/basic-languages/less/less': __uri('monaco-editor/min/vs/basic-languages/less/less'),
'vs/basic-languages/lua/lua': __uri('monaco-editor/min/vs/basic-languages/lua/lua'),
'vs/basic-languages/markdown/markdown': __uri('monaco-editor/min/vs/basic-languages/markdown/markdown'),
'vs/basic-languages/msdax/msdax': __uri('monaco-editor/min/vs/basic-languages/msdax/msdax'),
'vs/basic-languages/objective-c/objective-c': __uri(
'monaco-editor/min/vs/basic-languages/objective-c/objective-c'
),
'vs/basic-languages/php/php': __uri('monaco-editor/min/vs/basic-languages/php/php'),
'vs/basic-languages/postiats/postiats': __uri('monaco-editor/min/vs/basic-languages/postiats/postiats'),
'vs/basic-languages/powershell/powershell': __uri(
'monaco-editor/min/vs/basic-languages/powershell/powershell'
),
'vs/basic-languages/pug/pug': __uri('monaco-editor/min/vs/basic-languages/pug/pug'),
'vs/basic-languages/python/python': __uri('monaco-editor/min/vs/basic-languages/python/python'),
'vs/basic-languages/r/r': __uri('monaco-editor/min/vs/basic-languages/r/r'),
'vs/basic-languages/razor/razor': __uri('monaco-editor/min/vs/basic-languages/razor/razor'),
'vs/basic-languages/redis/redis': __uri('monaco-editor/min/vs/basic-languages/redis/redis'),
'vs/basic-languages/redshift/redshift': __uri('monaco-editor/min/vs/basic-languages/redshift/redshift'),
'vs/basic-languages/ruby/ruby': __uri('monaco-editor/min/vs/basic-languages/ruby/ruby'),
'vs/basic-languages/rust/rust': __uri('monaco-editor/min/vs/basic-languages/rust/rust'),
'vs/basic-languages/sb/sb': __uri('monaco-editor/min/vs/basic-languages/sb/sb'),
'vs/basic-languages/scheme/scheme': __uri('monaco-editor/min/vs/basic-languages/scheme/scheme'),
'vs/basic-languages/scss/scss': __uri('monaco-editor/min/vs/basic-languages/scss/scss'),
'vs/basic-languages/shell/shell': __uri('monaco-editor/min/vs/basic-languages/shell/shell'),
'vs/basic-languages/solidity/solidity': __uri('monaco-editor/min/vs/basic-languages/solidity/solidity'),
'vs/basic-languages/sql/sql': __uri('monaco-editor/min/vs/basic-languages/sql/sql'),
'vs/basic-languages/st/st': __uri('monaco-editor/min/vs/basic-languages/st/st'),
'vs/basic-languages/swift/swift': __uri('monaco-editor/min/vs/basic-languages/swift/swift'),
'vs/basic-languages/typescript/typescript': __uri(
'monaco-editor/min/vs/basic-languages/typescript/typescript'
),
'vs/basic-languages/vb/vb': __uri('monaco-editor/min/vs/basic-languages/vb/vb'),
'vs/basic-languages/xml/xml': __uri('monaco-editor/min/vs/basic-languages/xml/xml'),
'vs/basic-languages/yaml/yaml': __uri('monaco-editor/min/vs/basic-languages/yaml/yaml'),
'vs/editor/editor.main': __uri('monaco-editor/min/vs/editor/editor.main.js'),
'vs/editor/editor.main.css': __uri('monaco-editor/min/vs/editor/editor.main.css'),
'vs/editor/editor.main.nls': __uri('monaco-editor/min/vs/editor/editor.main.nls.js'),
'vs/editor/editor.main.nls.zh-cn': __uri('monaco-editor/min/vs/editor/editor.main.nls.zh-cn.js'),
// 'vs/editor/contrib/suggest/media/String_16x.svg': __uri('monaco-editor/min/vs/editor/contrib/suggest/media/String_16x.svg'),
// 'vs/editor/contrib/suggest/media/String_inverse_16x.svg': __uri('monaco-editor/min/vs/editor/contrib/suggest/media/String_inverse_16x.svg'),
// 'vs/editor/standalone/browser/quickOpen/symbol-sprite.svg': __uri('monaco-editor/min/vs/editor/standalone/browser/quickOpen/symbol-sprite.svg'),
'vs/language/typescript/tsMode': __uri('monaco-editor/min/vs/language/typescript/tsMode.js'),
// 'vs/language/typescript/lib/typescriptServices': __uri('monaco-editor/min/vs/language/typescript/lib/typescriptServices.js'),
'vs/language/typescript/tsWorker': __uri('monaco-editor/min/vs/language/typescript/tsWorker.js'),
'vs/language/json/jsonMode': __uri('monaco-editor/min/vs/language/json/jsonMode.js'),
'vs/language/json/jsonWorker': __uri('monaco-editor/min/vs/language/json/jsonWorker.js'),
'vs/language/html/htmlMode': __uri('monaco-editor/min/vs/language/html/htmlMode.js'),
'vs/language/html/htmlWorker': __uri('monaco-editor/min/vs/language/html/htmlWorker.js'),
'vs/language/css/cssMode': __uri('monaco-editor/min/vs/language/css/cssMode.js'),
'vs/language/css/cssWorker': __uri('monaco-editor/min/vs/language/css/cssWorker.js')
}
};
Object.keys(config.paths).forEach((key: keyof typeof config.paths) => {
config.paths[key] = filterUrl(config.paths[key].replace(/\.js$/, ''));
});
req.config(config);
if (/^(https?:)?\/\//.test(config.paths.vs)) {
(window as any).MonacoEnvironment = {
getWorkerUrl: function () {
return `data:text/javascript;charset=utf-8,${encodeURIComponent(`
self.MonacoEnvironment = {
baseUrl: '${config.paths.vs}',
paths: ${JSON.stringify(config.paths)}
};
importScripts('${__uri('monaco-editor/min/vs/base/worker/workerMain.js')}');`)}`;
}
};
} else {
delete (window as any).MonacoEnvironment;
}
req(['vs/editor/editor.main'], function (ret: any) {
callback(ret);
});
}