Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: add delete key shortcut for deleting items (#1655)

Muhammad Alibfc77fc9a46a
diff --git a/apps/web/src/common/multi-select.ts b/apps/web/src/common/multi-select.ts
index f66ae69c4..830decbc3 100644
--- a/apps/web/src/common/multi-select.ts
+++ b/apps/web/src/common/multi-select.ts
@@ -21,6 +21,8 @@ import { store as noteStore } from "../stores/note-store";
import { store as notebookStore } from "../stores/notebook-store";
import { store as attachmentStore } from "../stores/attachment-store";
import { store as reminderStore } from "../stores/reminder-store";
+import { store as tagStore } from "../stores/tag-store";
+import { store as appStore } from "../stores/app-store";
import { db } from "./db";
import { showToast } from "../utils/toast";
import Vault from "./vault";
@@ -59,6 +61,8 @@ async function moveNotesToTrash(ids: string[], confirm = true) {
}
async function moveNotebooksToTrash(ids: string[]) {
+ if (!ids.length) return;
+
const isMultiselect = ids.length > 1;
if (isMultiselect) {
if (!(await showMultiDeleteConfirmation(ids.length))) return;
@@ -134,9 +138,42 @@ async function moveRemindersToTrash(ids: string[]) {
showToast("success", `${pluralize(ids.length, "reminder")} deleted.`);
}
+async function deleteTags(ids: string[]) {
+ if (
+ !(await ConfirmDialog.show({
+ title: "Are you sure?",
+ message:
+ "Are you sure you want to permanently delete these tags? This action is IRREVERSIBLE.",
+ negativeButtonText: "No",
+ positiveButtonText: "Yes"
+ }))
+ )
+ return;
+
+ await TaskManager.startTask({
+ type: "status",
+ id: "deleteTags",
+ title: "Deleting tags",
+ action: async (report) => {
+ report({
+ text: `Deleting ${pluralize(ids.length, "tag")}...`
+ });
+ for (const id of ids) {
+ await db.tags.remove(id);
+ }
+ await appStore.refreshNavItems();
+ await tagStore.refresh();
+ await noteStore.refresh();
+ }
+ });
+
+ showToast("success", `${pluralize(ids.length, "tag")} deleted.`);
+}
+
export const Multiselect = {
moveRemindersToTrash,
moveNotebooksToTrash,
moveNotesToTrash,
- deleteAttachments
+ deleteAttachments,
+ deleteTags
};
diff --git a/apps/web/src/components/attachment/index.tsx b/apps/web/src/components/attachment/index.tsx
index 8d2e52c97..8890191c1 100644
--- a/apps/web/src/components/attachment/index.tsx
+++ b/apps/web/src/components/attachment/index.tsx
@@ -57,6 +57,7 @@ import { Attachment as AttachmentType } from "@notesnook/core";
import { useEditorStore } from "../../stores/editor-store";
import { PromptDialog } from "../../dialogs/prompt";
import { DialogManager } from "../../common/dialog-manager";
+import { useStore as useSelectionStore } from "../../stores/selection-store";
const FILE_ICONS: Record<string, Icon> = {
"image/": FileImage,
@@ -135,6 +136,13 @@ export function Attachment({
Menu.openMenu(AttachmentMenuItems(item, status));
}}
onClick={onSelected}
+ onKeyPress={async (e) => {
+ if (e.key === "Delete") {
+ await Multiselect.deleteAttachments(
+ useSelectionStore.getState().selectedItems
+ );
+ }
+ }}
style={style}
ref={rowRef}
>
diff --git a/apps/web/src/components/note/index.tsx b/apps/web/src/components/note/index.tsx
index 9a402da66..e21a2445c 100644
--- a/apps/web/src/components/note/index.tsx
+++ b/apps/web/src/components/note/index.tsx
@@ -133,8 +133,9 @@ function Note(props: NoteProps) {
onKeyPress={async (e) => {
if (e.key === "Delete") {
// @ts-expect-error write tests for this
- const selectedItems = selectionStore.get().selectedItems;
- await Multiselect.moveNotesToTrash([item.id, ...selectedItems]);
+ await Multiselect.moveNotesToTrash(
+ selectionStore.get().selectedItems
+ );
}
}}
colors={{
diff --git a/apps/web/src/components/notebook/index.tsx b/apps/web/src/components/notebook/index.tsx
index c547a4fa4..f6e4b925e 100644
--- a/apps/web/src/components/notebook/index.tsx
+++ b/apps/web/src/components/notebook/index.tsx
@@ -44,6 +44,7 @@ import { Notebook as NotebookType } from "@notesnook/core";
import { handleDrop } from "../../common/drop-handler";
import { useDragHandler } from "../../hooks/use-drag-handler";
import { ConfirmDialog } from "../../dialogs/confirm";
+import { useStore as useSelectionStore } from "../../stores/selection-store";
type NotebookProps = {
item: NotebookType;
@@ -80,6 +81,13 @@ function Notebook(props: NotebookProps) {
handleDrop(e.dataTransfer, notebook);
}}
+ onKeyPress={async (e) => {
+ if (e.key === "Delete") {
+ await Multiselect.moveNotebooksToTrash(
+ useSelectionStore.getState().selectedItems
+ );
+ }
+ }}
title={notebook.title}
body={notebook.description as string}
menuItems={notebookMenuItems}
diff --git a/apps/web/src/components/reminder/index.tsx b/apps/web/src/components/reminder/index.tsx
index 9681fccc0..9ba136ea1 100644
--- a/apps/web/src/components/reminder/index.tsx
+++ b/apps/web/src/components/reminder/index.tsx
@@ -43,6 +43,7 @@ import { MenuItem } from "@notesnook/ui";
import { Reminder as ReminderType } from "@notesnook/core/dist/types";
import { ConfirmDialog } from "../../dialogs/confirm";
import { EditReminderDialog } from "../../dialogs/add-reminder-dialog";
+import { useStore as useSelectionStore } from "../../stores/selection-store";
const RECURRING_MODE_MAP = {
week: "Weekly",
@@ -74,6 +75,13 @@ function Reminder(props: ReminderProps) {
isDisabled={reminder.disabled}
isCompact={compact}
onClick={() => EditReminderDialog.show({ reminderId: reminder.id })}
+ onKeyPress={async (e) => {
+ if (e.key === "Delete") {
+ await Multiselect.moveRemindersToTrash(
+ useSelectionStore.getState().selectedItems
+ );
+ }
+ }}
footer={
<Flex
sx={{
diff --git a/apps/web/src/components/sub-notebook/index.tsx b/apps/web/src/components/sub-notebook/index.tsx
index 560517709..907cf53d5 100644
--- a/apps/web/src/components/sub-notebook/index.tsx
+++ b/apps/web/src/components/sub-notebook/index.tsx
@@ -29,6 +29,8 @@ import { useCallback, useRef } from "react";
import { handleDrop } from "../../common/drop-handler";
import { useDragHandler } from "../../hooks/use-drag-handler";
import { AddNotebookDialog } from "../../dialogs/add-notebook-dialog";
+import { useStore as useSelectionStore } from "../../stores/selection-store";
+import { Multiselect } from "../../common/multi-select";
type SubNotebookProps = {
item: Notebook;
@@ -112,6 +114,11 @@ function SubNotebook(props: SubNotebookProps) {
});
navigate(`/notebooks/${rootId}/${item.id}`);
}
+ } else if (e.code === "Delete") {
+ console.log("Delete", useSelectionStore.getState().selectedItems);
+ await Multiselect.moveNotebooksToTrash(
+ useSelectionStore.getState().selectedItems
+ );
}
}}
title={
diff --git a/apps/web/src/components/tag/index.tsx b/apps/web/src/components/tag/index.tsx
index 6607e0837..d7a0b73c8 100644
--- a/apps/web/src/components/tag/index.tsx
+++ b/apps/web/src/components/tag/index.tsx
@@ -21,16 +21,14 @@ import ListItem from "../list-item";
import { navigate } from "../../navigation";
import { Text } from "@theme-ui/components";
import { store as appStore } from "../../stores/app-store";
-import { store as tagStore } from "../../stores/tag-store";
-import { store as noteStore } from "../../stores/note-store";
import { db } from "../../common/db";
import { Edit, Shortcut, DeleteForver } from "../icons";
-import { showToast } from "../../utils/toast";
-import { pluralize } from "@notesnook/common";
import { MenuItem } from "@notesnook/ui";
import { Tag as TagType } from "@notesnook/core";
import { handleDrop } from "../../common/drop-handler";
import { EditTagDialog } from "../../dialogs/item-dialog";
+import { useStore as useSelectionStore } from "../../stores/selection-store";
+import { Multiselect } from "../../common/multi-select";
type TagProps = { item: TagType; totalNotes: number };
function Tag(props: TagProps) {
@@ -54,6 +52,13 @@ function Tag(props: TagProps) {
{totalNotes}
</Text>
}
+ onKeyPress={async (e) => {
+ if (e.key === "Delete") {
+ await Multiselect.deleteTags(
+ useSelectionStore.getState().selectedItems
+ );
+ }
+ }}
menuItems={menuItems}
onClick={() => {
navigate(`/tags/${id}`);
@@ -96,11 +101,7 @@ const menuItems: (tag: TagType, ids?: string[]) => MenuItem[] = (
title: "Delete",
icon: DeleteForver.path,
onClick: async () => {
- await db.tags.remove(...ids);
- showToast("success", `${pluralize(ids.length, "tag")} deleted`);
- await appStore.refreshNavItems();
- await tagStore.refresh();
- await noteStore.refresh();
+ await Multiselect.deleteTags(ids);
},
multiSelect: true
}
diff --git a/apps/web/src/components/trash-item/index.tsx b/apps/web/src/components/trash-item/index.tsx
index eed80a193..ca175982a 100644
--- a/apps/web/src/components/trash-item/index.tsx
+++ b/apps/web/src/components/trash-item/index.tsx
@@ -28,6 +28,7 @@ import { MenuItem } from "@notesnook/ui";
import { TrashItem as TrashItemType } from "@notesnook/core/dist/types";
import { useEditorStore } from "../../stores/editor-store";
import { showMultiPermanentDeleteConfirmation } from "../../dialogs/confirm";
+import { useStore as useSelectionStore } from "../../stores/selection-store";
type TrashItemProps = { item: TrashItemType; date: number };
function TrashItem(props: TrashItemProps) {
@@ -40,6 +41,11 @@ function TrashItem(props: TrashItemProps) {
item={item}
title={item.title}
body={item.itemType === "note" ? item.headline : item.description}
+ onKeyPress={async (e) => {
+ if (e.key === "Delete") {
+ await deleteTrash(useSelectionStore.getState().selectedItems);
+ }
+ }}
footer={
<Flex
mt={1}
@@ -87,14 +93,15 @@ const menuItems: (item: TrashItemType, ids?: string[]) => MenuItem[] = (
icon: DeleteForver.path,
variant: "dangerous",
onClick: async () => {
- if (!(await showMultiPermanentDeleteConfirmation(ids.length))) return;
- await store.delete(...ids);
- showToast(
- "success",
- `${pluralize(ids.length, "item")} permanently deleted`
- );
+ await deleteTrash(ids);
},
multiSelect: true
}
];
};
+
+async function deleteTrash(ids: string[]) {
+ if (!(await showMultiPermanentDeleteConfirmation(ids.length))) return;
+ await store.delete(...ids);
+ showToast("success", `${pluralize(ids.length, "item")} permanently deleted`);
+}
diff --git a/apps/web/src/dialogs/confirm.tsx b/apps/web/src/dialogs/confirm.tsx
index 48ea6f1a3..280da7ecb 100644
--- a/apps/web/src/dialogs/confirm.tsx
+++ b/apps/web/src/dialogs/confirm.tsx
@@ -26,6 +26,7 @@ import { db } from "../common/db";
import { getChangelog } from "../utils/version";
import { downloadUpdate } from "../utils/updater";
import { ErrorText } from "../components/error-text";
+import { pluralize } from "@notesnook/common";
type Check = { text: string; default?: boolean };
export type ConfirmDialogProps<TCheckId extends string> = BaseDialogProps<
@@ -143,7 +144,7 @@ export const ConfirmDialog = DialogManager.register(function ConfirmDialog<
export function showMultiDeleteConfirmation(length: number) {
return ConfirmDialog.show({
- title: `Delete ${length} items?`,
+ title: `Delete ${pluralize(length, "item")}?`,
message: `These items will be **kept in your Trash for ${
db.settings.getTrashCleanupInterval() || 7
} days** after which they will be permanently deleted.`,
@@ -154,7 +155,7 @@ export function showMultiDeleteConfirmation(length: number) {
export function showMultiPermanentDeleteConfirmation(length: number) {
return ConfirmDialog.show({
- title: `Permanently delete ${length} items?`,
+ title: `Permanently delete ${pluralize(length, "item")}?`,
message:
"These items will be **permanently deleted**. This is IRREVERSIBLE.",
positiveButtonText: "Yes",