Skip to content

Commit 1406ed8

Browse files
GustedStelios Malathouras
Gusted
authored and
Stelios Malathouras
committed
Require codereview to have content (go-gitea#18156)
- Report a validityError when the codeReview have no comment. - Resolves go-gitea#18151 - Refactor
1 parent ebd52fd commit 1406ed8

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

web_src/js/features/comp/CommentEasyMDE.js

+28
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,31 @@ export function getAttachedEasyMDE(el) {
9696
}
9797
return el._data_easyMDE;
9898
}
99+
100+
/**
101+
* validate if the given textarea from a form, is non-empty.
102+
* @param {jQuery | HTMLElement} form
103+
* @param {jQuery | HTMLElement} textarea
104+
* @returns {boolean} returns true if validation succeeded.
105+
*/
106+
export function validateTextareaNonEmpty(form, textarea) {
107+
if (form instanceof jQuery) {
108+
form = form[0];
109+
}
110+
if (textarea instanceof jQuery) {
111+
textarea = textarea[0];
112+
}
113+
114+
const $markdownEditorTextArea = $(getAttachedEasyMDE(textarea).codemirror.getInputField());
115+
// The original edit area HTML element is hidden and replaced by the
116+
// SimpleMDE/EasyMDE editor, breaking HTML5 input validation if the text area is empty.
117+
// This is a workaround for this upstream bug.
118+
// See https://github.com/sparksuite/simplemde-markdown-editor/issues/324
119+
if (textarea.value.length) {
120+
$markdownEditorTextArea.prop('required', true);
121+
form.reportValidity();
122+
return false;
123+
}
124+
$markdownEditorTextArea.prop('required', false);
125+
return true;
126+
}

web_src/js/features/repo-diff.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {initCompReactionSelector} from './comp/ReactionSelector.js';
22
import {initRepoIssueContentHistory} from './repo-issue-content.js';
3+
import {validateTextareaNonEmpty} from './comp/CommentEasyMDE.js';
34
const {csrfToken} = window.config;
45

56
export function initRepoDiffReviewButton() {
@@ -23,7 +24,13 @@ export function initRepoDiffFileViewToggle() {
2324
export function initRepoDiffConversationForm() {
2425
$(document).on('submit', '.conversation-holder form', async (e) => {
2526
e.preventDefault();
27+
2628
const form = $(e.target);
29+
const $textArea = form.find('textarea');
30+
if (!validateTextareaNonEmpty(form, $textArea)) {
31+
return;
32+
}
33+
2734
const newConversationHolder = $(await $.post(form.attr('action'), form.serialize()));
2835
const {path, side, idx} = newConversationHolder.data();
2936

web_src/js/features/repo-wiki.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {initMarkupContent} from '../markup/content.js';
2+
import {validateTextareaNonEmpty} from './comp/CommentEasyMDE.js';
23
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
34

45
const {csrfToken} = window.config;
@@ -121,19 +122,8 @@ export function initRepoWikiForm() {
121122
const $markdownEditorTextArea = $(easyMDE.codemirror.getInputField());
122123
$markdownEditorTextArea.addClass('js-quick-submit');
123124

124-
$form.on('submit', function (e) {
125-
// The original edit area HTML element is hidden and replaced by the
126-
// SimpleMDE/EasyMDE editor, breaking HTML5 input validation if the text area is empty.
127-
// This is a workaround for this upstream bug.
128-
// See https://github.com/sparksuite/simplemde-markdown-editor/issues/324
129-
const input = $editArea.val();
130-
if (!input.length) {
131-
e.preventDefault();
132-
$markdownEditorTextArea.prop('required', true);
133-
this.reportValidity();
134-
} else {
135-
$markdownEditorTextArea.prop('required', false);
136-
}
125+
$form.on('submit', function () {
126+
validateTextareaNonEmpty(this, $editArea);
137127
});
138128

139129
setTimeout(() => {

0 commit comments

Comments
 (0)