Commit b98308ee authored by David O'Regan's avatar David O'Regan

Merge branch '292498/webide-switch-before-closing' into 'master'

In WebIDE, when closing a file, switch to the new one before closing the active one

See merge request gitlab-org/gitlab!51483
parents 9e06c4dc 0e95eb63
...@@ -17,15 +17,8 @@ export const closeFile = ({ commit, state, dispatch, getters }, file) => { ...@@ -17,15 +17,8 @@ export const closeFile = ({ commit, state, dispatch, getters }, file) => {
const indexOfClosedFile = state.openFiles.findIndex((f) => f.key === file.key); const indexOfClosedFile = state.openFiles.findIndex((f) => f.key === file.key);
const fileWasActive = file.active; const fileWasActive = file.active;
if (file.pending) { if (state.openFiles.length > 1 && fileWasActive) {
commit(types.REMOVE_PENDING_TAB, file); const nextIndexToOpen = indexOfClosedFile === 0 ? 1 : indexOfClosedFile - 1;
} else {
commit(types.TOGGLE_FILE_OPEN, path);
commit(types.SET_FILE_ACTIVE, { path, active: false });
}
if (state.openFiles.length > 0 && fileWasActive) {
const nextIndexToOpen = indexOfClosedFile === 0 ? 0 : indexOfClosedFile - 1;
const nextFileToOpen = state.openFiles[nextIndexToOpen]; const nextFileToOpen = state.openFiles[nextIndexToOpen];
if (nextFileToOpen.pending) { if (nextFileToOpen.pending) {
...@@ -35,14 +28,22 @@ export const closeFile = ({ commit, state, dispatch, getters }, file) => { ...@@ -35,14 +28,22 @@ export const closeFile = ({ commit, state, dispatch, getters }, file) => {
keyPrefix: nextFileToOpen.staged ? 'staged' : 'unstaged', keyPrefix: nextFileToOpen.staged ? 'staged' : 'unstaged',
}); });
} else { } else {
dispatch('setFileActive', nextFileToOpen.path);
dispatch('router/push', getters.getUrlForPath(nextFileToOpen.path), { root: true }); dispatch('router/push', getters.getUrlForPath(nextFileToOpen.path), { root: true });
} }
} else if (!state.openFiles.length) { } else if (state.openFiles.length === 1) {
dispatch('router/push', `/project/${state.currentProjectId}/tree/${state.currentBranchId}/`, { dispatch('router/push', `/project/${state.currentProjectId}/tree/${state.currentBranchId}/`, {
root: true, root: true,
}); });
} }
if (file.pending) {
commit(types.REMOVE_PENDING_TAB, file);
} else {
commit(types.TOGGLE_FILE_OPEN, path);
commit(types.SET_FILE_ACTIVE, { path, active: false });
}
eventHub.$emit(`editor.update.model.dispose.${file.key}`); eventHub.$emit(`editor.update.model.dispose.${file.key}`);
}; };
......
---
title: In WebIDE switch files before closing the active one
merge_request: 51483
author:
type: fixed
...@@ -75,7 +75,7 @@ describe('IDE store file actions', () => { ...@@ -75,7 +75,7 @@ describe('IDE store file actions', () => {
}); });
}); });
it('closes file & opens next available file', () => { it('switches to the next available file before closing the current one ', () => {
const f = file('newOpenFile'); const f = file('newOpenFile');
store.state.openFiles.push(f); store.state.openFiles.push(f);
...@@ -90,10 +90,12 @@ describe('IDE store file actions', () => { ...@@ -90,10 +90,12 @@ describe('IDE store file actions', () => {
}); });
it('removes file if it pending', () => { it('removes file if it pending', () => {
store.state.openFiles.push({ store.state.openFiles = [
...localFile, {
pending: true, ...localFile,
}); pending: true,
},
];
return store.dispatch('closeFile', localFile).then(() => { return store.dispatch('closeFile', localFile).then(() => {
expect(store.state.openFiles.length).toBe(0); expect(store.state.openFiles.length).toBe(0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment