diff --git a/apps/mobile/App.js b/apps/mobile/App.js index 18060e6b9..6bed146eb 100644 --- a/apps/mobile/App.js +++ b/apps/mobile/App.js @@ -1,4 +1,4 @@ -import Storage from 'notes-core/api/database'; +import Storage from 'notes-core/api/index'; import React, {useEffect, useState} from 'react'; import {Platform, StatusBar, View, Text} from 'react-native'; import * as Animatable from 'react-native-animatable'; diff --git a/apps/mobile/android/app/src/main/assets/texteditor.html b/apps/mobile/android/app/src/main/assets/texteditor.html index ace881731..59daa9da1 100644 --- a/apps/mobile/android/app/src/main/assets/texteditor.html +++ b/apps/mobile/android/app/src/main/assets/texteditor.html @@ -1207,6 +1207,8 @@ document.getElementById('titleInput').onchange = function () { editor.focus(); + console.log(JSON.stringify(editor.getContents())); + var titleMessage = { type: 'title', value: document.getElementById('titleInput').value @@ -1625,9 +1627,12 @@ let m = {}; m.delta = editor.getContents(); + m.text = editor.getText(); + m.html = editor.root.innerHTML; m.type = 'content'; + window.ReactNativeWebView.postMessage(JSON.stringify(m)); }); diff --git a/apps/mobile/src/components/ActionSheetComponent/index.js b/apps/mobile/src/components/ActionSheetComponent/index.js index 6c0adaa11..e812ad10f 100644 --- a/apps/mobile/src/components/ActionSheetComponent/index.js +++ b/apps/mobile/src/components/ActionSheetComponent/index.js @@ -87,8 +87,10 @@ export const ActionSheetComponent = ({ text: '', }); - await db.addTag(note.dateCreated, tag); - setNote({...db.getNote(note.dateCreated)}); + await db.notes.note(note.id).tag(tag); + + setNote({...db.notes.note(note.id).data}); + tagToAdd = ''; }; @@ -106,12 +108,11 @@ export const ActionSheetComponent = ({ let oldProps = {...note}; if (oldProps.tags.length === 0) return; //oldProps.tags.splice(oldProps.tags.length - 1); - await db.removeTag( - note.dateCreated, - oldProps.tags[oldProps.tags.length - 1], - ); + await db.notes + .note(note.id) + .untag(oldProps.tags[oldProps.tags.length - 1]); - setNote({...db.getNote(note.dateCreated)}); + setNote({...db.notes.note(note.id).data}); tagsInputRef.setNativeProps({ text: tagInputValue, @@ -125,25 +126,25 @@ export const ActionSheetComponent = ({ }; const localRefresh = (type, nodispatch = false) => { - if (!note || !note.dateCreated) return; + if (!note || !note.id) return; let toAdd; switch (type) { case 'note': { - toAdd = db.getNote(note.dateCreated); + toAdd = db.notes.note(note.id).data; break; } case 'notebook': { - toAdd = db.getNotebook(note.dateCreated); + toAdd = db.notebooks.notebook(note.id).data; break; } case 'topic': { - toAdd = db.getTopic(note.notebookId, note.title); + toAdd = db.notebooks.notebook(note.notebookId).topics.topic(note.title); break; } } - if (!toAdd || !toAdd.dateCreated) return; + if (!toAdd || !toAdd.id) return; if (!nodispatch) { dispatch({type: type}); @@ -258,11 +259,11 @@ export const ActionSheetComponent = ({ name: 'Pin', icon: 'tag', func: async () => { - if (!note.dateCreated) return; + if (!note.id) return; if (note.type === 'note') { - await db.pinNote(note.dateCreated); + await db.notes.note(note.id).pin(); } else { - await db.pinNotebook(note.dateCreated); + await db.notebooks.notebook(note.id).pin(); } dispatch({type: ACTIONS.PINNED}); localRefresh(item.type); @@ -275,11 +276,11 @@ export const ActionSheetComponent = ({ name: 'Favorite', icon: 'star', func: async () => { - if (!note.dateCreated) return; + if (!note.id) return; if (note.type === 'note') { - await db.favoriteNotes([note.dateCreated]); + await db.notes.note(note.id).favorite(); } else { - await db.favoriteNotebooks([note.dateCreated]); + await db.notebooks.notebook(note.id).favorite(); } dispatch({type: ACTIONS.FAVORITES}); localRefresh(item.type); @@ -292,7 +293,7 @@ export const ActionSheetComponent = ({ name: 'Add to Vault', icon: 'lock', func: () => { - if (!note.dateCreated) return; + if (!note.id) return; note.locked ? close('unlock') : close('lock'); }, close: true, @@ -307,11 +308,7 @@ export const ActionSheetComponent = ({ onPress={async () => { let oldProps = {...note}; - oldProps.tags.splice(oldProps.tags.indexOf(tag), 1); - await db.addNote({ - dateCreated: note.dateCreated, - tags: oldProps.tags, - }); + await db.notes.note(note.id).untag(oldProps.tags.indexOf(tag)); localRefresh(item.type); }} style={{ @@ -350,13 +347,13 @@ export const ActionSheetComponent = ({ } else { noteColors.push(color); } - - db.addNote({ + // TODO + /* db.addNote({ dateCreated: note.dateCreated, colors: noteColors, content: note.content, title: note.title, - }); + }); */ localRefresh(item.type); }} style={{ @@ -418,7 +415,7 @@ export const ActionSheetComponent = ({ ) : null; const _renderColumnItem = item => - (note.dateCreated && columnItems.includes(item.name)) || + (note.id && columnItems.includes(item.name)) || (item.name === 'Dark Mode' && columnItems.includes(item.name)) ? ( - {!note.dateCreated ? ( + {!note.id ? ( {note.type === 'note' - ? 'Last edited on ' + timeConverter(note.dateEditted) + ? 'Last edited on ' + timeConverter(note.dateEdited) : null} {note.type !== 'note' && !note.dateDeleted ? 'Created on ' + timeConverter(note.dateCreated) @@ -624,7 +621,7 @@ export const ActionSheetComponent = ({ )} - {note.dateCreated ? ( + {note.id ? ( ) : null} - {hasColors && note.dateCreated ? ( + {hasColors && note.id ? ( ) : null} - {hasTags && note.dateCreated ? ( + {hasTags && note.id ? ( {}, ''); - let dateCreated = toEdit && toEdit.dateCreated ? toEdit.dateCreated : null; + let id = toEdit && toEdit.id ? toEdit.id : null; - await db.addNotebook({ + // TODO + await db.notebooks.add({ title: this.title, description: this.description, topics, - dateCreated: dateCreated, + id: id, }); + updateEvent({type: ACTIONS.NOTEBOOKS}); this.close(); ToastEvent.show('New notebook added', 'success', 3000, () => {}, ''); diff --git a/apps/mobile/src/components/AddTopicDialog/index.js b/apps/mobile/src/components/AddTopicDialog/index.js index d9e480207..b4272fd08 100644 --- a/apps/mobile/src/components/AddTopicDialog/index.js +++ b/apps/mobile/src/components/AddTopicDialog/index.js @@ -24,7 +24,8 @@ export class AddTopicDialog extends React.Component { if (!this.title) return ToastEvent.show('Title is required', 'error', 3000, () => {}, ''); - db.addTopicToNotebook(this.props.notebookID, this.title); + await db.notebooks.notebook(this.props.notebookID).topics.add(this.title); + eSendEvent(eOnNewTopicAdded); ToastEvent.show('New topic added', 'success', 3000, () => {}, ''); this.close(); diff --git a/apps/mobile/src/components/Dialog/index.js b/apps/mobile/src/components/Dialog/index.js index 47b3e5b1d..77ec39b15 100644 --- a/apps/mobile/src/components/Dialog/index.js +++ b/apps/mobile/src/components/Dialog/index.js @@ -30,16 +30,18 @@ export class Dialog extends Component { switch (template.action) { case dialogActions.ACTION_DELETE: { if (item.type === 'note') { - await db.deleteNotes(item.dateCreated); + await db.notes.delete(item.id); ToastEvent.show('Note moved to trash', 'error', 3000); updateEvent({type: item.type}); } else if (item.type === 'topic') { - await db.deleteTopicFromNotebook(notebookID, item.title); + //TODO + //db.notebooks.notebook(notebookID).topic + //await db.deleteTopicFromNotebook(notebookID, item.title); updateEvent({type: 'notebook'}); ToastEvent.show('Topic deleted', 'error', 3000); } else if (item.type === 'notebook') { - await db.deleteNotebooks(item.dateCreated); + await db.notebooks.delete(item.id); updateEvent({type: item.type}); ToastEvent.show('Notebook moved to trash', 'error', 3000); } @@ -58,7 +60,9 @@ export class Dialog extends Component { break; } case dialogActions.ACTION_EMPTY_TRASH: { - await db.clearTrash(); + // TODO + //await db.clearTrash(); + updateEvent({type: ACTIONS.TRASH}); ToastEvent.show('Trash cleared', 'error', 1000, () => {}, ''); this.setState({ @@ -75,7 +79,8 @@ export class Dialog extends Component { }); } case dialogActions.ACTION_TRASH: { - db.restoreItem(item.dateCreated); + // TODO + //db.restoreItem(item.dateCreated); ToastEvent.show( item.type.slice(0, 1).toUpperCase() + item.type.slice(1) + diff --git a/apps/mobile/src/components/DialogManager/index.js b/apps/mobile/src/components/DialogManager/index.js index a9373859d..8aff9f39d 100644 --- a/apps/mobile/src/components/DialogManager/index.js +++ b/apps/mobile/src/components/DialogManager/index.js @@ -456,7 +456,7 @@ export class DialogManager extends Component { notebookID={ actionSheetData.extraData ? actionSheetData.extraData.notebookID - : item.dateCreated + : item.id } colors={colors} /> diff --git a/apps/mobile/src/components/NoteItem/index.js b/apps/mobile/src/components/NoteItem/index.js index dce73e90a..06e9e23dc 100644 --- a/apps/mobile/src/components/NoteItem/index.js +++ b/apps/mobile/src/components/NoteItem/index.js @@ -1,23 +1,17 @@ import React from 'react'; -import { - Dimensions, - Text, - TouchableOpacity, - View, - DeviceEventEmitter, -} from 'react-native'; +import {Dimensions, Text, TouchableOpacity, View} from 'react-native'; import Icon from 'react-native-vector-icons/Feather'; import {DDS} from '../../../App'; import {ph, pv, SIZE, WEIGHT} from '../../common/common'; +import {eSendEvent} from '../../services/eventManager'; +import {eOnLoadNote} from '../../services/events'; import NavigationService from '../../services/NavigationService'; import {getElevation, timeSince} from '../../utils/utils'; import { ActionSheetEvent, - TEMPLATE_TRASH, simpleDialogEvent, + TEMPLATE_TRASH, } from '../DialogManager'; -import {eSendEvent} from '../../services/eventManager'; -import {eOnLoadNote, eOpenSimpleDialog} from '../../services/events'; const w = Dimensions.get('window').width; const h = Dimensions.get('window').height; @@ -66,7 +60,7 @@ export default class NoteItem extends React.Component { pinned, index, } = this.props; - + console.log('rerendering', index, item.content.text.length); return ( { - /* let noteIds = []; - selectedItemsList.forEach(item => noteIds.push(item.dateCreated)); - if (!noteToMove.notebook.notebook) { - await db.moveNotes(null, { - topic: item.title, - id: item.notebookId, - }); - await db.addNoteToTopic( - notebookID, - item.title, - noteToMove.dateCreated, - ); - } else if (selectedItemsList) { - - await db.moveNotes(null, { - topic: item.title, - id: item.notebookId, - }); - await db.moveNote(noteToMove.dateCreated, noteToMove.notebook, { - notebook: notebookID, - topic: item.title, - }); + let noteIds = []; + selectedItemsList.forEach(item => noteIds.push(item.id)); - }*/ + db.notes.move( + { + topic: item.title, + id: item.notebookID, + }, + noteIds, + ); moveNoteHideEvent(); diff --git a/apps/mobile/src/components/NotesList/index.js b/apps/mobile/src/components/NotesList/index.js index bf575644c..d61b6de26 100644 --- a/apps/mobile/src/components/NotesList/index.js +++ b/apps/mobile/src/components/NotesList/index.js @@ -34,7 +34,7 @@ export const NotesList = ({isGrouped = false}) => { { marginHorizontal: 0, }} currentEditingNote={ - currentEditingNote === item.dateCreated ? currentEditingNote : null + currentEditingNote === item.id ? currentEditingNote : null } onLongPress={() => { dispatch({type: ACTIONS.SELECTION_MODE, enabled: !selectionMode}); @@ -167,7 +167,7 @@ export const NotesList = ({isGrouped = false}) => { }}> ); - const _listKeyExtractor = (item, index) => item.dateCreated.toString(); + const _listKeyExtractor = (item, index) => item.id.toString(); return isGrouped && searchResults.length === 0 ? ( { <> item.dateCreated.toString()} + keyExtractor={(item, index) => item.id.toString()} renderItem={({item, index}) => item.type === 'note' ? ( { }, [selectedItemsList]); return ( - { {children} - + ); }; diff --git a/apps/mobile/src/components/VaultDialog/index.js b/apps/mobile/src/components/VaultDialog/index.js index 04b93cbcb..359a471d5 100644 --- a/apps/mobile/src/components/VaultDialog/index.js +++ b/apps/mobile/src/components/VaultDialog/index.js @@ -50,7 +50,7 @@ export class VaultDialog extends Component { let item; if (n.content.cipher) { try { - item = await db.unlockNote(n.dateCreated, password, this.props.perm); + item = await db.notes.note(n.id).unlock(password, this.props.perm); } catch (error) {} } else { item = n; @@ -63,7 +63,7 @@ export class VaultDialog extends Component { this.close(this.props.shareAfterUnlock, item); } else { - await db.lockNote(this.props.note.dateCreated, 'password'); + await db.notes.note(this.props.note.id).lock('password'); this.close(); } }; diff --git a/apps/mobile/src/provider/reducer.js b/apps/mobile/src/provider/reducer.js index 636fa6d61..cd67d2b01 100644 --- a/apps/mobile/src/provider/reducer.js +++ b/apps/mobile/src/provider/reducer.js @@ -5,7 +5,8 @@ import {ACTIONS} from './actions'; export const reducer = (state, action) => { switch (action.type) { case ACTIONS.NOTES: - let notes = db.groupNotes(); + let notes = db.notes.group(); + return { ...state, notes: [...notes], @@ -18,36 +19,36 @@ export const reducer = (state, action) => { }; } case ACTIONS.NOTEBOOKS: { - let notebooks = [...db.getNotebooks()]; + let notebooks = [...db.notebooks.all]; return { ...state, notebooks: notebooks, }; } case ACTIONS.TRASH: { - let trash = [...db.getTrash()]; - + let trash = []; + // TODO return { ...state, trash: trash, }; } case ACTIONS.PINNED: { - let pinned = [...db.getPinned()]; + let pinned = [...db.notes.pinned]; return { ...state, pinned: pinned, }; } case ACTIONS.TAGS: { - let tagList = db.getTags(); + let tagList = db.notes.tags; return { ...state, tags: [...tagList], }; } case ACTIONS.FAVORITES: { - let favorites = [...db.getFavorites()]; + let favorites = [...db.notes.favorites]; return { ...state, @@ -74,7 +75,7 @@ export const reducer = (state, action) => { } else { selectedItems.push(action.item); } - console.log(action.item, selectedItems); + return { ...state, selectedItemsList: selectedItems, @@ -101,7 +102,7 @@ export const reducer = (state, action) => { case ACTIONS.CURRENT_EDITING_NOTE: { return { ...state, - currentEditingNote: action.dateCreated, + currentEditingNote: action.id, }; } default: diff --git a/apps/mobile/src/utils/storage.js b/apps/mobile/src/utils/storage.js index 8d716b448..098fa04a1 100644 --- a/apps/mobile/src/utils/storage.js +++ b/apps/mobile/src/utils/storage.js @@ -3,11 +3,13 @@ import FastStorage from 'react-native-fast-storage'; var Aes = NativeModules.Aes; async function read(key) { - return await FastStorage.getItem(key); + let json = await FastStorage.getItem(key); + return JSON.parse(json); } async function write(key, data) { - return await FastStorage.setItem(key, data); + let json = JSON.stringify(data); + return await FastStorage.setItem(key, json); } function remove(key) { diff --git a/apps/mobile/src/views/Editor/index.js b/apps/mobile/src/views/Editor/index.js index d72d6d7a2..f02db3c99 100755 --- a/apps/mobile/src/views/Editor/index.js +++ b/apps/mobile/src/views/Editor/index.js @@ -39,7 +39,7 @@ import {AnimatedSafeAreaView} from '../Home'; let EditorWebView; let note = {}; -let timestamp = null; +let id = null; let dateEdited = null; var content = null; var title = null; @@ -65,7 +65,7 @@ const Editor = ({navigation, noMenu}) => { }, []); const loadNote = item => { - if (note && note.dateCreated) { + if (note && note.id) { saveNote(true).then(() => { dispatch({type: ACTIONS.NOTES}); if (item && item.type === 'new') { @@ -75,7 +75,7 @@ const Editor = ({navigation, noMenu}) => { if (DDS.isTab) { dispatch({ type: ACTIONS.CURRENT_EDITING_NOTE, - dateCreated: item.dateCreated, + id: item.id, }); } @@ -91,7 +91,7 @@ const Editor = ({navigation, noMenu}) => { if (DDS.isTab) { dispatch({ type: ACTIONS.CURRENT_EDITING_NOTE, - dateCreated: item.dateCreated, + id: item.id, }); } updateEditor(); @@ -100,7 +100,7 @@ const Editor = ({navigation, noMenu}) => { }; const clearEditor = () => { - timestamp = null; + id = null; title = null; content = null; note = {}; @@ -163,24 +163,24 @@ const Editor = ({navigation, noMenu}) => { }; } - let dateCreated = await db.addNote({ + let rId = await db.notes.add({ title, content: { text: content.text, delta: content.delta, }, - dateCreated: timestamp, + id: id, }); - if (timestamp !== dateCreated) { - timestamp = dateCreated; + if (id !== rId) { + id = rId; - note = db.getNote(timestamp); + note = db.notes.note(id); if (DDS.isTab) { dispatch({ type: ACTIONS.CURRENT_EDITING_NOTE, - dateCreated: timestamp, + id: id, }); } } @@ -191,10 +191,11 @@ const Editor = ({navigation, noMenu}) => { }); } saveCounter++; - if (timestamp) { - let lockednote = db.getNote(timestamp); + if (id) { + let lockednote = db.notes.note(id); if (lockNote && lockednote.locked) { - await db.lockNote(timestamp, 'password'); + // TODO + await db.notes.note(id).lock('password'); } } }; @@ -238,7 +239,7 @@ const Editor = ({navigation, noMenu}) => { if (navigation && navigation.state.params && navigation.state.params.note) { note = navigation.state.params.note; updateEditor(); - } else if (note && note.dateCreated) { + } else if (note && note.id) { updateEditor(); } else { post('focusTitle'); @@ -258,9 +259,9 @@ const Editor = ({navigation, noMenu}) => { }, timeout); }); - const updateEditor = () => { + const updateEditor = async () => { title = note.title; - timestamp = note.dateCreated; + id = note.id; dateEdited = note.dateEditted; content = note.content; saveCounter = 0; @@ -279,7 +280,9 @@ const Editor = ({navigation, noMenu}) => { if (note.content.text === '' && note.content.delta === null) { post('clear'); } else if (note.content.delta) { - post(JSON.stringify(note.content.delta)); + let delta = await db.notes.note(id).delta(); + + post(JSON.stringify(delta)); } else { post(JSON.stringify({type: 'text', value: note.content.text})); } @@ -420,7 +423,7 @@ const Editor = ({navigation, noMenu}) => { }}> { - simpleDialogEvent(TEMPLATE_INFO(timestamp)); + simpleDialogEvent(TEMPLATE_INFO(note.dateCreated)); }} style={{ color: colors.icon, @@ -505,7 +508,7 @@ const Editor = ({navigation, noMenu}) => { } title = null; content = null; - timestamp = null; + id = null; timer = null; }; }, [noMenu]); diff --git a/apps/mobile/src/views/Folders/index.js b/apps/mobile/src/views/Folders/index.js index f78cd347d..cd0e90f27 100644 --- a/apps/mobile/src/views/Folders/index.js +++ b/apps/mobile/src/views/Folders/index.js @@ -120,7 +120,7 @@ export const Folders = ({navigation}) => { <> item.dateCreated.toString()} + keyExtractor={(item, index) => item.id.toString()} renderItem={({item, index}) => item.type === 'notebook' ? ( @@ -213,7 +213,7 @@ export const Folders = ({navigation}) => { } data={notebooks} - keyExtractor={(item, index) => item.dateCreated.toString()} + keyExtractor={(item, index) => item.id.toString()} renderItem={({item, index}) => ( { const [state, dispatch] = useTracked(); @@ -29,24 +28,27 @@ export const Notebook = ({navigation}) => { let notebook; let isFocused = useIsFocused(); + const onLoad = () => { + topics = db.notebooks.notebook(navigation.state.params.notebook.dateCreated) + .topics; + notebook = db.notebooks.notebook( + navigation.state.params.notebook.dateCreated, + ); + + setTopics([...topics]); + }; + useEffect(() => { params = navigation.state.params; let topic = params.notebook.topics; notebook = params.notebook; - console.log(navigation); setTopics([...topic]); }, []); useEffect(() => { - eSubscribeEvent(eOnNewTopicAdded, () => { - notebook = db.getNotebook(navigation.state.params.notebook.dateCreated); - setTopics([...notebook.topics]); - }); + eSubscribeEvent(eOnNewTopicAdded, onLoad); return () => { - eUnSubscribeEvent(eOnNewTopicAdded, () => { - notebook = db.getNotebook(navigation.state.params.notebook.dateCreated); - setTopics([...notebook.topics]); - }); + eUnSubscribeEvent(eOnNewTopicAdded, onLoad); }; }, []); @@ -89,7 +91,7 @@ export const Notebook = ({navigation}) => { }); }} noteToMove={params.note} - notebookID={params.notebook.dateCreated} + notebookID={params.notebook.id} isMove={params.isMove} refresh={() => {}} item={item} @@ -143,7 +145,6 @@ export const Notebook = ({navigation}) => { canGoBack={true} data={topics} bottomButtonOnPress={() => { - console.log(navigation.state.params.notebook); let n = navigation.state.params.notebook; AddTopicEvent(n); }}> diff --git a/apps/mobile/src/views/Notes/index.js b/apps/mobile/src/views/Notes/index.js index 29e8125a1..9183de94e 100644 --- a/apps/mobile/src/views/Notes/index.js +++ b/apps/mobile/src/views/Notes/index.js @@ -26,10 +26,12 @@ export const Notes = ({navigation}) => { useEffect(() => { if (params.type === 'tag') { - let notesInTag = db.getTag(params.tag.title); + let notesInTag = db.notes.tagged(params.tag.title); setNotes([...notesInTag]); } else { - let allNotes = db.getTopic(params.notebookID, params.title); + let allNotes = db.notebooks + .notebook(params.notebookID) + .topics.topic(params.title); if (allNotes && allNotes.length > 0) { setNotes(allNotes); } diff --git a/apps/mobile/src/views/Tags/index.js b/apps/mobile/src/views/Tags/index.js index 0afd6024e..22be9f97d 100644 --- a/apps/mobile/src/views/Tags/index.js +++ b/apps/mobile/src/views/Tags/index.js @@ -2,21 +2,18 @@ import React, {useEffect, useState} from 'react'; import { Dimensions, FlatList, - SafeAreaView, - Text, - View, - TouchableOpacity, - RefreshControl, Platform, + RefreshControl, + Text, + TouchableOpacity, + View, } from 'react-native'; import {pv, SIZE, WEIGHT} from '../../common/common'; -import {Header} from '../../components/header'; +import Container from '../../components/Container'; import {TagsPlaceHolder} from '../../components/ListPlaceholders'; import {useTracked} from '../../provider'; import {ACTIONS} from '../../provider/actions'; -import {db} from '../../../App'; import NavigationService from '../../services/NavigationService'; -import Container from '../../components/Container'; const w = Dimensions.get('window').width; const h = Dimensions.get('window').height;