keyboard.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. // XXX There is no reason to define these shortcuts globally, they should be
  3. // attached to a template (most of them will go in the `board` template).
  4. function getHoveredCardId() {
  5. const card = $('.js-minicard:hover').get(0);
  6. if (!card) return null;
  7. return Blaze.getData(card)._id;
  8. }
  9. function getSelectedCardId() {
  10. return Session.get('currentCard') || Session.get('selectedCard') || getHoveredCardId();
  11. }
  12. Mousetrap.bind('?', () => {
  13. FlowRouter.go('shortcuts');
  14. });
  15. Mousetrap.bind('w', () => {
  16. if (Sidebar.isOpen() && Sidebar.getView() === 'home') {
  17. Sidebar.toggle();
  18. } else {
  19. Sidebar.setView();
  20. }
  21. });
  22. Mousetrap.bind('q', () => {
  23. const currentBoardId = Session.get('currentBoard');
  24. const currentUserId = Meteor.userId();
  25. if (currentBoardId && currentUserId) {
  26. Filter.members.toggle(currentUserId);
  27. }
  28. });
  29. Mousetrap.bind('a', () => {
  30. const currentBoardId = Session.get('currentBoard');
  31. const currentUserId = Meteor.userId();
  32. if (currentBoardId && currentUserId) {
  33. Filter.assignees.toggle(currentUserId);
  34. }
  35. });
  36. Mousetrap.bind('x', () => {
  37. if (Filter.isActive()) {
  38. Filter.reset();
  39. }
  40. });
  41. Mousetrap.bind('f', () => {
  42. if (Sidebar.isOpen() && Sidebar.getView() === 'filter') {
  43. Sidebar.toggle();
  44. } else {
  45. Sidebar.setView('filter');
  46. }
  47. });
  48. Mousetrap.bind('/', () => {
  49. if (Sidebar.isOpen() && Sidebar.getView() === 'search') {
  50. Sidebar.toggle();
  51. } else {
  52. Sidebar.setView('search');
  53. }
  54. });
  55. Mousetrap.bind(['down', 'up'], (evt, key) => {
  56. if (!Utils.getCurrentCardId()) {
  57. return;
  58. }
  59. const nextFunc = key === 'down' ? 'next' : 'prev';
  60. const nextCard = $('.js-minicard.is-selected')
  61. [nextFunc]('.js-minicard')
  62. .get(0);
  63. if (nextCard) {
  64. const nextCardId = Blaze.getData(nextCard)._id;
  65. Utils.goCardId(nextCardId);
  66. }
  67. });
  68. numbArray = _.range(1,10).map(x => 'shift+'+String(x))
  69. Mousetrap.bind(numbArray, (evt, key) => {
  70. num = parseInt(key.substr(6, key.length));
  71. const currentUserId = Meteor.userId();
  72. if (currentUserId === null) {
  73. return;
  74. }
  75. const currentBoardId = Session.get('currentBoard');
  76. board = ReactiveCache.getBoard(currentBoardId);
  77. labels = board.labels;
  78. if(MultiSelection.isActive())
  79. {
  80. const cardIds = MultiSelection.getSelectedCardIds();
  81. for (const cardId of cardIds)
  82. {
  83. card = ReactiveCache.getCard(cardId);
  84. if(num <= board.labels.length)
  85. {
  86. card.removeLabel(labels[num-1]["_id"]);
  87. }
  88. }
  89. }
  90. });
  91. numArray = _.range(1,10).map(x => String(x))
  92. Mousetrap.bind(numArray, (evt, key) => {
  93. num = parseInt(key);
  94. const currentUserId = Meteor.userId();
  95. const currentBoardId = Session.get('currentBoard');
  96. if (currentUserId === null) {
  97. return;
  98. }
  99. board = ReactiveCache.getBoard(currentBoardId);
  100. labels = board.labels;
  101. if(MultiSelection.isActive() && ReactiveCache.getCurrentUser().isBoardMember())
  102. {
  103. const cardIds = MultiSelection.getSelectedCardIds();
  104. for (const cardId of cardIds)
  105. {
  106. card = ReactiveCache.getCard(cardId);
  107. if(num <= board.labels.length)
  108. {
  109. card.addLabel(labels[num-1]["_id"]);
  110. }
  111. }
  112. return;
  113. }
  114. const cardId = getSelectedCardId();
  115. if (!cardId) {
  116. return;
  117. }
  118. if (ReactiveCache.getCurrentUser().isBoardMember()) {
  119. const card = ReactiveCache.getCard(cardId);
  120. if(num <= board.labels.length)
  121. {
  122. card.toggleLabel(labels[num-1]["_id"]);
  123. }
  124. }
  125. });
  126. Mousetrap.bind('space', evt => {
  127. const cardId = getSelectedCardId();
  128. if (!cardId) {
  129. return;
  130. }
  131. const currentUserId = Meteor.userId();
  132. if (currentUserId === null) {
  133. return;
  134. }
  135. if (ReactiveCache.getCurrentUser().isBoardMember()) {
  136. const card = ReactiveCache.getCard(cardId);
  137. card.toggleMember(currentUserId);
  138. // We should prevent scrolling in card when spacebar is clicked
  139. // This should do it according to Mousetrap docs, but it doesn't
  140. evt.preventDefault();
  141. }
  142. });
  143. Mousetrap.bind('c', evt => {
  144. const cardId = getSelectedCardId();
  145. if (!cardId) {
  146. return;
  147. }
  148. const currentUserId = Meteor.userId();
  149. if (currentUserId === null) {
  150. return;
  151. }
  152. if (Utils.canModifyBoard()) {
  153. const card = ReactiveCache.getCard(cardId);
  154. card.archive();
  155. // We should prevent scrolling in card when spacebar is clicked
  156. // This should do it according to Mousetrap docs, but it doesn't
  157. evt.preventDefault();
  158. }
  159. });
  160. Template.keyboardShortcuts.helpers({
  161. mapping: [
  162. {
  163. keys: ['w'],
  164. action: 'shortcut-toggle-sidebar',
  165. },
  166. {
  167. keys: ['q'],
  168. action: 'shortcut-filter-my-cards',
  169. },
  170. {
  171. keys: ['a'],
  172. action: 'shortcut-filter-my-assigned-cards',
  173. },
  174. {
  175. keys: ['f'],
  176. action: 'shortcut-toggle-filterbar',
  177. },
  178. {
  179. keys: ['/'],
  180. action: 'shortcut-toggle-searchbar',
  181. },
  182. {
  183. keys: ['x'],
  184. action: 'shortcut-clear-filters',
  185. },
  186. {
  187. keys: ['?'],
  188. action: 'shortcut-show-shortcuts',
  189. },
  190. {
  191. keys: ['ESC'],
  192. action: 'shortcut-close-dialog',
  193. },
  194. {
  195. keys: ['@'],
  196. action: 'shortcut-autocomplete-members',
  197. },
  198. {
  199. keys: ['SPACE'],
  200. action: 'shortcut-assign-self',
  201. },
  202. {
  203. keys: ['c'],
  204. action: 'archive-card',
  205. },
  206. {
  207. keys: ['number keys 1-9'],
  208. action: 'toggle-labels'
  209. },
  210. {
  211. keys: ['shift + number keys 1-9'],
  212. action: 'remove-labels-multiselect'
  213. },
  214. ],
  215. });