-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathMonacoEditor.svelte
267 lines (249 loc) · 6 KB
/
MonacoEditor.svelte
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<script module>
const appStarting = new Promise((resolve) => setTimeout(resolve, 300));
</script>
<script>
import { onDestroy, onMount, createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
import { loadMonacoEditor } from './scripts/monaco-loader';
export let code = '';
export let rightCode = '';
export let language = 'javascript';
export let readOnly = false;
export let diffEditor = false;
export let markers = [];
export let rightMarkers = [];
export let provideCodeActions = null;
export let editorOptions = {};
export let waiting = null;
let rootElement,
editor,
setLeftValue,
setRightValue,
setLeftMarkers,
setRightMarkers,
getLeftEditor,
codeActionProviderDisposable;
const loadingMonaco = loadMonacoEditor();
const starting = appStarting;
$: loading = Promise.all([waiting, loadingMonaco, starting]);
$: {
if (setLeftValue) {
setLeftValue(code);
}
}
$: {
if (setRightValue) {
setRightValue(rightCode);
}
}
$: {
if (setLeftMarkers) {
setLeftMarkers(markers);
}
}
$: {
if (setRightMarkers) {
setRightMarkers(rightMarkers);
}
}
$: {
disposeCodeActionProvider();
if (provideCodeActions) {
loadingMonaco.then((monaco) => setupCodeActionProvider(monaco, provideCodeActions));
}
}
let started = false;
onMount(async () => {
started = true;
await loading;
const monaco = await loadingMonaco;
const options = {
value: code,
readOnly,
theme: 'vs-dark',
language,
automaticLayout: true,
fontSize: 14,
// tabSize: 2,
minimap: {
enabled: false
},
renderControlCharacters: true,
renderIndentGuides: true,
renderValidationDecorations: 'on',
renderWhitespace: 'boundary',
scrollBeyondLastLine: false,
useInlineViewWhenSpaceIsLimited: false,
...editorOptions
};
if (diffEditor) {
editor = monaco.editor.createDiffEditor(rootElement, {
originalEditable: true,
...options
});
const original = monaco.editor.createModel(code, language);
const modified = monaco.editor.createModel(rightCode, language);
const leftEditor = editor.getOriginalEditor();
const rightEditor = editor.getModifiedEditor();
rightEditor.updateOptions({ readOnly: true });
editor.setModel({ original, modified });
original.onDidChangeContent(() => {
const value = original.getValue();
code = value;
});
setLeftValue = (code) => {
const value = original.getValue();
if (code !== value) {
original.setValue(code);
}
};
setRightValue = (code) => {
const value = modified.getValue();
if (code !== value) {
modified.setValue(code);
}
};
setLeftMarkers = (markers) => {
updateMarkers(leftEditor, markers);
};
setRightMarkers = (markers) => {
updateMarkers(rightEditor, markers);
};
getLeftEditor = () => leftEditor;
setLeftMarkers(markers);
setRightMarkers(rightMarkers);
} else {
editor = monaco.editor.create(rootElement, options);
editor.onDidChangeModelContent(() => {
const value = editor.getValue();
code = value;
});
editor.onDidChangeCursorPosition((evt) => {
dispatch('changeCursorPosition', evt);
});
editor.onDidFocusEditorText((evt) => {
dispatch('focusEditorText', evt);
});
setLeftValue = (code) => {
const value = editor.getValue();
if (code !== value) {
editor.setValue(code);
}
};
setRightValue = () => {
/* noop */
};
setLeftMarkers = (markers) => {
updateMarkers(editor, markers);
};
setRightMarkers = () => {
/* noop */
};
getLeftEditor = () => editor;
setLeftMarkers(markers);
}
});
onDestroy(() => {
disposeCodeActionProvider();
dispose(editor);
// rootElement.innerHTML = ""
editor = null;
});
export function setCursorPosition(loc, { columnOffset = 0 } = {}) {
if (editor) {
const leftEditor = diffEditor ? editor?.getOriginalEditor() : editor;
leftEditor.setSelection({
startLineNumber: loc.start.line,
startColumn: loc.start.column + columnOffset,
endLineNumber: loc.end.line,
endColumn: loc.end.column + columnOffset
});
}
}
async function updateMarkers(editor, markers) {
const monaco = await loadingMonaco;
const model = editor.getModel();
const id = editor.getId();
monaco.editor.setModelMarkers(model, id, JSON.parse(JSON.stringify(markers)));
}
/**
* Dispose.
* @param {any} x The target object.
* @returns {void}
*/
function dispose(x) {
if (x == null) {
return;
}
if (x.getOriginalEditor) {
dispose(x.getOriginalEditor());
}
if (x.getModifiedEditor) {
dispose(x.getModifiedEditor());
}
if (x.getModel) {
dispose(x.getModel());
}
if (x.dispose) {
x.dispose();
}
}
function setupCodeActionProvider(monaco, provideCodeActions) {
codeActionProviderDisposable = monaco.languages.registerCodeActionProvider(language, {
provideCodeActions(model, range, context) {
const editor = getLeftEditor?.();
if (editor?.getModel().url !== model.url) {
return {
actions: [],
dispose() {
/* nop */
}
};
}
return provideCodeActions(model, range, context);
}
});
}
function disposeCodeActionProvider() {
if (codeActionProviderDisposable) {
codeActionProviderDisposable.dispose();
}
}
function loadingTypewriter(node) {
const text = 'Loading...';
const duration = 300;
return {
duration,
tick: (t) => {
const i = ~~(text.length * t);
node.textContent = text.slice(0, i);
}
};
}
</script>
{#await loading}
{#if started}
<div
class="eslint-editor-monaco-root eslint-editor-monaco-root--wait"
in:loadingTypewriter
></div>
{/if}
{:then}
<div bind:this={rootElement} class="eslint-editor-monaco-root"></div>
{/await}
<style>
.eslint-editor-monaco-root {
width: 100%;
height: 100%;
}
.eslint-editor-monaco-root--wait {
color: #9cdcfe;
border: 1px solid #cfd4db;
background-color: #282c34;
font-family: Menlo, Monaco, 'Courier New', monospace;
font-size: 14px;
line-height: 21px;
padding-left: 52px;
box-sizing: border-box;
}
</style>