Skip to content

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

mobile: fix editor loading

Ammar Ahmed0e8ea3ddeb2f
diff --git a/apps/mobile/app/hooks/use-app-events.tsx b/apps/mobile/app/hooks/use-app-events.tsx
index 4512a5af2..fedcd5236 100644
--- a/apps/mobile/app/hooks/use-app-events.tsx
+++ b/apps/mobile/app/hooks/use-app-events.tsx
@@ -86,6 +86,7 @@ import { updateStatusBarColor } from "../utils/colors";
import { BETA } from "../utils/constants";
import {
eCloseSheet,
+ eEditorReset,
eLoginSessionExpired,
eOnLoadNote,
eOpenAnnouncementDialog,
@@ -235,7 +236,7 @@ async function checkForShareExtensionLaunchedInBackground() {
if (notesAddedFromIntent || shareExtensionOpened) {
const id = useTabStore.getState().getCurrentNoteId();
const note = id && (await db.notes.note(id));
- eSendEvent("webview_reset");
+ eSendEvent(eEditorReset);
if (note) setTimeout(() => eSendEvent("loadingNote", note), 1);
MMKV.removeItem("shareExtensionOpened");
}
@@ -532,6 +533,14 @@ export const useAppEvents = () => {
}
//@ts-ignore
globalThis["IS_SHARE_EXTENSION"] = false;
+
+ if (
+ SettingsService.getBackgroundEnterTime() + 60 * 1000 * 30 <
+ Date.now()
+ ) {
+ // Reset the editor if the app has been in background for more than 10 minutes.
+ eSendEvent(eEditorReset);
+ }
} else {
await saveEditorState();
if (
diff --git a/apps/mobile/app/screens/editor/index.tsx b/apps/mobile/app/screens/editor/index.tsx
index 69d96834a..9c711dd75 100755
--- a/apps/mobile/app/screens/editor/index.tsx
+++ b/apps/mobile/app/screens/editor/index.tsx
@@ -39,6 +39,7 @@ import {
eSubscribeEvent
} from "../../services/event-manager";
import {
+ eEditorReset,
eOnLoadNote,
eUnlockNote,
eUnlockWithBiometrics,
@@ -54,7 +55,8 @@ import { syncTabs, useTabStore } from "./tiptap/use-tab-store";
import {
editorController,
editorState,
- openInternalLink
+ openInternalLink,
+ randId
} from "./tiptap/utils";
import { tabBarRef } from "../../utils/global-refs";
@@ -103,24 +105,20 @@ const Editor = React.memo(
noToolbar,
noHeader
});
- const renderKey = useRef(`editor-0` + editorId);
+ const renderKey = useRef(randId("editor-id") + editorId);
useImperativeHandle(ref, () => ({
get: () => editor
}));
useLockedNoteHandler();
const onError = useCallback(() => {
- renderKey.current =
- renderKey.current === `editor-0`
- ? `editor-1` + editorId
- : `editor-0` + editorId;
-
+ renderKey.current = randId("editor-id") + editorId;
editor.state.current.ready = false;
editor.setLoading(true);
}, [editor, editorId]);
useEffect(() => {
- const sub = [eSubscribeEvent("webview_reset", onError)];
+ const sub = [eSubscribeEvent(eEditorReset, onError)];
return () => {
sub.forEach((s) => s?.unsubscribe());
};
diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor.ts b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
index 56a3a22dd..3e5d0af12 100644
--- a/apps/mobile/app/screens/editor/tiptap/use-editor.ts
+++ b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
@@ -50,6 +50,7 @@ import SettingsService from "../../../services/settings";
import { useSettingStore } from "../../../stores/use-setting-store";
import { useTagStore } from "../../../stores/use-tag-store";
import {
+ eEditorReset,
eEditorTabFocused,
eOnLoadNote,
eShowMergeDialog,
@@ -464,6 +465,7 @@ export const useEditor = (
overlay(false);
return;
}
+ console.log("LOADING NOTE", event.item.id);
const item = event.item;
const currentTab = useTabStore
@@ -549,6 +551,7 @@ export const useEditor = (
loadingState.current === currentContents.current[item.id]?.data
) {
// If note is already loading, return.
+ console.log("Note is already loading...");
return;
}
@@ -886,7 +889,7 @@ export const useEditor = (
useTabStore.getState().currentTab
))
) {
- eSendEvent("webview_reset", "onReady");
+ eSendEvent(eEditorReset, "onReady");
return false;
} else {
syncTabs();
@@ -917,7 +920,6 @@ export const useEditor = (
} else if (state.current?.initialLoadCalled) {
const note = currentNotes.current[noteId];
if (note) {
- console.log("Loading note in onLoad...");
loadNote({
item: note
});
diff --git a/apps/mobile/app/services/settings.ts b/apps/mobile/app/services/settings.ts
index 095b61c05..0260eebf1 100644
--- a/apps/mobile/app/services/settings.ts
+++ b/apps/mobile/app/services/settings.ts
@@ -191,11 +191,11 @@ function canLockAppInBackground() {
}
let backgroundEnterTime = 0;
function appEnteredBackground() {
- if (canLockAppInBackground()) {
- backgroundEnterTime = Date.now();
- }
+ backgroundEnterTime = Date.now();
}
+const getBackgroundEnterTime = () => backgroundEnterTime;
+
function shouldLockAppOnEnterForeground() {
if (
useUserStore.getState().disableAppLockRequests ||
@@ -227,7 +227,8 @@ export const SettingsService = {
shouldLockAppOnEnterForeground,
canLockAppInBackground,
appEnteredBackground,
- setPrivacyScreen
+ setPrivacyScreen,
+ getBackgroundEnterTime
};
init();
diff --git a/apps/mobile/app/utils/events.js b/apps/mobile/app/utils/events.js
index ffdd393a8..4c786fd81 100644
--- a/apps/mobile/app/utils/events.js
+++ b/apps/mobile/app/utils/events.js
@@ -174,3 +174,4 @@ export const eUnlockWithPassword = "619";
export const eUpdateNoteInEditor = "620";
export const eOnEnterEditor = "621";
export const eOnExitEditor = "622";
+export const eEditorReset = "623";
diff --git a/packages/editor-mobile/src/components/editor.tsx b/packages/editor-mobile/src/components/editor.tsx
index 00901fb5e..03d6be0b8 100644
--- a/packages/editor-mobile/src/components/editor.tsx
+++ b/packages/editor-mobile/src/components/editor.tsx
@@ -296,8 +296,6 @@ const Tiptap = ({
pendingSaveRequests
.getPendingContentIds()
.then(async (result) => {
- logger("info", result.length, "PENDING ITEMS");
-
if (result && result.length) {
dbLogger("log", "Pending save requests found... restoring");
await pendingSaveRequests.postPendingRequests();
@@ -315,6 +313,16 @@ const Tiptap = ({
const noteState = tabRef.current.noteId
? state.noteState[tabRef.current.noteId]
: undefined;
+
+ post(
+ EventTypes.tabFocused,
+ !!globalThis.editorControllers[tabRef.current.id]?.content.current &&
+ !editorControllers[tabRef.current.id]?.loading,
+ tabRef.current.id,
+ state.getCurrentNoteId()
+ );
+ editorControllers[tabRef.current.id]?.updateTab();
+
if (noteState) {
if (
containerRef.current &&
@@ -339,14 +347,6 @@ const Tiptap = ({
) {
editorControllers[tabRef.current.id]?.setLoading(true);
}
- post(
- EventTypes.tabFocused,
- !!globalThis.editorControllers[tabRef.current.id]?.content.current &&
- !editorControllers[tabRef.current.id]?.loading,
- tabRef.current.id,
- state.getCurrentNoteId()
- );
- editorControllers[tabRef.current.id]?.updateTab();
} else {
isFocusedRef.current = false;
}
diff --git a/packages/editor-mobile/src/hooks/useTabStore.ts b/packages/editor-mobile/src/hooks/useTabStore.ts
index b73ed393d..4d6834b73 100644
--- a/packages/editor-mobile/src/hooks/useTabStore.ts
+++ b/packages/editor-mobile/src/hooks/useTabStore.ts
@@ -89,6 +89,8 @@ export const useTabStore = create(
currentTab: 0,
scrollPosition: {},
setNoteState: (noteId: string, state: Partial<NoteState>) => {
+ if (editorControllers[get().currentTab]?.loading) return;
+
const noteState = {
...get().noteState
};
@@ -96,6 +98,7 @@ export const useTabStore = create(
...get().noteState[noteId],
...state
};
+
set({
noteState
});