@@ -35,6 +35,8 @@ class App extends BaseComponent {
35
35
} ;
36
36
37
37
this . codeEditorRef = React . createRef ( ) ;
38
+
39
+ this . ignoreHistoryBlock = this . ignoreHistoryBlock . bind ( this ) ;
38
40
}
39
41
40
42
componentDidMount ( ) {
@@ -50,17 +52,14 @@ class App extends BaseComponent {
50
52
. then ( ( { categories } ) => this . props . setCategories ( categories ) )
51
53
. catch ( this . handleError ) ;
52
54
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 ) ;
57
56
}
58
57
59
58
componentWillUnmount ( ) {
60
59
delete window . signIn ;
61
60
delete window . signOut ;
62
61
63
- window . onbeforeunload = undefined ;
62
+ this . toggleHistoryBlock ( false ) ;
64
63
}
65
64
66
65
componentWillReceiveProps ( nextProps ) {
@@ -74,6 +73,24 @@ class App extends BaseComponent {
74
73
}
75
74
}
76
75
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
+
77
94
signIn ( accessToken ) {
78
95
Cookies . set ( 'access_token' , accessToken ) ;
79
96
GitHubApi . auth ( accessToken )
@@ -172,7 +189,7 @@ class App extends BaseComponent {
172
189
const { ext } = this . props . env ;
173
190
const { files } = this . props . current ;
174
191
const language = languages . find ( language => language . ext === ext ) ;
175
- const file = { ...language . skeleton } ;
192
+ const file = { ...language . skeleton } ;
176
193
let count = 0 ;
177
194
while ( files . some ( existingFile => existingFile . name === file . name ) ) file . name = `code-${ ++ count } .${ ext } ` ;
178
195
this . props . addFile ( file ) ;
@@ -239,7 +256,8 @@ class App extends BaseComponent {
239
256
< meta name = "description" content = { description } />
240
257
</ Helmet >
241
258
< 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 } />
243
261
< ResizableContainer className = { styles . workspace } horizontal weights = { workspaceWeights }
244
262
visibles = { [ navigatorOpened , true , true ] }
245
263
onChangeWeights = { weights => this . handleChangeWorkspaceWeights ( weights ) } >
0 commit comments