-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathindex.tsx
More file actions
316 lines (282 loc) · 9.76 KB
/
index.tsx
File metadata and controls
316 lines (282 loc) · 9.76 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { editor, Range } from "monaco-editor";
import React, { useEffect, useImperativeHandle, useRef, useState } from "react";
import { globalCache, systemConfig } from "@App/pages/store/global";
import { LinterWorker } from "@App/pkg/utils/monaco-editor";
import { fnPlaceHolder } from "@App/pages/store/AppContext";
fnPlaceHolder.setEditorTheme = (theme: string) => editor.setTheme(theme);
type Props = {
className?: string;
diffCode?: string; // 因为代码加载是异步的,diifCode有3种状态:undefined不确定,""没有diff,有diff,不确定的情况下,编辑器不会加载
editable?: boolean;
id: string;
code?: string;
};
const CodeEditor: React.ForwardRefRenderFunction<{ editor: editor.IStandaloneCodeEditor | undefined }, Props> = (
{ id, className, code, diffCode, editable },
ref
) => {
const [monacoEditor, setEditor] = useState<editor.IStandaloneCodeEditor>();
const [enableEslint, setEnableEslint] = useState(false);
const [eslintConfig, setEslintConfig] = useState("");
const div = useRef<HTMLDivElement>(null);
useImperativeHandle(ref, () => ({
editor: monacoEditor,
}));
useEffect(() => {
const loadConfigs = () => {
Promise.all([systemConfig.getEslintConfig(), systemConfig.getEnableEslint()]).then(
([eslintConfig, enableEslint]) => {
setEslintConfig(eslintConfig);
setEnableEslint(enableEslint);
}
);
};
loadConfigs();
}, []);
useEffect(() => {
if (diffCode === undefined || code === undefined || !div.current) {
return () => {};
}
let edit: editor.IStandaloneDiffEditor | editor.IStandaloneCodeEditor;
const inlineDiv = document.getElementById(id) as HTMLDivElement;
const commonEditorOptions = {
folding: true,
foldingStrategy: "indentation",
automaticLayout: true,
scrollbar: { alwaysConsumeMouseWheel: false },
overviewRulerBorder: false,
scrollBeyondLastLine: false,
glyphMargin: true,
unicodeHighlight: {
ambiguousCharacters: false,
},
// https://code.visualstudio.com/docs/editing/intellisense
// Controls whether suggestions should be accepted on commit characters. For example, in JavaScript, the semi-colon (`;`) can be a commit character that accepts a suggestion and types that character.
acceptSuggestionOnCommitCharacter: true,
// Controls if suggestions should be accepted on 'Enter' - in addition to 'Tab'. Helps to avoid ambiguity between inserting new lines or accepting suggestions. The value 'smart' means only accept a suggestion with Enter when it makes a textual change
acceptSuggestionOnEnter: "on",
// Controls the delay in ms after which quick suggestions will show up.
quickSuggestionsDelay: 10,
// Controls if suggestions should automatically show up when typing trigger characters
suggestOnTriggerCharacters: true,
// Controls if pressing tab inserts the best suggestion and if tab cycles through other suggestions
tabCompletion: "off",
// Controls whether sorting favours words that appear close to the cursor
suggest: {
localityBonus: true,
preview: true,
},
// Controls how suggestions are pre-selected when showing the suggest list
suggestSelection: "first",
// Enable word based suggestions
wordBasedSuggestions: "off",
// Enable parameter hints
parameterHints: {
enabled: true,
},
// https://qiita.com/H-goto16/items/43802950fc5c112c316b
// https://zenn.dev/udonj/articles/ultimate-vscode-customization-2024
// https://github.com/is0383kk/VSCode
quickSuggestions: {
other: true,
comments: true,
strings: true,
},
fastScrollSensitivity: 10,
smoothScrolling: true,
inlineSuggest: {
enabled: true,
},
guides: {
indentation: true,
},
renderLineHighlightOnlyWhenFocus: true,
snippetSuggestions: "top",
cursorBlinking: "phase",
cursorSmoothCaretAnimation: "off",
autoIndent: "advanced",
wrappingIndent: "indent",
wordSegmenterLocales: ["ja", "zh-CN", "zh-Hant-TW"] as string[],
renderLineHighlight: "gutter",
renderWhitespace: "selection",
renderControlCharacters: true,
dragAndDrop: false,
emptySelectionClipboard: false,
copyWithSyntaxHighlighting: false,
bracketPairColorization: {
enabled: true,
},
mouseWheelZoom: true,
links: true,
accessibilitySupport: "off",
largeFileOptimizations: true,
colorDecorators: true,
} as const;
let originalModel: editor.ITextModel | undefined;
let modifiedModel: editor.ITextModel | undefined;
if (diffCode) {
edit = editor.createDiffEditor(inlineDiv, {
hideUnchangedRegions: {
enabled: true,
},
enableSplitViewResizing: false,
renderSideBySide: false,
readOnly: true,
diffWordWrap: "off",
...commonEditorOptions,
});
// standalone model 不会随 editor.dispose 自动清理,需手动跟踪并在 cleanup 释放
originalModel = editor.createModel(diffCode, "javascript");
modifiedModel = editor.createModel(code, "javascript");
edit.setModel({
original: originalModel,
modified: modifiedModel,
});
} else {
edit = editor.create(inlineDiv, {
language: "javascript",
theme: document.body.getAttribute("arco-theme") === "dark" ? "vs-dark" : "vs",
readOnly: !editable,
...commonEditorOptions,
});
edit.setValue(code);
setEditor(edit);
}
return () => {
// 目前会出现:Uncaught (in promise) Canceled: Canceled
// 问题追踪:https://github.com/microsoft/monaco-editor/issues/4702
edit?.dispose();
originalModel?.dispose();
modifiedModel?.dispose();
};
}, [div, code, diffCode, editable, id]);
useEffect(() => {
if (!enableEslint) {
return () => {};
}
if (!monacoEditor) {
return () => {};
}
const model = monacoEditor.getModel();
if (!model) {
return () => {};
}
let timer: NodeJS.Timeout | null;
const lint = () => {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
timer = null;
LinterWorker.sendLinterMessage({
code: model.getValue(),
id,
config: JSON.parse(eslintConfig),
});
}, 500);
};
// 加载完成就检测一次
lint();
model.onDidChangeContent(() => {
lint();
});
// 在行号旁显示ESLint错误/警告图标
const diffEslint = (
makers: {
startLineNumber: number;
endLineNumber: number;
severity: number;
}[]
) => {
// 定义glyph class
const glyphMarginClassList = {
4: "icon-warn",
8: "icon-error",
};
// 先移除所有旧的Decorations
const oldDecorations = model
.getAllDecorations()
.filter(
(i) =>
i.options.glyphMarginClassName &&
Object.values(glyphMarginClassList).includes(i.options.glyphMarginClassName)
);
monacoEditor.removeDecorations(oldDecorations.map((i) => i.id));
/* 待改进 目前似乎monaco无法满足需求
// 获取所有ESLint ModelMarkers
const allMarkers = editor.getModelMarkers({ owner: "ESLint" });
*/
// 再重新添加新的Decorations
monacoEditor.createDecorationsCollection(
makers.map(({ startLineNumber, endLineNumber, severity }) => ({
range: new Range(startLineNumber, 1, endLineNumber, 1),
options: {
isWholeLine: true,
// @ts-ignore
glyphMarginClassName: glyphMarginClassList[severity],
/* 待改进 目前monaco似乎无法满足需求
glyphMarginHoverMessage: allMarkers.reduce(
(prev: any, next: any) => {
if (
next.startLineNumber === startLineNumber &&
next.endLineNumber === endLineNumber
) {
prev.push({
value: `${next.message} ESLinter [(${next.code.value})](${next.code.target})`,
isTrusted: true,
});
}
return prev;
},
[]
),
*/
},
}))
);
};
const handler = (message: any) => {
if (id !== message.id) {
return;
}
editor.setModelMarkers(model, "ESLint", message.markers);
const fix = new Map();
// 设置fix
message.markers.forEach(
(val: {
code: { value: any };
startLineNumber: any;
endLineNumber: any;
startColumn: any;
endColumn: any;
fix: any;
}) => {
if (val.fix) {
fix.set(
`${val.code.value}|${val.startLineNumber}|${val.endLineNumber}|${val.startColumn}|${val.endColumn}`,
val.fix
);
}
}
);
globalCache.set("eslint-fix", fix);
// 在行号旁显示ESLint错误/警告图标
const formatMarkers = message.markers.map(
({
startLineNumber,
endLineNumber,
severity,
}: {
startLineNumber: number;
endLineNumber: number;
severity: number;
}) => ({ startLineNumber, endLineNumber, severity })
);
diffEslint(formatMarkers);
};
LinterWorker.hook.addListener("message", handler);
return () => {
LinterWorker.hook.removeListener("message", handler);
};
}, [id, monacoEditor, enableEslint, eslintConfig]);
return <div id={id} className={className} ref={div} />;
};
export default React.forwardRef(CodeEditor);