@@ -17,9 +17,9 @@ import {
17
17
ToastContainer ,
18
18
VisualizationViewer ,
19
19
} from '/components' ;
20
- import { CategoryApi , GitHubApi } from '/apis' ;
20
+ import { AlgorithmApi , GitHubApi } from '/apis' ;
21
21
import { actions } from '/reducers' ;
22
- import { extension , handleError , refineGist } from '/common/util' ;
22
+ import { extension , getFiles , getTitleArray , handleError , refineGist } from '/common/util' ;
23
23
import { exts , languages , us } from '/common/config' ;
24
24
import { README_MD , SCRATCH_PAPER_MD } from '/skeletons' ;
25
25
import styles from './stylesheet.scss' ;
@@ -49,7 +49,7 @@ class App extends React.Component {
49
49
const accessToken = Cookies . get ( 'access_token' ) ;
50
50
if ( accessToken ) this . signIn ( accessToken ) ;
51
51
52
- CategoryApi . getCategories ( )
52
+ AlgorithmApi . getCategories ( )
53
53
. then ( ( { categories } ) => this . props . setCategories ( categories ) )
54
54
. catch ( handleError . bind ( this ) ) ;
55
55
@@ -65,7 +65,12 @@ class App extends React.Component {
65
65
66
66
componentWillReceiveProps ( nextProps ) {
67
67
const { params } = nextProps . match ;
68
- const { categoryKey, algorithmKey, gistId } = nextProps . current ;
68
+ const { algorithm, scratchPaper } = nextProps . current ;
69
+
70
+ const categoryKey = algorithm && algorithm . categoryKey ;
71
+ const algorithmKey = algorithm && algorithm . algorithmKey ;
72
+ const gistId = scratchPaper && scratchPaper . gistId ;
73
+
69
74
if ( params . categoryKey !== categoryKey ||
70
75
params . algorithmKey !== algorithmKey ||
71
76
params . gistId !== gistId ) {
@@ -135,13 +140,14 @@ class App extends React.Component {
135
140
const { ext } = this . props . env ;
136
141
let fetchPromise = null ;
137
142
if ( categoryKey && algorithmKey ) {
138
- fetchPromise = CategoryApi . getAlgorithm ( categoryKey , algorithmKey )
139
- . then ( ( { algorithm } ) => algorithm ) ;
143
+ fetchPromise = AlgorithmApi . getAlgorithm ( categoryKey , algorithmKey )
144
+ . then ( ( { algorithm } ) => this . props . setAlgorithm ( algorithm ) ) ;
140
145
} else if ( [ 'new' , 'forked' ] . includes ( gistId ) ) {
141
146
gistId = 'new' ;
142
147
const language = languages . find ( language => language . ext === ext ) ;
143
- fetchPromise = Promise . resolve ( {
144
- titles : [ 'Scratch Paper' , 'Untitled' ] ,
148
+ fetchPromise = Promise . resolve ( this . props . setScratchPaper ( {
149
+ gistId,
150
+ title : 'Untitled' ,
145
151
files : [ {
146
152
name : 'README.md' ,
147
153
content : SCRATCH_PAPER_MD ,
@@ -151,24 +157,29 @@ class App extends React.Component {
151
157
content : language . skeleton ,
152
158
contributors : undefined ,
153
159
} ] ,
154
- } ) ;
160
+ } ) ) ;
155
161
} else if ( gistId ) {
156
- fetchPromise = GitHubApi . getGist ( gistId , { timestamp : Date . now ( ) } ) . then ( refineGist ) ;
162
+ fetchPromise = GitHubApi . getGist ( gistId , { timestamp : Date . now ( ) } )
163
+ . then ( refineGist )
164
+ . then ( this . props . setScratchPaper ) ;
157
165
} else {
158
166
fetchPromise = Promise . reject ( new Error ( ) ) ;
159
167
}
160
168
fetchPromise
161
- . then ( algorithm => this . props . setCurrent ( categoryKey , algorithmKey , gistId , algorithm . titles , algorithm . files , algorithm . gist ) )
162
169
. catch ( error => {
163
170
if ( error . message ) handleError . bind ( this ) ( error ) ;
164
- this . props . setCurrent ( undefined , undefined , undefined , [ 'Algorithm Visualizer' ] , [ {
165
- name : 'README.md' ,
166
- content : README_MD ,
167
- contributors : [ us ] ,
168
- } ] , undefined ) ;
171
+ this . props . setAlgorithm ( {
172
+ categoryName : 'Algorithm Visualizer' ,
173
+ algorithmName : 'Home' ,
174
+ files : [ {
175
+ name : 'README.md' ,
176
+ content : README_MD ,
177
+ contributors : [ us ] ,
178
+ } ] ,
179
+ } ) ;
169
180
} )
170
181
. finally ( ( ) => {
171
- const { files } = this . props . current ;
182
+ const files = getFiles ( this . props . current ) ;
172
183
let editorTabIndex = files . findIndex ( file => extension ( file . name ) === ext ) ;
173
184
if ( ! ~ editorTabIndex ) editorTabIndex = files . findIndex ( file => exts . includes ( extension ( file . name ) ) ) ;
174
185
if ( ! ~ editorTabIndex ) editorTabIndex = Math . min ( 0 , files . length - 1 ) ;
@@ -182,15 +193,15 @@ class App extends React.Component {
182
193
}
183
194
184
195
handleChangeEditorTabIndex ( editorTabIndex ) {
185
- const { files } = this . props . current ;
196
+ const files = getFiles ( this . props . current ) ;
186
197
if ( editorTabIndex === files . length ) this . handleAddFile ( ) ;
187
198
this . setState ( { editorTabIndex } ) ;
188
199
this . props . shouldBuild ( ) ;
189
200
}
190
201
191
202
handleAddFile ( ) {
192
203
const { ext } = this . props . env ;
193
- const { files } = this . props . current ;
204
+ const files = getFiles ( this . props . current ) ;
194
205
let name = `code.${ ext } ` ;
195
206
let count = 0 ;
196
207
while ( files . some ( file => file . name === name ) ) name = `code-${ ++ count } .${ ext } ` ;
@@ -210,7 +221,7 @@ class App extends React.Component {
210
221
211
222
handleDeleteFile ( ) {
212
223
const { editorTabIndex } = this . state ;
213
- const { files } = this . props . current ;
224
+ const files = getFiles ( this . props . current ) ;
214
225
this . handleChangeEditorTabIndex ( Math . min ( editorTabIndex , files . length - 2 ) ) ;
215
226
this . props . deleteFile ( editorTabIndex ) ;
216
227
}
@@ -220,15 +231,15 @@ class App extends React.Component {
220
231
}
221
232
222
233
isGistSaved ( ) {
223
- const { titles, files, lastTitles, lastFiles } = this . props . current ;
224
- const serializeTitles = titles => JSON . stringify ( titles ) ;
234
+ const { scratchPaper } = this . props . current ;
235
+ if ( ! scratchPaper ) return true ;
236
+ const { title, files, lastTitle, lastFiles } = scratchPaper ;
225
237
const serializeFiles = files => JSON . stringify ( files . map ( ( { name, content } ) => ( { name, content } ) ) ) ;
226
- return serializeTitles ( titles ) === serializeTitles ( lastTitles ) &&
227
- serializeFiles ( files ) === serializeFiles ( lastFiles ) ;
238
+ return title === lastTitle && serializeFiles ( files ) === serializeFiles ( lastFiles ) ;
228
239
}
229
240
230
241
getDescription ( ) {
231
- const { files } = this . props . current ;
242
+ const files = getFiles ( this . props . current ) ;
232
243
const readmeFile = files . find ( file => file . name === 'README.md' ) ;
233
244
if ( ! readmeFile ) return '' ;
234
245
const groups = / ^ \s * # .* \n + ( [ ^ \n ] + ) / . exec ( readmeFile . content ) ;
@@ -237,7 +248,8 @@ class App extends React.Component {
237
248
238
249
render ( ) {
239
250
const { navigatorOpened, workspaceWeights, editorTabIndex } = this . state ;
240
- const { titles, files } = this . props . current ;
251
+ const files = getFiles ( this . props . current ) ;
252
+ const titleArray = getTitleArray ( this . props . current ) ;
241
253
242
254
const gistSaved = this . isGistSaved ( ) ;
243
255
const description = this . getDescription ( ) ;
@@ -257,7 +269,7 @@ class App extends React.Component {
257
269
return (
258
270
< div className = { styles . app } >
259
271
< Helmet >
260
- < title > { gistSaved ? '' : '(Unsaved) ' } { titles . join ( ' - ' ) } </ title >
272
+ < title > { gistSaved ? '' : '(Unsaved) ' } { titleArray . join ( ' - ' ) } </ title >
261
273
< meta name = "description" content = { description } />
262
274
</ Helmet >
263
275
< Header className = { styles . header } onClickTitleBar = { ( ) => this . toggleNavigatorOpened ( ) }
0 commit comments