Files
notesnook/apps/mobile/app/utils/functions.js

151 lines
4.7 KiB
JavaScript
Raw Normal View History

/*
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
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
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";
import Navigation from "../services/navigation";
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) {
return new Promise((resolve) => {
presentDialog({
title: `Delete ${
items.length > 1 ? `${items.length} ${type}s` : `${type}`
}?`,
positiveText: "Delete",
negativeText: "Cancel",
positivePress: (_inputValue, value) => {
setTimeout(() => {
resolve({ delete: true, deleteNotes: value });
});
},
onClose: () => {
setTimeout(() => {
resolve({ delete: false });
});
},
context: context,
check: {
info: `Move all notes in ${
items.length > 1 ? `these ${type}s` : `this ${type}`
} to trash`,
type: "transparent"
}
});
});
}
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-11-24 15:11:38 +05:00
export const deleteItems = async (items, type, context) => {
const ids = items ? items : useSelectionStore.getState().selectedItemsList;
2023-06-10 12:17:23 +05:00
2023-11-24 15:11:38 +05:00
if (type === "reminder") {
await db.reminders.remove(...ids);
2023-01-07 16:03:54 +05:00
useRelationStore.getState().update();
2023-11-24 15:11:38 +05:00
} else if (type === "note") {
for (const id of ids) {
if (db.monographs.isPublished(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;
}
2023-11-24 15:11:38 +05:00
await db.notes.moveToTrash(id);
2023-01-07 16:03:54 +05:00
}
2020-12-16 12:29:36 +05:00
eSendEvent(eClearEditor);
2023-11-24 15:11:38 +05:00
} else if (type === "notebook") {
const result = await confirmDeleteAllNotes(ids, "notebook", context);
2023-09-12 17:07:26 +05:00
if (!result.delete) return;
2023-11-24 15:11:38 +05:00
for (const id of ids) {
await deleteNotebook(id, result.deleteNotes);
eSendEvent(eOnNotebookUpdated, await getParentNotebookId(id));
}
2020-11-14 12:25:57 +05:00
}
2020-11-14 10:02:38 +05:00
2023-11-24 15:11:38 +05:00
let message = `${ids.length} ${
ids.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-24 15:11:38 +05:00
let deletedIds = [...ids];
if (type === "notebook" || type === "note") {
2023-08-29 20:42:45 +05:00
ToastManager.show({
2021-02-20 15:03:02 +05:00
heading: message,
type: "success",
2021-02-20 15:03:02 +05:00
func: async () => {
2023-11-24 15:11:38 +05:00
await db.trash.restore(...deletedIds);
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();
2023-11-24 15:11:38 +05:00
if (type === "notebook") {
deletedIds.forEach(async (id) => {
eSendEvent(eOnNotebookUpdated, await getParentNotebookId(id));
});
}
2020-11-14 12:25:57 +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-11-24 15:11:38 +05:00
if (!items) {
useSelectionStore.getState().clearSelection();
}
2021-06-05 21:10:20 +05:00
useMenuStore.getState().setColorNotes();
2023-11-24 15:11:38 +05:00
if (type === "notebook") {
ids.forEach(async (id) => {
eSendEvent(eOnNotebookUpdated, await getParentNotebookId(id));
});
useMenuStore.getState().setMenuPins();
}
2021-01-01 15:29:10 +05:00
};
2021-02-10 10:09:48 +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
};