From 396f055900072b4426f7268593d0041a6dba573e Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 13 Mar 2024 11:41:19 +0100 Subject: [PATCH] Convert alchemy.hotkeys.coffee in ES module --- app/assets/javascripts/alchemy/admin.js | 1 - .../alchemy/alchemy.hotkeys.js.coffee | 49 --------------- app/javascript/alchemy_admin.js | 1 - app/javascript/alchemy_admin/gui.js | 4 +- app/javascript/alchemy_admin/hotkeys.js | 61 +++++++++++++++++++ 5 files changed, 64 insertions(+), 52 deletions(-) delete mode 100644 app/assets/javascripts/alchemy/alchemy.hotkeys.js.coffee create mode 100644 app/javascript/alchemy_admin/hotkeys.js diff --git a/app/assets/javascripts/alchemy/admin.js b/app/assets/javascripts/alchemy/admin.js index a2ece8899d..c691d02ca6 100644 --- a/app/assets/javascripts/alchemy/admin.js +++ b/app/assets/javascripts/alchemy/admin.js @@ -9,7 +9,6 @@ //= require alchemy/alchemy.elements_window //= require alchemy/alchemy.fixed_elements //= require alchemy/alchemy.growler -//= require alchemy/alchemy.hotkeys //= require alchemy/alchemy.image_overlay //= require alchemy/alchemy.link_dialog //= require alchemy/alchemy.list_filter diff --git a/app/assets/javascripts/alchemy/alchemy.hotkeys.js.coffee b/app/assets/javascripts/alchemy/alchemy.hotkeys.js.coffee deleted file mode 100644 index 8c3f03b702..0000000000 --- a/app/assets/javascripts/alchemy/alchemy.hotkeys.js.coffee +++ /dev/null @@ -1,49 +0,0 @@ -window.Alchemy = {} if typeof(window.Alchemy) is 'undefined' - -# Handles Alchemy hotkeys -# -Alchemy.bindedHotkeys = [] - -Alchemy.Hotkeys = (scope) -> - - # Unbind all previously registered hotkeys. - unless scope - $(document).off('keypress') - for hotkey in Alchemy.bindedHotkeys - key.unbind(hotkey) - - # Binds keyboard shortcuts to search fields. - $search_fields = $('.search_input_field', scope) - $search_fields_clear = $('.search_field_clear, .js_filter_field_clear', scope) - - key 'alt+f', -> - key.setScope('search') - $search_fields.focus() - false - Alchemy.bindedHotkeys.push('alt+f') - - key 'esc', 'search', -> - $search_fields_clear.click() - $search_fields.blur() - Alchemy.bindedHotkeys.push('esc') - - unless scope - $(document).on 'keypress', (e) -> - if !$(e.target).is('input, textarea') && String.fromCharCode(e.which) == '?' - Alchemy.openDialog '/admin/help', - title: Alchemy.t('help') - size: '400x492' - false - else - true - - # Binds click events to hotkeys. - # - # Simply add a data-alchemy-hotkey attribute to your link. - # If a hotkey is triggered by user, the click event of the element gets triggerd. - # - $('[data-alchemy-hotkey]', scope).each -> - $this = $(this) - hotkey = $this.data('alchemy-hotkey') - key hotkey, -> $this.click() - Alchemy.bindedHotkeys.push(hotkey) diff --git a/app/javascript/alchemy_admin.js b/app/javascript/alchemy_admin.js index 1d2e953846..787df7519d 100644 --- a/app/javascript/alchemy_admin.js +++ b/app/javascript/alchemy_admin.js @@ -1,6 +1,5 @@ import "@ungap/custom-elements" import "@hotwired/turbo-rails" -import "keymaster" import Rails from "@rails/ujs" diff --git a/app/javascript/alchemy_admin/gui.js b/app/javascript/alchemy_admin/gui.js index 83591deff3..ea04d9a3fc 100644 --- a/app/javascript/alchemy_admin/gui.js +++ b/app/javascript/alchemy_admin/gui.js @@ -1,8 +1,10 @@ +import Hotkeys from "alchemy_admin/hotkeys" + function init(scope) { if (!scope) { Alchemy.watchForDialogs() } - Alchemy.Hotkeys(scope) + Hotkeys(scope) Alchemy.ListFilter(scope) } diff --git a/app/javascript/alchemy_admin/hotkeys.js b/app/javascript/alchemy_admin/hotkeys.js new file mode 100644 index 0000000000..b3ca3b4b99 --- /dev/null +++ b/app/javascript/alchemy_admin/hotkeys.js @@ -0,0 +1,61 @@ +import "keymaster" + +Alchemy = window.Alchemy || {} +Alchemy.bindedHotkeys = [] + +export default function (scope) { + // Unbind all previously registered hotkeys. + if (!scope) { + $(document).off("keypress") + Alchemy.bindedHotkeys.forEach((hotkey) => key.unbind(hotkey)) + } + + // Binds keyboard shortcuts to search fields. + const $search_fields = $(".search_input_field", scope) + const $search_fields_clear = $( + ".search_field_clear, .js_filter_field_clear", + scope + ) + + key("alt+f", function () { + key.setScope("search") + $search_fields.focus() + return false + }) + Alchemy.bindedHotkeys.push("alt+f") + + key("esc", "search", function () { + $search_fields_clear.click() + $search_fields.blur() + }) + Alchemy.bindedHotkeys.push("esc") + + if (!scope) { + $(document).on("keypress", function (e) { + if ( + !$(e.target).is("input, textarea") && + String.fromCharCode(e.which) === "?" + ) { + Alchemy.openDialog("/admin/help", { + title: Alchemy.t("help"), + size: "400x492" + }) + return false + } else { + return true + } + }) + } + + // Binds click events to hotkeys. + // + // Simply add a data-alchemy-hotkey attribute to your link. + // If a hotkey is triggered by user, the click event of the element gets triggerd. + // + $("[data-alchemy-hotkey]", scope).each(function () { + const $this = $(this) + const hotkey = $this.data("alchemy-hotkey") + key(hotkey, () => $this.click()) + Alchemy.bindedHotkeys.push(hotkey) + }) +}