-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex12.html
117 lines (110 loc) · 4.68 KB
/
index12.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>monaco-editor basic use</title>
<script src="node_modules/monaco-editor/min/vs/loader.js"></script>
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<style>
body { background-color: pink; }
</style>
</head>
<body>
<div id="container" style="width:100%;height:100%; position:absolute;top:0;right:0;left:0;bottom:0;"></div>
<!--<button onclick="GetEditorValue();">获取编辑器值</button>
<button onclick="ReCreateEditor();">重新创建编辑器</button>-->
<script>
var monacoEditor;
//init monaco
require.config({
paths: { 'vs': 'node_modules/monaco-editor/min/vs' }, 'vs/nls': {
availableLanguages: {
'*': 'zh-cn'
}
}
});
function createDependencyProposals() {
// returning a static list of proposals, not even looking at the prefix (filtering is done by the Monaco editor),
// here you could do a server side lookup
return [{
label: 'lodash',
kind: monaco.languages.CompletionItemKind.Function,
documentation: "lodash 参数1 参数2 参数3 参数4 参数5",
insertText: 'lodash 15 15 15 15'
}, {
label: 'simpleText',
kind: monaco.languages.CompletionItemKind.Text,
insertText: 'simpleText'
}, {
label: 'testing',
kind: monaco.languages.CompletionItemKind.Keyword,
insertText: 'testing(${1:condition})',
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
}, {
label: 'ifelse',
kind: monaco.languages.CompletionItemKind.Snippet,
insertText: [
'if (${1:condition}) {',
'\t$0',
'} else {',
'\t',
'}'
].join('\n'),
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
documentation: 'If-Else Statement'
}
];
}
function SetEditorValue(obj) {
$("#container").children().remove();
require(['vs/editor/editor.main'], function () {
// Register a new language
monaco.languages.register({ id: 'mySpecialLanguage' });
// Register a tokens provider for the language
monaco.languages.setMonarchTokensProvider('mySpecialLanguage', {
tokenizer: {
root: [
[/lodash*/, "custom-error"],
[/\[notice.*/, "custom-notice"],
[/\[info.*/, "custom-info"],
[/\[[a-zA-Z 0-9:]+\]/, "custom-date"],
]
}
});
// Define a new theme that contains only rules that match this language
monaco.editor.defineTheme('myCoolTheme', {
base: 'vs',
inherit: false,
rules: [
{ token: 'custom-info', foreground: '808080' },
{ token: 'custom-error', foreground: 'ff0000', fontStyle: 'bold' },
{ token: 'custom-notice', foreground: 'FFA500' },
{ token: 'custom-date', foreground: '008800' },
]
});
// Register a completion item provider for the new language
monaco.languages.registerCompletionItemProvider('mySpecialLanguage', {
provideCompletionItems: function (model, position) {
return {
suggestions: createDependencyProposals()
};
}
});
monacoEditor = monaco.editor.create(document.getElementById('container'), {
theme: 'myCoolTheme',
value: obj,
language: 'mySpecialLanguage',
wordWrap: "on", //自动换行,注意大小写
wrappingIndent: "indent"
});
});
}
function GetEditorValue() {
return monacoEditor.getValue();
}
SetEditorValue("请依次输入lodash simpleText testing ifelse");
</script>
</body>
</html>