From a462fcaac8825e992c99e3298a28c649682c0c92 Mon Sep 17 00:00:00 2001 From: David Jimenez Date: Wed, 27 Oct 2021 05:33:22 +0100 Subject: [PATCH] Show client-side error if wiki page is empty (#17415) * fix: show client-side error if wiki page is empty Implement a JS, client-side validation workaround for a bug in the upstream editor library SimpleMDE which breaks HTML5 client-side validation when a wiki page is submitted. This allows native, client-side errors to appear if the text editor contents are empty. See upstream bugfix report: https://github.com/sparksuite/simplemde-markdown-editor/issues/324 Signed-off-by: David Jimenez Co-authored-by: Lauris BH --- templates/repo/wiki/new.tmpl | 2 +- web_src/js/features/repo-wiki.js | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/templates/repo/wiki/new.tmpl b/templates/repo/wiki/new.tmpl index 6de6ef9a2..d887d8ffa 100644 --- a/templates/repo/wiki/new.tmpl +++ b/templates/repo/wiki/new.tmpl @@ -22,7 +22,7 @@
- +
diff --git a/web_src/js/features/repo-wiki.js b/web_src/js/features/repo-wiki.js index ddd4e30a8..1acdb4da4 100644 --- a/web_src/js/features/repo-wiki.js +++ b/web_src/js/features/repo-wiki.js @@ -8,7 +8,9 @@ export function initRepoWikiForm() { let sideBySideChanges = 0; let sideBySideTimeout = null; let hasSimpleMDE = true; + if ($editArea.length > 0) { + const $form = $('.repository.wiki.new .ui.form'); const simplemde = new SimpleMDE({ autoDownloadFontAwesome: false, element: $editArea[0], @@ -105,7 +107,6 @@ export function initRepoWikiForm() { action(e) { e.toTextArea(); hasSimpleMDE = false; - const $form = $('.repository.wiki.new .ui.form'); const $root = $form.find('.field.content'); const loading = $root.data('loading'); $root.append(`
${loading}
`); @@ -116,7 +117,24 @@ export function initRepoWikiForm() { }, ] }); - $(simplemde.codemirror.getInputField()).addClass('js-quick-submit'); + + const $markdownEditorTextArea = $(simplemde.codemirror.getInputField()); + $markdownEditorTextArea.addClass('js-quick-submit'); + + $form.on('submit', function (e) { + // The original edit area HTML element is hidden and replaced by the + // SimpleMDE editor, breaking HTML5 input validation if the text area is empty. + // This is a workaround for this upstream bug. + // See https://github.com/sparksuite/simplemde-markdown-editor/issues/324 + const input = $editArea.val(); + if (!input.length) { + e.preventDefault(); + $markdownEditorTextArea.prop('required', true); + this.reportValidity(); + } else { + $markdownEditorTextArea.prop('required', false); + } + }); setTimeout(() => { const $bEdit = $('.repository.wiki.new .previewtabs a[data-tab="write"]');