File size: 1,773 Bytes
3c3f089
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { RootState } from "../store";
import { EditorValueInterface } from "../../_types/editorTypes";
import { treeTemplates, monacoOptions } from "../../constants";

type InitialStateType = {
  editorValue: EditorValueInterface;
  monacoInputValue: EditorValueInterface;
  logs: any;
  isLogTabOpen: boolean;
  options: any;
};

const initialState = {
  editorValue: treeTemplates["_empty"],
  monacoInputValue: treeTemplates["_empty"],
  logs: [],
  isLogTabOpen: false,
  options: monacoOptions,
};

export const editorSlice = createSlice({
  name: "editor",
  initialState: initialState,
  reducers: {
    set_editor_value: (state: InitialStateType, { payload }) => {
      state.editorValue = payload;
    },
    update_editor_code: (state: InitialStateType, { payload }) => {
      state.editorValue.tabs[payload.type].data = payload.content;
    },
    update_logs: (state: InitialStateType, { payload }) => {
      state.logs = [...state.logs, payload];
    },
    clear_logs: (state: InitialStateType) => {
      state.logs = [];
    },
    toggle_logs_tab: (state: InitialStateType) => {
      state.isLogTabOpen = !state.isLogTabOpen;
    },
    set_monaco_input_value: (
      state: InitialStateType,
      { payload }: PayloadAction<EditorValueInterface>
    ) => {
      state.monacoInputValue = payload;
    },
    set_options: (state: InitialStateType, { payload }) => {
      state.options = payload;
    },
  },
});

export const {
  update_editor_code,
  update_logs,
  clear_logs,
  toggle_logs_tab,
  set_monaco_input_value,
  set_editor_value,
  set_options,
} = editorSlice.actions;

export const editor_state = (state: RootState) => state.editor;

export default editorSlice.reducer;