loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

authored by

silverwind and committed by
GitHub
3a187eac 8e23524b

+22 -6
+1 -1
routers/web/repo/editor.go
··· 287 287 Operation: operation, 288 288 FromTreePath: ctx.Repo.TreePath, 289 289 TreePath: form.TreePath, 290 - ContentReader: strings.NewReader(strings.ReplaceAll(form.Content, "\r", "")), 290 + ContentReader: strings.NewReader(form.Content), 291 291 }, 292 292 }, 293 293 Signoff: form.Signoff,
+4 -3
templates/repo/editor/edit.tmpl
··· 34 34 {{end}} 35 35 </div> 36 36 <div class="ui bottom attached active tab segment" data-tab="write"> 37 - <textarea id="edit_area" name="content" class="gt-hidden" data-id="repo-{{.Repository.Name}}-{{.TreePath}}" 37 + <textarea id="edit_area" name="content" class="gt-hidden" 38 + data-id="repo-{{.Repository.Name}}-{{.TreePath}}" 38 39 data-url="{{.Repository.Link}}/markup" 39 40 data-context="{{.RepoLink}}" 40 41 data-previewable-extensions="{{.PreviewableExtensions}}" 41 - data-line-wrap-extensions="{{.LineWrapExtensions}}"> 42 - {{.FileContent}}</textarea> 42 + data-line-wrap-extensions="{{.LineWrapExtensions}}" 43 + data-initial-value="{{JsonUtils.EncodeToString .FileContent}}"></textarea> 43 44 <div class="editor-loading is-loading"></div> 44 45 </div> 45 46 <div class="ui bottom attached tab segment markup" data-tab="preview">
+17 -2
web_src/js/features/codeeditor.js
··· 62 62 const monaco = await import(/* webpackChunkName: "monaco" */'monaco-editor'); 63 63 64 64 initLanguages(monaco); 65 - let {language, ...other} = editorOpts; 65 + let {language, eol, ...other} = editorOpts; 66 66 if (!language) language = getLanguage(filename); 67 67 68 68 const container = document.createElement('div'); ··· 105 105 monaco.languages.register({id: 'vs.editor.nullLanguage'}); 106 106 monaco.languages.setLanguageConfiguration('vs.editor.nullLanguage', {}); 107 107 108 + // We encode the initial value in JSON on the backend to prevent browsers from 109 + // discarding the \r during HTML parsing: 110 + // https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream 111 + const value = JSON.parse(textarea.getAttribute('data-initial-value') || '""'); 112 + textarea.value = value; 113 + textarea.removeAttribute('data-initial-value'); 114 + 108 115 const editor = monaco.editor.create(container, { 109 - value: textarea.value, 116 + value, 110 117 theme: 'gitea', 111 118 language, 112 119 ...other, 113 120 }); 114 121 115 122 const model = editor.getModel(); 123 + 124 + // Monaco performs auto-detection of dominant EOL in the file, biased towards LF for 125 + // empty files. If there is an editorconfig value, override this detected value. 126 + if (eol in monaco.editor.EndOfLineSequence) { 127 + model.setEOL(monaco.editor.EndOfLineSequence[eol]); 128 + } 129 + 116 130 model.onDidChangeContent(() => { 117 131 textarea.value = editor.getValue(); 118 132 textarea.dispatchEvent(new Event('change')); // seems to be needed for jquery-are-you-sure ··· 187 201 opts.trimAutoWhitespace = ec.trim_trailing_whitespace === true; 188 202 opts.insertSpaces = ec.indent_style === 'space'; 189 203 opts.useTabStops = ec.indent_style === 'tab'; 204 + opts.eol = ec.end_of_line?.toUpperCase(); 190 205 return opts; 191 206 }