aboutsummaryrefslogtreecommitdiffstats
path: root/web_src
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2023-09-25 06:10:38 +0800
committerGitHub <noreply@github.com>2023-09-24 22:10:38 +0000
commita1029cb2cae973146759ff2d1458292e40a7a1fd (patch)
tree7586d659451295d4538611d091cf2070a1919ef0 /web_src
parent134c7636effb57ede02438dadcd2cdcb3c03771e (diff)
downloadgitea-a1029cb2cae973146759ff2d1458292e40a7a1fd.tar.gz
gitea-a1029cb2cae973146759ff2d1458292e40a7a1fd.zip
Fix EOL handling in web editor (#27141) (#27234)
Backport #27141 by @silverwind Fixes https://github.com/go-gitea/gitea/issues/27136. This does the following for Monaco's EOL setting: 1. Use editorconfig setting if present 2. Use the file's dominant line ending as detected by monaco, which uses LF for empty file Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'web_src')
-rw-r--r--web_src/js/features/codeeditor.js19
1 files changed, 17 insertions, 2 deletions
diff --git a/web_src/js/features/codeeditor.js b/web_src/js/features/codeeditor.js
index 7dbbcd3dd6..5f924fd086 100644
--- a/web_src/js/features/codeeditor.js
+++ b/web_src/js/features/codeeditor.js
@@ -62,7 +62,7 @@ export async function createMonaco(textarea, filename, editorOpts) {
const monaco = await import(/* webpackChunkName: "monaco" */'monaco-editor');
initLanguages(monaco);
- let {language, ...other} = editorOpts;
+ let {language, eol, ...other} = editorOpts;
if (!language) language = getLanguage(filename);
const container = document.createElement('div');
@@ -105,14 +105,28 @@ export async function createMonaco(textarea, filename, editorOpts) {
monaco.languages.register({id: 'vs.editor.nullLanguage'});
monaco.languages.setLanguageConfiguration('vs.editor.nullLanguage', {});
+ // We encode the initial value in JSON on the backend to prevent browsers from
+ // discarding the \r during HTML parsing:
+ // https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
+ const value = JSON.parse(textarea.getAttribute('data-initial-value') || '""');
+ textarea.value = value;
+ textarea.removeAttribute('data-initial-value');
+
const editor = monaco.editor.create(container, {
- value: textarea.value,
+ value,
theme: 'gitea',
language,
...other,
});
const model = editor.getModel();
+
+ // Monaco performs auto-detection of dominant EOL in the file, biased towards LF for
+ // empty files. If there is an editorconfig value, override this detected value.
+ if (eol in monaco.editor.EndOfLineSequence) {
+ model.setEOL(monaco.editor.EndOfLineSequence[eol]);
+ }
+
model.onDidChangeContent(() => {
textarea.value = editor.getValue();
textarea.dispatchEvent(new Event('change')); // seems to be needed for jquery-are-you-sure
@@ -187,5 +201,6 @@ function getEditorConfigOptions(ec) {
opts.trimAutoWhitespace = ec.trim_trailing_whitespace === true;
opts.insertSpaces = ec.indent_style === 'space';
opts.useTabStops = ec.indent_style === 'tab';
+ opts.eol = ec.end_of_line?.toUpperCase();
return opts;
}