Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JSX/TSX support in code editor #32833

Merged
merged 4 commits into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions web_src/js/features/codeeditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ function initLanguages(monaco: Monaco): void {
for (const extension of extensions || []) {
languagesByExt[extension] = id;
}
if (id === 'typescript') {
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
// this is needed to suppress error annotations in tsx regarding missing --jsx flag.
jsx: monaco.languages.typescript.JsxEmit.Preserve,
});
}
}
}

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

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

const model = monaco.editor.createModel(textarea.value, language, monaco.Uri.file(filename));

const editor = monaco.editor.create(container, {
value: textarea.value,
model,
theme: 'gitea',
language,
...other,
});

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

const model = editor.getModel();
if (!model) throw new Error('Unable to get editor model');
model.onDidChangeContent(() => {
textarea.value = editor.getValue({
preserveBOM: true,
Expand Down
Loading