Skip to content

Commit 1716e42

Browse files
authored
fix: code editor tab close (#8832)
1 parent a36c0eb commit 1716e42

File tree

2 files changed

+98
-129
lines changed

2 files changed

+98
-129
lines changed

frontend/src/views/host/file-management/code-editor/index.vue

Lines changed: 96 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -438,52 +438,21 @@ monaco.editor.defineTheme('vs', {
438438
const selectTab = ref();
439439
const fileTabs = ref([]);
440440
const removeTab = (targetPath: TabPaneName) => {
441-
if (isEdit.value) {
442-
ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), {
443-
confirmButtonText: i18n.global.t('commons.button.save'),
444-
cancelButtonText: i18n.global.t('commons.button.notSave'),
445-
type: 'info',
446-
distinguishCancelAndClose: true,
447-
})
448-
.then(() => {
449-
const tabs = fileTabs.value;
450-
let activeName = selectTab.value;
451-
if (activeName === targetPath) {
452-
tabs.forEach((tab, index) => {
453-
if (tab.path === targetPath) {
454-
const nextTab = tabs[index + 1] || tabs[index - 1];
455-
if (nextTab) {
456-
activeName = nextTab.path;
457-
}
458-
}
459-
});
460-
}
441+
const tabs = fileTabs.value;
442+
let activeName = selectTab.value;
461443
462-
selectTab.value = activeName;
463-
fileTabs.value = tabs.filter((tab) => tab.path !== targetPath);
464-
saveContent();
465-
})
466-
.finally(() => {});
467-
} else {
468-
const tabs = fileTabs.value;
469-
let activeName = selectTab.value;
444+
const updateTabs = () => {
470445
if (activeName === targetPath) {
471-
tabs.forEach((tab, index) => {
472-
if (tab.path === targetPath) {
473-
const nextTab = tabs[index + 1] || tabs[index - 1];
474-
if (nextTab) {
475-
activeName = nextTab.path;
476-
}
477-
}
478-
});
446+
const index = tabs.findIndex((tab) => tab.path === targetPath);
447+
const nextTab = tabs[index + 1] || tabs[index - 1];
448+
if (nextTab) {
449+
activeName = nextTab.path;
450+
}
479451
}
480452
selectTab.value = activeName;
481453
fileTabs.value = tabs.filter((tab) => tab.path !== targetPath);
482-
}
483-
getContent(selectTab.value, '');
484-
};
454+
};
485455
486-
const removeAllTab = (targetPath: string, type: string) => {
487456
if (isEdit.value) {
488457
ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), {
489458
confirmButtonText: i18n.global.t('commons.button.save'),
@@ -492,117 +461,112 @@ const removeAllTab = (targetPath: string, type: string) => {
492461
distinguishCancelAndClose: true,
493462
})
494463
.then(() => {
495-
const tabs = fileTabs.value;
496-
let activeName = selectTab.value;
497-
if (activeName !== targetPath) {
498-
tabs.forEach((tab, index) => {
499-
if (tab.path === targetPath) {
500-
const nextTab = tabs[index];
501-
if (nextTab) {
502-
activeName = nextTab.path;
503-
}
504-
}
505-
});
506-
}
507-
508-
selectTab.value = activeName;
509-
if (type === 'left') {
510-
fileTabs.value = fileTabs.value.filter((tab, index, arr) => {
511-
const targetIndex = arr.findIndex((t) => t.path === targetPath);
512-
return index >= targetIndex;
513-
});
514-
} else if (type === 'right') {
515-
fileTabs.value = fileTabs.value.filter((tab, index, arr) => {
516-
const targetIndex = arr.findIndex((t) => t.path === targetPath);
517-
return index <= targetIndex;
518-
});
519-
} else if (type === 'all') {
520-
fileTabs.value = [];
521-
selectTab.value = '';
522-
}
464+
updateTabs();
523465
saveContent();
466+
getContent(selectTab.value, '');
524467
})
525-
.finally(() => {});
526-
} else {
527-
const tabs = fileTabs.value;
528-
let activeName = selectTab.value;
529-
if (activeName !== targetPath) {
530-
tabs.forEach((tab, index) => {
531-
if (tab.path === targetPath) {
532-
const nextTab = tabs[index];
533-
if (nextTab) {
534-
activeName = nextTab.path;
535-
}
468+
.catch(() => {
469+
isEdit.value = false;
470+
editor.setValue(oldFileContent.value);
471+
updateTabs();
472+
if (fileTabs.value.length > 0) {
473+
getContent(selectTab.value, '');
536474
}
537475
});
476+
} else {
477+
updateTabs();
478+
getContent(selectTab.value, '');
479+
}
480+
};
481+
482+
const removeAllTab = (targetPath: string, type: 'left' | 'right' | 'all') => {
483+
const tabs = fileTabs.value;
484+
const targetIndex = tabs.findIndex((tab) => tab.path === targetPath);
485+
let activeName = selectTab.value;
486+
487+
const filterTabs = (): typeof fileTabs.value => {
488+
if (type === 'left') return tabs.slice(targetIndex);
489+
if (type === 'right') return tabs.slice(0, targetIndex + 1);
490+
return [];
491+
};
492+
493+
const updateTabs = () => {
494+
if (activeName !== targetPath && type !== 'all') {
495+
activeName = tabs[targetIndex]?.path || '';
538496
}
497+
const newTabs = type === 'all' ? [] : filterTabs();
498+
fileTabs.value = newTabs;
539499
selectTab.value = activeName;
540-
if (type === 'left') {
541-
fileTabs.value = fileTabs.value.filter((tab, index, arr) => {
542-
const targetIndex = arr.findIndex((t) => t.path === targetPath);
543-
return index >= targetIndex;
544-
});
545-
} else if (type === 'right') {
546-
fileTabs.value = fileTabs.value.filter((tab, index, arr) => {
547-
const targetIndex = arr.findIndex((t) => t.path === targetPath);
548-
return index <= targetIndex;
549-
});
550-
} else if (type === 'all') {
551-
fileTabs.value = [];
500+
501+
if (type === 'all') {
552502
selectTab.value = '';
503+
editor.dispose();
504+
} else if (newTabs.length > 0) {
505+
getContent(activeName, '');
553506
}
554-
}
555-
if (type === 'all') {
556-
editor.dispose();
507+
};
508+
509+
const onConfirm = () => {
510+
updateTabs();
511+
saveContent();
512+
};
513+
514+
const onCancel = () => {
515+
isEdit.value = false;
516+
editor.setValue(oldFileContent.value);
517+
updateTabs();
518+
};
519+
520+
if (isEdit.value) {
521+
ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), {
522+
confirmButtonText: i18n.global.t('commons.button.save'),
523+
cancelButtonText: i18n.global.t('commons.button.notSave'),
524+
type: 'info',
525+
distinguishCancelAndClose: true,
526+
})
527+
.then(onConfirm)
528+
.catch(onCancel);
557529
} else {
558-
getContent(selectTab.value, '');
530+
updateTabs();
531+
if (type === 'all') editor.dispose();
532+
else getContent(activeName, '');
559533
}
560534
};
561535
562536
const removeOtherTab = (targetPath: string) => {
537+
const tabs = fileTabs.value;
538+
const targetTab = tabs.find((tab) => tab.path === targetPath);
539+
if (!targetTab) return;
540+
541+
const updateTabs = () => {
542+
fileTabs.value = [targetTab];
543+
selectTab.value = targetTab.path;
544+
getContent(targetTab.path, '');
545+
};
546+
547+
const onConfirm = () => {
548+
updateTabs();
549+
saveContent();
550+
};
551+
552+
const onCancel = () => {
553+
isEdit.value = false;
554+
editor.setValue(oldFileContent.value);
555+
updateTabs();
556+
};
557+
563558
if (isEdit.value) {
564559
ElMessageBox.confirm(i18n.global.t('file.saveContentAndClose'), {
565560
confirmButtonText: i18n.global.t('commons.button.save'),
566561
cancelButtonText: i18n.global.t('commons.button.notSave'),
567562
type: 'info',
568563
distinguishCancelAndClose: true,
569564
})
570-
.then(() => {
571-
const tabs = fileTabs.value;
572-
let activeName = selectTab.value;
573-
if (activeName !== targetPath) {
574-
tabs.forEach((tab, index) => {
575-
if (tab.path === targetPath) {
576-
const nextTab = tabs[index];
577-
if (nextTab) {
578-
activeName = nextTab.path;
579-
}
580-
}
581-
});
582-
}
583-
584-
selectTab.value = activeName;
585-
fileTabs.value = tabs.filter((tab) => tab.path === targetPath);
586-
saveContent();
587-
})
588-
.finally(() => {});
565+
.then(onConfirm)
566+
.catch(onCancel);
589567
} else {
590-
const tabs = fileTabs.value;
591-
let activeName = selectTab.value;
592-
if (activeName !== targetPath) {
593-
tabs.forEach((tab, index) => {
594-
if (tab.path === targetPath) {
595-
const nextTab = tabs[index];
596-
if (nextTab) {
597-
activeName = nextTab.path;
598-
}
599-
}
600-
});
601-
}
602-
selectTab.value = activeName;
603-
fileTabs.value = tabs.filter((tab) => tab.path === targetPath);
568+
updateTabs();
604569
}
605-
getContent(selectTab.value, '');
606570
};
607571
608572
const changeTab = (targetPath: TabPaneName) => {
@@ -646,6 +610,9 @@ const em = defineEmits(['close']);
646610
const handleClose = () => {
647611
const closeEditor = () => {
648612
open.value = false;
613+
selectTab.value = '';
614+
fileTabs.value = [];
615+
isEdit.value = false;
649616
if (editor) {
650617
editor.dispose();
651618
}

frontend/src/views/host/file-management/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,8 @@ const handleSearchResult = (res: ResultData<File.File>) => {
684684
};
685685
686686
const open = async (row: File.File) => {
687+
calculateBtn.value = false;
688+
disableBtn.value = false;
687689
if (row.isDir) {
688690
if (row.name.indexOf('.1panel_clash') > -1) {
689691
MsgWarning(i18n.global.t('file.clashOpenAlert'));

0 commit comments

Comments
 (0)