forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
current.js
144 lines (139 loc) · 3.89 KB
/
current.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { combineActions, createAction, handleActions } from 'redux-actions';
import { README_MD } from 'files';
import { extension, isSaved } from 'common/util';
const prefix = 'CURRENT';
const setHome = createAction(`${prefix}/SET_HOME`, () => defaultState);
const setAlgorithm = createAction(`${prefix}/SET_ALGORITHM`, ({ categoryKey, categoryName, algorithmKey, algorithmName, files, description }) => ({
algorithm: { categoryKey, algorithmKey },
titles: [categoryName, algorithmName],
files,
description,
}));
const setScratchPaper = createAction(`${prefix}/SET_SCRATCH_PAPER`, ({ login, gistId, title, files }) => ({
scratchPaper: { login, gistId },
titles: ['Scratch Paper', title],
files,
description: homeDescription,
}));
const setEditingFile = createAction(`${prefix}/SET_EDITING_FILE`, file => ({ file }));
const modifyTitle = createAction(`${prefix}/MODIFY_TITLE`, title => ({ title }));
const addFile = createAction(`${prefix}/ADD_FILE`, file => ({ file }));
const renameFile = createAction(`${prefix}/RENAME_FILE`, (file, name) => ({ file, name }));
const modifyFile = createAction(`${prefix}/MODIFY_FILE`, (file, content) => ({ file, content }));
const deleteFile = createAction(`${prefix}/DELETE_FILE`, file => ({ file }));
export const actions = {
setHome,
setAlgorithm,
setScratchPaper,
setEditingFile,
modifyTitle,
addFile,
modifyFile,
deleteFile,
renameFile,
};
const homeTitles = ['Algorithm Visualizer'];
const homeFiles = [README_MD];
const homeDescription = 'Algorithm Visualizer is an interactive online platform that visualizes algorithms from code.';
const defaultState = {
algorithm: {
categoryKey: 'algorithm-visualizer',
algorithmKey: 'home',
},
scratchPaper: undefined,
titles: homeTitles,
files: homeFiles,
lastTitles: homeTitles,
lastFiles: homeFiles,
description: homeDescription,
editingFile: undefined,
shouldBuild: true,
saved: true,
};
export default handleActions({
[combineActions(
setHome,
setAlgorithm,
setScratchPaper,
)]: (state, { payload }) => {
const { algorithm, scratchPaper, titles, files, description } = payload;
return {
...state,
algorithm,
scratchPaper,
titles,
files,
lastTitles: titles,
lastFiles: files,
description,
editingFile: undefined,
shouldBuild: true,
saved: true,
};
},
[setEditingFile]: (state, { payload }) => {
const { file } = payload;
return {
...state,
editingFile: file,
shouldBuild: true,
};
},
[modifyTitle]: (state, { payload }) => {
const { title } = payload;
const newState = {
...state,
titles: [state.titles[0], title],
};
return {
...newState,
saved: isSaved(newState),
};
},
[addFile]: (state, { payload }) => {
const { file } = payload;
const newState = {
...state,
files: [...state.files, file],
editingFile: file,
shouldBuild: true,
};
return {
...newState,
saved: isSaved(newState),
};
},
[combineActions(
renameFile,
modifyFile,
)]: (state, { payload }) => {
const { file, ...update } = payload;
const editingFile = { ...file, ...update };
const newState = {
...state,
files: state.files.map(oldFile => oldFile === file ? editingFile : oldFile),
editingFile,
shouldBuild: extension(editingFile.name) === 'md',
};
return {
...newState,
saved: isSaved(newState),
};
},
[deleteFile]: (state, { payload }) => {
const { file } = payload;
const index = state.files.indexOf(file);
const files = state.files.filter(oldFile => oldFile !== file);
const editingFile = files[Math.min(index, files.length - 1)];
const newState = {
...state,
files,
editingFile,
shouldBuild: true,
};
return {
...newState,
saved: isSaved(newState),
};
},
}, defaultState);