File tree 3 files changed +38
-13
lines changed
3 files changed +38
-13
lines changed Original file line number Diff line number Diff line change @@ -96,3 +96,31 @@ export function getAttachedEasyMDE(el) {
96
96
}
97
97
return el . _data_easyMDE ;
98
98
}
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
+ }
Original file line number Diff line number Diff line change 1
1
import { initCompReactionSelector } from './comp/ReactionSelector.js' ;
2
2
import { initRepoIssueContentHistory } from './repo-issue-content.js' ;
3
+ import { validateTextareaNonEmpty } from './comp/CommentEasyMDE.js' ;
3
4
const { csrfToken} = window . config ;
4
5
5
6
export function initRepoDiffReviewButton ( ) {
@@ -23,7 +24,13 @@ export function initRepoDiffFileViewToggle() {
23
24
export function initRepoDiffConversationForm ( ) {
24
25
$ ( document ) . on ( 'submit' , '.conversation-holder form' , async ( e ) => {
25
26
e . preventDefault ( ) ;
27
+
26
28
const form = $ ( e . target ) ;
29
+ const $textArea = form . find ( 'textarea' ) ;
30
+ if ( ! validateTextareaNonEmpty ( form , $textArea ) ) {
31
+ return ;
32
+ }
33
+
27
34
const newConversationHolder = $ ( await $ . post ( form . attr ( 'action' ) , form . serialize ( ) ) ) ;
28
35
const { path, side, idx} = newConversationHolder . data ( ) ;
29
36
Original file line number Diff line number Diff line change 1
1
import { initMarkupContent } from '../markup/content.js' ;
2
+ import { validateTextareaNonEmpty } from './comp/CommentEasyMDE.js' ;
2
3
import { initCompMarkupContentPreviewTab } from './comp/MarkupContentPreview.js' ;
3
4
4
5
const { csrfToken} = window . config ;
@@ -121,19 +122,8 @@ export function initRepoWikiForm() {
121
122
const $markdownEditorTextArea = $ ( easyMDE . codemirror . getInputField ( ) ) ;
122
123
$markdownEditorTextArea . addClass ( 'js-quick-submit' ) ;
123
124
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 ) ;
137
127
} ) ;
138
128
139
129
setTimeout ( ( ) => {
You can’t perform that action at this time.
0 commit comments