@@ -75,13 +75,27 @@ export function createCommentEasyMDE(textarea) {
7575 }
7676 } ) ;
7777 attachTribute ( inputField , { mentions : true , emoji : true } ) ;
78+ attachEasyMDEToElements ( easyMDE ) ;
79+ return easyMDE ;
80+ }
7881
82+ /**
83+ * attach the EasyMDE object to its input elements (InputField, TextArea)
84+ * @param {EasyMDE } easyMDE
85+ */
86+ export function attachEasyMDEToElements ( easyMDE ) {
7987 // 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 ( ) ;
8091 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 ;
8396}
8497
98+
8599/**
86100 * get the attached EasyMDE editor created by createCommentEasyMDE
87101 * @param el jQuery or HTMLElement
@@ -98,29 +112,27 @@ export function getAttachedEasyMDE(el) {
98112}
99113
100114/**
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
104117 * @returns {boolean } returns true if validation succeeded.
105118 */
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 ( ) ) ;
115121 // The original edit area HTML element is hidden and replaced by the
116122 // SimpleMDE/EasyMDE editor, breaking HTML5 input validation if the text area is empty.
117123 // This is a workaround for this upstream bug.
118124 // 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+ }
122134 return false ;
123135 }
124- $markdownEditorTextArea . prop ( 'required' , false ) ;
136+ $mdeInputField . prop ( 'required' , false ) ;
125137 return true ;
126138}
0 commit comments