keyboard.js 7.5 KB

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