diff --git a/assets/controllers/shortcuts_controller.js b/assets/controllers/shortcuts_controller.js new file mode 100644 index 000000000..3d5c44fb5 --- /dev/null +++ b/assets/controllers/shortcuts_controller.js @@ -0,0 +1,104 @@ +import { Controller } from '@hotwired/stimulus'; +import Mousetrap from 'mousetrap'; + +export default class extends Controller { + static targets = ['openOriginal', 'markAsFavorite', 'markAsRead', 'deleteEntry', 'showAddUrl', 'showSearch', 'showActions']; + + connect() { + /* Go to */ + Mousetrap.bind('g u', () => { + window.location.href = Routing.generate('homepage'); + }); + Mousetrap.bind('g s', () => { + window.location.href = Routing.generate('starred'); + }); + Mousetrap.bind('g r', () => { + window.location.href = Routing.generate('archive'); + }); + Mousetrap.bind('g a', () => { + window.location.href = Routing.generate('all'); + }); + Mousetrap.bind('g t', () => { + window.location.href = Routing.generate('tag'); + }); + Mousetrap.bind('g c', () => { + window.location.href = Routing.generate('config'); + }); + Mousetrap.bind('g i', () => { + window.location.href = Routing.generate('import'); + }); + Mousetrap.bind('g d', () => { + window.location.href = Routing.generate('developer'); + }); + Mousetrap.bind('?', () => { + window.location.href = Routing.generate('howto'); + }); + Mousetrap.bind('g l', () => { + window.location.href = Routing.generate('fos_user_security_logout'); + }); + + /* open original article */ + Mousetrap.bind('o', () => { + if (!this.hasOpenOriginalTarget) { + return; + } + + this.openOriginalTarget.click(); + }); + + /* mark as favorite */ + Mousetrap.bind('f', () => { + if (!this.hasMarkAsFavoriteTarget) { + return; + } + + this.markAsFavoriteTarget.click(); + }); + + /* mark as read */ + Mousetrap.bind('a', () => { + if (!this.hasMarkAsReadTarget) { + return; + } + + this.markAsReadTarget.click(); + }); + + /* delete */ + Mousetrap.bind('del', () => { + if (!this.hasDeleteEntryTarget) { + return; + } + + this.deleteEntryTarget.click(); + }); + + /* Actions */ + Mousetrap.bind('g n', (e) => { + if (!this.hasShowAddUrlTarget) { + return; + } + + e.preventDefault(); + this.showAddUrlTarget.click(); + }); + + Mousetrap.bind('s', (e) => { + if (!this.hasShowSearchTarget) { + return; + } + + e.preventDefault(); + this.showSearchTarget.click(); + }); + + Mousetrap.bind('esc', (e) => { + if (!this.hasShowActionsTarget) { + return; + } + + e.preventDefault(); + this.showActionsTarget.click(); + }); + } +} diff --git a/assets/index.js b/assets/index.js index 706667eec..04b76a01f 100755 --- a/assets/index.js +++ b/assets/index.js @@ -17,7 +17,6 @@ import '@fontsource/oswald'; /* Import shortcuts */ import './js/shortcuts/main'; -import './js/shortcuts/entry'; /* Theme style */ import './scss/index.scss'; diff --git a/assets/js/shortcuts/entry.js b/assets/js/shortcuts/entry.js deleted file mode 100644 index 3c15f3d29..000000000 --- a/assets/js/shortcuts/entry.js +++ /dev/null @@ -1,26 +0,0 @@ -import Mousetrap from 'mousetrap'; -import $ from 'jquery'; - -$(document).ready(() => { - if ($('#article').length > 0) { - /* open original article */ - Mousetrap.bind('o', () => { - $('ul.sidenav a.original i')[0].click(); - }); - - /* mark as favorite */ - Mousetrap.bind('f', () => { - $('ul.sidenav a.favorite i')[0].click(); - }); - - /* mark as read */ - Mousetrap.bind('a', () => { - $('ul.sidenav a.markasread i')[0].click(); - }); - - /* delete */ - Mousetrap.bind('del', () => { - $('ul.sidenav a.delete i')[0].click(); - }); - } -}); diff --git a/assets/js/shortcuts/main.js b/assets/js/shortcuts/main.js index 8033d9651..2cf96dba9 100644 --- a/assets/js/shortcuts/main.js +++ b/assets/js/shortcuts/main.js @@ -1,18 +1,6 @@ import Mousetrap from 'mousetrap'; import $ from 'jquery'; -/* Go to */ -Mousetrap.bind('g u', () => { window.location.href = Routing.generate('homepage'); }); -Mousetrap.bind('g s', () => { window.location.href = Routing.generate('starred'); }); -Mousetrap.bind('g r', () => { window.location.href = Routing.generate('archive'); }); -Mousetrap.bind('g a', () => { window.location.href = Routing.generate('all'); }); -Mousetrap.bind('g t', () => { window.location.href = Routing.generate('tag'); }); -Mousetrap.bind('g c', () => { window.location.href = Routing.generate('config'); }); -Mousetrap.bind('g i', () => { window.location.href = Routing.generate('import'); }); -Mousetrap.bind('g d', () => { window.location.href = Routing.generate('developer'); }); -Mousetrap.bind('?', () => { window.location.href = Routing.generate('howto'); }); -Mousetrap.bind('g l', () => { window.location.href = Routing.generate('fos_user_security_logout'); }); - function toggleFocus(cardToToogleFocus) { if (cardToToogleFocus) { $(cardToToogleFocus).toggleClass('z-depth-4'); @@ -48,21 +36,6 @@ $(document).ready(() => { /* Focus current card */ toggleFocus(card); - /* Actions */ - Mousetrap.bind('g n', () => { - $('#nav-btn-add').trigger('click'); - return false; - }); - - Mousetrap.bind('s', () => { - $('#nav-btn-search').trigger('click'); - return false; - }); - - Mousetrap.bind('esc', () => { - $('.close').trigger('click'); - }); - /* Select right card. If there's a next page, go to next page */ Mousetrap.bind('right', () => { if (cardIndex >= 0 && cardIndex < cardNumber - 1) { diff --git a/templates/Entry/entry.html.twig b/templates/Entry/entry.html.twig index b1c969097..c702c2351 100644 --- a/templates/Entry/entry.html.twig +++ b/templates/Entry/entry.html.twig @@ -52,7 +52,7 @@