Skip to content

Commit

Permalink
fix the logic of extracting targets for deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
hdykokd committed Apr 20, 2024
1 parent ee4f64c commit 0a56e6b
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions src/components/Tabs.svelte
Expand Up @@ -61,14 +61,14 @@
const getDirname = (leaf: Leaf) => {
// @ts-expect-error
const file = leaf.view?.file;
if (!file) return ''
if (!file) return '';
return file.parent?.path || '';
};
const getFilename = (leaf: Leaf) => {
// @ts-expect-error
const file = leaf.view?.file;
if (!file) return ''
if (!file) return '';
// title
// @ts-expect-error
Expand Down Expand Up @@ -101,7 +101,7 @@
}
}
}
}
};
const handleMouseDown = async (ev: MouseEvent, leaf: Leaf) => {
ev.stopPropagation();
Expand Down Expand Up @@ -164,15 +164,15 @@
if (leafIdSet.has(leaf.id)) {
const result = action(leaf);
if (result instanceof Promise) {
await result.catch(console.error)
await result.catch(console.error);
}
leafIdSet.delete(leaf.id);
}
if (leafIdSet.size === 0) {
updateView.bind(view)();
return;
}
};
}
updateView.bind(view)();
};
Expand Down Expand Up @@ -212,18 +212,22 @@
updateView.bind(view)();
};
const handleClickBulkPin = (leafIds: string[], pinned: boolean) => {
return handleBulkAction(leafIds, (leaf) => leaf.setPinned(pinned))
return handleBulkAction(leafIds, (leaf) => leaf.setPinned(pinned));
};
const handleClickDelete = async (leaf: Leaf) => {
// @ts-expect-error
await deleteVaultFile(plugin.app, leaf.view.file.path);
const file = leaf.view.file;
if (file.deleted) return;
await deleteVaultFile(plugin.app, file.path);
updateView.bind(view)();
};
const handleClickBulkDelete = async (leafIds: string[]) => {
return handleBulkAction(leafIds, async (leaf) => {
// @ts-expect-error
await deleteVaultFile(plugin.app, leaf.view.file.path)
})
const file = leaf.view.file;
if (file.deleted) return;
await deleteVaultFile(plugin.app, file.path);
});
};
const handleClickTrashSystem = async (leaf: Leaf) => {
// @ts-expect-error
Expand Down Expand Up @@ -374,6 +378,20 @@
const isBulkEdit = selectedLeafIds.length > 1 && selectedLeafIds.includes(selectedLeaf.id);
const leafIds = isBulkEdit ? selectedLeafIds : [selectedLeaf.id];
const titleTabs = isBulkEdit ? `${leafIds.length} tabs` : `tab`;
const uniqueFilepaths = Array.from(
new Set(
leaves
.filter((l) => leafIds.includes(l.id))
.map((l) => {
// @ts-expect-error
const file = l.view.file;
const dirname = file.parent?.path || '';
const filename = file.name;
return `${dirname}${filename}`;
}),
),
);
const titleFiles = isBulkEdit && uniqueFilepaths.length ? `${uniqueFilepaths.length} files` : `file`;
const onClose = () => {
selectedLeafIds = [];
Expand Down Expand Up @@ -425,8 +443,8 @@
});
menu.addSeparator();
if (isBulkEdit) {
const pinnedLeaves: Leaf[] = []
const unpinnedLeaves : Leaf[] = []
const pinnedLeaves: Leaf[] = [];
const unpinnedLeaves: Leaf[] = [];
leaves.forEach((l) => {
if (l.pinned && leafIds.includes(l.id)) {
pinnedLeaves.push(l);
Expand All @@ -438,13 +456,16 @@
if (unpinnedLeaves.length) {
menu.addItem((item) => {
return item
.setTitle(`Pin ${unpinnedLeaves.length} tabs`)
.setIcon('pin')
.onClick((e: MouseEvent) => {
e.preventDefault();
handleClickBulkPin(unpinnedLeaves.map(l => l.id), true);
onClose();
});
.setTitle(`Pin ${unpinnedLeaves.length} tabs`)
.setIcon('pin')
.onClick((e: MouseEvent) => {
e.preventDefault();
handleClickBulkPin(
unpinnedLeaves.map((l) => l.id),
true,
);
onClose();
});
});
}
if (pinnedLeaves.length) {
Expand All @@ -454,7 +475,10 @@
.setIcon('pin-off')
.onClick((e: MouseEvent) => {
e.preventDefault();
handleClickBulkPin(pinnedLeaves.map(l => l.id), false);
handleClickBulkPin(
pinnedLeaves.map((l) => l.id),
false,
);
onClose();
});
});
Expand Down Expand Up @@ -488,7 +512,7 @@
menu.addSeparator();
menu.addItem((item) => {
return item
.setTitle(`Trash ${titleTabs} to local`)
.setTitle(`Trash ${titleFiles} to local`)
.setIcon('trash')
.onClick((e: MouseEvent) => {
e.preventDefault();
Expand All @@ -502,7 +526,7 @@
});
menu.addItem((item) => {
return item
.setTitle(`Trash ${titleTabs} to system`)
.setTitle(`Trash ${titleFiles} to system`)
.setIcon('trash')
.onClick((e: MouseEvent) => {
e.preventDefault();
Expand All @@ -516,7 +540,7 @@
});
menu.addItem((item) => {
return item
.setTitle(`Delete ${titleTabs}`)
.setTitle(`Delete ${titleFiles}`)
.setIcon('trash-2')
.onClick((e: MouseEvent) => {
e.preventDefault();
Expand Down

0 comments on commit 0a56e6b

Please sign in to comment.