shortcut.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. let shortcut_key_list = {};
  3. window.addEventListener("keyup", e => {
  4. delete shortcut_key_list[e.code];
  5. });
  6. window.addEventListener("blur", () => {
  7. shortcut_key_list = {};
  8. });
  9. document.addEventListener("visibilitychange", () => {
  10. if(document.hidden) {
  11. shortcut_key_list = {};
  12. }
  13. });
  14. window.addEventListener("keydown", e => {
  15. let shortcut_check = e.target.tagName.toLowerCase();
  16. if(shortcut_check === 'input' || shortcut_check === 'textarea') {
  17. return;
  18. } else if(e.repeat) {
  19. return;
  20. }
  21. if(e.ctrlKey || e.metaKey || e.altKey) {
  22. return;
  23. }
  24. shortcut_key_list[e.code] = 1;
  25. if(Object.keys(shortcut_key_list).length === 1) {
  26. let doc_shortcut = /^\/(w|w_from|history|edit|acl|topic|xref)\//i;
  27. if(shortcut_key_list['KeyF'] === 1) {
  28. window.location.href = '/';
  29. } else if(shortcut_key_list['KeyC'] === 1) {
  30. window.location.href = '/recent_change';
  31. } else if(shortcut_key_list['KeyD'] === 1) {
  32. window.location.href = '/recent_discuss';
  33. } else if(shortcut_key_list['KeyA'] === 1) {
  34. window.location.href = '/random';
  35. }
  36. if(window.location.pathname.match(doc_shortcut)) {
  37. let doc_href = window.location.pathname.replace(doc_shortcut, '');
  38. if(shortcut_key_list['KeyW'] === 1) {
  39. window.location.pathname = '/w/' + doc_href;
  40. } else if(shortcut_key_list['KeyE'] === 1) {
  41. window.location.pathname = '/edit/' + doc_href;
  42. } else if(shortcut_key_list['KeyH'] === 1) {
  43. window.location.pathname = '/history/' + doc_href;
  44. }
  45. }
  46. }
  47. });