diff --git a/Gemfile b/Gemfile index 64a32d01a..1d8d39cd7 100644 --- a/Gemfile +++ b/Gemfile @@ -27,6 +27,7 @@ gem "rqrcode" gem "redcarpet" gem "rouge" gem "jbuilder" +gem "platform_agent" # Telemetry and logging gem "sentry-ruby" diff --git a/Gemfile.lock b/Gemfile.lock index 3965d502b..7353afc2c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -272,6 +272,9 @@ GEM parser (3.3.8.0) ast (~> 2.4.1) racc + platform_agent (1.0.1) + activesupport (>= 5.2.0) + useragent (~> 0.16.3) pp (0.6.2) prettyprint prettyprint (0.2.0) @@ -456,6 +459,7 @@ DEPENDENCIES importmap-rails jbuilder kamal + platform_agent propshaft puma (>= 5.0) rails! diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4f75453ff..20385eda0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,5 @@ class ApplicationController < ActionController::Base - include Authentication, CurrentTimezone + include Authentication, CurrentTimezone, SetPlatform stale_when_importmap_changes allow_browser versions: :modern diff --git a/app/controllers/concerns/set_platform.rb b/app/controllers/concerns/set_platform.rb new file mode 100644 index 000000000..54e77d517 --- /dev/null +++ b/app/controllers/concerns/set_platform.rb @@ -0,0 +1,12 @@ +module SetPlatform + extend ActiveSupport::Concern + + included do + helper_method :platform + end + + private + def platform + @platform ||= ApplicationPlatform.new(request.user_agent) + end +end diff --git a/app/helpers/hotkeys_helper.rb b/app/helpers/hotkeys_helper.rb new file mode 100644 index 000000000..a1894104c --- /dev/null +++ b/app/helpers/hotkeys_helper.rb @@ -0,0 +1,19 @@ +module HotkeysHelper + def hotkey_label(hotkey) + hotkey.split(",").first if hotkey + case hotkey + when Array + hotkey.map { |key| + if key == "ctrl" && platform.mac? + "⌘" + elsif key == "enter" + platform.mac? ? "return" : "enter" + else + key + end + }.join("+") + else + hotkey.split(",").first if hotkey + end + end +end diff --git a/app/models/application_platform.rb b/app/models/application_platform.rb new file mode 100644 index 000000000..c8c24a50b --- /dev/null +++ b/app/models/application_platform.rb @@ -0,0 +1,29 @@ +class ApplicationPlatform < PlatformAgent + def ios? + match? /iPhone|iPad/ + end + + def android? + match? /Android/ + end + + def mac? + match? /Macintosh/ + end + + def chrome? + user_agent.browser.match? /Chrome/ + end + + def safari? + user_agent.browser.match? /Safari/ + end + + def mobile? + ios? || android? + end + + def desktop? + !mobile? + end +end diff --git a/app/views/cards/index/_header.html.erb b/app/views/cards/index/_header.html.erb index 7ec915b4d..499e70b45 100644 --- a/app/views/cards/index/_header.html.erb +++ b/app/views/cards/index/_header.html.erb @@ -7,7 +7,7 @@
- Press ⌘j anytime to open this, esc to close. + Press <%= hotkey_label(["ctrl", "J"]) -%> anytime to open this, esc to close.
<% end %>- Press ⌘j anytime to open this, esc to close. + Press <%= hotkey_label(["ctrl", "K"]) -%> anytime to open this, esc to close.
<% end %>