mobile: ensure note is saved when change note

This commit is contained in:
ammarahm-ed
2023-04-05 02:05:52 +05:00
parent 71ecc05bd6
commit 156ce7e711
3 changed files with 41 additions and 23 deletions

View File

@@ -268,12 +268,28 @@ export const useEditorEvents = (
(event: WebViewMessageEvent) => { (event: WebViewMessageEvent) => {
const data = event.nativeEvent.data; const data = event.nativeEvent.data;
const editorMessage = JSON.parse(data) as EditorMessage; const editorMessage = JSON.parse(data) as EditorMessage;
if (editorMessage.type === EventTypes.content) {
editor.saveContent({
type: editorMessage.type,
content: editorMessage.value as string,
forSessionId: editorMessage.sessionId
});
} else if (editorMessage.type === EventTypes.title) {
editor.saveContent({
type: editorMessage.type,
title: editorMessage.value as string,
forSessionId: editorMessage.sessionId
});
}
if ( if (
editorMessage.sessionId !== editor.sessionId && editorMessage.sessionId !== editor.sessionId &&
editorMessage.type !== EditorEvents.status editorMessage.type !== EditorEvents.status
) { ) {
return; return;
} }
switch (editorMessage.type) { switch (editorMessage.type) {
case EventTypes.logger: case EventTypes.logger:
logger.info("[WEBVIEW LOG]", editorMessage.value); logger.info("[WEBVIEW LOG]", editorMessage.value);
@@ -281,21 +297,8 @@ export const useEditorEvents = (
case EventTypes.contentchange: case EventTypes.contentchange:
editor.onContentChanged(); editor.onContentChanged();
break; break;
case EventTypes.content:
editor.saveContent({
type: editorMessage.type,
content: editorMessage.value as string
});
break;
case EventTypes.selection: case EventTypes.selection:
break; break;
case EventTypes.title:
editor.saveContent({
type: editorMessage.type,
title: editorMessage.value as string
});
break;
case EventTypes.reminders: case EventTypes.reminders:
if (!editor.note.current) { if (!editor.note.current) {
ToastEvent.show({ ToastEvent.show({

View File

@@ -77,6 +77,7 @@ export const useEditor = (
const lastContentChangeTime = useRef<number>(0); const lastContentChangeTime = useRef<number>(0);
const lock = useRef(false); const lock = useRef(false);
const loadedImages = useRef<{ [name: string]: boolean }>({}); const loadedImages = useRef<{ [name: string]: boolean }>({});
const lockedSessionId = useRef<string>();
const postMessage = useCallback( const postMessage = useCallback(
async <T>(type: string, data: T) => async <T>(type: string, data: T) =>
@@ -243,6 +244,11 @@ export const useEditor = (
state.current.currentlyEditing state.current.currentlyEditing
) { ) {
setTimeout(() => { setTimeout(() => {
if (
(currentNote.current?.id && currentNote.current?.id !== id) ||
!state.current.currentlyEditing
)
return;
id && useEditorStore.getState().setCurrentlyEditingNote(id); id && useEditorStore.getState().setCurrentlyEditingNote(id);
}); });
} }
@@ -378,10 +384,11 @@ export const useEditor = (
} }
lastContentChangeTime.current = item.dateEdited; lastContentChangeTime.current = item.dateEdited;
const nextSessionId = makeSessionId(item as NoteType); const nextSessionId = makeSessionId(item as NoteType);
lockedSessionId.current = nextSessionId;
sessionHistoryId.current = Date.now(); sessionHistoryId.current = Date.now();
setSessionId(nextSessionId); setSessionId(nextSessionId);
commands.setSessionId(nextSessionId);
sessionIdRef.current = nextSessionId; sessionIdRef.current = nextSessionId;
await commands.setSessionId(nextSessionId);
currentNote.current = item as NoteType; currentNote.current = item as NoteType;
await commands.setStatus(timeConverter(item.dateEdited), "Saved"); await commands.setStatus(timeConverter(item.dateEdited), "Saved");
await postMessage(EditorEvents.title, item.title); await postMessage(EditorEvents.title, item.title);
@@ -389,6 +396,11 @@ export const useEditor = (
useEditorStore.getState().setReadonly(item.readonly); useEditorStore.getState().setReadonly(item.readonly);
await commands.setTags(currentNote.current); await commands.setTags(currentNote.current);
commands.setSettings(); commands.setSettings();
setTimeout(() => {
if (lockedSessionId.current === nextSessionId) {
lockedSessionId.current = undefined;
}
}, 300);
overlay(false); overlay(false);
loadImages(); loadImages();
} }
@@ -495,14 +507,16 @@ export const useEditor = (
({ ({
title, title,
content, content,
type type,
forSessionId
}: { }: {
title?: string; title?: string;
content?: string; content?: string;
type: string; type: string;
forSessionId: string;
}) => { }) => {
if (lock.current || lockedSessionId.current === forSessionId) return;
lastContentChangeTime.current = Date.now(); lastContentChangeTime.current = Date.now();
if (lock.current) return;
if (type === EditorEvents.content) { if (type === EditorEvents.content) {
currentContent.current = { currentContent.current = {
data: content, data: content,
@@ -511,16 +525,16 @@ export const useEditor = (
}; };
} }
const noteIdFromSessionId = const noteIdFromSessionId =
!sessionIdRef.current || sessionIdRef.current.startsWith("session") !forSessionId || forSessionId.startsWith("session")
? null ? null
: sessionIdRef.current.split("_")[0]; : forSessionId.split("_")[0];
const noteId = currentNote.current?.id || noteIdFromSessionId; const noteId = noteIdFromSessionId || currentNote.current?.id;
const params = { const params = {
title, title,
data: content, data: content,
type: "tiptap", type: "tiptap",
sessionId, sessionId: forSessionId,
id: noteId, id: noteId,
sessionHistoryId: sessionHistoryId.current sessionHistoryId: sessionHistoryId.current
}; };
@@ -530,7 +544,7 @@ export const useEditor = (
if ( if (
currentNote.current && currentNote.current &&
!params.id && !params.id &&
params.sessionId === sessionId params.sessionId === forSessionId
) { ) {
params.id = currentNote.current?.id; params.id = currentNote.current?.id;
} }
@@ -543,7 +557,7 @@ export const useEditor = (
150 150
); );
}, },
[sessionId, withTimer, onChange, saveNote] [withTimer, onChange, saveNote]
); );
const restoreEditorState = useCallback(async () => { const restoreEditorState = useCallback(async () => {

View File

@@ -83,6 +83,7 @@ export function useEditorController(update: () => void): EditorController {
}, []); }, []);
const contentChange = useCallback((editor: Editor) => { const contentChange = useCallback((editor: Editor) => {
const currentSessionId = globalThis.sessionId;
post(EventTypes.contentchange); post(EventTypes.contentchange);
if (!editor) return; if (!editor) return;
if (typeof timers.current.change === "number") { if (typeof timers.current.change === "number") {
@@ -90,7 +91,7 @@ export function useEditorController(update: () => void): EditorController {
} }
timers.current.change = setTimeout(() => { timers.current.change = setTimeout(() => {
htmlContentRef.current = editor.getHTML(); htmlContentRef.current = editor.getHTML();
post(EventTypes.content, htmlContentRef.current); post(EventTypes.content, htmlContentRef.current, currentSessionId);
}, 300); }, 300);
}, []); }, []);