Closed harman28 closed 7 years ago
Hey @harman28! :wave:
Thanks for this PR! I'm a little skeptical about adding another dependency, though... Especially because it looks like Mousetrap hasn't been updated in the last year.
Instead of using Mousetrap, I implemented the same functionality in what I hope is easily understandable CoffeeScript using jQuery:
$ ->
handleKeyBindings()
$(document).on 'page:change', ->
handleKeyBindings()
handleKeyBindings = ->
# Elements that, when active, do not trigger a page change
blacklist = [ "input", "select" ]
# Key-to-url bindings
bindings = {}
# Store all `data-keybinding` attributes with their corresponding URLs
$('a[data-keybinding]').each (i, el) ->
key = $(el).data("keybinding")
bindings[key] = $(el).attr('href')
# Detect a keypress and navigate to the proper URL if appropriate
$('body').keypress (e) ->
if bindings[e.key] != undefined and !$(document.activeElement).is(blacklist.join(","))
window.location.href = bindings[e.key]
Would you mind migrating to this script instead? I think it's just simpler all around.
To do so, you can:
app/assets/javascripts/keybindings.js.coffee
to the code aboveapp/assets/stylesheets/mousetrap.scss
Gemfile
Gemfile.lock
app/assets/javascripts/application.js
app/assets/stylesheets/application.scss
Let me know what you think! 🤔
@J3RN Makes sense. I've made the changes. Please have another look whenever convenient.
@J3RN Made the changes you suggested. Have a look.
This looks good! Thanks, @harman28!
Added keybindings for a variety of actions, in response to #36
n
-> new entity form.enter
-> submit result page.1
-> Home view2
-> Tags index view3
-> Tasks index view4
-> Time Entries index view@J3RN Please have a look.