Skip to content

Commit 4db2012

Browse files
author
Gusted
committed
Refactor
1 parent c416f00 commit 4db2012

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

web_src/js/features/comp/CommentEasyMDE.js

Lines changed: 28 additions & 0 deletions
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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {initCompReactionSelector} from './comp/ReactionSelector.js';
22
import {initRepoIssueContentHistory} from './repo-issue-content.js';
3-
import {getAttachedEasyMDE} from './comp/CommentEasyMDE.js';
3+
import {validateTextareaNonEmpty} from './comp/CommentEasyMDE.js';
44
const {csrfToken} = window.config;
55

66
export function initRepoDiffReviewButton() {
@@ -27,13 +27,9 @@ export function initRepoDiffConversationForm() {
2727

2828
const form = $(e.target);
2929
const $textArea = form.find('textarea');
30-
const $markdownEditorTextArea = $(getAttachedEasyMDE($textArea).codemirror.getInputField());
31-
if (!$textArea.val().length) {
32-
$markdownEditorTextArea.prop('required', true);
33-
form.get(0).reportValidity();
30+
if (!validateTextareaNonEmpty(form, $textArea)) {
3431
return;
3532
}
36-
$markdownEditorTextArea.prop('required', false);
3733

3834
const newConversationHolder = $(await $.post(form.attr('action'), form.serialize()));
3935
const {path, side, idx} = newConversationHolder.data();

web_src/js/features/repo-wiki.js

Lines changed: 3 additions & 13 deletions
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)