/
editor.js
108 lines (99 loc) · 2.63 KB
/
editor.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { combineReducers, createSlice } from '@reduxjs/toolkit';
import defaultEditorHeight from '../config/editorSettings';
const initialState = {
meta: {},
text: {},
textPlaybook: {},
langs: {},
};
export const makeEditorTextKey = (userId, lang) => `${userId}:${lang}`;
const getCurrentEditorHeight = (state, userId) => {
const {
[userId]: { editorHeight },
} = state;
return editorHeight || defaultEditorHeight;
};
const meta = createSlice({
name: 'meta',
initialState: initialState.meta,
reducers: {
updateEditorLang: (state, { payload: { userId, currentLangSlug } }) => {
state[userId] = {
...state[userId],
userId,
currentLangSlug,
};
},
updateEditorTextPlaybook: (state, { payload: { userId, langSlug } }) => {
state[userId] = {
...state[userId],
userId,
currentLangSlug: langSlug,
};
},
updateEditorText: (state, { payload: { userId, langSlug } }) => {
state[userId] = {
...state[userId],
userId,
currentLangSlug: langSlug,
};
},
compressEditorHeight: (state, { payload: { userId } }) => {
const currentHeight = getCurrentEditorHeight(state, userId);
const newEditorHeight = currentHeight > 100 ? currentHeight - 100 : currentHeight;
state[userId] = {
...state[userId],
editorHeight: newEditorHeight,
};
},
expandEditorHeight: (state, { payload: { userId } }) => {
const currentHeight = getCurrentEditorHeight(state, userId);
state[userId] = {
...state[userId],
editorHeight: currentHeight + 100,
};
},
},
});
const text = createSlice({
name: 'text',
initialState: initialState.text,
extraReducers: {
[meta.actions.updateEditorText]: (
state,
{ payload: { userId, langSlug, editorText } },
) => {
state[makeEditorTextKey(userId, langSlug)] = editorText;
},
},
});
const textPlaybook = createSlice({
name: 'textPlaybook',
initialState: initialState.textPlaybook,
extraReducers: {
[meta.actions.updateEditorTextPlaybook]: (state, { payload: { userId, editorText } }) => {
state[userId] = editorText;
},
},
});
const langs = createSlice({
name: 'langs',
initialState: initialState.langs,
reducers: {
setLangs: (state, { payload: { langs: newLangs } }) => {
state.langs = newLangs;
},
},
});
export const actions = {
...text.actions,
...textPlaybook.actions,
...langs.actions,
...meta.actions,
};
export default combineReducers({
meta: meta.reducer,
text: text.reducer,
textPlaybook: textPlaybook.reducer,
langs: langs.reducer,
});