keyboard.js 8.2 KB

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