From 2c0fc67ab9a7e6091f75ed1e44aca7ea0a413b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sat, 23 Sep 2017 21:59:39 -0400 Subject: [PATCH] Use Mousetrap for image viewer shortcuts --- client/js/renderPreview.js | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/client/js/renderPreview.js b/client/js/renderPreview.js index ab73b070..1ae515a0 100644 --- a/client/js/renderPreview.js +++ b/client/js/renderPreview.js @@ -5,6 +5,7 @@ const options = require("./options"); const socket = require("./socket"); const templates = require("../views"); const input = $("#input"); +const Mousetrap = require("mousetrap"); module.exports = renderPreview; @@ -111,21 +112,12 @@ imageViewer.on("click", function(event, data = {}) { closeImageViewer(data); }); -$(document).keydown(function(e) { - switch (e.keyCode ? e.keyCode : e.which) { - case 27: // Escape - closeImageViewer(); - break; - case 37: // Left arrow - if (imageViewer.hasClass("opened")) { - imageViewer.find(".previous-image-btn").click(); - } - break; - case 39: // Right arrow - if (imageViewer.hasClass("opened")) { - imageViewer.find(".next-image-btn").click(); - } - break; +Mousetrap.bind("esc", () => closeImageViewer()); + +Mousetrap.bind(["left", "right"], (e, key) => { + if (imageViewer.hasClass("opened")) { + const direction = key === "left" ? "previous" : "next"; + imageViewer.find(`.${direction}-image-btn`).click(); } });