mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-11 18:24:43 +02:00
Fixed RN history
This commit is contained in:
parent
aee3e9e4bf
commit
7be22369ac
@ -62,11 +62,10 @@ const initialRoute = {
|
||||
defaultState.route = initialRoute;
|
||||
|
||||
let navHistory = [];
|
||||
navHistory.push(initialRoute);
|
||||
|
||||
function historyCanGoBackTo(route) {
|
||||
if (route.routeName == 'Note' && !route.noteId) return false;
|
||||
if (route.routeName == 'Folder' && !route.folderId) return false;
|
||||
if (route.routeName == 'Note') return false;
|
||||
if (route.routeName == 'Folder') return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -86,26 +85,25 @@ const reducer = (state = defaultState, action) => {
|
||||
reg.logger().info('Reducer action', action.type);
|
||||
|
||||
let newState = state;
|
||||
let historyGoingBack = false;
|
||||
|
||||
try {
|
||||
switch (action.type) {
|
||||
|
||||
|
||||
case 'Navigation/BACK':
|
||||
|
||||
if (navHistory.length < 2) break;
|
||||
if (!navHistory.length) break;
|
||||
|
||||
action = navHistory.pop(); // Current page
|
||||
action = navHistory.pop(); // Previous page
|
||||
|
||||
while (!historyCanGoBackTo(action)) {
|
||||
if (!navHistory.length) {
|
||||
action = null;
|
||||
break;
|
||||
}
|
||||
action = navHistory.pop();
|
||||
let newAction = null;
|
||||
while (navHistory.length) {
|
||||
newAction = navHistory.pop();
|
||||
if (newAction.routeName != state.route.routeName) break;
|
||||
}
|
||||
|
||||
if (!action) action = Object.assign({}, initialRoute);
|
||||
action = newAction ? newAction : navHistory.pop();
|
||||
|
||||
historyGoingBack = true;
|
||||
|
||||
// Fall throught
|
||||
|
||||
@ -114,6 +112,32 @@ const reducer = (state = defaultState, action) => {
|
||||
const currentRoute = state.route;
|
||||
const currentRouteName = currentRoute ? currentRoute.routeName : '';
|
||||
|
||||
if (!historyGoingBack && historyCanGoBackTo(currentRoute)) {
|
||||
// If the route *name* is the same (even if the other parameters are different), we
|
||||
// overwrite the last route in the history with the current one. If the route name
|
||||
// is different, we push a new history entry.
|
||||
if (currentRoute.routeName == action.routeName) {
|
||||
// nothing
|
||||
} else {
|
||||
navHistory.push(currentRoute);
|
||||
}
|
||||
}
|
||||
|
||||
// HACK: whenever a new screen is loaded, all the previous screens of that type
|
||||
// are overwritten with the new screen parameters. This is because the way notes
|
||||
// are currently loaded is not optimal (doesn't retain history properly) so
|
||||
// this is a simple fix without doing a big refactoring to change the way notes
|
||||
// are loaded. Might be good enough since going back to different folders
|
||||
// is probably not a common workflow.
|
||||
for (let i = 0; i < navHistory.length; i++) {
|
||||
let n = navHistory[i];
|
||||
if (n.routeName == action.routeName) {
|
||||
navHistory[i] = Object.assign({}, action);
|
||||
}
|
||||
}
|
||||
|
||||
if (action.routeName == 'Welcome') navHistory = [];
|
||||
|
||||
reg.logger().info('Route: ' + currentRouteName + ' => ' + action.routeName);
|
||||
|
||||
newState = Object.assign({}, state);
|
||||
@ -132,19 +156,7 @@ const reducer = (state = defaultState, action) => {
|
||||
|
||||
newState.route = action;
|
||||
|
||||
// If the route *name* is the same (even if the other parameters are different), we
|
||||
// overwrite the last route in the history with the current one. If the route name
|
||||
// is different, we push a new history entry.
|
||||
|
||||
if (currentRouteName == action.routeName) {
|
||||
if (navHistory.length) navHistory[navHistory.length - 1] = action;
|
||||
// If the current screen is already the requested screen, don't do anything
|
||||
} else {
|
||||
if (action.routeName == 'Welcome') navHistory = [];
|
||||
navHistory.push(action);
|
||||
}
|
||||
|
||||
newState.historyCanGoBack = navHistory.length >= 2;
|
||||
newState.historyCanGoBack = !!navHistory.length;
|
||||
|
||||
if (newState.route.routeName == 'Notes') {
|
||||
Setting.setValue('activeFolderId', newState.selectedFolderId);
|
||||
@ -412,7 +424,11 @@ class AppComponent extends React.Component {
|
||||
|
||||
async componentDidMount() {
|
||||
await initialize(this.props.dispatch, this.backButtonHandler.bind(this));
|
||||
reg.scheduleSync();
|
||||
if (Setting.value('env') == 'dev') {
|
||||
|
||||
} else {
|
||||
reg.scheduleSync();
|
||||
}
|
||||
}
|
||||
|
||||
backButtonHandler() {
|
||||
|
Loading…
Reference in New Issue
Block a user