diff options
author | silverwind <me@silverwind.io> | 2023-09-24 21:51:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-24 19:51:02 +0000 |
commit | 3a187eace5643a3dc681f87bdd8d0edbb742aa8f (patch) | |
tree | 1345da1ee1ca0b2fac66a42c7f93357db508b869 | |
parent | 8e23524b18f4075e7db3dc1a7270abc0d9793dcb (diff) | |
download | gitea-3a187eace5643a3dc681f87bdd8d0edbb742aa8f.tar.gz gitea-3a187eace5643a3dc681f87bdd8d0edbb742aa8f.zip |
Fix EOL handling in web editor (#27141)
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
-rw-r--r-- | routers/web/repo/editor.go | 2 | ||||
-rw-r--r-- | templates/repo/editor/edit.tmpl | 7 | ||||
-rw-r--r-- | web_src/js/features/codeeditor.js | 19 |
3 files changed, 22 insertions, 6 deletions
diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 0a606582e5..9d9fee3a77 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -287,7 +287,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b Operation: operation, FromTreePath: ctx.Repo.TreePath, TreePath: form.TreePath, - ContentReader: strings.NewReader(strings.ReplaceAll(form.Content, "\r", "")), + ContentReader: strings.NewReader(form.Content), }, }, Signoff: form.Signoff, diff --git a/templates/repo/editor/edit.tmpl b/templates/repo/editor/edit.tmpl index 2b303be97c..abe2e345fc 100644 --- a/templates/repo/editor/edit.tmpl +++ b/templates/repo/editor/edit.tmpl @@ -34,12 +34,13 @@ {{end}} </div> <div class="ui bottom attached active tab segment" data-tab="write"> - <textarea id="edit_area" name="content" class="gt-hidden" data-id="repo-{{.Repository.Name}}-{{.TreePath}}" + <textarea id="edit_area" name="content" class="gt-hidden" + data-id="repo-{{.Repository.Name}}-{{.TreePath}}" data-url="{{.Repository.Link}}/markup" data-context="{{.RepoLink}}" data-previewable-extensions="{{.PreviewableExtensions}}" - data-line-wrap-extensions="{{.LineWrapExtensions}}"> -{{.FileContent}}</textarea> + data-line-wrap-extensions="{{.LineWrapExtensions}}" + data-initial-value="{{JsonUtils.EncodeToString .FileContent}}"></textarea> <div class="editor-loading is-loading"></div> </div> <div class="ui bottom attached tab segment markup" data-tab="preview"> 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; } |