Skip to content

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

mobile: use editor state to check whether content update is needed on item sync

ammarahm-ed64bf627462fe
diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor-events.ts b/apps/mobile/app/screens/editor/tiptap/use-editor-events.ts
index 17f4423cb..8de248764 100644
--- a/apps/mobile/app/screens/editor/tiptap/use-editor-events.ts
+++ b/apps/mobile/app/screens/editor/tiptap/use-editor-events.ts
@@ -76,7 +76,8 @@ export const EventTypes = {
monograph: "editor-event:monograph",
properties: "editor-event:properties",
fullscreen: "editor-event:fullscreen",
- link: "editor-event:link"
+ link: "editor-event:link",
+ contentchange: "editor-event:content-change"
};
const publishNote = async (editor: useEditorType) => {
@@ -297,6 +298,9 @@ export const useEditorEvents = (
case EventTypes.logger:
logger.info("[WEBVIEW LOG]", editorMessage.value);
break;
+ case EventTypes.contentchange:
+ editor.onContentChanged();
+ break;
case EventTypes.content:
editor.saveContent({
type: editorMessage.type,
diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor.ts b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
index f10ae4de7..852465436 100644
--- a/apps/mobile/app/screens/editor/tiptap/use-editor.ts
+++ b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
@@ -73,7 +73,7 @@ export const useEditor = (
const insets = useGlobalSafeAreaInsets();
const isDefaultEditor = editorId === "";
const saveCount = useRef(0);
- const lastSuccessfulSaveTime = useRef<number>(0);
+ const lastContentChangeTime = useRef<number>(0);
const lock = useRef(false);
const postMessage = useCallback(
@@ -158,7 +158,7 @@ export const useEditor = (
saveCount.current = 0;
useEditorStore.getState().setReadonly(false);
postMessage(EditorEvents.title, "");
- lastSuccessfulSaveTime.current = 0;
+ lastContentChangeTime.current = 0;
await commands.clearContent();
await commands.clearTags();
if (resetState) {
@@ -250,7 +250,7 @@ export const useEditor = (
note = db.notes?.note(id)?.data as Note;
await commands.setStatus(timeConverter(note.dateEdited), "Saved");
- lastSuccessfulSaveTime.current = note.dateEdited;
+ lastContentChangeTime.current = note.dateEdited;
if (
saveCount.current < 2 ||
@@ -307,7 +307,7 @@ export const useEditor = (
sessionHistoryId.current = Date.now();
await commands.setSessionId(nextSessionId);
await commands.focus();
- lastSuccessfulSaveTime.current = 0;
+ lastContentChangeTime.current = 0;
useEditorStore.getState().setReadonly(false);
} else {
if (!item.forced && currentNote.current?.id === item.id) return;
@@ -315,7 +315,7 @@ export const useEditor = (
overlay(true, item);
currentNote.current && (await reset(false));
await loadContent(item as NoteType);
- lastSuccessfulSaveTime.current = item.dateEdited;
+ lastContentChangeTime.current = item.dateEdited;
const nextSessionId = makeSessionId(item as NoteType);
sessionHistoryId.current = Date.now();
setSessionId(nextSessionId);
@@ -371,8 +371,10 @@ export const useEditor = (
if (!currentNote.current || noteId !== currentNote.current.id) return;
const isContentEncrypted = typeof (data as Content)?.data === "object";
const note = db.notes?.note(currentNote.current?.id).data as NoteType;
- lock.current = true;
+
+ if (lastContentChangeTime.current >= note.dateEdited) return;
+ lock.current = true;
if (data.type === "tiptap") {
if (!currentNote.current.locked && isContentEncrypted) {
lockNoteWithVault(note);
@@ -388,11 +390,10 @@ export const useEditor = (
}
} else {
const _nextContent = await db.content?.raw(note.contentId);
- lastSuccessfulSaveTime.current = note.dateEdited;
- if (_nextContent !== currentContent.current?.data) {
- await postMessage(EditorEvents.updatehtml, _nextContent.data);
- currentContent.current = _nextContent;
- }
+ if (_nextContent === currentContent.current?.data) return;
+ lastContentChangeTime.current = note.dateEdited;
+ await postMessage(EditorEvents.updatehtml, _nextContent.data);
+ currentContent.current = _nextContent;
}
} else {
const note = data as NoteType;
@@ -438,6 +439,7 @@ export const useEditor = (
content?: string;
type: string;
}) => {
+ lastContentChangeTime.current = Date.now();
if (lock.current) return;
if (type === EditorEvents.content) {
currentContent.current = {
@@ -537,6 +539,10 @@ export const useEditor = (
loadNote
]);
+ const onContentChanged = () => {
+ lastContentChangeTime.current = Date.now();
+ };
+
return {
ref: editorRef,
onLoad,
@@ -550,6 +556,7 @@ export const useEditor = (
note: currentNote,
onReady,
saveContent,
+ onContentChanged,
editorId: editorId
};
};
diff --git a/packages/editor-mobile/src/hooks/useEditorController.ts b/packages/editor-mobile/src/hooks/useEditorController.ts
index 36225250a..810f112be 100644
--- a/packages/editor-mobile/src/hooks/useEditorController.ts
+++ b/packages/editor-mobile/src/hooks/useEditorController.ts
@@ -77,10 +77,12 @@ export function useEditorController(update: () => void): EditorController {
const selectionChange = useCallback((_editor: Editor) => {}, []);
const titleChange = useCallback((title: string) => {
+ post(EventTypes.contentchange);
post(EventTypes.title, title);
}, []);
const contentChange = useCallback((editor: Editor) => {
+ post(EventTypes.contentchange);
if (!editor) return;
if (typeof timers.current.change === "number") {
clearTimeout(timers.current?.change);
diff --git a/packages/editor-mobile/src/utils/index.ts b/packages/editor-mobile/src/utils/index.ts
index 030aae2ab..678b5e054 100644
--- a/packages/editor-mobile/src/utils/index.ts
+++ b/packages/editor-mobile/src/utils/index.ts
@@ -142,14 +142,18 @@ export const EventTypes = {
monograph: "editor-event:monograph",
properties: "editor-event:properties",
fullscreen: "editor-event:fullscreen",
- link: "editor-event:link"
+ link: "editor-event:link",
+ contentchange: "editor-event:content-change"
} as const;
export function isReactNative(): boolean {
return !!window.ReactNativeWebView;
}
-export function logger(type: "info" | "warn" | "error", ...logs: unknown[]):void {
+export function logger(
+ type: "info" | "warn" | "error",
+ ...logs: unknown[]
+): void {
const logString = logs
.map((log) => {
return typeof log !== "string" ? JSON.stringify(log) : log;
@@ -162,7 +166,7 @@ export function logger(type: "info" | "warn" | "error", ...logs: unknown[]):void
export function post<T extends keyof typeof EventTypes>(
type: typeof EventTypes[T],
value?: unknown
-):void {
+): void {
if (isReactNative()) {
window.ReactNativeWebView.postMessage(
JSON.stringify({