diff --git a/app/javascript/controllers/player_controller.js b/app/javascript/controllers/player_controller.js index 362edcb..b75b26c 100644 --- a/app/javascript/controllers/player_controller.js +++ b/app/javascript/controllers/player_controller.js @@ -1,5 +1,6 @@ import { Controller } from "@hotwired/stimulus"; import FakeAudio from "fake_audio"; +import { useHotkeys } from "stimulus-use/hotkeys" function secondsToDuration(num) { let mins = Math.floor(num / 60); @@ -20,6 +21,10 @@ export default class extends Controller { this.handleTimeUpdate = this.handleTimeUpdate.bind(this); this.handleEnded = this.handleEnded.bind(this); this.playing = false; + + useHotkeys(this, { + 'space': [this.toggle], + }) } trackValueChanged() { @@ -59,6 +64,10 @@ export default class extends Controller { } } + toggle() { + this.playing ? this.pause() : this.play(); + } + play() { this.element.classList.add(this.playingClass); this.audio.play(); diff --git a/config/importmap.rb b/config/importmap.rb index 446c8cb..285be35 100644 --- a/config/importmap.rb +++ b/config/importmap.rb @@ -10,3 +10,5 @@ pin "@hotwired/stimulus", to: "https://ga.jspm.io/npm:@hotwired/stimulus@3.2.1/dist/stimulus.js" pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true pin_all_from "app/javascript/controllers", under: "controllers" +pin "stimulus-use/hotkeys", to: "https://ga.jspm.io/npm:stimulus-use@0.52.0/dist/hotkeys.js" +pin "hotkeys-js", to: "https://ga.jspm.io/npm:hotkeys-js@3.11.2/dist/hotkeys.esm.js"