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,52 +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) => {
if (isEdit.value) {
ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), {
confirmButtonText: i18n.global.t('commons.button.save'),
cancelButtonText: i18n.global.t('commons.button.notSave'),
type: 'info',
distinguishCancelAndClose: true,
})
.then(() => {
const tabs = fileTabs.value; const tabs = fileTabs.value;
let activeName = selectTab.value; 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; const updateTabs = () => {
fileTabs.value = tabs.filter((tab) => tab.path !== targetPath);
saveContent();
})
.finally(() => {});
} else {
const tabs = fileTabs.value;
let activeName = selectTab.value;
if (activeName === targetPath) { if (activeName === targetPath) {
tabs.forEach((tab, index) => { const index = tabs.findIndex((tab) => tab.path === targetPath);
if (tab.path === targetPath) {
const nextTab = tabs[index + 1] || tabs[index - 1]; const nextTab = tabs[index + 1] || tabs[index - 1];
if (nextTab) { if (nextTab) {
activeName = nextTab.path; activeName = nextTab.path;
} }
} }
});
}
selectTab.value = activeName; selectTab.value = activeName;
fileTabs.value = tabs.filter((tab) => tab.path !== targetPath); fileTabs.value = tabs.filter((tab) => tab.path !== targetPath);
}
getContent(selectTab.value, '');
}; };
const removeAllTab = (targetPath: string, type: string) => {
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'),
@ -492,74 +461,100 @@ const removeAllTab = (targetPath: string, type: string) => {
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];
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(); saveContent();
})
.finally(() => {});
} else {
const tabs = fileTabs.value;
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 = '';
}
}
if (type === 'all') {
editor.dispose();
} else {
getContent(selectTab.value, ''); getContent(selectTab.value, '');
})
.catch(() => {
isEdit.value = false;
editor.setValue(oldFileContent.value);
updateTabs();
if (fileTabs.value.length > 0) {
getContent(selectTab.value, '');
}
});
} else {
updateTabs();
getContent(selectTab.value, '');
}
};
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) {
ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), {
confirmButtonText: i18n.global.t('commons.button.save'),
cancelButtonText: i18n.global.t('commons.button.notSave'),
type: 'info',
distinguishCancelAndClose: true,
})
.then(onConfirm)
.catch(onCancel);
} else {
updateTabs();
if (type === 'all') editor.dispose();
else getContent(activeName, '');
} }
}; };
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'));