File tree Expand file tree Collapse file tree 3 files changed +38
-13
lines changed Expand file tree Collapse file tree 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) {
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+ }
Original file line number Diff line number Diff line change 11import { initCompReactionSelector } from './comp/ReactionSelector.js' ;
22import { initRepoIssueContentHistory } from './repo-issue-content.js' ;
3+ import { validateTextareaNonEmpty } from './comp/CommentEasyMDE.js' ;
34const { csrfToken} = window . config ;
45
56export function initRepoDiffReviewButton ( ) {
@@ -23,7 +24,13 @@ export function initRepoDiffFileViewToggle() {
2324export 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
Original file line number Diff line number Diff line change 11import { initMarkupContent } from '../markup/content.js' ;
2+ import { validateTextareaNonEmpty } from './comp/CommentEasyMDE.js' ;
23import { initCompMarkupContentPreviewTab } from './comp/MarkupContentPreview.js' ;
34
45const { 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 ( ( ) => {
You can’t perform that action at this time.
0 commit comments