Skip to content

Make Ctrl+Enter (quick submit) work for issue comment and wiki editor #19729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion services/forms/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ func (f *CodeCommentForm) Validate(req *http.Request, errs binding.Errors) bindi
// SubmitReviewForm for submitting a finished code review
type SubmitReviewForm struct {
Content string
Type string `binding:"Required;In(approve,comment,reject)"`
Type string
CommitID string
Files []string
}
Expand All @@ -643,6 +643,8 @@ func (f SubmitReviewForm) ReviewType() models.ReviewType {
return models.ReviewTypeComment
case "reject":
return models.ReviewTypeReject
case "":
return models.ReviewTypeComment // default to comment
default:
return models.ReviewTypeUnknown
}
Expand Down
12 changes: 10 additions & 2 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ export function initFootLanguageMenu() {


export function initGlobalEnterQuickSubmit() {
$('.js-quick-submit').on('keydown', function (e) {
$(document).on('keydown', '.js-quick-submit', function (e) {
if (((e.ctrlKey && !e.altKey) || e.metaKey) && (e.keyCode === 13 || e.keyCode === 10)) {
$(this).closest('form').trigger('submit');
const $form = $(this).closest('form');
// the same logic as `codeMirrorQuickSubmit` function
if ($form.length) {
// 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
$form.trigger('submit');
} else {
// 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.
$(e.target).trigger('ce-quick-submit');
}
}
});
}
Expand Down
21 changes: 20 additions & 1 deletion web_src/js/features/comp/EasyMDE.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ export async function createCommentEasyMDE(textarea, easyMDEOptions = {}) {
title: 'Revert to simple textarea',
},
], ...easyMDEOptions});

const inputField = easyMDE.codemirror.getInputField();
inputField.classList.add('js-quick-submit');

easyMDE.codemirror.setOption('extraKeys', {
'Cmd-Enter': codeMirrorQuickSubmit,
'Ctrl-Enter': codeMirrorQuickSubmit,
Enter: (cm) => {
const tributeContainer = document.querySelector('.tribute-container');
if (!tributeContainer || tributeContainer.style.display === 'none') {
Expand Down Expand Up @@ -149,3 +152,19 @@ export function validateTextareaNonEmpty($textarea) {
$mdeInputField.prop('required', false);
return true;
}

/**
* @param {CodeMirror.EditorFromTextArea} codeMirror
*/
export function codeMirrorQuickSubmit(codeMirror) {
const textarea = codeMirror.getTextArea();
const form = textarea.closest('form');
// the same logic as the global `.js-quick-submit` handler.
if (form) {
// 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
$(form).trigger('submit');
} else {
// 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.
textarea.dispatchEvent(new CustomEvent('ce-quick-submit'));
}
}
10 changes: 8 additions & 2 deletions web_src/js/features/repo-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,20 @@ async function onEditContent(event) {
initEasyMDEImagePaste(easyMDE, $dropzone[0], $dropzone.find('.files'));
}

const $saveButton = $editContentZone.find('.save.button');
$textarea.on('ce-quick-submit', () => {
$saveButton.trigger('click');
});

$editContentZone.find('.cancel.button').on('click', () => {
$renderContent.show();
$editContentZone.hide();
if (dz) {
dz.emit('reload');
}
});
$editContentZone.find('.save.button').on('click', () => {

$saveButton.on('click', () => {
$renderContent.show();
$editContentZone.hide();
const $attachments = $dropzone.find('.files').find('[name=files]').map(function () {
Expand Down Expand Up @@ -400,7 +406,7 @@ async function onEditContent(event) {
initCommentContent();
});
});
} else {
} else { // use existing form
$textarea = $segment.find('textarea');
easyMDE = getAttachedEasyMDE($textarea);
}
Expand Down
15 changes: 11 additions & 4 deletions web_src/js/features/repo-wiki.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import $ from 'jquery';
import {initMarkupContent} from '../markup/content.js';
import {attachEasyMDEToElements, importEasyMDE, validateTextareaNonEmpty} from './comp/EasyMDE.js';
import {
attachEasyMDEToElements,
codeMirrorQuickSubmit,
importEasyMDE,
validateTextareaNonEmpty,
} from './comp/EasyMDE.js';
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';

const {csrfToken} = window.config;
Expand Down Expand Up @@ -122,10 +127,12 @@ async function initRepoWikiFormEditor() {
]
});

attachEasyMDEToElements(easyMDE);
easyMDE.codemirror.setOption('extraKeys', {
'Cmd-Enter': codeMirrorQuickSubmit,
'Ctrl-Enter': codeMirrorQuickSubmit,
});

const $mdeInputField = $(easyMDE.codemirror.getInputField());
$mdeInputField.addClass('js-quick-submit');
attachEasyMDEToElements(easyMDE);

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