J3RN / time-tracker

A time tracking application
http://timesheet.j3rn.com
MIT License
6 stars 14 forks source link

Keybindings #79

Closed harman28 closed 7 years ago

harman28 commented 7 years ago

Added keybindings for a variety of actions, in response to #36

@J3RN Please have a look.

J3RN commented 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:

Let me know what you think! 🤔

harman28 commented 7 years ago

@J3RN Makes sense. I've made the changes. Please have another look whenever convenient.

harman28 commented 7 years ago

@J3RN Made the changes you suggested. Have a look.

J3RN commented 7 years ago

This looks good! Thanks, @harman28!