keyboard.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. window.addEventListener('keydown', (e) => {
  5. // Only handle event if coming from body
  6. if (e.target !== document.body) return;
  7. // Only handle event if it's in another language
  8. if (String.fromCharCode(e.which).toLowerCase() === e.key) return;
  9. // Trigger the corresponding action
  10. Mousetrap.trigger(String.fromCharCode(e.which).toLowerCase());
  11. });
  12. function getHoveredCardId() {
  13. const card = $('.js-minicard:hover').get(0);
  14. if (!card) return null;
  15. return Blaze.getData(card)._id;
  16. }
  17. function getSelectedCardId() {
  18. return Session.get('currentCard') || Session.get('selectedCard') || getHoveredCardId();
  19. }
  20. Mousetrap.bind('?', () => {
  21. FlowRouter.go('shortcuts');
  22. });
  23. Mousetrap.bind('w', () => {
  24. if (Sidebar.isOpen() && Sidebar.getView() === 'home') {
  25. Sidebar.toggle();
  26. } else {
  27. Sidebar.setView();
  28. }
  29. });
  30. Mousetrap.bind('q', () => {
  31. const currentBoardId = Session.get('currentBoard');
  32. const currentUserId = Meteor.userId();
  33. if (currentBoardId && currentUserId) {
  34. Filter.members.toggle(currentUserId);
  35. }
  36. });
  37. Mousetrap.bind('a', () => {
  38. const currentBoardId = Session.get('currentBoard');
  39. const currentUserId = Meteor.userId();
  40. if (currentBoardId && currentUserId) {
  41. Filter.assignees.toggle(currentUserId);
  42. }
  43. });
  44. Mousetrap.bind('x', () => {
  45. if (Filter.isActive()) {
  46. Filter.reset();
  47. }
  48. });
  49. Mousetrap.bind('f', () => {
  50. if (Sidebar.isOpen() && Sidebar.getView() === 'filter') {
  51. Sidebar.toggle();
  52. } else {
  53. Sidebar.setView('filter');
  54. }
  55. });
  56. Mousetrap.bind('/', () => {
  57. if (Sidebar.isOpen() && Sidebar.getView() === 'search') {
  58. Sidebar.toggle();
  59. } else {
  60. Sidebar.setView('search');
  61. }
  62. });
  63. Mousetrap.bind(['down', 'up'], (evt, key) => {
  64. if (!Utils.getCurrentCardId()) {
  65. return;
  66. }
  67. const nextFunc = key === 'down' ? 'next' : 'prev';
  68. const nextCard = $('.js-minicard.is-selected')
  69. [nextFunc]('.js-minicard')
  70. .get(0);
  71. if (nextCard) {
  72. const nextCardId = Blaze.getData(nextCard)._id;
  73. Utils.goCardId(nextCardId);
  74. }
  75. });
  76. numbArray = _.range(1,10).map(x => 'shift+'+String(x))
  77. Mousetrap.bind(numbArray, (evt, key) => {
  78. num = parseInt(key.substr(6, key.length));
  79. const currentUserId = Meteor.userId();
  80. if (currentUserId === null) {
  81. return;
  82. }
  83. const currentBoardId = Session.get('currentBoard');
  84. board = ReactiveCache.getBoard(currentBoardId);
  85. labels = board.labels;
  86. if(MultiSelection.isActive())
  87. {
  88. const cardIds = MultiSelection.getSelectedCardIds();
  89. for (const cardId of cardIds)
  90. {
  91. card = ReactiveCache.getCard(cardId);
  92. if(num <= board.labels.length)
  93. {
  94. card.removeLabel(labels[num-1]["_id"]);
  95. }
  96. }
  97. }
  98. });
  99. numArray = _.range(1,10).map(x => String(x))
  100. Mousetrap.bind(numArray, (evt, key) => {
  101. num = parseInt(key);
  102. const currentUserId = Meteor.userId();
  103. const currentBoardId = Session.get('currentBoard');
  104. if (currentUserId === null) {
  105. return;
  106. }
  107. board = ReactiveCache.getBoard(currentBoardId);
  108. labels = board.labels;
  109. if(MultiSelection.isActive() && ReactiveCache.getCurrentUser().isBoardMember())
  110. {
  111. const cardIds = MultiSelection.getSelectedCardIds();
  112. for (const cardId of cardIds)
  113. {
  114. card = ReactiveCache.getCard(cardId);
  115. if(num <= board.labels.length)
  116. {
  117. card.addLabel(labels[num-1]["_id"]);
  118. }
  119. }
  120. return;
  121. }
  122. const cardId = getSelectedCardId();
  123. if (!cardId) {
  124. return;
  125. }
  126. if (ReactiveCache.getCurrentUser().isBoardMember()) {
  127. const card = ReactiveCache.getCard(cardId);
  128. if(num <= board.labels.length)
  129. {
  130. card.toggleLabel(labels[num-1]["_id"]);
  131. }
  132. }
  133. });
  134. Mousetrap.bind('m', evt => {
  135. const cardId = getSelectedCardId();
  136. if (!cardId) {
  137. return;
  138. }
  139. const currentUserId = Meteor.userId();
  140. if (currentUserId === null) {
  141. return;
  142. }
  143. if (ReactiveCache.getCurrentUser().isBoardMember()) {
  144. const card = ReactiveCache.getCard(cardId);
  145. card.toggleAssignee(currentUserId);
  146. // We should prevent scrolling in card when spacebar is clicked
  147. // This should do it according to Mousetrap docs, but it doesn't
  148. evt.preventDefault();
  149. }
  150. });
  151. Mousetrap.bind('space', evt => {
  152. const cardId = getSelectedCardId();
  153. if (!cardId) {
  154. return;
  155. }
  156. const currentUserId = Meteor.userId();
  157. if (currentUserId === null) {
  158. return;
  159. }
  160. if (ReactiveCache.getCurrentUser().isBoardMember()) {
  161. const card = ReactiveCache.getCard(cardId);
  162. card.toggleMember(currentUserId);
  163. // We should prevent scrolling in card when spacebar is clicked
  164. // This should do it according to Mousetrap docs, but it doesn't
  165. evt.preventDefault();
  166. }
  167. });
  168. Mousetrap.bind('-', evt => {
  169. const cardId = getSelectedCardId();
  170. if (!cardId) {
  171. return;
  172. }
  173. const currentUserId = Meteor.userId();
  174. if (currentUserId === null) {
  175. return;
  176. }
  177. if (Utils.canModifyBoard()) {
  178. const card = ReactiveCache.getCard(cardId);
  179. card.archive();
  180. // We should prevent scrolling in card when spacebar is clicked
  181. // This should do it according to Mousetrap docs, but it doesn't
  182. evt.preventDefault();
  183. }
  184. });
  185. Mousetrap.bind('n', evt => {
  186. const cardId = getSelectedCardId();
  187. if (!cardId) {
  188. return;
  189. }
  190. const currentUserId = Meteor.userId();
  191. if (currentUserId === null) {
  192. return;
  193. }
  194. if (Utils.canModifyBoard()) {
  195. // Find the current hovered card
  196. const card = ReactiveCache.getCard(cardId);
  197. // Find the button and click it
  198. $(`#js-list-${card.listId} .list-body .minicards .open-minicard-composer`).click()
  199. // We should prevent scrolling in card when spacebar is clicked
  200. // This should do it according to Mousetrap docs, but it doesn't
  201. evt.preventDefault();
  202. }
  203. });
  204. Template.keyboardShortcuts.helpers({
  205. mapping: [
  206. {
  207. keys: ['w'],
  208. action: 'shortcut-toggle-sidebar',
  209. },
  210. {
  211. keys: ['q'],
  212. action: 'shortcut-filter-my-cards',
  213. },
  214. {
  215. keys: ['a'],
  216. action: 'shortcut-filter-my-assigned-cards',
  217. },
  218. {
  219. keys: ['n'],
  220. action: 'add-card-to-bottom-of-list',
  221. },
  222. {
  223. keys: ['f'],
  224. action: 'shortcut-toggle-filterbar',
  225. },
  226. {
  227. keys: ['/'],
  228. action: 'shortcut-toggle-searchbar',
  229. },
  230. {
  231. keys: ['x'],
  232. action: 'shortcut-clear-filters',
  233. },
  234. {
  235. keys: ['?'],
  236. action: 'shortcut-show-shortcuts',
  237. },
  238. {
  239. keys: ['ESC'],
  240. action: 'shortcut-close-dialog',
  241. },
  242. {
  243. keys: ['@'],
  244. action: 'shortcut-autocomplete-members',
  245. },
  246. {
  247. keys: ['SPACE'],
  248. action: 'shortcut-add-self',
  249. },
  250. {
  251. keys: ['n'],
  252. action: 'shortcut-assign-self',
  253. },
  254. {
  255. keys: ['-'],
  256. action: 'archive-card',
  257. },
  258. {
  259. keys: ['number keys 1-9'],
  260. action: 'toggle-labels'
  261. },
  262. {
  263. keys: ['shift + number keys 1-9'],
  264. action: 'remove-labels-multiselect'
  265. },
  266. ],
  267. });