keyboard.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. Mousetrap.bind('n', evt => {
  161. const cardId = getSelectedCardId();
  162. if (!cardId) {
  163. return;
  164. }
  165. const currentUserId = Meteor.userId();
  166. if (currentUserId === null) {
  167. return;
  168. }
  169. if (Utils.canModifyBoard()) {
  170. // Find the current hovered card
  171. const card = ReactiveCache.getCard(cardId);
  172. // Find the button and click it
  173. $(`#js-list-${card.listId} .list-body .minicards .open-minicard-composer`).click()
  174. // We should prevent scrolling in card when spacebar is clicked
  175. // This should do it according to Mousetrap docs, but it doesn't
  176. evt.preventDefault();
  177. }
  178. });
  179. Template.keyboardShortcuts.helpers({
  180. mapping: [
  181. {
  182. keys: ['w'],
  183. action: 'shortcut-toggle-sidebar',
  184. },
  185. {
  186. keys: ['q'],
  187. action: 'shortcut-filter-my-cards',
  188. },
  189. {
  190. keys: ['a'],
  191. action: 'shortcut-filter-my-assigned-cards',
  192. },
  193. {
  194. keys: ['n'],
  195. action: 'add-card-to-bottom-of-list',
  196. },
  197. {
  198. keys: ['f'],
  199. action: 'shortcut-toggle-filterbar',
  200. },
  201. {
  202. keys: ['/'],
  203. action: 'shortcut-toggle-searchbar',
  204. },
  205. {
  206. keys: ['x'],
  207. action: 'shortcut-clear-filters',
  208. },
  209. {
  210. keys: ['?'],
  211. action: 'shortcut-show-shortcuts',
  212. },
  213. {
  214. keys: ['ESC'],
  215. action: 'shortcut-close-dialog',
  216. },
  217. {
  218. keys: ['@'],
  219. action: 'shortcut-autocomplete-members',
  220. },
  221. {
  222. keys: ['SPACE'],
  223. action: 'shortcut-assign-self',
  224. },
  225. {
  226. keys: ['c'],
  227. action: 'archive-card',
  228. },
  229. {
  230. keys: ['number keys 1-9'],
  231. action: 'toggle-labels'
  232. },
  233. {
  234. keys: ['shift + number keys 1-9'],
  235. action: 'remove-labels-multiselect'
  236. },
  237. ],
  238. });