keypress.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { masterSet, playPaused, queuePushed, queueRemoved, stateSet } from '../../../../actions';
  2. import { ActionKeyPressed, Keys } from '../../../../hooks/vim';
  3. import { CmusUIState, LibraryModeWindow, Overlay, View } from '../types';
  4. import { handleOrder } from './order';
  5. import { handleScroll } from './scroll';
  6. import { withGlobalAction } from './utils';
  7. const libraryModeWindows: LibraryModeWindow[] = Object.values(LibraryModeWindow);
  8. const switchLibraryMode = (state: CmusUIState): CmusUIState => ({
  9. ...state,
  10. library: {
  11. ...state.library,
  12. modeWindow:
  13. libraryModeWindows[
  14. (libraryModeWindows.indexOf(state.library.modeWindow) + 1) % libraryModeWindows.length
  15. ],
  16. },
  17. });
  18. function toggleExpandArtist(library: CmusUIState['library']): CmusUIState['library'] {
  19. if (library.activeArtist === null) {
  20. return library;
  21. }
  22. if (library.expandedArtists.includes(library.activeArtist)) {
  23. return {
  24. ...library,
  25. expandedArtists: library.expandedArtists.filter(
  26. (compare) => compare !== library.activeArtist,
  27. ),
  28. activeAlbum: null,
  29. };
  30. }
  31. return { ...library, expandedArtists: [...library.expandedArtists, library.activeArtist] };
  32. }
  33. const activateSong = (state: CmusUIState, songId: number): CmusUIState =>
  34. withGlobalAction(
  35. state,
  36. stateSet({
  37. playing: true,
  38. songId,
  39. currentTime: 0,
  40. seekTime: 0,
  41. }),
  42. );
  43. function handleActivate(state: CmusUIState): CmusUIState {
  44. switch (state.view) {
  45. case View.Library:
  46. if (state.library.modeWindow === LibraryModeWindow.SongList) {
  47. if (!state.library.activeSongId) {
  48. return state;
  49. }
  50. return activateSong(state, state.library.activeSongId);
  51. }
  52. return state;
  53. case View.Queue:
  54. if (!state.queue.active) {
  55. return state;
  56. }
  57. return activateSong(state, state.queue.active);
  58. case View.ClientList:
  59. if (!state.clientList.active) {
  60. return state;
  61. }
  62. return withGlobalAction(state, masterSet(state.clientList.active));
  63. default:
  64. return state;
  65. }
  66. }
  67. export function handleKeyPress(state: CmusUIState, action: ActionKeyPressed): CmusUIState {
  68. switch (action.key) {
  69. case Keys.colon:
  70. return { ...state, commandMode: true };
  71. case Keys['1']:
  72. return { ...state, view: View.Library };
  73. case Keys['2']:
  74. return { ...state, view: View.ClientList };
  75. case Keys['3']:
  76. return { ...state, view: View.Queue };
  77. case Keys.tab:
  78. if (state.view === View.Library) {
  79. return switchLibraryMode(state);
  80. }
  81. return state;
  82. case Keys.space:
  83. if (state.view === View.Library) {
  84. if (state.library.modeWindow === LibraryModeWindow.ArtistList) {
  85. return { ...state, library: toggleExpandArtist(state.library) };
  86. }
  87. }
  88. return state;
  89. case Keys.enter:
  90. return handleActivate(state);
  91. case Keys.esc:
  92. return { ...state, overlay: null };
  93. case Keys.question:
  94. return { ...state, overlay: Overlay.Help };
  95. case Keys.B:
  96. return { ...state, skipSong: { delta: 1, serialNumber: state.skipSong.serialNumber + 1 } };
  97. case Keys.Z:
  98. return { ...state, skipSong: { delta: -1, serialNumber: state.skipSong.serialNumber + 1 } };
  99. case Keys.C:
  100. return withGlobalAction(state, playPaused());
  101. case Keys.D:
  102. if (state.view === View.Queue && state.queue.active) {
  103. return withGlobalAction(state, queueRemoved(state.queue.active));
  104. }
  105. return state;
  106. case Keys.E:
  107. if (
  108. state.view === View.Library &&
  109. state.library.modeWindow === LibraryModeWindow.SongList &&
  110. state.library.activeSongId
  111. ) {
  112. return withGlobalAction(state, queuePushed(state.library.activeSongId));
  113. }
  114. return state;
  115. case Keys.J:
  116. return handleScroll(state, 1);
  117. case Keys.K:
  118. return handleScroll(state, -1);
  119. case Keys.p:
  120. return handleOrder(state, 1);
  121. case Keys.P:
  122. return handleOrder(state, -1);
  123. case Keys.pageDown:
  124. return handleScroll(state, 20);
  125. case Keys.pageUp:
  126. return handleScroll(state, -20);
  127. default:
  128. return state;
  129. }
  130. }