2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
import { Linking } from "react-native";
|
2023-06-10 12:17:23 +05:00
|
|
|
import { db } from "../common/database";
|
|
|
|
|
import { presentDialog } from "../components/dialog/functions";
|
2023-08-29 20:42:45 +05:00
|
|
|
import { eSendEvent, ToastManager } from "../services/event-manager";
|
2022-08-26 16:19:39 +05:00
|
|
|
import Navigation from "../services/navigation";
|
|
|
|
|
import SearchService from "../services/search";
|
|
|
|
|
import { useMenuStore } from "../stores/use-menu-store";
|
2023-01-07 16:03:54 +05:00
|
|
|
import { useRelationStore } from "../stores/use-relation-store";
|
2023-06-10 12:17:23 +05:00
|
|
|
import { useSelectionStore } from "../stores/use-selection-store";
|
2023-11-16 08:54:37 +05:00
|
|
|
import { eClearEditor, eOnNotebookUpdated } from "./events";
|
|
|
|
|
import { getParentNotebookId } from "./notebooks";
|
2020-11-14 10:02:38 +05:00
|
|
|
|
2023-06-10 12:17:23 +05:00
|
|
|
function confirmDeleteAllNotes(items, type, context) {
|
2023-01-13 14:43:52 +05:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
presentDialog({
|
2023-03-17 14:21:03 +05:00
|
|
|
title: `Delete ${
|
|
|
|
|
items.length > 1 ? `${items.length} ${type}s` : `${type}`
|
2023-01-13 14:43:52 +05:00
|
|
|
}?`,
|
2023-03-17 14:21:03 +05:00
|
|
|
positiveText: "Delete",
|
|
|
|
|
negativeText: "Cancel",
|
2024-03-04 15:54:42 +05:00
|
|
|
positivePress: (_inputValue, value) => {
|
2023-03-31 23:59:37 +05:00
|
|
|
setTimeout(() => {
|
|
|
|
|
resolve({ delete: true, deleteNotes: value });
|
|
|
|
|
});
|
2023-01-13 14:43:52 +05:00
|
|
|
},
|
|
|
|
|
onClose: () => {
|
2023-03-31 23:59:37 +05:00
|
|
|
setTimeout(() => {
|
|
|
|
|
resolve({ delete: false });
|
|
|
|
|
});
|
2023-01-13 14:43:52 +05:00
|
|
|
},
|
2023-03-17 14:21:03 +05:00
|
|
|
context: context,
|
|
|
|
|
check: {
|
|
|
|
|
info: `Move all notes in ${
|
|
|
|
|
items.length > 1 ? `these ${type}s` : `this ${type}`
|
|
|
|
|
} to trash`,
|
|
|
|
|
type: "transparent"
|
|
|
|
|
}
|
2023-01-13 14:43:52 +05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-16 08:54:37 +05:00
|
|
|
async function deleteNotebook(id, deleteNotes) {
|
|
|
|
|
const notebook = await db.notebooks.notebook(id);
|
|
|
|
|
const parentId = getParentNotebookId(id);
|
|
|
|
|
if (deleteNotes) {
|
|
|
|
|
const noteRelations = await db.relations.from(notebook, "note").get();
|
|
|
|
|
await db.notes.delete(...noteRelations.map((relation) => relation.toId));
|
|
|
|
|
}
|
|
|
|
|
const subnotebooks = await db.relations.from(notebook, "notebook").get();
|
|
|
|
|
for (const subnotebook of subnotebooks) {
|
|
|
|
|
await deleteNotebook(subnotebook.toId, deleteNotes);
|
|
|
|
|
}
|
|
|
|
|
await db.notebooks.remove(id);
|
|
|
|
|
if (parentId) {
|
|
|
|
|
eSendEvent(eOnNotebookUpdated, parentId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 14:43:52 +05:00
|
|
|
export const deleteItems = async (item, context) => {
|
2021-06-15 13:52:07 +05:00
|
|
|
if (item && db.monographs.isPublished(item.id)) {
|
2023-08-29 20:42:45 +05:00
|
|
|
ToastManager.show({
|
2022-08-26 16:19:39 +05:00
|
|
|
heading: "Can not delete note",
|
|
|
|
|
message: "Unpublish note to delete it",
|
|
|
|
|
type: "error",
|
|
|
|
|
context: "global"
|
2021-12-11 17:34:52 +05:00
|
|
|
});
|
2021-06-15 13:52:07 +05:00
|
|
|
return;
|
|
|
|
|
}
|
2020-11-14 10:02:38 +05:00
|
|
|
|
2023-11-16 08:54:37 +05:00
|
|
|
const itemsToDelete = item
|
2023-10-30 13:22:03 +05:00
|
|
|
? [item]
|
|
|
|
|
: useSelectionStore.getState().selectedItemsList;
|
2023-06-10 12:17:23 +05:00
|
|
|
|
2023-11-16 08:54:37 +05:00
|
|
|
let notes = itemsToDelete.filter((i) => i.type === "note");
|
|
|
|
|
let notebooks = itemsToDelete.filter((i) => i.type === "notebook");
|
|
|
|
|
let reminders = itemsToDelete.filter((i) => i.type === "reminder");
|
2023-01-07 16:03:54 +05:00
|
|
|
|
|
|
|
|
if (reminders.length > 0) {
|
|
|
|
|
for (let reminder of reminders) {
|
|
|
|
|
await db.reminders.remove(reminder.id);
|
|
|
|
|
}
|
|
|
|
|
useRelationStore.getState().update();
|
|
|
|
|
}
|
2021-12-11 17:34:52 +05:00
|
|
|
|
2020-12-16 12:29:36 +05:00
|
|
|
if (notes?.length > 0) {
|
2023-01-07 16:03:54 +05:00
|
|
|
for (const note of notes) {
|
|
|
|
|
if (db.monographs.isPublished(note.id)) {
|
2023-08-29 20:42:45 +05:00
|
|
|
ToastManager.show({
|
2023-01-07 16:03:54 +05:00
|
|
|
heading: "Some notes are published",
|
|
|
|
|
message: "Unpublish published notes to delete them",
|
|
|
|
|
type: "error",
|
|
|
|
|
context: "global"
|
|
|
|
|
});
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
await db.notes.delete(note.id);
|
|
|
|
|
}
|
2020-12-16 12:29:36 +05:00
|
|
|
eSendEvent(eClearEditor);
|
|
|
|
|
}
|
2023-01-07 16:03:54 +05:00
|
|
|
|
2020-12-16 12:29:36 +05:00
|
|
|
if (notebooks?.length > 0) {
|
2023-06-10 12:17:23 +05:00
|
|
|
const result = await confirmDeleteAllNotes(notebooks, "notebook", context);
|
2023-09-12 17:07:26 +05:00
|
|
|
if (!result.delete) return;
|
2023-11-16 08:54:37 +05:00
|
|
|
for (const notebook of notebooks) {
|
|
|
|
|
await deleteNotebook(notebook.id, result.deleteNotes);
|
2023-01-13 14:43:52 +05:00
|
|
|
}
|
2020-11-14 12:25:57 +05:00
|
|
|
}
|
2020-11-14 10:02:38 +05:00
|
|
|
|
2023-11-16 08:54:37 +05:00
|
|
|
let message = `${itemsToDelete.length} ${
|
|
|
|
|
itemsToDelete.length === 1 ? "item" : "items"
|
2023-06-10 12:17:23 +05:00
|
|
|
} moved to trash.`;
|
2020-11-14 10:02:38 +05:00
|
|
|
|
2023-11-16 08:54:37 +05:00
|
|
|
let deletedItems = [...itemsToDelete];
|
|
|
|
|
if (reminders.length === 0 && (notes.length > 0 || notebooks.length > 0)) {
|
2023-08-29 20:42:45 +05:00
|
|
|
ToastManager.show({
|
2021-02-20 15:03:02 +05:00
|
|
|
heading: message,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "success",
|
2021-02-20 15:03:02 +05:00
|
|
|
func: async () => {
|
2021-12-11 17:34:52 +05:00
|
|
|
let trash = db.trash.all;
|
2020-12-16 12:29:36 +05:00
|
|
|
let ids = [];
|
2023-06-10 12:17:23 +05:00
|
|
|
for (var i = 0; i < deletedItems.length; i++) {
|
|
|
|
|
let it = deletedItems[i];
|
2022-08-26 16:19:39 +05:00
|
|
|
let trashItem = trash.find((item) => item.id === it.id);
|
2020-12-16 12:29:36 +05:00
|
|
|
ids.push(trashItem.id);
|
2020-11-14 12:25:57 +05:00
|
|
|
}
|
2020-12-31 14:18:58 +05:00
|
|
|
await db.trash.restore(...ids);
|
2023-04-01 14:06:16 +05:00
|
|
|
Navigation.queueRoutesForUpdate();
|
2021-06-05 21:10:20 +05:00
|
|
|
useMenuStore.getState().setMenuPins();
|
|
|
|
|
useMenuStore.getState().setColorNotes();
|
2023-08-29 20:42:45 +05:00
|
|
|
ToastManager.hide();
|
2020-11-14 12:25:57 +05:00
|
|
|
},
|
2022-08-26 16:19:39 +05:00
|
|
|
actionText: "Undo"
|
2021-02-20 15:03:02 +05:00
|
|
|
});
|
2020-11-14 12:25:57 +05:00
|
|
|
}
|
2023-11-16 08:54:37 +05:00
|
|
|
|
2023-04-01 14:06:16 +05:00
|
|
|
Navigation.queueRoutesForUpdate();
|
2023-10-30 13:22:03 +05:00
|
|
|
if (!item) {
|
|
|
|
|
useSelectionStore.getState().clearSelection();
|
|
|
|
|
}
|
2021-12-08 23:15:27 +05:00
|
|
|
useMenuStore.getState().setMenuPins();
|
2021-06-05 21:10:20 +05:00
|
|
|
useMenuStore.getState().setColorNotes();
|
2022-03-23 12:23:51 +05:00
|
|
|
SearchService.updateAndSearch();
|
2021-01-01 15:29:10 +05:00
|
|
|
};
|
2021-02-10 10:09:48 +05:00
|
|
|
|
2022-08-27 15:23:11 +05:00
|
|
|
export const openLinkInBrowser = async (link) => {
|
2021-02-10 10:09:48 +05:00
|
|
|
try {
|
2023-06-10 12:17:23 +05:00
|
|
|
Linking.openURL(link);
|
2021-02-10 10:09:48 +05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error.message);
|
|
|
|
|
}
|
2021-02-16 16:11:10 +05:00
|
|
|
};
|