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

143 lines
4.0 KiB
JavaScript
Raw Normal View History

2022-01-22 12:57:05 +05:00
import { Linking } from 'react-native';
import { history } from '.';
2022-02-28 23:25:18 +05:00
import { eSendEvent, ToastEvent } from '../services/event-manager';
import Navigation from '../services/navigation';
2022-04-15 23:17:33 +05:00
import SearchService from '../services/search';
2022-04-24 05:59:14 +05:00
import { useSelectionStore } from '../stores/use-selection-store';
import { useMenuStore } from '../stores/use-menu-store';
2022-08-16 16:48:10 +05:00
import { db } from '../common/database';
2022-02-28 13:48:59 +05:00
import { eClearEditor } from './events';
2020-11-14 10:02:38 +05:00
2021-12-11 17:34:52 +05:00
export const deleteItems = async item => {
2021-06-15 13:52:07 +05:00
if (item && db.monographs.isPublished(item.id)) {
ToastEvent.show({
2021-12-11 17:34:52 +05:00
heading: 'Can not delete note',
message: 'Unpublish note to delete it',
type: 'error',
context: 'global'
});
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
2021-12-11 17:34:52 +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');
2020-12-16 12:29:36 +05:00
if (notes?.length > 0) {
2021-12-11 17:34:52 +05:00
let ids = notes
.map(i => {
if (db.monographs.isPublished(i.id)) {
ToastEvent.show({
heading: 'Some notes are published',
message: 'Unpublish published notes to delete them',
type: 'error',
context: 'global'
});
return null;
}
return i.id;
})
.filter(n => n !== null);
2021-06-15 13:52:07 +05:00
2020-12-31 14:18:38 +05:00
await db.notes.delete(...ids);
2021-12-31 09:05:44 +05:00
2022-04-24 16:30:49 +05:00
Navigation.queueRoutesForUpdate(
'TaggedNotes',
'ColoredNotes',
'TopicNotes',
'Favorites',
'Notes',
'Trash'
2022-04-24 16:30:49 +05:00
);
2020-12-16 12:29:36 +05:00
eSendEvent(eClearEditor);
}
if (topics?.length > 0) {
for (var i = 0; i < topics.length; i++) {
let it = topics[i];
await db.notebooks.notebook(it.notebookId).topics.delete(it.id);
2020-11-14 12:25:57 +05:00
}
2021-12-31 09:05:44 +05:00
2021-12-31 15:19:54 +05:00
// layoutmanager.withAnimation(150);
2022-04-24 16:30:49 +05:00
Navigation.queueRoutesForUpdate('Notebook', 'Notebooks');
2021-06-05 21:10:20 +05:00
useMenuStore.getState().setMenuPins();
2021-02-20 15:03:02 +05:00
ToastEvent.show({
heading: 'Topics deleted',
2021-12-11 17:34:52 +05:00
type: 'success'
2021-02-20 15:03:02 +05:00
});
2020-12-16 12:29:36 +05:00
}
if (notebooks?.length > 0) {
2021-12-11 17:34:52 +05:00
let ids = notebooks.map(i => i.id);
2020-12-31 14:18:38 +05:00
await db.notebooks.delete(...ids);
2021-12-31 09:05:44 +05:00
2021-12-31 15:19:54 +05:00
//layoutmanager.withAnimation(150);
2022-04-24 16:30:49 +05:00
Navigation.queueRoutesForUpdate(
'TaggedNotes',
'ColoredNotes',
'TopicNotes',
'Favorites',
'Notes',
'Notebooks',
'Trash'
2022-04-24 16:30:49 +05:00
);
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
2020-11-14 12:25:57 +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];
2021-01-01 15:29:10 +05:00
if (topics.length === 0 && (notes.length > 0 || notebooks.length > 0)) {
2021-02-20 15:03:02 +05:00
ToastEvent.show({
heading: message,
type: 'success',
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];
2021-12-11 17:34:52 +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
2021-12-31 15:19:54 +05:00
//layoutmanager.withAnimation(150);
2022-04-24 16:30:49 +05:00
Navigation.queueRoutesForUpdate(
'TaggedNotes',
'ColoredNotes',
'TopicNotes',
'Favorites',
'Notes',
'Notebook',
'Notebooks',
'Trash'
);
2021-06-05 21:10:20 +05:00
useMenuStore.getState().setMenuPins();
useMenuStore.getState().setColorNotes();
2020-11-14 12:25:57 +05:00
ToastEvent.hide();
},
2021-12-11 17:34:52 +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-04-24 16:30:49 +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
console.log('running search again');
SearchService.updateAndSearch();
2021-01-01 15:29:10 +05:00
};
2021-02-10 10:09:48 +05:00
export const openLinkInBrowser = async (link, colors) => {
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
};