diff --git a/app/webpacker/js/hotkeys.js b/app/webpacker/js/hotkeys.js new file mode 100644 index 0000000000..6a2c3a7b12 --- /dev/null +++ b/app/webpacker/js/hotkeys.js @@ -0,0 +1,20 @@ +import hotkeys from "hotkeys-js"; + +// Enable hotkeys on form elements +hotkeys.filter = function (event) { + var tagName = (event.target || event.srcElement).tagName; + hotkeys.setScope(/^(INPUT|TEXTAREA|SELECT|BUTTON)$/.test(tagName) ? "input" : "other"); + return true; +}; + +// Submit form +// Although 'enter' will submit the form in many cases, it doesn't cover elements such +// as select and textarea. This shortcut is a standard used across many major websites. +hotkeys("ctrl+enter, command+enter", function (event, handler) { + const form = event.target.form; + + // If element has a non-angular form + if (form && !form.classList.contains("ng")) { + form.submit(); + } +}); diff --git a/app/webpacker/packs/admin.js b/app/webpacker/packs/admin.js index 2980fdf0de..81735d3040 100644 --- a/app/webpacker/packs/admin.js +++ b/app/webpacker/packs/admin.js @@ -1,6 +1,7 @@ import "controllers"; import "channels"; import "@hotwired/turbo"; +import "../js/hotkeys"; import "../js/mrujs"; import "../js/matomo"; import "../js/moment";