Skip to content

Commit 7352ee5

Browse files
committed
improve
1 parent 5cc0801 commit 7352ee5

File tree

6 files changed

+94
-45
lines changed

6 files changed

+94
-45
lines changed

templates/repo/issue/comment_tab.tmpl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
{{if not $textareaContent}}{{$textareaContent = .PullRequestTemplate}}{{end}}
44
{{if not $textareaContent}}{{$textareaContent = .content}}{{end}}
55

6-
{{template "shared/combomarkdowneditor" (dict
7-
"locale" $.locale
8-
"MarkdownPreviewUrl" (print .Repository.Link "/markup")
9-
"MarkdownPreviewContext" .RepoLink
10-
"TextareaName" "content"
11-
"TextareaContent" $textareaContent
12-
"DropzoneParentContainer" "form, .ui.form"
13-
)}}
6+
<div class="field">
7+
{{template "shared/combomarkdowneditor" (dict
8+
"locale" $.locale
9+
"MarkdownPreviewUrl" (print .Repository.Link "/markup")
10+
"MarkdownPreviewContext" .RepoLink
11+
"TextareaName" "content"
12+
"TextareaContent" $textareaContent
13+
"DropzoneParentContainer" "form, .ui.form"
14+
)}}
15+
</div>
1416

1517
{{if .IsAttachmentEnabled}}
1618
<div class="field">

web_src/css/editor-markdown.css

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@
1818
cursor: pointer;
1919
}
2020

21-
.combo-markdown-editor .markdown-text-editor {
21+
.ui.form .combo-markdown-editor textarea.markdown-text-editor,
22+
.combo-markdown-editor textarea.markdown-text-editor {
2223
display: block;
2324
width: 100%;
24-
height: 200px;
25+
min-height: 200px;
26+
max-height: calc(100vh - 200px);
27+
resize: none;
28+
}
29+
30+
.combo-markdown-editor .CodeMirror-scroll {
31+
max-height: calc(100vh - 200px);
2532
}

web_src/css/repository.css

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,6 @@
544544
margin: 0;
545545
}
546546

547-
.repository .comment textarea {
548-
max-height: none !important;
549-
}
550-
551547
.repository.new.issue .comment.form .comment .avatar {
552548
width: 3em;
553549
}
@@ -1068,11 +1064,6 @@
10681064
min-height: 5rem;
10691065
}
10701066

1071-
.repository.view.issue .comment-list .comment .ui.form textarea {
1072-
height: 200px;
1073-
font-family: var(--fonts-monospace);
1074-
}
1075-
10761067
.repository.view.issue .comment-list .comment .edit.buttons {
10771068
margin-top: 10px;
10781069
}
@@ -1191,15 +1182,6 @@
11911182
margin-top: -8px;
11921183
}
11931184

1194-
.repository .comment.form .content textarea {
1195-
height: 200px;
1196-
font-family: var(--fonts-monospace);
1197-
}
1198-
1199-
.repository .comment.form .content .CodeMirror-scroll {
1200-
max-height: 85vh;
1201-
}
1202-
12031185
.repository .milestone.list {
12041186
list-style: none;
12051187
padding-top: 15px;
@@ -2129,9 +2111,6 @@
21292111
margin-top: 0;
21302112
}
21312113

2132-
.repository.wiki .form .CodeMirror-scroll {
2133-
max-height: 85vh;
2134-
}
21352114

21362115
@media (max-width: 767px) {
21372116
.repository.wiki .dividing.header .stackable.grid .button {

web_src/css/review.css

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,11 @@
160160
margin: 0.5em;
161161
}
162162

163+
.comment-code-cloud .editor-statusbar {
164+
display: none;
165+
}
166+
163167
.comment-code-cloud .footer {
164-
border-top: 1px solid var(--color-secondary);
165168
padding: 10px 0;
166169
}
167170

@@ -248,7 +251,7 @@ a.blob-excerpt:hover {
248251
}
249252
}
250253

251-
.review-box-panel .combo-markdown-editor textarea {
254+
.review-box-panel .combo-markdown-editor {
252255
width: 730px;
253256
max-width: calc(100vw - 70px);
254257
}

web_src/js/features/comp/ComboMarkdownEditor.js

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import '@github/markdown-toolbar-element';
2+
import $ from 'jquery';
23
import {attachTribute} from '../tribute.js';
34
import {hideElem, showElem} from '../../utils/dom.js';
45
import {initEasyMDEImagePaste, initTextareaImagePaste} from './ImagePaste.js';
5-
import $ from 'jquery';
66
import {initMarkupContent} from '../../markup/content.js';
77
import {handleGlobalEnterQuickSubmit} from './QuickSubmit.js';
88
import {attachRefIssueContextPopup} from '../contextpopup.js';
@@ -39,31 +39,57 @@ class ComboMarkdownEditor {
3939
}
4040

4141
async init() {
42+
this.prepareEasyMDEToolbarActions();
43+
44+
this.setupTab();
45+
this.setupDropzone();
46+
47+
this.setupTextarea();
48+
49+
await attachTribute(this.textarea, {mentions: true, emoji: true});
50+
51+
if (this.userPreferredEditor === 'easymde') {
52+
await this.switchToEasyMDE();
53+
}
54+
}
55+
56+
applyEditorHeights(el, heights) {
57+
if (!heights) return;
58+
if (heights.minHeight) el.style.minHeight = heights.minHeight;
59+
if (heights.height) el.style.height = heights.height;
60+
if (heights.maxHeight) el.style.maxHeight = heights.maxHeight;
61+
}
62+
63+
setupTextarea() {
4264
this.textarea = this.container.querySelector('.markdown-text-editor');
4365
this.textarea._giteaComboMarkdownEditor = this;
44-
this.textarea.id = `_combo_markdown_editor_${String(elementIdCounter)}`;
45-
this.textarea.addEventListener('input', (e) => {this.options?.onContentChanged?.(this, e)});
66+
this.textarea.id = `_combo_markdown_editor_${String(elementIdCounter++)}`;
67+
this.textarea.addEventListener('input', (e) => {
68+
this.textareaAutoResize();
69+
this.options?.onContentChanged?.(this, e);
70+
});
71+
this.applyEditorHeights(this.textarea, this.options.editorHeights);
72+
this.textareaAutoResize();
4673
this.textareaMarkdownToolbar = this.container.querySelector('markdown-toolbar');
4774
this.textareaMarkdownToolbar.setAttribute('for', this.textarea.id);
4875

49-
elementIdCounter++;
50-
5176
this.switchToEasyMDEButton = this.container.querySelector('.markdown-switch-easymde');
5277
this.switchToEasyMDEButton?.addEventListener('click', async (e) => {
5378
e.preventDefault();
79+
this.userPreferredEditor = 'easymde';
5480
await this.switchToEasyMDE();
5581
});
5682

57-
await attachTribute(this.textarea, {mentions: true, emoji: true});
83+
if (this.dropzone) {
84+
initTextareaImagePaste(this.textarea, this.dropzone);
85+
}
86+
}
5887

88+
setupDropzone() {
5989
const dropzoneParentContainer = this.container.getAttribute('data-dropzone-parent-container');
6090
if (dropzoneParentContainer) {
6191
this.dropzone = this.container.closest(this.container.getAttribute('data-dropzone-parent-container'))?.querySelector('.dropzone');
62-
initTextareaImagePaste(this.textarea, this.dropzone);
6392
}
64-
65-
this.setupTab();
66-
this.prepareEasyMDEToolbarActions();
6793
}
6894

6995
setupTab() {
@@ -134,7 +160,10 @@ class ComboMarkdownEditor {
134160
title: 'Add Checkbox (checked)',
135161
},
136162
'gitea-switch-to-textarea': {
137-
action: this.switchToTextarea.bind(this),
163+
action: () => {
164+
this.userPreferredEditor = 'textarea';
165+
this.switchToTextarea();
166+
},
138167
className: 'fa fa-file',
139168
title: 'Revert to simple textarea',
140169
},
@@ -169,7 +198,7 @@ class ComboMarkdownEditor {
169198
return processed;
170199
}
171200

172-
async switchToTextarea() {
201+
switchToTextarea() {
173202
showElem(this.textareaMarkdownToolbar);
174203
if (this.easyMDE) {
175204
this.easyMDE.toTextArea();
@@ -218,6 +247,7 @@ class ComboMarkdownEditor {
218247
}
219248
},
220249
});
250+
this.applyEditorHeights(this.container.querySelector('.CodeMirror-scroll'), this.options.editorHeights);
221251
await attachTribute(this.easyMDE.codemirror.getInputField(), {mentions: true, emoji: true});
222252
initEasyMDEImagePaste(this.easyMDE, this.dropzone);
223253
hideElem(this.textareaMarkdownToolbar);
@@ -235,6 +265,7 @@ class ComboMarkdownEditor {
235265
this.easyMDE.value(v);
236266
} else {
237267
this.textarea.value = v;
268+
this.textareaAutoResize();
238269
}
239270
}
240271

@@ -246,6 +277,24 @@ class ComboMarkdownEditor {
246277
}
247278
}
248279

280+
textareaAutoResize() {
281+
if (this.textareaInitalHeight === undefined) {
282+
this.textareaInitalHeight = this.textarea.offsetHeight;
283+
}
284+
const offset = this.textarea.offsetHeight - this.textarea.clientHeight;
285+
if (!this.lastValue || Math.abs(this.lastValue.length - this.textarea.value.length) > 2) {
286+
// the value has changed a lot, so reset the height to calculate the real scroll height, it might cause UI flickering
287+
this.textarea.style.height = 'auto';
288+
} else {
289+
// try to shrink a little to see if a line is deleted (since the value doesn't change much), it won't cause UI flickering
290+
// the magic number is a general number which fits most line-height styles.
291+
this.textarea.style.height = `${this.textarea.scrollHeight + offset - 40}px`;
292+
}
293+
// make sure the height is not smaller than the initial height
294+
this.textarea.style.height = `${Math.max(this.textareaInitalHeight, this.textarea.scrollHeight + offset)}px`;
295+
this.lastValue = this.textarea.value;
296+
}
297+
249298
moveCursorToEnd() {
250299
this.textarea.focus();
251300
this.textarea.setSelectionRange(this.textarea.value.length, this.textarea.value.length);
@@ -254,6 +303,13 @@ class ComboMarkdownEditor {
254303
this.easyMDE.codemirror.setCursor(this.easyMDE.codemirror.lineCount(), 0);
255304
}
256305
}
306+
307+
get userPreferredEditor() {
308+
return window.localStorage.getItem(`markdown-editor-${this.options.useScene ?? 'default'}`);
309+
}
310+
set userPreferredEditor(s) {
311+
window.localStorage.setItem(`markdown-editor-${this.options.useScene ?? 'default'}`, s);
312+
}
257313
}
258314

259315
export function getComboMarkdownEditor(el) {

web_src/js/features/repo-wiki.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ async function initRepoWikiFormEditor() {
4444
renderEasyMDEPreview();
4545

4646
editor = await initComboMarkdownEditor($editorContainer, {
47+
useScene: 'wiki',
48+
editorHeights: {minHeight: '300px', height: 'calc(100vh - 600px)'},
4749
previewMode: 'gfm',
4850
previewWiki: true,
4951
easyMDEOptions: {

0 commit comments

Comments
 (0)