From 1ddfd73da6810029940949eda86e59b5fe65304c Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 22 Apr 2021 19:52:44 +1000 Subject: [PATCH] catch ctrl/cmd+enter to save settings --- ts/deckconfig/BUILD.bazel | 2 ++ ts/deckconfig/DeckConfigPage.svelte | 27 +++++++++++++++++++++------ ts/deckconfig/TextInputModal.svelte | 1 + ts/sveltelib/shortcuts.ts | 13 +++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 ts/sveltelib/shortcuts.ts diff --git a/ts/deckconfig/BUILD.bazel b/ts/deckconfig/BUILD.bazel index 7be31eff7..4c464a618 100644 --- a/ts/deckconfig/BUILD.bazel +++ b/ts/deckconfig/BUILD.bazel @@ -26,6 +26,7 @@ compile_svelte( name = "svelte", srcs = svelte_files, deps = [ + "//ts/sveltelib", "@npm//@types/bootstrap", ], ) @@ -94,6 +95,7 @@ esbuild( ":bootstrap-icons", "@npm//bootstrap", ":base_css", + "//ts/sveltelib", ] + svelte_names, ) diff --git a/ts/deckconfig/DeckConfigPage.svelte b/ts/deckconfig/DeckConfigPage.svelte index 8b252a729..2997930d1 100644 --- a/ts/deckconfig/DeckConfigPage.svelte +++ b/ts/deckconfig/DeckConfigPage.svelte @@ -6,8 +6,21 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import ConfigSelector from "./ConfigSelector.svelte"; import ConfigEditor from "./ConfigEditor.svelte"; import type { DeckConfigState } from "./lib"; + import { primaryModifierForPlatform } from "sveltelib/shortcuts"; export let state: DeckConfigState; + + function onKeyDown(evt: KeyboardEvent): void { + if ( + evt.code === "Enter" && + evt.getModifierState(primaryModifierForPlatform()) + ) { + state.save(false); + } else { + console.log(evt.getModifierState(primaryModifierForPlatform())); + console.log(primaryModifierForPlatform()); + } + } - +
+ - + -
- +
+ +
diff --git a/ts/deckconfig/TextInputModal.svelte b/ts/deckconfig/TextInputModal.svelte index d15b0d165..917e4dbed 100644 --- a/ts/deckconfig/TextInputModal.svelte +++ b/ts/deckconfig/TextInputModal.svelte @@ -33,6 +33,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html function onKeyUp(evt: KeyboardEvent): void { if (evt.code === "Enter") { + evt.stopPropagation(); onOkClicked(); } } diff --git a/ts/sveltelib/shortcuts.ts b/ts/sveltelib/shortcuts.ts new file mode 100644 index 000000000..80a01ad0c --- /dev/null +++ b/ts/sveltelib/shortcuts.ts @@ -0,0 +1,13 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +function isApplePlatform(): boolean { + return ( + window.navigator.platform.startsWith("Mac") || + window.navigator.platform.startsWith("iP") + ); +} + +export function primaryModifierForPlatform(): string { + return isApplePlatform() ? "Meta" : "Control"; +}