Skip to content

Commit a462fca

Browse files
dvejmzlafriks
andauthored
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: sparksuite/simplemde-markdown-editor#324 Signed-off-by: David Jimenez <[email protected]> Co-authored-by: Lauris BH <[email protected]>
1 parent 8f9ac43 commit a462fca

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

templates/repo/wiki/new.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</div>
2323
<div class="field content" data-loading="{{.i18n.Tr "loading"}}">
2424
<div class="ui bottom active tab" data-tab="write">
25-
<textarea class="js-quick-submit" id="edit_area" name="content" data-id="wiki-{{.title}}" data-url="{{.Repository.APIURL}}/markdown" data-context="{{.RepoLink}}" required>{{if .PageIsWikiEdit}}{{.content}}{{else}}{{.i18n.Tr "repo.wiki.welcome"}}{{end}}</textarea>
25+
<textarea class="js-quick-submit" id="edit_area" name="content" data-id="wiki-{{.title}}" data-url="{{.Repository.APIURL}}/markdown" data-context="{{.RepoLink}}">{{if .PageIsWikiEdit}}{{.content}}{{else}}{{.i18n.Tr "repo.wiki.welcome"}}{{end}}</textarea>
2626
</div>
2727
</div>
2828
<div class="field">

web_src/js/features/repo-wiki.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export function initRepoWikiForm() {
88
let sideBySideChanges = 0;
99
let sideBySideTimeout = null;
1010
let hasSimpleMDE = true;
11+
1112
if ($editArea.length > 0) {
13+
const $form = $('.repository.wiki.new .ui.form');
1214
const simplemde = new SimpleMDE({
1315
autoDownloadFontAwesome: false,
1416
element: $editArea[0],
@@ -105,7 +107,6 @@ export function initRepoWikiForm() {
105107
action(e) {
106108
e.toTextArea();
107109
hasSimpleMDE = false;
108-
const $form = $('.repository.wiki.new .ui.form');
109110
const $root = $form.find('.field.content');
110111
const loading = $root.data('loading');
111112
$root.append(`<div class="ui bottom tab markup" data-tab="preview">${loading}</div>`);
@@ -116,7 +117,24 @@ export function initRepoWikiForm() {
116117
},
117118
]
118119
});
119-
$(simplemde.codemirror.getInputField()).addClass('js-quick-submit');
120+
121+
const $markdownEditorTextArea = $(simplemde.codemirror.getInputField());
122+
$markdownEditorTextArea.addClass('js-quick-submit');
123+
124+
$form.on('submit', function (e) {
125+
// The original edit area HTML element is hidden and replaced by the
126+
// SimpleMDE 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+
}
137+
});
120138

121139
setTimeout(() => {
122140
const $bEdit = $('.repository.wiki.new .previewtabs a[data-tab="write"]');

0 commit comments

Comments
 (0)