Module: HotkeysRails::Helper
- Defined in:
- lib/hotkeys_rails/helper.rb
Instance Method Summary collapse
- #button_tag(content_or_options = nil, options = nil, &block) ⇒ Object
- #button_to(name = nil, options = nil, html_options = nil, &block) ⇒ Object
-
#hotkey(*keys, action: :click) ⇒ Object
Returns data attributes for Stimulus hotkey controller.
-
#hotkey_hint(*keys) ⇒ Object
Renders element with hide-on-touch class.
-
#hotkey_label(*keys) ⇒ Object
Platform-aware label for keyboard shortcuts.
- #link_to(name = nil, options = nil, html_options = nil, &block) ⇒ Object
Instance Method Details
#button_tag(content_or_options = nil, options = nil, &block) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/hotkeys_rails/helper.rb', line 72 def ( = nil, = nil, &block) if .is_a?(Hash) = extract_hotkey_option() super(nil, , &block) else = extract_hotkey_option() super(, , &block) end end |
#button_to(name = nil, options = nil, html_options = nil, &block) ⇒ Object
67 68 69 70 |
# File 'lib/hotkeys_rails/helper.rb', line 67 def (name = nil, = nil, = nil, &block) = extract_hotkey_option() super end |
#hotkey(*keys, action: :click) ⇒ Object
Returns data attributes for Stimulus hotkey controller
hotkey(:esc)
=> { controller: "hotkey", action: "keydown.esc@document->hotkey#click" }
hotkey(:ctrl, :enter)
=> { controller: "hotkey", action: "keydown.ctrl+enter@document->hotkey#click keydown.meta+enter@document->hotkey#click" }
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/hotkeys_rails/helper.rb', line 11 def hotkey(*keys, action: :click) keys = keys.flatten.map(&:to_s) actions = if keys.include?("ctrl") chord = keys.join("+") = keys.map { |k| k == "ctrl" ? "meta" : k }.join("+") "keydown.#{chord}@document->hotkey##{action} keydown.#{}@document->hotkey##{action}" else "keydown.#{keys.join("+")}@document->hotkey##{action}" end { controller: "hotkey", action: actions } end |
#hotkey_hint(*keys) ⇒ Object
Renders element with hide-on-touch class
hotkey_hint(:ctrl, :s)
=> ⌘S
51 52 53 |
# File 'lib/hotkeys_rails/helper.rb', line 51 def hotkey_hint(*keys) content_tag(:kbd, hotkey_label(*keys), class: "hide-on-touch") end |
#hotkey_label(*keys) ⇒ Object
Platform-aware label for keyboard shortcuts
hotkey_label(:ctrl, :enter)
=> "⌘Return" on Mac, "Ctrl+Enter" elsewhere
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hotkeys_rails/helper.rb', line 30 def hotkey_label(*keys) keys.flatten.map do |key| case key.to_s when "ctrl" then mac? ? "⌘" : "Ctrl+" when "meta" then mac? ? "⌘" : "Win+" when "alt" then mac? ? "⌥" : "Alt+" when "shift" then mac? ? "⇧" : "Shift+" when "enter" then mac? ? "Return" : "Enter" when "esc" then "Esc" else key.to_s.upcase end # Mac symbols (⌘⌥⇧) don't need + separator, Windows modifiers do (Ctrl+, Alt+, Shift+) # gsub removes trailing + from Mac symbols: "⌘+" -> "⌘" end.join.gsub(/[⌘⌥⇧]\+/, &:chop) end |
#link_to(name = nil, options = nil, html_options = nil, &block) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/hotkeys_rails/helper.rb', line 55 def link_to(name = nil, = nil, = nil, &block) if block_given? # link_to(url, html_options) { content } = extract_hotkey_option() super(name, , &block) else # link_to(name, url, html_options) = extract_hotkey_option() super(name, , ) end end |