@@ -35,6 +35,8 @@ class App extends BaseComponent {
3535 } ;
3636
3737 this . codeEditorRef = React . createRef ( ) ;
38+
39+ this . ignoreHistoryBlock = this . ignoreHistoryBlock . bind ( this ) ;
3840 }
3941
4042 componentDidMount ( ) {
@@ -50,17 +52,14 @@ class App extends BaseComponent {
5052 . then ( ( { categories } ) => this . props . setCategories ( categories ) )
5153 . catch ( this . handleError ) ;
5254
53- this . props . history . block ( ( location ) => {
54- if ( location . pathname === this . props . location . pathname ) return ;
55- if ( ! this . isSaved ( ) ) return 'Are you sure want to discard changes?' ;
56- } ) ;
55+ this . toggleHistoryBlock ( true ) ;
5756 }
5857
5958 componentWillUnmount ( ) {
6059 delete window . signIn ;
6160 delete window . signOut ;
6261
63- window . onbeforeunload = undefined ;
62+ this . toggleHistoryBlock ( false ) ;
6463 }
6564
6665 componentWillReceiveProps ( nextProps ) {
@@ -74,6 +73,24 @@ class App extends BaseComponent {
7473 }
7574 }
7675
76+ toggleHistoryBlock ( enable = ! this . unblock ) {
77+ if ( enable ) {
78+ this . unblock = this . props . history . block ( ( location ) => {
79+ if ( location . pathname === this . props . location . pathname ) return ;
80+ if ( ! this . isSaved ( ) ) return 'Are you sure want to discard changes?' ;
81+ } ) ;
82+ } else {
83+ this . unblock ( ) ;
84+ this . unblock = undefined ;
85+ }
86+ }
87+
88+ ignoreHistoryBlock ( process ) {
89+ this . toggleHistoryBlock ( false ) ;
90+ process ( ) ;
91+ this . toggleHistoryBlock ( true ) ;
92+ }
93+
7794 signIn ( accessToken ) {
7895 Cookies . set ( 'access_token' , accessToken ) ;
7996 GitHubApi . auth ( accessToken )
@@ -172,7 +189,7 @@ class App extends BaseComponent {
172189 const { ext } = this . props . env ;
173190 const { files } = this . props . current ;
174191 const language = languages . find ( language => language . ext === ext ) ;
175- const file = { ...language . skeleton } ;
192+ const file = { ...language . skeleton } ;
176193 let count = 0 ;
177194 while ( files . some ( existingFile => existingFile . name === file . name ) ) file . name = `code-${ ++ count } .${ ext } ` ;
178195 this . props . addFile ( file ) ;
@@ -239,7 +256,8 @@ class App extends BaseComponent {
239256 < meta name = "description" content = { description } />
240257 </ Helmet >
241258 < Header className = { styles . header } onClickTitleBar = { ( ) => this . toggleNavigatorOpened ( ) } saved = { saved }
242- navigatorOpened = { navigatorOpened } loadScratchPapers = { ( ) => this . loadScratchPapers ( ) } file = { file } />
259+ navigatorOpened = { navigatorOpened } loadScratchPapers = { ( ) => this . loadScratchPapers ( ) } file = { file }
260+ ignoreHistoryBlock = { this . ignoreHistoryBlock } />
243261 < ResizableContainer className = { styles . workspace } horizontal weights = { workspaceWeights }
244262 visibles = { [ navigatorOpened , true , true ] }
245263 onChangeWeights = { weights => this . handleChangeWorkspaceWeights ( weights ) } >
0 commit comments