keyboard.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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('currentCard') || 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. numbArray = _.range(1,10).map(x => 'shift+'+String(x))
  61. Mousetrap.bind(numbArray, (evt, key) => {
  62. num = parseInt(key.substr(6, key.length));
  63. const currentUserId = Meteor.userId();
  64. if (currentUserId === null) {
  65. return;
  66. }
  67. const currentBoardId = Session.get('currentBoard');
  68. board = Boards.findOne(currentBoardId);
  69. labels = board.labels;
  70. if(MultiSelection.isActive())
  71. {
  72. const cardIds = MultiSelection.getSelectedCardIds();
  73. for (const cardId of cardIds)
  74. {
  75. card = Cards.findOne(cardId);
  76. if(num <= board.labels.length)
  77. {
  78. card.removeLabel(labels[num-1]["_id"]);
  79. }
  80. }
  81. }
  82. });
  83. numArray = _.range(1,10).map(x => String(x))
  84. Mousetrap.bind(numArray, (evt, key) => {
  85. num = parseInt(key);
  86. const currentUserId = Meteor.userId();
  87. const currentBoardId = Session.get('currentBoard');
  88. if (currentUserId === null) {
  89. return;
  90. }
  91. board = Boards.findOne(currentBoardId);
  92. labels = board.labels;
  93. if(MultiSelection.isActive() && Meteor.user().isBoardMember())
  94. {
  95. const cardIds = MultiSelection.getSelectedCardIds();
  96. for (const cardId of cardIds)
  97. {
  98. card = Cards.findOne(cardId);
  99. if(num <= board.labels.length)
  100. {
  101. card.addLabel(labels[num-1]["_id"]);
  102. }
  103. }
  104. return;
  105. }
  106. const cardId = getSelectedCardId();
  107. if (!cardId) {
  108. return;
  109. }
  110. if (Meteor.user().isBoardMember()) {
  111. const card = Cards.findOne(cardId);
  112. if(num <= board.labels.length)
  113. {
  114. card.toggleLabel(labels[num-1]["_id"]);
  115. }
  116. }
  117. });
  118. Mousetrap.bind('space', evt => {
  119. const cardId = getSelectedCardId();
  120. if (!cardId) {
  121. return;
  122. }
  123. const currentUserId = Meteor.userId();
  124. if (currentUserId === null) {
  125. return;
  126. }
  127. if (Meteor.user().isBoardMember()) {
  128. const card = Cards.findOne(cardId);
  129. card.toggleMember(currentUserId);
  130. // We should prevent scrolling in card when spacebar is clicked
  131. // This should do it according to Mousetrap docs, but it doesn't
  132. evt.preventDefault();
  133. }
  134. });
  135. Mousetrap.bind('c', evt => {
  136. const cardId = getSelectedCardId();
  137. if (!cardId) {
  138. return;
  139. }
  140. const currentUserId = Meteor.userId();
  141. if (currentUserId === null) {
  142. return;
  143. }
  144. if (
  145. Meteor.user().isBoardMember() &&
  146. !Meteor.user().isCommentOnly() &&
  147. !Meteor.user().isWorker()
  148. ) {
  149. const card = Cards.findOne(cardId);
  150. card.archive();
  151. // We should prevent scrolling in card when spacebar is clicked
  152. // This should do it according to Mousetrap docs, but it doesn't
  153. evt.preventDefault();
  154. }
  155. });
  156. Template.keyboardShortcuts.helpers({
  157. mapping: [
  158. {
  159. keys: ['w'],
  160. action: 'shortcut-toggle-sidebar',
  161. },
  162. {
  163. keys: ['q'],
  164. action: 'shortcut-filter-my-cards',
  165. },
  166. {
  167. keys: ['f'],
  168. action: 'shortcut-toggle-filterbar',
  169. },
  170. {
  171. keys: ['/'],
  172. action: 'shortcut-toggle-searchbar',
  173. },
  174. {
  175. keys: ['x'],
  176. action: 'shortcut-clear-filters',
  177. },
  178. {
  179. keys: ['?'],
  180. action: 'shortcut-show-shortcuts',
  181. },
  182. {
  183. keys: ['ESC'],
  184. action: 'shortcut-close-dialog',
  185. },
  186. {
  187. keys: ['@'],
  188. action: 'shortcut-autocomplete-members',
  189. },
  190. {
  191. keys: ['SPACE'],
  192. action: 'shortcut-assign-self',
  193. },
  194. {
  195. keys: ['c'],
  196. action: 'archive-card',
  197. },
  198. {
  199. keys: ['number keys 1-9'],
  200. action: 'toggle-labels'
  201. },
  202. {
  203. keys: ['shift + number keys 1-9'],
  204. action: 'remove-labels-multiselect'
  205. },
  206. ],
  207. });