Commit 0e95eb63 authored by Denys Mishunov's avatar Denys Mishunov Committed by David O'Regan

Switch files before closing active one

To avoid disposing the repo_editor component, we have to
make sure we always have an active file if we have any
open files at all. Othewrwise the whole component gets
diposed that triggers all possible events and unnecessary
processing related to diposing the old one and setting up
the new editor instance
parent 92c36c07
...@@ -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, ...localFile,
pending: true, 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