keypress.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { masterSet, playPaused, queuePushed, 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.E:
  102. if (
  103. state.view === View.Library &&
  104. state.library.modeWindow === LibraryModeWindow.SongList &&
  105. state.library.activeSongId
  106. ) {
  107. return withGlobalAction(state, queuePushed(state.library.activeSongId));
  108. }
  109. return state;
  110. case Keys.J:
  111. return handleScroll(state, 1);
  112. case Keys.K:
  113. return handleScroll(state, -1);
  114. case Keys.p:
  115. return handleOrder(state, 1);
  116. case Keys.P:
  117. return handleOrder(state, -1);
  118. case Keys.pageDown:
  119. return handleScroll(state, 20);
  120. case Keys.pageUp:
  121. return handleScroll(state, -20);
  122. default:
  123. return state;
  124. }
  125. }