Skip to content

Commit

Permalink
Merge duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed May 17, 2022
1 parent cf430d6 commit 91f23cd
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 33 deletions.
4 changes: 2 additions & 2 deletions services/forms/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ func (f *SubmitReviewForm) Validate(req *http.Request, errs binding.Errors) bind
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}

// ReviewType will return the corresponding reviewtype for type
// ReviewType will return the corresponding ReviewType for type
func (f SubmitReviewForm) ReviewType() models.ReviewType {
switch f.Type {
case "approve":
Expand All @@ -644,7 +644,7 @@ func (f SubmitReviewForm) ReviewType() models.ReviewType {
case "reject":
return models.ReviewTypeReject
case "":
return models.ReviewTypeComment // default to comment
return models.ReviewTypeComment // default to comment when doing quick-submit (Ctrl+Enter) on the review form
default:
return models.ReviewTypeUnknown
}
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/diff/box.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
<a class="preview item" data-url="{{$.Repository.HTMLURL}}/markdown" data-context="{{$.RepoLink}}">{{$.i18n.Tr "preview"}}</a>
</div>
<div class="ui bottom attached active write tab segment">
<textarea class="review-textarea" tabindex="1" name="content"></textarea>
<textarea class="review-textarea js-quick-submit" tabindex="1" name="content"></textarea>
</div>
<div class="ui bottom attached tab preview segment markup">
{{$.i18n.Tr "loading"}}
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/issue/view_content.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
</div>
<div class="field">
<div class="ui bottom active tab write">
<textarea tabindex="1" name="content"></textarea>
<textarea tabindex="1" name="content" class="js-quick-submit"></textarea>
</div>
<div class="ui bottom tab preview markup">
{{$.i18n.Tr "loading"}}
Expand Down
4 changes: 2 additions & 2 deletions templates/user/settings/keys_ssh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</div>
<div class="field {{if .Err_Content}}error{{end}}">
<label for="content">{{.i18n.Tr "settings.key_content"}}</label>
<textarea id="ssh-key-content" name="content" placeholder="{{.i18n.Tr "settings.key_content_ssh_placeholder"}}" required>{{.content}}</textarea>
<textarea id="ssh-key-content" name="content" class="js-quick-submit" placeholder="{{.i18n.Tr "settings.key_content_ssh_placeholder"}}" required>{{.content}}</textarea>
</div>
<input name="type" type="hidden" value="ssh">
<button class="ui green button">
Expand Down Expand Up @@ -81,7 +81,7 @@
</div>
<div class="field">
<label for="signature">{{$.i18n.Tr "settings.ssh_token_signature"}}</label>
<textarea id="ssh-key-signature" name="signature" placeholder="{{$.i18n.Tr "settings.key_signature_ssh_placeholder"}}" required>{{$.signature}}</textarea>
<textarea id="ssh-key-signature" name="signature" class="js-quick-submit" placeholder="{{$.i18n.Tr "settings.key_signature_ssh_placeholder"}}" required>{{$.signature}}</textarea>
</div>
<input name="type" type="hidden" value="verify_ssh">
<button class="ui green button">
Expand Down
29 changes: 18 additions & 11 deletions web_src/js/features/common-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,28 @@ export function initFootLanguageMenu() {


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

export function handleGlobalEnterQuickSubmit(target) {
const $target = $(target);
const $form = $(target).closest('form');
if ($form.length) {
// here use the event to trigger the submit event (instead of calling `submit()` method directly)
// 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, dispatch an event to the target, let the target's event handler to do the AJAX request.
// the 'ce-' prefix means this is a CustomEvent
$target.trigger('ce-quick-submit');
}
}

export function initGlobalButtonClickOnEnter() {
$(document).on('keypress', '.ui.button', (e) => {
if (e.keyCode === 13 || e.keyCode === 32) { // enter key or space bar
Expand Down
14 changes: 4 additions & 10 deletions web_src/js/features/comp/EasyMDE.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import $ from 'jquery';
import attachTribute from '../tribute.js';
import {handleGlobalEnterQuickSubmit} from '../common-global.js';

/**
* @returns {EasyMDE}
Expand Down Expand Up @@ -154,17 +155,10 @@ export function validateTextareaNonEmpty($textarea) {
}

/**
* there is no guarantee that the CodeMirror object is inside the same form as the textarea,
* so can not call handleGlobalEnterQuickSubmit directly.
* @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'));
}
handleGlobalEnterQuickSubmit(codeMirror.getTextArea());
}
7 changes: 1 addition & 6 deletions web_src/js/features/repo-wiki.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import $ from 'jquery';
import {initMarkupContent} from '../markup/content.js';
import {
attachEasyMDEToElements,
codeMirrorQuickSubmit,
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

0 comments on commit 91f23cd

Please sign in to comment.