Skip to content

Commit e7b0fdb

Browse files
committed
Improve jsx/tsx support in code editor
1 parent 5bc030e commit e7b0fdb

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

web_src/js/features/codeeditor.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ function initLanguages(monaco: Monaco): void {
5858
for (const extension of extensions || []) {
5959
languagesByExt[extension] = id;
6060
}
61+
if (id === 'typescript') {
62+
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
63+
// this is needed to supporess error annotations in tsx files regarding missing --jsx flag.
64+
jsx: monaco.languages.typescript.JsxEmit.Preserve,
65+
});
66+
}
6167
}
6268
}
6369

@@ -72,6 +78,8 @@ function updateEditor(monaco: Monaco, editor: IStandaloneCodeEditor, filename: s
7278
const language = model.getLanguageId();
7379
const newLanguage = getLanguage(filename);
7480
if (language !== newLanguage) monaco.editor.setModelLanguage(model, newLanguage);
81+
// TODO: Need to update the model uri with the new filename, but there is no easy way currently, see
82+
// https://github.com/microsoft/monaco-editor/discussions/3751
7583
}
7684

7785
// export editor for customization - https://github.com/go-gitea/gitea/issues/10409
@@ -135,18 +143,18 @@ export async function createMonaco(textarea: HTMLTextAreaElement, filename: stri
135143
});
136144
updateTheme(monaco);
137145

146+
const model = monaco.editor.createModel(textarea.value, language, monaco.Uri.file(filename));
147+
138148
const editor = monaco.editor.create(container, {
139-
value: textarea.value,
149+
model,
140150
theme: 'gitea',
141-
language,
142151
...other,
143152
});
144153

145154
monaco.editor.addKeybindingRules([
146155
{keybinding: monaco.KeyCode.Enter, command: null}, // disable enter from accepting code completion
147156
]);
148157

149-
const model = editor.getModel();
150158
if (!model) throw new Error('Unable to get editor model');
151159
model.onDidChangeContent(() => {
152160
textarea.value = editor.getValue({

0 commit comments

Comments
 (0)