@@ -3,10 +3,7 @@ import Cookies from 'js-cookie';
33import { connect } from 'react-redux' ;
44import Promise from 'bluebird' ;
55import { Helmet } from 'react-helmet' ;
6- import AutosizeInput from 'react-input-autosize' ;
76import queryString from 'query-string' ;
8- import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
9- import faPlus from '@fortawesome/fontawesome-free-solid/faPlus' ;
107import {
118 BaseComponent ,
129 CodeEditor ,
@@ -32,7 +29,6 @@ class App extends BaseComponent {
3229 this . state = {
3330 navigatorOpened : true ,
3431 workspaceWeights : [ 1 , 2 , 2 ] ,
35- editorTabIndex : - 1 ,
3632 } ;
3733
3834 this . codeEditorRef = React . createRef ( ) ;
@@ -79,11 +75,12 @@ class App extends BaseComponent {
7975
8076 toggleHistoryBlock ( enable = ! this . unblock ) {
8177 if ( enable ) {
78+ const { saved } = this . props . current ;
8279 const warningMessage = 'Are you sure you want to discard changes?' ;
83- window . onbeforeunload = ( ) => this . isSaved ( ) ? undefined : warningMessage ;
80+ window . onbeforeunload = ( ) => saved ? undefined : warningMessage ;
8481 this . unblock = this . props . history . block ( ( location ) => {
8582 if ( location . pathname === this . props . location . pathname ) return ;
86- if ( ! this . isSaved ( ) ) return warningMessage ;
83+ if ( ! saved ) return warningMessage ;
8784 } ) ;
8885 } else {
8986 window . onbeforeunload = undefined ;
@@ -189,97 +186,44 @@ class App extends BaseComponent {
189186 selectDefaultTab ( ) {
190187 const { ext } = this . props . env ;
191188 const { files } = this . props . current ;
192- let editorTabIndex = files . findIndex ( file => extension ( file . name ) === 'json' ) ;
193- if ( ! ~ editorTabIndex ) files . findIndex ( file => extension ( file . name ) === ext ) ;
194- if ( ! ~ editorTabIndex ) editorTabIndex = files . findIndex ( file => exts . includes ( extension ( file . name ) ) ) ;
195- if ( ! ~ editorTabIndex ) editorTabIndex = Math . min ( 0 , files . length - 1 ) ;
196- this . handleChangeEditorTabIndex ( editorTabIndex ) ;
189+ const editingFile = files . find ( file => extension ( file . name ) === 'json' ) ||
190+ files . find ( file => extension ( file . name ) === ext ) ||
191+ files . find ( file => exts . includes ( extension ( file . name ) ) ) ||
192+ files [ files . length - 1 ] ;
193+ this . props . setEditingFile ( editingFile ) ;
197194 }
198195
199196 handleChangeWorkspaceWeights ( workspaceWeights ) {
200197 this . setState ( { workspaceWeights } ) ;
201198 this . codeEditorRef . current . getWrappedInstance ( ) . handleResize ( ) ;
202199 }
203200
204- handleChangeEditorTabIndex ( editorTabIndex ) {
205- const { files } = this . props . current ;
206- if ( editorTabIndex === files . length ) this . handleAddFile ( ) ;
207- this . setState ( { editorTabIndex } ) ;
208- this . props . shouldBuild ( ) ;
209- }
210-
211- handleAddFile ( ) {
212- const { ext } = this . props . env ;
213- const { files } = this . props . current ;
214- const language = languages . find ( language => language . ext === ext ) ;
215- const file = { ...language . skeleton } ;
216- let count = 0 ;
217- while ( files . some ( existingFile => existingFile . name === file . name ) ) file . name = `code-${ ++ count } .${ ext } ` ;
218- this . props . addFile ( file ) ;
219- }
220-
221- handleRenameFile ( e ) {
222- const { value } = e . target ;
223- const { editorTabIndex } = this . state ;
224- this . props . renameFile ( editorTabIndex , value ) ;
225- }
226-
227- handleDeleteFile ( ) {
228- const { editorTabIndex } = this . state ;
229- const { files } = this . props . current ;
230- this . handleChangeEditorTabIndex ( Math . min ( editorTabIndex , files . length - 2 ) ) ;
231- this . props . deleteFile ( editorTabIndex ) ;
232- }
233-
234201 toggleNavigatorOpened ( navigatorOpened = ! this . state . navigatorOpened ) {
235202 this . setState ( { navigatorOpened } ) ;
236203 }
237204
238- isSaved ( ) {
239- const { titles, files, lastTitles, lastFiles } = this . props . current ;
240- const serialize = ( titles , files ) => JSON . stringify ( {
241- titles,
242- files : files . map ( ( { name, content } ) => ( { name, content } ) ) ,
243- } ) ;
244- return serialize ( titles , files ) === serialize ( lastTitles , lastFiles ) ;
245- }
246-
247205 render ( ) {
248- const { navigatorOpened, workspaceWeights, editorTabIndex } = this . state ;
206+ const { navigatorOpened, workspaceWeights } = this . state ;
249207
250- const { files, titles, description } = this . props . current ;
251- const saved = this . isSaved ( ) ;
208+ const { titles, description, saved } = this . props . current ;
252209 const title = `${ saved ? '' : '(Unsaved) ' } ${ titles . join ( ' - ' ) } ` ;
253- const file = files [ editorTabIndex ] ;
254-
255- const editorTitles = files . map ( file => file . name ) ;
256- if ( file ) {
257- editorTitles [ editorTabIndex ] = (
258- < AutosizeInput className = { styles . input_title } value = { file . name }
259- onClick = { e => e . stopPropagation ( ) } onChange = { e => this . handleRenameFile ( e ) } />
260- ) ;
261- }
262- editorTitles . push (
263- < FontAwesomeIcon fixedWidth icon = { faPlus } /> ,
264- ) ;
265210
266211 return (
267212 < div className = { styles . app } >
268213 < Helmet >
269214 < title > { title } </ title >
270215 < meta name = "description" content = { description } />
271216 </ Helmet >
272- < Header className = { styles . header } onClickTitleBar = { ( ) => this . toggleNavigatorOpened ( ) } saved = { saved }
273- navigatorOpened = { navigatorOpened } loadScratchPapers = { ( ) => this . loadScratchPapers ( ) } file = { file }
217+ < Header className = { styles . header } onClickTitleBar = { ( ) => this . toggleNavigatorOpened ( ) }
218+ navigatorOpened = { navigatorOpened } loadScratchPapers = { ( ) => this . loadScratchPapers ( ) }
274219 ignoreHistoryBlock = { this . ignoreHistoryBlock } />
275220 < ResizableContainer className = { styles . workspace } horizontal weights = { workspaceWeights }
276221 visibles = { [ navigatorOpened , true , true ] }
277222 onChangeWeights = { weights => this . handleChangeWorkspaceWeights ( weights ) } >
278223 < Navigator />
279224 < VisualizationViewer className = { styles . visualization_viewer } />
280- < TabContainer className = { styles . editor_tab_container } titles = { editorTitles } tabIndex = { editorTabIndex }
281- onChangeTabIndex = { tabIndex => this . handleChangeEditorTabIndex ( tabIndex ) } >
282- < CodeEditor ref = { this . codeEditorRef } file = { file } onClickDelete = { ( ) => this . handleDeleteFile ( ) } />
225+ < TabContainer className = { styles . editor_tab_container } >
226+ < CodeEditor ref = { this . codeEditorRef } />
283227 </ TabContainer >
284228 </ ResizableContainer >
285229 < ToastContainer className = { styles . toast_container } />
0 commit comments