Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

update notes-core

Ammar Ahmedf11ce772d733
diff --git a/apps/mobile/src/components/launcher/index.js b/apps/mobile/src/components/launcher/index.js
index ccf4e9dc0..434704a6f 100644
--- a/apps/mobile/src/components/launcher/index.js
+++ b/apps/mobile/src/components/launcher/index.js
@@ -64,10 +64,9 @@ const Launcher = React.memo(
await restoreEditorState();
setImmediate(() => {
db.notes.init().then(() => {
- setNotes();
- setFavorites();
- setLoading(false);
Walkthrough.init();
+ initialize();
+ setImmediate(() => setLoading(false));
});
});
};
@@ -75,7 +74,6 @@ const Launcher = React.memo(
const init = async () => {
if (!dbInitCompleted.current) {
await db.init();
- initialize();
dbInitCompleted.current = true;
}
diff --git a/apps/mobile/src/screens/editor/tiptap/useEditor.ts b/apps/mobile/src/screens/editor/tiptap/useEditor.ts
index b7bb06912..4567b4927 100644
--- a/apps/mobile/src/screens/editor/tiptap/useEditor.ts
+++ b/apps/mobile/src/screens/editor/tiptap/useEditor.ts
@@ -16,7 +16,14 @@ import { timeConverter } from '../../../utils/time';
import { NoteType } from '../../../utils/types';
import Commands from './commands';
import { AppState, Content, EditorState, Note, SavePayload } from './types';
-import { defaultState, EditorEvents, isEditorLoaded, makeSessionId, post } from './utils';
+import {
+ defaultState,
+ EditorEvents,
+ isContentInvalid,
+ isEditorLoaded,
+ makeSessionId,
+ post
+} from './utils';
export const useEditor = (editorId = '', readonly?: boolean) => {
const [loading, setLoading] = useState(false);
@@ -33,6 +40,7 @@ export const useEditor = (editorId = '', readonly?: boolean) => {
const tags = useTagStore(state => state.tags);
const insets = useSafeAreaInsets();
const isDefaultEditor = editorId === '';
+ const saveCount = useRef(0);
const postMessage = useCallback(
async (type: string, data: any) => await post(editorRef, sessionIdRef.current, type, data),
@@ -103,6 +111,7 @@ export const useEditor = (editorId = '', readonly?: boolean) => {
currentNote.current = null;
currentContent.current = null;
sessionHistoryId.current = undefined;
+ saveCount.current = 0;
await commands.clearContent();
console.log('reset state: ', resetState);
if (resetState) {
@@ -133,9 +142,16 @@ export const useEditor = (editorId = '', readonly?: boolean) => {
let locked = note?.locked;
if (note?.conflicted) return;
+ if (isContentInvalid(data)) {
+ // Create a new history session if recieved empty or invalid content
+ // To ensure that history is preserved for correct content.
+ sessionHistoryId.current = Date.now();
+ currentSessionHistoryId = sessionHistoryId.current;
+ }
+
let noteData: Partial<Note> = {
id,
- sessionId: currentSessionHistoryId
+ sessionId: isContentInvalid(data) ? null : currentSessionHistoryId
};
if (title) {
@@ -167,17 +183,19 @@ export const useEditor = (editorId = '', readonly?: boolean) => {
//@ts-ignore
await db.vault?.save(noteData);
}
- if (id && sessionId === currentSessionId) {
+ if (id && sessionIdRef.current === currentSessionId) {
note = db.notes?.note(id)?.data as Note;
await commands.setStatus(timeConverter(note.dateEdited), 'Saved');
if (
+ saveCount.current < 2 ||
currentNote.current?.title !== note.title ||
- currentNote.current?.headline !== note.headline
+ currentNote.current?.headline?.slice(0, 200) !== note.headline?.slice(0, 200)
) {
Navigation.queueRoutesForUpdate('ColoredNotes', 'Notes', 'TaggedNotes', 'TopicNotes');
}
}
+ saveCount.current++;
return id;
} catch (e) {
@@ -221,7 +239,7 @@ export const useEditor = (editorId = '', readonly?: boolean) => {
currentNote.current && (await reset(false));
await loadContent(item);
let nextSessionId = makeSessionId(item);
- sessionHistoryId.current = item.dateEdited;
+ sessionHistoryId.current = Date.now();
setSessionId(nextSessionId);
await commands.setSessionId(nextSessionId);
currentNote.current = item;
diff --git a/apps/mobile/src/screens/editor/tiptap/utils.ts b/apps/mobile/src/screens/editor/tiptap/utils.ts
index 2c735d104..3bca0c93f 100644
--- a/apps/mobile/src/screens/editor/tiptap/utils.ts
+++ b/apps/mobile/src/screens/editor/tiptap/utils.ts
@@ -76,3 +76,14 @@ export const getResponse = async (type: string): Promise<WebviewResponseData | f
}, 5000);
});
};
+
+export function isContentInvalid(content: string | undefined) {
+ return (
+ !content ||
+ content === '' ||
+ content.trim() === '' ||
+ content === '<p></p>' ||
+ content === '<p><br></p>' ||
+ content === '<p>&nbsp;</p>'
+ );
+}
diff --git a/apps/mobile/src/services/sync.js b/apps/mobile/src/services/sync.js
index 030329173..37e22143d 100644
--- a/apps/mobile/src/services/sync.js
+++ b/apps/mobile/src/services/sync.js
@@ -54,14 +54,10 @@ const run = async (context = 'global', forced = false, full = true) => {
}
}
} finally {
- userstore.setLastSynced(await db.lastSynced());
- initialize();
- if (editorController.current?.note?.id) {
- //await updateNoteInEditor();
+ if (full || forced) {
+ db.eventManager.publish(EVENTS.syncCompleted);
}
- db.eventManager.publish(EVENTS.syncCompleted);
console.log('sync done');
- userstore.setSyncing(false);
}
return result;
};