From d31c3536461b29564890180b46b424ab1690140d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20H=C3=B8jberg?= Date: Tue, 8 Jun 2021 17:03:29 -0400 Subject: [PATCH] Add preventDefault for global Finder shortcuts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Firefox has some clashes with the keyboard shortcuts of the Finder that can't be prevented from within Elm; add a simple— slightly hackish— event handler on the JavaScript side to preventDefault on the specific Finder keyboard events. --- src/CodebaseTree.elm | 1 - src/preventDefaultGlobalKeyboardEvents.js | 25 +++++++++++++++++++++++ src/ucm.js | 3 +++ src/unisonShare.js | 3 +++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/preventDefaultGlobalKeyboardEvents.js diff --git a/src/CodebaseTree.elm b/src/CodebaseTree.elm index dd21de8..c015db3 100644 --- a/src/CodebaseTree.elm +++ b/src/CodebaseTree.elm @@ -86,7 +86,6 @@ update env msg model = |> RemoteData.withDefault False newModel = - -- TODO: Update to Loading { model | expandedNamespaceListings = FQNSet.toggle fqn model.expandedNamespaceListings , rootNamespaceListing = nextNamespaceListing diff --git a/src/preventDefaultGlobalKeyboardEvents.js b/src/preventDefaultGlobalKeyboardEvents.js new file mode 100644 index 0000000..08b3256 --- /dev/null +++ b/src/preventDefaultGlobalKeyboardEvents.js @@ -0,0 +1,25 @@ +/* + * Firefox has binds for Ctrl+k, Cmd+k, and "/" We want to use those to open + * the Finder. + * + * Unfortunately we can't do this in Elm, since Browser.Events doesn't support + * preventDefault, so we're duplicating the shortcuts and calling + * preventDefault in JS. The Elm handler picks up the event later on. + * + * TODO: This is a bit brittle and relies on the Elm handler being added after + * this one. There might be a better solution build with ports for this. + */ + +function preventDefaultGlobalKeyboardEvents() { + window.addEventListener("keydown", (ev) => { + if ( + ev.key === "/" || + (ev.metaKey && ev.key == "k") || + (ev.ctrlKey && ev.key == "k") + ) { + ev.preventDefault(); + } + }); +} + +export default preventDefaultGlobalKeyboardEvents; diff --git a/src/ucm.js b/src/ucm.js index 67e277c..82d8dea 100644 --- a/src/ucm.js +++ b/src/ucm.js @@ -1,5 +1,6 @@ import "./init"; import detectOs from "./detectOs"; +import preventDefaultGlobalKeyboardEvents from "./preventDefaultGlobalKeyboardEvents"; import { Elm } from "./Ucm.elm"; const basePath = new URL(document.baseURI).pathname; @@ -22,5 +23,7 @@ const flags = { appContext: "Ucm", }; +preventDefaultGlobalKeyboardEvents(); + // The main entry point for the `ucm` target of the Codebase UI. Elm.Ucm.init({ flags }); diff --git a/src/unisonShare.js b/src/unisonShare.js index 1bf64fb..d695fb7 100644 --- a/src/unisonShare.js +++ b/src/unisonShare.js @@ -1,5 +1,6 @@ import "./init"; import detectOs from "./detectOs"; +import preventDefaultGlobalKeyboardEvents from "./preventDefaultGlobalKeyboardEvents"; import { Elm } from "./UnisonShare.elm"; const basePath = new URL(document.baseURI).pathname; @@ -12,5 +13,7 @@ const flags = { appContext: "UnisonShare", }; +preventDefaultGlobalKeyboardEvents(); + // The main entry point for the `UnisonShare` target of the Codebase UI. Elm.UnisonShare.init({ flags });