2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
|
|
|
|
Copyright (C) 2022 Streetwriters (Private) Limited
|
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
import { history } from ".";
|
|
|
|
|
import { eSendEvent, ToastEvent } from "../services/event-manager";
|
|
|
|
|
import Navigation from "../services/navigation";
|
|
|
|
|
import SearchService from "../services/search";
|
|
|
|
|
import { useSelectionStore } from "../stores/use-selection-store";
|
|
|
|
|
import { useMenuStore } from "../stores/use-menu-store";
|
|
|
|
|
import { db } from "../common/database";
|
|
|
|
|
import { eClearEditor } from "./events";
|
2023-01-07 16:03:54 +05:00
|
|
|
import { useRelationStore } from "../stores/use-relation-store";
|
2023-01-13 14:43:52 +05:00
|
|
|
import { presentDialog } from "../components/dialog/functions";
|
2020-11-14 10:02:38 +05:00
|
|
|
|
2023-01-13 14:43:52 +05:00
|
|
|
function deleteNotesConfirmDialog(items, type, context) {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
presentDialog({
|
|
|
|
|
title: "Delete Contained Notes?",
|
|
|
|
|
paragraph: `Do you want to delete notes within ${
|
|
|
|
|
items.length > 1 ? `these ${type}s` : `this ${type}`
|
|
|
|
|
}?`,
|
|
|
|
|
positiveText: "Yes",
|
|
|
|
|
negativeText: "No",
|
|
|
|
|
positivePress: () => {
|
|
|
|
|
resolve(true);
|
|
|
|
|
},
|
|
|
|
|
onClose: () => {
|
|
|
|
|
resolve(false);
|
|
|
|
|
},
|
|
|
|
|
context: context
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const deleteItems = async (item, context) => {
|
2021-06-15 13:52:07 +05:00
|
|
|
if (item && db.monographs.isPublished(item.id)) {
|
|
|
|
|
ToastEvent.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-12-31 19:54:08 +05:00
|
|
|
if (item && item.id && history.selectedItemsList.length === 0) {
|
2020-11-14 12:25:57 +05:00
|
|
|
history.selectedItemsList = [];
|
|
|
|
|
history.selectedItemsList.push(item);
|
|
|
|
|
}
|
2020-11-14 10:02:38 +05:00
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
let notes = history.selectedItemsList.filter((i) => i.type === "note");
|
|
|
|
|
let notebooks = history.selectedItemsList.filter(
|
|
|
|
|
(i) => i.type === "notebook"
|
|
|
|
|
);
|
|
|
|
|
let topics = history.selectedItemsList.filter((i) => i.type === "topic");
|
2023-01-07 16:03:54 +05:00
|
|
|
let reminders = history.selectedItemsList.filter(
|
|
|
|
|
(i) => i.type === "reminder"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let routesForUpdate = [
|
|
|
|
|
"TaggedNotes",
|
|
|
|
|
"ColoredNotes",
|
|
|
|
|
"TopicNotes",
|
|
|
|
|
"Favorites",
|
|
|
|
|
"Notes"
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (reminders.length > 0) {
|
|
|
|
|
for (let reminder of reminders) {
|
|
|
|
|
await db.reminders.remove(reminder.id);
|
|
|
|
|
}
|
|
|
|
|
routesForUpdate.push("Reminders");
|
|
|
|
|
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)) {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: "Some notes are published",
|
|
|
|
|
message: "Unpublish published notes to delete them",
|
|
|
|
|
type: "error",
|
|
|
|
|
context: "global"
|
|
|
|
|
});
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
await db.notes.delete(note.id);
|
|
|
|
|
}
|
|
|
|
|
routesForUpdate.push("Trash");
|
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 (topics?.length > 0) {
|
2023-01-13 14:43:52 +05:00
|
|
|
const deleteNotes = await deleteNotesConfirmDialog(
|
|
|
|
|
topics,
|
|
|
|
|
"topic",
|
|
|
|
|
context
|
|
|
|
|
);
|
2023-01-07 16:03:54 +05:00
|
|
|
for (const topic of topics) {
|
2023-01-13 14:43:52 +05:00
|
|
|
if (deleteNotes) {
|
|
|
|
|
const notes = db.notebooks
|
|
|
|
|
.notebook(topic.notebookId)
|
|
|
|
|
.topics.topic(topic.id).all;
|
|
|
|
|
await db.notes.delete(...notes.map((note) => note.id));
|
|
|
|
|
}
|
2023-01-07 16:03:54 +05:00
|
|
|
await db.notebooks.notebook(topic.notebookId).topics.delete(topic.id);
|
2020-11-14 12:25:57 +05:00
|
|
|
}
|
2023-01-07 16:03:54 +05:00
|
|
|
routesForUpdate.push("Notebook", "Notebooks");
|
2021-06-05 21:10:20 +05:00
|
|
|
useMenuStore.getState().setMenuPins();
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
2023-01-13 14:43:52 +05:00
|
|
|
heading: `${topics.length > 1 ? "Topics" : "Topic"} deleted`,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "success"
|
2021-02-20 15:03:02 +05:00
|
|
|
});
|
2020-12-16 12:29:36 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (notebooks?.length > 0) {
|
2023-01-13 14:43:52 +05:00
|
|
|
const deleteNotes = await deleteNotesConfirmDialog(
|
|
|
|
|
notebooks,
|
|
|
|
|
"notebook",
|
|
|
|
|
context
|
|
|
|
|
);
|
|
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
let ids = notebooks.map((i) => i.id);
|
2023-01-13 14:43:52 +05:00
|
|
|
if (deleteNotes) {
|
|
|
|
|
for (let id of ids) {
|
|
|
|
|
const topics = db.notebooks.notebook(id).topics.all;
|
|
|
|
|
for (let topic of topics) {
|
|
|
|
|
const notes = db.notebooks
|
|
|
|
|
.notebook(topic.notebookId)
|
|
|
|
|
.topics.topic(topic.id).all;
|
|
|
|
|
await db.notes.delete(...notes.map((note) => note.id));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-31 14:18:38 +05:00
|
|
|
await db.notebooks.delete(...ids);
|
2023-01-07 16:03:54 +05:00
|
|
|
routesForUpdate.push("Notebook", "Notebooks");
|
2021-06-05 21:10:20 +05:00
|
|
|
useMenuStore.getState().setMenuPins();
|
2020-11-14 12:25:57 +05:00
|
|
|
}
|
2020-11-14 10:02:38 +05:00
|
|
|
|
2023-01-07 16:03:54 +05:00
|
|
|
Navigation.queueRoutesForUpdate(...routesForUpdate);
|
|
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
let msgPart = history.selectedItemsList.length === 1 ? " item" : " items";
|
|
|
|
|
let message = history.selectedItemsList.length + msgPart + " moved to trash.";
|
2020-11-14 10:02:38 +05:00
|
|
|
|
2020-11-14 12:25:57 +05:00
|
|
|
let itemsCopy = [...history.selectedItemsList];
|
2023-01-07 16:03:54 +05:00
|
|
|
if (
|
|
|
|
|
topics.length === 0 &&
|
|
|
|
|
reminders.length === 0 &&
|
|
|
|
|
(notes.length > 0 || notebooks.length > 0)
|
|
|
|
|
) {
|
2021-02-20 15:03:02 +05:00
|
|
|
ToastEvent.show({
|
|
|
|
|
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 = [];
|
2020-11-14 12:25:57 +05:00
|
|
|
for (var i = 0; i < itemsCopy.length; i++) {
|
|
|
|
|
let it = itemsCopy[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);
|
2021-12-31 09:05:44 +05:00
|
|
|
|
2023-01-07 16:03:54 +05:00
|
|
|
Navigation.queueRoutesForUpdate(routesForUpdate);
|
2021-06-05 21:10:20 +05:00
|
|
|
useMenuStore.getState().setMenuPins();
|
|
|
|
|
useMenuStore.getState().setColorNotes();
|
2020-11-14 12:25:57 +05:00
|
|
|
ToastEvent.hide();
|
|
|
|
|
},
|
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
|
|
|
}
|
2021-06-07 11:53:27 +05:00
|
|
|
history.selectedItemsList = [];
|
2022-08-26 16:19:39 +05:00
|
|
|
Navigation.queueRoutesForUpdate("Trash");
|
2021-12-31 12:35:39 +05:00
|
|
|
useSelectionStore.getState().clearSelection(true);
|
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 {
|
|
|
|
|
const url = link;
|
2022-04-15 23:17:33 +05:00
|
|
|
Linking.openURL(url);
|
2021-02-10 10:09:48 +05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error.message);
|
|
|
|
|
}
|
2021-02-16 16:11:10 +05:00
|
|
|
};
|