Skip to content
This repository was archived by the owner on Jul 19, 2022. It is now read-only.

Commit 04aa4c6

Browse files
authored
Merge pull request #146 from unisonweb/prevent-default-global-keyboard-events
Add preventDefault for global Finder shortcuts
2 parents dc30257 + d31c353 commit 04aa4c6

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

src/CodebaseTree.elm

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ update env msg model =
8686
|> RemoteData.withDefault False
8787

8888
newModel =
89-
-- TODO: Update to Loading
9089
{ model
9190
| expandedNamespaceListings = FQNSet.toggle fqn model.expandedNamespaceListings
9291
, rootNamespaceListing = nextNamespaceListing
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Firefox has binds for Ctrl+k, Cmd+k, and "/" We want to use those to open
3+
* the Finder.
4+
*
5+
* Unfortunately we can't do this in Elm, since Browser.Events doesn't support
6+
* preventDefault, so we're duplicating the shortcuts and calling
7+
* preventDefault in JS. The Elm handler picks up the event later on.
8+
*
9+
* TODO: This is a bit brittle and relies on the Elm handler being added after
10+
* this one. There might be a better solution build with ports for this.
11+
*/
12+
13+
function preventDefaultGlobalKeyboardEvents() {
14+
window.addEventListener("keydown", (ev) => {
15+
if (
16+
ev.key === "/" ||
17+
(ev.metaKey && ev.key == "k") ||
18+
(ev.ctrlKey && ev.key == "k")
19+
) {
20+
ev.preventDefault();
21+
}
22+
});
23+
}
24+
25+
export default preventDefaultGlobalKeyboardEvents;

src/ucm.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import "./init";
22
import detectOs from "./detectOs";
3+
import preventDefaultGlobalKeyboardEvents from "./preventDefaultGlobalKeyboardEvents";
34
import { Elm } from "./Ucm.elm";
45

56
const basePath = new URL(document.baseURI).pathname;
@@ -22,5 +23,7 @@ const flags = {
2223
appContext: "Ucm",
2324
};
2425

26+
preventDefaultGlobalKeyboardEvents();
27+
2528
// The main entry point for the `ucm` target of the Codebase UI.
2629
Elm.Ucm.init({ flags });

src/unisonShare.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import "./init";
22
import detectOs from "./detectOs";
3+
import preventDefaultGlobalKeyboardEvents from "./preventDefaultGlobalKeyboardEvents";
34
import { Elm } from "./UnisonShare.elm";
45

56
const basePath = new URL(document.baseURI).pathname;
@@ -12,5 +13,7 @@ const flags = {
1213
appContext: "UnisonShare",
1314
};
1415

16+
preventDefaultGlobalKeyboardEvents();
17+
1518
// The main entry point for the `UnisonShare` target of the Codebase UI.
1619
Elm.UnisonShare.init({ flags });

0 commit comments

Comments
 (0)