Skip to content

Commit 962e341

Browse files
committed
Rewrite selection reducer in RTK
1 parent a173049 commit 962e341

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

ui/frontend/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ module.exports = {
9292
'reducers/output/wasm.ts',
9393
'reducers/page.ts',
9494
'reducers/position.ts',
95+
'reducers/selection.ts',
9596
'reducers/versions.ts',
9697
'reducers/websocket.ts',
9798
'websocketActions.ts',

ui/frontend/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ node_modules
4343
!reducers/output/wasm.ts
4444
!reducers/page.ts
4545
!reducers/position.ts
46+
!reducers/selection.ts
4647
!reducers/versions.ts
4748
!reducers/websocket.ts
4849
!websocketActions.ts

ui/frontend/actions.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
PrimaryActionAuto,
2121
PrimaryActionCore,
2222
ProcessAssembly,
23-
Position,
2423
} from './types';
2524

2625
import { performCommonExecute, wsExecuteRequest } from './reducers/output/execute';
@@ -56,7 +55,6 @@ export enum ActionType {
5655
ChangeMode = 'CHANGE_MODE',
5756
ChangeEdition = 'CHANGE_EDITION',
5857
ChangeBacktrace = 'CHANGE_BACKTRACE',
59-
SelectText = 'SELECT_TEXT',
6058
}
6159

6260
export const initializeApplication = () => createAction(ActionType.InitializeApplication);
@@ -189,9 +187,6 @@ export const performCompileToNightlyHir =
189187
export const performCompileToWasm =
190188
performAndSwitchPrimaryAction(performCompileToCdylibWasmOnly, PrimaryActionCore.Wasm);
191189

192-
export const selectText = (start: Position, end: Position) =>
193-
createAction(ActionType.SelectText, { start, end });
194-
195190
function parseChannel(s?: string): Channel | null {
196191
switch (s) {
197192
case 'stable':
@@ -293,7 +288,6 @@ export type Action =
293288
| ReturnType<typeof changeProcessAssembly>
294289
| ReturnType<typeof changeAceTheme>
295290
| ReturnType<typeof changeMonacoTheme>
296-
| ReturnType<typeof selectText>
297291
| ReturnType<typeof editCode>
298292
| ReturnType<typeof addCrateType>
299293
| ReturnType<typeof navigateToIndex>

ui/frontend/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { Provider } from 'react-redux';
99
import { v4 } from 'uuid';
1010

1111
import {
12-
selectText,
1312
reExecuteWithBacktrace,
1413
} from './actions';
1514
import { configureRustErrors } from './highlighting';
@@ -25,6 +24,7 @@ import { performCratesLoad } from './reducers/crates';
2524
import { gotoPosition } from './reducers/position';
2625
import { addImport, editCode, enableFeatureGate } from './reducers/code';
2726
import { browserWidthChanged } from './reducers/browser';
27+
import { selectText } from './reducers/selection';
2828

2929
const store = configureStore(window);
3030

ui/frontend/reducers/selection.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import { Action, ActionType } from '../actions';
2-
import { Selection } from '../types';
1+
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
32

4-
const DEFAULT: Selection = {
5-
};
3+
import { Position, Selection } from '../types';
64

7-
export default function position(state = DEFAULT, action: Action) {
8-
switch (action.type) {
9-
case ActionType.SelectText: {
10-
const { start, end } = action;
11-
return { ...state, start, end };
12-
}
13-
default:
14-
return state;
15-
}
16-
}
5+
const initialState: Selection = {};
6+
7+
const slice = createSlice({
8+
name: 'selection',
9+
initialState,
10+
reducers: {
11+
selectText: {
12+
reducer: (_state, action: PayloadAction<Selection>) => action.payload,
13+
14+
prepare: (start: Position, end: Position) => ({ payload: { start, end } }),
15+
},
16+
},
17+
});
18+
19+
export const { selectText } = slice.actions;
20+
21+
export default slice.reducer;

0 commit comments

Comments
 (0)