close tabs question

This commit is contained in:
Jan Prochazka
2022-09-29 19:35:58 +02:00
parent 7604889b72
commit dd33d96ef6
2 changed files with 79 additions and 3 deletions

View File

@@ -1,5 +1,23 @@
<script lang="ts" context="module">
const closeTabFunc = closeCondition => tabid => {
function allowCloseTabs(tabs) {
if (tabs.length == 0) return Promise.resolve(true);
return new Promise(resolve => {
showModal(CloseTabModal, {
onCancel: () => resolve(false),
onConfirm: () => resolve(true),
tabs,
});
});
}
const closeTabFunc = closeCondition => async tabid => {
const activeCandidate = getOpenedTabs().find(x => x.tabid == tabid);
const closeCandidates = getOpenedTabs()
.filter(x => closeCondition(x, activeCandidate))
.filter(x => x.unsaved && x.closedTime == null);
if (!(await allowCloseTabs(closeCandidates))) return;
openedTabs.update(files => {
const active = files.find(x => x.tabid == tabid);
if (!active) return files;
@@ -22,7 +40,13 @@
});
};
export const closeMultipleTabs = (closeCondition, deleteFromHistory = false) => {
export const closeMultipleTabs = async (closeCondition, deleteFromHistory = false) => {
const closeCandidates = getOpenedTabs()
.filter(x => closeCondition(x))
.filter(x => x.unsaved && x.closedTime == null);
if (!(await allowCloseTabs(closeCandidates))) return;
openedTabs.update(files => {
const newFiles = deleteFromHistory
? files.filter(x => !closeCondition(x))
@@ -45,7 +69,11 @@
};
const closeTab = closeTabFunc((x, active) => x.tabid == active.tabid);
const closeAll = () => {
const closeAll = async () => {
const closeCandidates = getOpenedTabs().filter(x => x.unsaved && x.closedTime == null);
if (!(await allowCloseTabs(closeCandidates))) return;
const closedTime = new Date().getTime();
openedTabs.update(tabs =>
tabs.map(tab => ({
@@ -213,6 +241,7 @@
import { duplicateTab, getTabDbKey, sortTabs, groupTabs } from '../utility/openNewTab';
import { useConnectionColorFactory } from '../utility/useConnectionColor';
import TabCloseButton from '../elements/TabCloseButton.svelte';
import CloseTabModal from '../modals/CloseTabModal.svelte';
$: connectionList = useConnectionList();