mobile: improve editor content loading

This commit is contained in:
ammarahm-ed
2023-09-20 10:18:21 +05:00
committed by Ammar Ahmed
parent 6d2be3a083
commit 6d324fe8da

View File

@@ -22,7 +22,7 @@ import { EVENTS } from "@notesnook/core/dist/common";
import { useThemeEngineStore } from "@notesnook/theme"; import { useThemeEngineStore } from "@notesnook/theme";
import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import WebView from "react-native-webview"; import WebView from "react-native-webview";
import { db } from "../../../common/database"; import { DatabaseLogger, db } from "../../../common/database";
import useGlobalSafeAreaInsets from "../../../hooks/use-global-safe-area-insets"; import useGlobalSafeAreaInsets from "../../../hooks/use-global-safe-area-insets";
import { DDS } from "../../../services/device-detection"; import { DDS } from "../../../services/device-detection";
import { import {
@@ -147,10 +147,12 @@ export const useEditor = (
lastContentChangeTime.current = 0; lastContentChangeTime.current = 0;
resetContent && (await commands.clearContent()); resetContent && (await commands.clearContent());
resetContent && (await commands.clearTags()); resetContent && (await commands.clearTags());
const newSessionId = makeSessionId();
await commands.setSessionId(newSessionId);
setSessionId(newSessionId);
if (resetState) { if (resetState) {
const newSessionId = makeSessionId();
await commands.setSessionId(newSessionId);
setSessionId(newSessionId);
isDefaultEditor && isDefaultEditor &&
useEditorStore.getState().setCurrentlyEditingNote(null); useEditorStore.getState().setCurrentlyEditingNote(null);
placeholderTip.current = TipManager.placeholderTip(); placeholderTip.current = TipManager.placeholderTip();
@@ -267,7 +269,8 @@ export const useEditor = (
saveCount.current++; saveCount.current++;
return id; return id;
} catch (e) { } catch (e) {
console.log("Error saving note: ", e); console.error(e);
DatabaseLogger.error(e as Error);
} }
}, },
[commands, isDefaultEditor, postMessage, readonly, reset] [commands, isDefaultEditor, postMessage, readonly, reset]
@@ -275,7 +278,6 @@ export const useEditor = (
const loadContent = useCallback(async (note: NoteType) => { const loadContent = useCallback(async (note: NoteType) => {
currentNote.current = note; currentNote.current = note;
console.log(note, note.contentId);
if (note.locked || note.content) { if (note.locked || note.content) {
currentContent.current = { currentContent.current = {
data: note.content?.data, data: note.content?.data,
@@ -356,7 +358,6 @@ export const useEditor = (
) { ) {
state.current.ready = true; state.current.ready = true;
} }
console.log(currentNote.current?.id, currentNote.current?.title);
if (item && item.type === "new") { if (item && item.type === "new") {
currentNote.current && (await reset()); currentNote.current && (await reset());
@@ -372,16 +373,23 @@ export const useEditor = (
if (!item.forced && currentNote.current?.id === item.id) return; if (!item.forced && currentNote.current?.id === item.id) return;
state.current.movedAway = false; state.current.movedAway = false;
state.current.currentlyEditing = true; state.current.currentlyEditing = true;
isDefaultEditor && editorState.setCurrentlyEditingNote(item.id);
currentNote.current && (await reset(false, false)); if (currentNote.current?.id !== item.id) {
currentNote.current && (await reset(false, false));
isDefaultEditor && editorState.setCurrentlyEditingNote(item.id);
}
await loadContent(item as NoteType); await loadContent(item as NoteType);
if ( if (
currentNote.current?.id === item.id &&
loadingState.current && loadingState.current &&
currentContent.current?.data && currentContent.current?.data &&
loadingState.current === currentContent.current?.data loadingState.current === currentContent.current?.data
) { ) {
return; return;
} }
if ( if (
!currentContent.current?.data || !currentContent.current?.data ||
currentContent.current?.data.length < 50000 currentContent.current?.data.length < 50000
@@ -400,14 +408,12 @@ export const useEditor = (
sessionHistoryId.current = Date.now(); sessionHistoryId.current = Date.now();
setSessionId(nextSessionId); setSessionId(nextSessionId);
console.log("nextSessionId", nextSessionId);
commands.setSessionId(nextSessionId); commands.setSessionId(nextSessionId);
sessionIdRef.current = nextSessionId; sessionIdRef.current = nextSessionId;
currentNote.current = item as NoteType; currentNote.current = item as NoteType;
await commands.setStatus(getFormattedDate(item.dateEdited), "Saved"); await commands.setStatus(getFormattedDate(item.dateEdited), "Saved");
await postMessage(EditorEvents.title, item.title); await postMessage(EditorEvents.title, item.title);
loadingState.current = currentContent.current?.data; loadingState.current = currentContent.current?.data;
console.log("setting html...");
if (currentContent.current?.data) { if (currentContent.current?.data) {
await postMessage( await postMessage(
EditorEvents.html, EditorEvents.html,
@@ -415,7 +421,6 @@ export const useEditor = (
10000 10000
); );
} }
console.log("set html done...");
loadingState.current = undefined; loadingState.current = undefined;
useEditorStore.getState().setReadonly(item.readonly); useEditorStore.getState().setReadonly(item.readonly);
await commands.setTags(currentNote.current); await commands.setTags(currentNote.current);
@@ -541,6 +546,14 @@ export const useEditor = (
}) => { }) => {
if (lock.current || lockedSessionId.current === forSessionId) return; if (lock.current || lockedSessionId.current === forSessionId) return;
lastContentChangeTime.current = Date.now(); lastContentChangeTime.current = Date.now();
if (
sessionHistoryId.current &&
Date.now() - sessionHistoryId.current > 5 * 60 * 1000
) {
sessionHistoryId.current = Date.now();
}
if (type === EditorEvents.content) { if (type === EditorEvents.content) {
currentContent.current = { currentContent.current = {
data: content, data: content,