Skip to content

Commit 1f2381a

Browse files
committed
Improve history block
1 parent 13b131c commit 1f2381a

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/frontend/components/App/index.jsx

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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)}>

src/frontend/components/Header/index.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,11 @@ class Header extends BaseComponent {
9898
const { scratchPaper } = this.props.current;
9999
const { gistId } = scratchPaper;
100100
if (gistId === 'new') {
101-
this.props.markSaved();
102-
this.props.history.push('/');
101+
this.props.ignoreHistoryBlock(() => this.props.history.push('/'));
103102
} else {
104103
GitHubApi.deleteGist(gistId)
105104
.then(() => {
106-
this.props.markSaved();
107-
this.props.history.push('/');
105+
this.props.ignoreHistoryBlock(() => this.props.history.push('/'));
108106
})
109107
.then(this.props.loadScratchPapers)
110108
.catch(this.handleError);

src/frontend/reducers/current.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const setScratchPaper = createAction(`${prefix}/SET_SCRATCH_PAPER`, ({ login, gi
2626
lastFiles: files,
2727
};
2828
});
29-
const markSaved = createAction(`${prefix}/MARK_SAVED`);
3029
const modifyTitle = createAction(`${prefix}/MODIFY_TITLE`, title => ({ title }));
3130
const addFile = createAction(`${prefix}/ADD_FILE`, file => ({ file }));
3231
const modifyFile = createAction(`${prefix}/MODIFY_FILE`, file => ({ file }));
@@ -37,7 +36,6 @@ export const actions = {
3736
setHome,
3837
setAlgorithm,
3938
setScratchPaper,
40-
markSaved,
4139
modifyTitle,
4240
addFile,
4341
modifyFile,
@@ -68,13 +66,6 @@ export default handleActions({
6866
...state,
6967
...payload,
7068
}),
71-
[markSaved]: state => {
72-
return {
73-
...state,
74-
lastTitles: state.titles,
75-
lastFiles: state.files,
76-
};
77-
},
7869
[modifyTitle]: (state, { payload }) => {
7970
const { title } = payload;
8071
return {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy