@@ -75,13 +75,27 @@ export function createCommentEasyMDE(textarea) {
75
75
}
76
76
} ) ;
77
77
attachTribute ( inputField , { mentions : true , emoji : true } ) ;
78
+ attachEasyMDEToElements ( easyMDE ) ;
79
+ return easyMDE ;
80
+ }
78
81
82
+ /**
83
+ * attach the EasyMDE object to its input elements (InputField, TextArea)
84
+ * @param {EasyMDE } easyMDE
85
+ */
86
+ export function attachEasyMDEToElements ( easyMDE ) {
79
87
// TODO: that's the only way we can do now to attach the EasyMDE object to a HTMLElement
88
+
89
+ // InputField is used by CodeMirror to accept user input
90
+ const inputField = easyMDE . codemirror . getInputField ( ) ;
80
91
inputField . _data_easyMDE = easyMDE ;
81
- textarea . _data_easyMDE = easyMDE ;
82
- return easyMDE ;
92
+
93
+ // TextArea is the real textarea element in the form
94
+ const textArea = easyMDE . codemirror . getTextArea ( ) ;
95
+ textArea . _data_easyMDE = easyMDE ;
83
96
}
84
97
98
+
85
99
/**
86
100
* get the attached EasyMDE editor created by createCommentEasyMDE
87
101
* @param el jQuery or HTMLElement
@@ -98,29 +112,27 @@ export function getAttachedEasyMDE(el) {
98
112
}
99
113
100
114
/**
101
- * validate if the given textarea from a form, is non-empty.
102
- * @param {jQuery | HTMLElement } form
103
- * @param {jQuery | HTMLElement } textarea
115
+ * validate if the given EasyMDE textarea is is non-empty.
116
+ * @param {jQuery } $textarea
104
117
* @returns {boolean } returns true if validation succeeded.
105
118
*/
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 ( ) ) ;
119
+ export function validateTextareaNonEmpty ( $textarea ) {
120
+ const $mdeInputField = $ ( getAttachedEasyMDE ( $textarea ) . codemirror . getInputField ( ) ) ;
115
121
// The original edit area HTML element is hidden and replaced by the
116
122
// SimpleMDE/EasyMDE editor, breaking HTML5 input validation if the text area is empty.
117
123
// This is a workaround for this upstream bug.
118
124
// See https://github.com/sparksuite/simplemde-markdown-editor/issues/324
119
- if ( textarea . value . length ) {
120
- $markdownEditorTextArea . prop ( 'required' , true ) ;
121
- form . reportValidity ( ) ;
125
+ if ( ! $textarea . val ( ) ) {
126
+ $mdeInputField . prop ( 'required' , true ) ;
127
+ const $form = $textarea . parents ( 'form' ) ;
128
+ if ( ! $form . length ) {
129
+ // this should never happen. we put a alert here in case the textarea would be forgotten to be put in a form
130
+ alert ( 'Require non-empty content' ) ;
131
+ } else {
132
+ $form [ 0 ] . reportValidity ( ) ;
133
+ }
122
134
return false ;
123
135
}
124
- $markdownEditorTextArea . prop ( 'required' , false ) ;
136
+ $mdeInputField . prop ( 'required' , false ) ;
125
137
return true ;
126
138
}
0 commit comments