fix: code editor tab close (#8832)

This commit is contained in:
2025-05-26 16:02:06 +08:00 committed by GitHub
parent a36c0eb22f
commit 1716e42ffe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 102 additions and 133 deletions

View file

@ -438,6 +438,21 @@ monaco.editor.defineTheme('vs', {
const selectTab = ref(); const selectTab = ref();
const fileTabs = ref([]); const fileTabs = ref([]);
const removeTab = (targetPath: TabPaneName) => { const removeTab = (targetPath: TabPaneName) => {
const tabs = fileTabs.value;
let activeName = selectTab.value;
const updateTabs = () => {
if (activeName === targetPath) {
const index = tabs.findIndex((tab) => tab.path === targetPath);
const nextTab = tabs[index + 1] || tabs[index - 1];
if (nextTab) {
activeName = nextTab.path;
}
}
selectTab.value = activeName;
fileTabs.value = tabs.filter((tab) => tab.path !== targetPath);
};
if (isEdit.value) { if (isEdit.value) {
ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), { ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), {
confirmButtonText: i18n.global.t('commons.button.save'), confirmButtonText: i18n.global.t('commons.button.save'),
@ -446,44 +461,62 @@ const removeTab = (targetPath: TabPaneName) => {
distinguishCancelAndClose: true, distinguishCancelAndClose: true,
}) })
.then(() => { .then(() => {
const tabs = fileTabs.value; updateTabs();
let activeName = selectTab.value;
if (activeName === targetPath) {
tabs.forEach((tab, index) => {
if (tab.path === targetPath) {
const nextTab = tabs[index + 1] || tabs[index - 1];
if (nextTab) {
activeName = nextTab.path;
}
}
});
}
selectTab.value = activeName;
fileTabs.value = tabs.filter((tab) => tab.path !== targetPath);
saveContent(); saveContent();
getContent(selectTab.value, '');
}) })
.finally(() => {}); .catch(() => {
} else { isEdit.value = false;
const tabs = fileTabs.value; editor.setValue(oldFileContent.value);
let activeName = selectTab.value; updateTabs();
if (activeName === targetPath) { if (fileTabs.value.length > 0) {
tabs.forEach((tab, index) => { getContent(selectTab.value, '');
if (tab.path === targetPath) {
const nextTab = tabs[index + 1] || tabs[index - 1];
if (nextTab) {
activeName = nextTab.path;
}
} }
}); });
} } else {
selectTab.value = activeName; updateTabs();
fileTabs.value = tabs.filter((tab) => tab.path !== targetPath);
}
getContent(selectTab.value, ''); getContent(selectTab.value, '');
}
}; };
const removeAllTab = (targetPath: string, type: string) => { const removeAllTab = (targetPath: string, type: 'left' | 'right' | 'all') => {
const tabs = fileTabs.value;
const targetIndex = tabs.findIndex((tab) => tab.path === targetPath);
let activeName = selectTab.value;
const filterTabs = (): typeof fileTabs.value => {
if (type === 'left') return tabs.slice(targetIndex);
if (type === 'right') return tabs.slice(0, targetIndex + 1);
return [];
};
const updateTabs = () => {
if (activeName !== targetPath && type !== 'all') {
activeName = tabs[targetIndex]?.path || '';
}
const newTabs = type === 'all' ? [] : filterTabs();
fileTabs.value = newTabs;
selectTab.value = activeName;
if (type === 'all') {
selectTab.value = '';
editor.dispose();
} else if (newTabs.length > 0) {
getContent(activeName, '');
}
};
const onConfirm = () => {
updateTabs();
saveContent();
};
const onCancel = () => {
isEdit.value = false;
editor.setValue(oldFileContent.value);
updateTabs();
};
if (isEdit.value) { if (isEdit.value) {
ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), { ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), {
confirmButtonText: i18n.global.t('commons.button.save'), confirmButtonText: i18n.global.t('commons.button.save'),
@ -491,75 +524,37 @@ const removeAllTab = (targetPath: string, type: string) => {
type: 'info', type: 'info',
distinguishCancelAndClose: true, distinguishCancelAndClose: true,
}) })
.then(() => { .then(onConfirm)
const tabs = fileTabs.value; .catch(onCancel);
let activeName = selectTab.value;
if (activeName !== targetPath) {
tabs.forEach((tab, index) => {
if (tab.path === targetPath) {
const nextTab = tabs[index];
if (nextTab) {
activeName = nextTab.path;
}
}
});
}
selectTab.value = activeName;
if (type === 'left') {
fileTabs.value = fileTabs.value.filter((tab, index, arr) => {
const targetIndex = arr.findIndex((t) => t.path === targetPath);
return index >= targetIndex;
});
} else if (type === 'right') {
fileTabs.value = fileTabs.value.filter((tab, index, arr) => {
const targetIndex = arr.findIndex((t) => t.path === targetPath);
return index <= targetIndex;
});
} else if (type === 'all') {
fileTabs.value = [];
selectTab.value = '';
}
saveContent();
})
.finally(() => {});
} else { } else {
const tabs = fileTabs.value; updateTabs();
let activeName = selectTab.value; if (type === 'all') editor.dispose();
if (activeName !== targetPath) { else getContent(activeName, '');
tabs.forEach((tab, index) => {
if (tab.path === targetPath) {
const nextTab = tabs[index];
if (nextTab) {
activeName = nextTab.path;
}
}
});
}
selectTab.value = activeName;
if (type === 'left') {
fileTabs.value = fileTabs.value.filter((tab, index, arr) => {
const targetIndex = arr.findIndex((t) => t.path === targetPath);
return index >= targetIndex;
});
} else if (type === 'right') {
fileTabs.value = fileTabs.value.filter((tab, index, arr) => {
const targetIndex = arr.findIndex((t) => t.path === targetPath);
return index <= targetIndex;
});
} else if (type === 'all') {
fileTabs.value = [];
selectTab.value = '';
}
}
if (type === 'all') {
editor.dispose();
} else {
getContent(selectTab.value, '');
} }
}; };
const removeOtherTab = (targetPath: string) => { const removeOtherTab = (targetPath: string) => {
const tabs = fileTabs.value;
const targetTab = tabs.find((tab) => tab.path === targetPath);
if (!targetTab) return;
const updateTabs = () => {
fileTabs.value = [targetTab];
selectTab.value = targetTab.path;
getContent(targetTab.path, '');
};
const onConfirm = () => {
updateTabs();
saveContent();
};
const onCancel = () => {
isEdit.value = false;
editor.setValue(oldFileContent.value);
updateTabs();
};
if (isEdit.value) { if (isEdit.value) {
ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), { ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), {
confirmButtonText: i18n.global.t('commons.button.save'), confirmButtonText: i18n.global.t('commons.button.save'),
@ -567,42 +562,11 @@ const removeOtherTab = (targetPath: string) => {
type: 'info', type: 'info',
distinguishCancelAndClose: true, distinguishCancelAndClose: true,
}) })
.then(() => { .then(onConfirm)
const tabs = fileTabs.value; .catch(onCancel);
let activeName = selectTab.value;
if (activeName !== targetPath) {
tabs.forEach((tab, index) => {
if (tab.path === targetPath) {
const nextTab = tabs[index];
if (nextTab) {
activeName = nextTab.path;
}
}
});
}
selectTab.value = activeName;
fileTabs.value = tabs.filter((tab) => tab.path === targetPath);
saveContent();
})
.finally(() => {});
} else { } else {
const tabs = fileTabs.value; updateTabs();
let activeName = selectTab.value;
if (activeName !== targetPath) {
tabs.forEach((tab, index) => {
if (tab.path === targetPath) {
const nextTab = tabs[index];
if (nextTab) {
activeName = nextTab.path;
} }
}
});
}
selectTab.value = activeName;
fileTabs.value = tabs.filter((tab) => tab.path === targetPath);
}
getContent(selectTab.value, '');
}; };
const changeTab = (targetPath: TabPaneName) => { const changeTab = (targetPath: TabPaneName) => {
@ -646,6 +610,9 @@ const em = defineEmits(['close']);
const handleClose = () => { const handleClose = () => {
const closeEditor = () => { const closeEditor = () => {
open.value = false; open.value = false;
selectTab.value = '';
fileTabs.value = [];
isEdit.value = false;
if (editor) { if (editor) {
editor.dispose(); editor.dispose();
} }

View file

@ -684,6 +684,8 @@ const handleSearchResult = (res: ResultData<File.File>) => {
}; };
const open = async (row: File.File) => { const open = async (row: File.File) => {
calculateBtn.value = false;
disableBtn.value = false;
if (row.isDir) { if (row.isDir) {
if (row.name.indexOf('.1panel_clash') > -1) { if (row.name.indexOf('.1panel_clash') > -1) {
MsgWarning(i18n.global.t('file.clashOpenAlert')); MsgWarning(i18n.global.t('file.clashOpenAlert'));