From: Kohei Yoshino Date: Mon, 15 Oct 2018 20:40:30 +0000 (-0400) Subject: Bug 1009716 - Add (Cmd|Ctrl)+Enter shortcut for submitting from text areas. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c67628b72796e7dda25daecaac1879be86f9738;p=thirdparty%2Fbugzilla.git Bug 1009716 - Add (Cmd|Ctrl)+Enter shortcut for submitting from text areas. --- diff --git a/js/field.js b/js/field.js index db2f9ac39..ec802b202 100644 --- a/js/field.js +++ b/js/field.js @@ -974,3 +974,34 @@ function show_comment_edit() { YAHOO.util.Dom.addClass(comment_tab, 'active_comment_tab'); comment_tab.setAttribute('aria-selected', 'true'); } + +/** + * Comment form keyboard shortcuts + */ + +window.addEventListener('DOMContentLoaded', () => { + const on_mac = navigator.platform === 'MacIntel'; + const $comment = document.querySelector('#comment'); + const $save_button = document.querySelector('.save-btn, #commit'); + + if (!$comment || !$save_button) { + return; + } + + $comment.addEventListener('keydown', event => { + const { isComposing, key, altKey, ctrlKey, metaKey, shiftKey } = event; + const accelKey = on_mac ? metaKey && !ctrlKey : ctrlKey; + const has_value = /\S/.test($comment.value); + + if (isComposing) { + return; + } + + // Accel + Enter = Save + if (has_value && key === 'Enter' && accelKey && !altKey && !shiftKey) { + event.preventDefault(); + // Click the Save button to trigger the `submit` event handler + $save_button.click(); + } + }); +}, { once: true });