mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-24 07:29:30 +01:00
mobile: use editor state to check whether
content update is needed on item sync
This commit is contained in:
@@ -76,7 +76,8 @@ export const EventTypes = {
|
|||||||
monograph: "editor-event:monograph",
|
monograph: "editor-event:monograph",
|
||||||
properties: "editor-event:properties",
|
properties: "editor-event:properties",
|
||||||
fullscreen: "editor-event:fullscreen",
|
fullscreen: "editor-event:fullscreen",
|
||||||
link: "editor-event:link"
|
link: "editor-event:link",
|
||||||
|
contentchange: "editor-event:content-change"
|
||||||
};
|
};
|
||||||
|
|
||||||
const publishNote = async (editor: useEditorType) => {
|
const publishNote = async (editor: useEditorType) => {
|
||||||
@@ -297,6 +298,9 @@ export const useEditorEvents = (
|
|||||||
case EventTypes.logger:
|
case EventTypes.logger:
|
||||||
logger.info("[WEBVIEW LOG]", editorMessage.value);
|
logger.info("[WEBVIEW LOG]", editorMessage.value);
|
||||||
break;
|
break;
|
||||||
|
case EventTypes.contentchange:
|
||||||
|
editor.onContentChanged();
|
||||||
|
break;
|
||||||
case EventTypes.content:
|
case EventTypes.content:
|
||||||
editor.saveContent({
|
editor.saveContent({
|
||||||
type: editorMessage.type,
|
type: editorMessage.type,
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ export const useEditor = (
|
|||||||
const insets = useGlobalSafeAreaInsets();
|
const insets = useGlobalSafeAreaInsets();
|
||||||
const isDefaultEditor = editorId === "";
|
const isDefaultEditor = editorId === "";
|
||||||
const saveCount = useRef(0);
|
const saveCount = useRef(0);
|
||||||
const lastSuccessfulSaveTime = useRef<number>(0);
|
const lastContentChangeTime = useRef<number>(0);
|
||||||
const lock = useRef(false);
|
const lock = useRef(false);
|
||||||
|
|
||||||
const postMessage = useCallback(
|
const postMessage = useCallback(
|
||||||
@@ -158,7 +158,7 @@ export const useEditor = (
|
|||||||
saveCount.current = 0;
|
saveCount.current = 0;
|
||||||
useEditorStore.getState().setReadonly(false);
|
useEditorStore.getState().setReadonly(false);
|
||||||
postMessage(EditorEvents.title, "");
|
postMessage(EditorEvents.title, "");
|
||||||
lastSuccessfulSaveTime.current = 0;
|
lastContentChangeTime.current = 0;
|
||||||
await commands.clearContent();
|
await commands.clearContent();
|
||||||
await commands.clearTags();
|
await commands.clearTags();
|
||||||
if (resetState) {
|
if (resetState) {
|
||||||
@@ -250,7 +250,7 @@ export const useEditor = (
|
|||||||
note = db.notes?.note(id)?.data as Note;
|
note = db.notes?.note(id)?.data as Note;
|
||||||
await commands.setStatus(timeConverter(note.dateEdited), "Saved");
|
await commands.setStatus(timeConverter(note.dateEdited), "Saved");
|
||||||
|
|
||||||
lastSuccessfulSaveTime.current = note.dateEdited;
|
lastContentChangeTime.current = note.dateEdited;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
saveCount.current < 2 ||
|
saveCount.current < 2 ||
|
||||||
@@ -307,7 +307,7 @@ export const useEditor = (
|
|||||||
sessionHistoryId.current = Date.now();
|
sessionHistoryId.current = Date.now();
|
||||||
await commands.setSessionId(nextSessionId);
|
await commands.setSessionId(nextSessionId);
|
||||||
await commands.focus();
|
await commands.focus();
|
||||||
lastSuccessfulSaveTime.current = 0;
|
lastContentChangeTime.current = 0;
|
||||||
useEditorStore.getState().setReadonly(false);
|
useEditorStore.getState().setReadonly(false);
|
||||||
} else {
|
} else {
|
||||||
if (!item.forced && currentNote.current?.id === item.id) return;
|
if (!item.forced && currentNote.current?.id === item.id) return;
|
||||||
@@ -315,7 +315,7 @@ export const useEditor = (
|
|||||||
overlay(true, item);
|
overlay(true, item);
|
||||||
currentNote.current && (await reset(false));
|
currentNote.current && (await reset(false));
|
||||||
await loadContent(item as NoteType);
|
await loadContent(item as NoteType);
|
||||||
lastSuccessfulSaveTime.current = item.dateEdited;
|
lastContentChangeTime.current = item.dateEdited;
|
||||||
const nextSessionId = makeSessionId(item as NoteType);
|
const nextSessionId = makeSessionId(item as NoteType);
|
||||||
sessionHistoryId.current = Date.now();
|
sessionHistoryId.current = Date.now();
|
||||||
setSessionId(nextSessionId);
|
setSessionId(nextSessionId);
|
||||||
@@ -371,8 +371,10 @@ export const useEditor = (
|
|||||||
if (!currentNote.current || noteId !== currentNote.current.id) return;
|
if (!currentNote.current || noteId !== currentNote.current.id) return;
|
||||||
const isContentEncrypted = typeof (data as Content)?.data === "object";
|
const isContentEncrypted = typeof (data as Content)?.data === "object";
|
||||||
const note = db.notes?.note(currentNote.current?.id).data as NoteType;
|
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 (data.type === "tiptap") {
|
||||||
if (!currentNote.current.locked && isContentEncrypted) {
|
if (!currentNote.current.locked && isContentEncrypted) {
|
||||||
lockNoteWithVault(note);
|
lockNoteWithVault(note);
|
||||||
@@ -388,11 +390,10 @@ export const useEditor = (
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const _nextContent = await db.content?.raw(note.contentId);
|
const _nextContent = await db.content?.raw(note.contentId);
|
||||||
lastSuccessfulSaveTime.current = note.dateEdited;
|
if (_nextContent === currentContent.current?.data) return;
|
||||||
if (_nextContent !== currentContent.current?.data) {
|
lastContentChangeTime.current = note.dateEdited;
|
||||||
await postMessage(EditorEvents.updatehtml, _nextContent.data);
|
await postMessage(EditorEvents.updatehtml, _nextContent.data);
|
||||||
currentContent.current = _nextContent;
|
currentContent.current = _nextContent;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const note = data as NoteType;
|
const note = data as NoteType;
|
||||||
@@ -438,6 +439,7 @@ export const useEditor = (
|
|||||||
content?: string;
|
content?: string;
|
||||||
type: string;
|
type: string;
|
||||||
}) => {
|
}) => {
|
||||||
|
lastContentChangeTime.current = Date.now();
|
||||||
if (lock.current) return;
|
if (lock.current) return;
|
||||||
if (type === EditorEvents.content) {
|
if (type === EditorEvents.content) {
|
||||||
currentContent.current = {
|
currentContent.current = {
|
||||||
@@ -537,6 +539,10 @@ export const useEditor = (
|
|||||||
loadNote
|
loadNote
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const onContentChanged = () => {
|
||||||
|
lastContentChangeTime.current = Date.now();
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ref: editorRef,
|
ref: editorRef,
|
||||||
onLoad,
|
onLoad,
|
||||||
@@ -550,6 +556,7 @@ export const useEditor = (
|
|||||||
note: currentNote,
|
note: currentNote,
|
||||||
onReady,
|
onReady,
|
||||||
saveContent,
|
saveContent,
|
||||||
|
onContentChanged,
|
||||||
editorId: editorId
|
editorId: editorId
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -77,10 +77,12 @@ export function useEditorController(update: () => void): EditorController {
|
|||||||
const selectionChange = useCallback((_editor: Editor) => {}, []);
|
const selectionChange = useCallback((_editor: Editor) => {}, []);
|
||||||
|
|
||||||
const titleChange = useCallback((title: string) => {
|
const titleChange = useCallback((title: string) => {
|
||||||
|
post(EventTypes.contentchange);
|
||||||
post(EventTypes.title, title);
|
post(EventTypes.title, title);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const contentChange = useCallback((editor: Editor) => {
|
const contentChange = useCallback((editor: Editor) => {
|
||||||
|
post(EventTypes.contentchange);
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
if (typeof timers.current.change === "number") {
|
if (typeof timers.current.change === "number") {
|
||||||
clearTimeout(timers.current?.change);
|
clearTimeout(timers.current?.change);
|
||||||
|
|||||||
@@ -142,14 +142,18 @@ export const EventTypes = {
|
|||||||
monograph: "editor-event:monograph",
|
monograph: "editor-event:monograph",
|
||||||
properties: "editor-event:properties",
|
properties: "editor-event:properties",
|
||||||
fullscreen: "editor-event:fullscreen",
|
fullscreen: "editor-event:fullscreen",
|
||||||
link: "editor-event:link"
|
link: "editor-event:link",
|
||||||
|
contentchange: "editor-event:content-change"
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export function isReactNative(): boolean {
|
export function isReactNative(): boolean {
|
||||||
return !!window.ReactNativeWebView;
|
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
|
const logString = logs
|
||||||
.map((log) => {
|
.map((log) => {
|
||||||
return typeof log !== "string" ? JSON.stringify(log) : 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>(
|
export function post<T extends keyof typeof EventTypes>(
|
||||||
type: typeof EventTypes[T],
|
type: typeof EventTypes[T],
|
||||||
value?: unknown
|
value?: unknown
|
||||||
):void {
|
): void {
|
||||||
if (isReactNative()) {
|
if (isReactNative()) {
|
||||||
window.ReactNativeWebView.postMessage(
|
window.ReactNativeWebView.postMessage(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
|||||||
Reference in New Issue
Block a user