Skip to content

Commit 91f23cd

Browse files
committed
Merge duplicate code
1 parent cf430d6 commit 91f23cd

File tree

7 files changed

+29
-33
lines changed

7 files changed

+29
-33
lines changed

services/forms/repo_form.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ func (f *SubmitReviewForm) Validate(req *http.Request, errs binding.Errors) bind
634634
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
635635
}
636636

637-
// ReviewType will return the corresponding reviewtype for type
637+
// ReviewType will return the corresponding ReviewType for type
638638
func (f SubmitReviewForm) ReviewType() models.ReviewType {
639639
switch f.Type {
640640
case "approve":
@@ -644,7 +644,7 @@ func (f SubmitReviewForm) ReviewType() models.ReviewType {
644644
case "reject":
645645
return models.ReviewTypeReject
646646
case "":
647-
return models.ReviewTypeComment // default to comment
647+
return models.ReviewTypeComment // default to comment when doing quick-submit (Ctrl+Enter) on the review form
648648
default:
649649
return models.ReviewTypeUnknown
650650
}

templates/repo/diff/box.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
<a class="preview item" data-url="{{$.Repository.HTMLURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
181181
</div>
182182
<div class="ui bottom attached active write tab segment">
183-
<textarea class="review-textarea" tabindex="1" name="content"></textarea>
183+
<textarea class="review-textarea js-quick-submit" tabindex="1" name="content"></textarea>
184184
</div>
185185
<div class="ui bottom attached tab preview segment markup">
186186
{{$.i18n.Tr "loading"}}

templates/repo/issue/view_content.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
</div>
203203
<div class="field">
204204
<div class="ui bottom active tab write">
205-
<textarea tabindex="1" name="content"></textarea>
205+
<textarea tabindex="1" name="content" class="js-quick-submit"></textarea>
206206
</div>
207207
<div class="ui bottom tab preview markup">
208208
{{$.i18n.Tr "loading"}}

templates/user/settings/keys_ssh.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</div>
2121
<div class="field {{if .Err_Content}}error{{end}}">
2222
<label for="content">{{.i18n.Tr "settings.key_content"}}</label>
23-
<textarea id="ssh-key-content" name="content" placeholder="{{.i18n.Tr "settings.key_content_ssh_placeholder"}}" required>{{.content}}</textarea>
23+
<textarea id="ssh-key-content" name="content" class="js-quick-submit" placeholder="{{.i18n.Tr "settings.key_content_ssh_placeholder"}}" required>{{.content}}</textarea>
2424
</div>
2525
<input name="type" type="hidden" value="ssh">
2626
<button class="ui green button">
@@ -81,7 +81,7 @@
8181
</div>
8282
<div class="field">
8383
<label for="signature">{{$.i18n.Tr "settings.ssh_token_signature"}}</label>
84-
<textarea id="ssh-key-signature" name="signature" placeholder="{{$.i18n.Tr "settings.key_signature_ssh_placeholder"}}" required>{{$.signature}}</textarea>
84+
<textarea id="ssh-key-signature" name="signature" class="js-quick-submit" placeholder="{{$.i18n.Tr "settings.key_signature_ssh_placeholder"}}" required>{{$.signature}}</textarea>
8585
</div>
8686
<input name="type" type="hidden" value="verify_ssh">
8787
<button class="ui green button">

web_src/js/features/common-global.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,28 @@ export function initFootLanguageMenu() {
4444

4545

4646
export function initGlobalEnterQuickSubmit() {
47-
$(document).on('keydown', '.js-quick-submit', function (e) {
48-
if (((e.ctrlKey && !e.altKey) || e.metaKey) && (e.keyCode === 13 || e.keyCode === 10)) {
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-
}
47+
$(document).on('keydown', '.js-quick-submit', (e) => {
48+
if (((e.ctrlKey && !e.altKey) || e.metaKey) && (e.key === 'Enter')) {
49+
handleGlobalEnterQuickSubmit(e.target);
50+
return false;
5851
}
5952
});
6053
}
6154

55+
export function handleGlobalEnterQuickSubmit(target) {
56+
const $target = $(target);
57+
const $form = $(target).closest('form');
58+
if ($form.length) {
59+
// here use the event to trigger the submit event (instead of calling `submit()` method directly)
60+
// otherwise the `areYouSure` handler won't be executed, then there will be an annoying "confirm to leave" dialog
61+
$form.trigger('submit');
62+
} else {
63+
// if no form, then the editor is for an AJAX request, dispatch an event to the target, let the target's event handler to do the AJAX request.
64+
// the 'ce-' prefix means this is a CustomEvent
65+
$target.trigger('ce-quick-submit');
66+
}
67+
}
68+
6269
export function initGlobalButtonClickOnEnter() {
6370
$(document).on('keypress', '.ui.button', (e) => {
6471
if (e.keyCode === 13 || e.keyCode === 32) { // enter key or space bar

web_src/js/features/comp/EasyMDE.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import $ from 'jquery';
22
import attachTribute from '../tribute.js';
3+
import {handleGlobalEnterQuickSubmit} from '../common-global.js';
34

45
/**
56
* @returns {EasyMDE}
@@ -154,17 +155,10 @@ export function validateTextareaNonEmpty($textarea) {
154155
}
155156

156157
/**
158+
* there is no guarantee that the CodeMirror object is inside the same form as the textarea,
159+
* so can not call handleGlobalEnterQuickSubmit directly.
157160
* @param {CodeMirror.EditorFromTextArea} codeMirror
158161
*/
159162
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-
}
163+
handleGlobalEnterQuickSubmit(codeMirror.getTextArea());
170164
}

web_src/js/features/repo-wiki.js

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

116
const {csrfToken} = window.config;

0 commit comments

Comments
 (0)