Skip to content

Commit 1c894c0

Browse files
committed
Make Ctrl+Enter (quick submit) work for issue comment and wiki editor
1 parent bcf13b6 commit 1c894c0

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

web_src/js/features/common-global.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ export function initFootLanguageMenu() {
4444

4545

4646
export function initGlobalEnterQuickSubmit() {
47-
$('.js-quick-submit').on('keydown', function (e) {
47+
$(document).on('keydown', '.js-quick-submit', function (e) {
4848
if (((e.ctrlKey && !e.altKey) || e.metaKey) && (e.keyCode === 13 || e.keyCode === 10)) {
49-
$(this).closest('form').trigger('submit');
49+
const $form = $(this).closest('form');
50+
// the same logic as `codeMirrorQuickSubmit` function
51+
if ($form.length) {
52+
// here must use jQuery to trigger the submit event, otherwise the `areYouSure` handler won't be executed, then there will be an annoying "confirm to leave" dialog
53+
$form.trigger('submit');
54+
} else {
55+
// if no form, then the editor is for an AJAX request, we dispatch an event to the target, let the target's event handler to do the AJAX request.
56+
$(e.target).trigger('ce-quick-submit');
57+
}
5058
}
5159
});
5260
}

web_src/js/features/comp/EasyMDE.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ export async function createCommentEasyMDE(textarea, easyMDEOptions = {}) {
7171
title: 'Revert to simple textarea',
7272
},
7373
], ...easyMDEOptions});
74+
7475
const inputField = easyMDE.codemirror.getInputField();
75-
inputField.classList.add('js-quick-submit');
76+
7677
easyMDE.codemirror.setOption('extraKeys', {
78+
'Cmd-Enter': codeMirrorQuickSubmit,
79+
'Ctrl-Enter': codeMirrorQuickSubmit,
7780
Enter: (cm) => {
7881
const tributeContainer = document.querySelector('.tribute-container');
7982
if (!tributeContainer || tributeContainer.style.display === 'none') {
@@ -149,3 +152,19 @@ export function validateTextareaNonEmpty($textarea) {
149152
$mdeInputField.prop('required', false);
150153
return true;
151154
}
155+
156+
/**
157+
* @param {CodeMirror.EditorFromTextArea} codeMirror
158+
*/
159+
export function codeMirrorQuickSubmit(codeMirror) {
160+
const textarea = codeMirror.getTextArea();
161+
const form = textarea.closest('form');
162+
// the same logic as the global `.js-quick-submit` handler.
163+
if (form) {
164+
// here must use jQuery to trigger the submit event, otherwise the `areYouSure` handler won't be executed, then there will be an annoying "confirm to leave" dialog
165+
$(form).trigger('submit');
166+
} else {
167+
// if no form, then the editor is for an AJAX request, we dispatch an event to the textarea, let the textarea's event handler to do the AJAX request.
168+
textarea.dispatchEvent(new CustomEvent('ce-quick-submit'));
169+
}
170+
}

web_src/js/features/repo-legacy.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,20 @@ async function onEditContent(event) {
355355
initEasyMDEImagePaste(easyMDE, $dropzone[0], $dropzone.find('.files'));
356356
}
357357

358+
const $saveButton = $editContentZone.find('.save.button');
359+
$textarea.on('ce-quick-submit', () => {
360+
$saveButton.trigger('click');
361+
});
362+
358363
$editContentZone.find('.cancel.button').on('click', () => {
359364
$renderContent.show();
360365
$editContentZone.hide();
361366
if (dz) {
362367
dz.emit('reload');
363368
}
364369
});
365-
$editContentZone.find('.save.button').on('click', () => {
370+
371+
$saveButton.on('click', () => {
366372
$renderContent.show();
367373
$editContentZone.hide();
368374
const $attachments = $dropzone.find('.files').find('[name=files]').map(function () {
@@ -400,7 +406,7 @@ async function onEditContent(event) {
400406
initCommentContent();
401407
});
402408
});
403-
} else {
409+
} else { // use existing form
404410
$textarea = $segment.find('textarea');
405411
easyMDE = getAttachedEasyMDE($textarea);
406412
}

web_src/js/features/repo-wiki.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import $ from 'jquery';
22
import {initMarkupContent} from '../markup/content.js';
3-
import {attachEasyMDEToElements, importEasyMDE, validateTextareaNonEmpty} from './comp/EasyMDE.js';
3+
import {
4+
attachEasyMDEToElements,
5+
codeMirrorQuickSubmit,
6+
importEasyMDE,
7+
validateTextareaNonEmpty,
8+
} from './comp/EasyMDE.js';
49
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
510

611
const {csrfToken} = window.config;
@@ -122,10 +127,12 @@ async function initRepoUncycloFormEditor() {
122127
]
123128
});
124129

125-
attachEasyMDEToElements(easyMDE);
130+
easyMDE.codemirror.setOption('extraKeys', {
131+
'Cmd-Enter': codeMirrorQuickSubmit,
132+
'Ctrl-Enter': codeMirrorQuickSubmit,
133+
});
126134

127-
const $mdeInputField = $(easyMDE.codemirror.getInputField());
128-
$mdeInputField.addClass('js-quick-submit');
135+
attachEasyMDEToElements(easyMDE);
129136

130137
$form.on('submit', () => {
131138
if (!validateTextareaNonEmpty($editArea)) {

0 commit comments

Comments
 (0)