From 78e2311f59b1ac59c8e31bb8d0f27bf4a678f79f Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 29 Dec 2023 14:56:44 +1100 Subject: [PATCH] Add keyboard shortcut to submit form in admin Unfortunately it doesn't work for Angular forms. I'm not sure if there's a way to trigger an angular submit. --- app/webpacker/js/hotkeys.js | 20 ++++++++++++++++++++ app/webpacker/packs/admin.js | 1 + 2 files changed, 21 insertions(+) create mode 100644 app/webpacker/js/hotkeys.js 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";