keyboard.js 5.1 KB

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