Module: HotkeysRails::Helper

Defined in:
lib/hotkeys_rails/helper.rb

Instance Method Summary collapse

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 button_tag(content_or_options = nil, options = nil, &block)
  if content_or_options.is_a?(Hash)
    options = extract_hotkey_option(content_or_options)
    super(nil, options, &block)
  else
    options = extract_hotkey_option(options)
    super(content_or_options, options, &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 button_to(name = nil, options = nil, html_options = nil, &block)
  html_options = extract_hotkey_option(html_options)
  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("+")
    meta_chord = keys.map { |k| k == "ctrl" ? "meta" : k }.join("+")
    "keydown.#{chord}@document->hotkey##{action} keydown.#{meta_chord}@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)
  (: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


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hotkeys_rails/helper.rb', line 55

def link_to(name = nil, options = nil, html_options = nil, &block)
  if block_given?
    # link_to(url, html_options) { content }
    html_options = extract_hotkey_option(options)
    super(name, html_options, &block)
  else
    # link_to(name, url, html_options)
    html_options = extract_hotkey_option(html_options)
    super(name, options, html_options)
  end
end