From 5e0698b7f3925a3046c72662ab7f692e3f892bce Mon Sep 17 00:00:00 2001 From: Molkobain Date: Sat, 1 Oct 2022 16:16:25 +0200 Subject: [PATCH] Add support for "Ctrl + Enter" and "Meta (Cmd) + Enter" submit on multi-line fields --- application/cmdbabstract.class.inc.php | 11 +++++- .../portal/public/js/portal_form_handler.js | 12 ++++++ .../portal/templates/layout.html.twig | 4 +- js/ckeditor.on-init.js | 12 ++++++ .../activity-panel/caselog-entry-form.js | 38 ++++++++++++------- 5 files changed, 61 insertions(+), 16 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index bff4791a6..f67c75426 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -2278,7 +2278,7 @@ JS {$sValidationSpan}{$sReloadSpan} HTML; $oPage->add_ready_script( - << - {# Hilighter for code snippets created with CKEditor #} + {# - Hilighter for code snippets created with CKEditor #} + {# - Custom settings #} + {# Date-time picker for Bootstrap #} {# Typeahead files for autocomplete #} diff --git a/js/ckeditor.on-init.js b/js/ckeditor.on-init.js index 4e519d82f..71570aff9 100644 --- a/js/ckeditor.on-init.js +++ b/js/ckeditor.on-init.js @@ -6,6 +6,18 @@ // WARNING: This code cannot be placed directly within the page as CKEditor could not be loaded yet. // As it can be loaded from an XHR call (several times), we need to ensure it will be called when necessary (see PHP WebResourcesHelper) +CKEDITOR.on('instanceReady', function (oEvent) { + // Keyboard listener + oEvent.editor.on('key', function(oKeyEvent){ + const oKeyboardEvent = oKeyEvent.data.domEvent.$; + + // Submit value on "Ctrl + Enter" or "Meta (Cmd) + Enter" keyboard shortcut + if ((oKeyboardEvent.ctrlKey || oKeyboardEvent.metaKey) && oKeyboardEvent.key === 'Enter') { + $('#'+ oEvent.editor.name).closest('form').trigger('submit'); + } + }); +}); + // For disabling the CKEditor at init time when the corresponding textarea is disabled ! if ((CKEDITOR !== undefined) && (CKEDITOR.plugins.registered['disabler'] === undefined)) { CKEDITOR.plugins.add( 'disabler', diff --git a/js/layouts/activity-panel/caselog-entry-form.js b/js/layouts/activity-panel/caselog-entry-form.js index dd524838d..3d87f5959 100644 --- a/js/layouts/activity-panel/caselog-entry-form.js +++ b/js/layouts/activity-panel/caselog-entry-form.js @@ -84,20 +84,32 @@ $(function() { // Handlers for the CKEditor itself CKEDITOR.on('instanceReady', function (oEvent) { // Handle only the current CKEditor instance - if (oEvent.editor.name === me.options.text_input_id) { - // Update depending elements on change - // Note: That when images are uploaded, the "change" event is triggered before the image upload is complete, meaning that we don't have the tag yet. - me._GetCKEditorInstance().on('change', function () { - const bWasDraftBefore = me.is_draft; - const bIsDraftNow = !me._IsInputEmpty(); - - if (bWasDraftBefore !== bIsDraftNow) { - me.is_draft = bIsDraftNow; - me._UpdateEditingVisualHint(); - // Note: We must not call me._UpdateSubmitButtonState() as it will be updated by the disable_submission/enable_submission events - } - }); + if (oEvent.editor.name !== me.options.text_input_id) { + return; } + + // Update depending elements on change + // Note: That when images are uploaded, the "change" event is triggered before the image upload is complete, meaning that we don't have the tag yet. + me._GetCKEditorInstance().on('change', function () { + const bWasDraftBefore = me.is_draft; + const bIsDraftNow = !me._IsInputEmpty(); + + if (bWasDraftBefore !== bIsDraftNow) { + me.is_draft = bIsDraftNow; + me._UpdateEditingVisualHint(); + // Note: We must not call me._UpdateSubmitButtonState() as it will be updated by the disable_submission/enable_submission events + } + }); + + // Dispatch submission to the right pipeline on submit + $(me.element).on('submit', function (oSubmitEvent) { + oSubmitEvent.preventDefault(); + if (me._IsSubmitAutonomous()) { + me._RequestSubmission(); + } else { + me._GetGeneralFormElement().trigger('submit'); + } + }); }); if (false === this._IsSubmitAutonomous()) {