keyboard.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // XXX There is no reason to define these shortcuts globally, they should be
  2. // attached to a template (most of them will go in the `board` template).
  3. function getHoveredCardId() {
  4. const card = $('.js-minicard:hover').get(0);
  5. if (!card) return null;
  6. return Blaze.getData(card)._id;
  7. }
  8. function getSelectedCardId() {
  9. return Session.get('selectedCard') || getHoveredCardId();
  10. }
  11. Mousetrap.bind('?', () => {
  12. FlowRouter.go('shortcuts');
  13. });
  14. Mousetrap.bind('w', () => {
  15. if (Sidebar.isOpen() && Sidebar.getView() === 'home') {
  16. Sidebar.toggle();
  17. } else {
  18. Sidebar.setView();
  19. }
  20. });
  21. Mousetrap.bind('q', () => {
  22. const currentBoardId = Session.get('currentBoard');
  23. const currentUserId = Meteor.userId();
  24. if (currentBoardId && currentUserId) {
  25. Filter.members.toggle(currentUserId);
  26. }
  27. });
  28. Mousetrap.bind('x', () => {
  29. if (Filter.isActive()) {
  30. Filter.reset();
  31. }
  32. });
  33. Mousetrap.bind('f', () => {
  34. if (Sidebar.isOpen() && Sidebar.getView() === 'filter') {
  35. Sidebar.toggle();
  36. } else {
  37. Sidebar.setView('filter');
  38. }
  39. });
  40. Mousetrap.bind('/', () => {
  41. if (Sidebar.isOpen() && Sidebar.getView() === 'search') {
  42. Sidebar.toggle();
  43. } else {
  44. Sidebar.setView('search');
  45. }
  46. });
  47. Mousetrap.bind(['down', 'up'], (evt, key) => {
  48. if (!Utils.getCurrentCardId()) {
  49. return;
  50. }
  51. const nextFunc = key === 'down' ? 'next' : 'prev';
  52. const nextCard = $('.js-minicard.is-selected')
  53. [nextFunc]('.js-minicard')
  54. .get(0);
  55. if (nextCard) {
  56. const nextCardId = Blaze.getData(nextCard)._id;
  57. Utils.goCardId(nextCardId);
  58. }
  59. });
  60. Mousetrap.bind('space', evt => {
  61. const cardId = getSelectedCardId();
  62. if (!cardId) {
  63. return;
  64. }
  65. const currentUserId = Meteor.userId();
  66. if (currentUserId === null) {
  67. return;
  68. }
  69. if (Meteor.user().isBoardMember()) {
  70. const card = Cards.findOne(cardId);
  71. card.toggleMember(currentUserId);
  72. // We should prevent scrolling in card when spacebar is clicked
  73. // This should do it according to Mousetrap docs, but it doesn't
  74. evt.preventDefault();
  75. }
  76. });
  77. Mousetrap.bind('c', evt => {
  78. const cardId = getSelectedCardId();
  79. if (!cardId) {
  80. return;
  81. }
  82. const currentUserId = Meteor.userId();
  83. if (currentUserId === null) {
  84. return;
  85. }
  86. if (
  87. Meteor.user().isBoardMember() &&
  88. !Meteor.user().isCommentOnly() &&
  89. !Meteor.user().isWorker()
  90. ) {
  91. const card = Cards.findOne(cardId);
  92. card.archive();
  93. // We should prevent scrolling in card when spacebar is clicked
  94. // This should do it according to Mousetrap docs, but it doesn't
  95. evt.preventDefault();
  96. }
  97. });
  98. Template.keyboardShortcuts.helpers({
  99. mapping: [
  100. {
  101. keys: ['w'],
  102. action: 'shortcut-toggle-sidebar',
  103. },
  104. {
  105. keys: ['q'],
  106. action: 'shortcut-filter-my-cards',
  107. },
  108. {
  109. keys: ['f'],
  110. action: 'shortcut-toggle-filterbar',
  111. },
  112. {
  113. keys: ['/'],
  114. action: 'shortcut-toggle-searchbar',
  115. },
  116. {
  117. keys: ['x'],
  118. action: 'shortcut-clear-filters',
  119. },
  120. {
  121. keys: ['?'],
  122. action: 'shortcut-show-shortcuts',
  123. },
  124. {
  125. keys: ['ESC'],
  126. action: 'shortcut-close-dialog',
  127. },
  128. {
  129. keys: ['@'],
  130. action: 'shortcut-autocomplete-members',
  131. },
  132. {
  133. keys: ['SPACE'],
  134. action: 'shortcut-assign-self',
  135. },
  136. {
  137. keys: ['c'],
  138. action: 'archive-card',
  139. },
  140. ],
  141. });