Skip to content

Commit cb7a272

Browse files
committed
fix warning on leave and autosave exalidraw
1 parent 426685a commit cb7a272

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

app/_components/FeatureComponents/Notes/Parts/TipTap/CustomExtensions/ExcalidrawExtension.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ export const ExcalidrawNodeView = ({
106106
}
107107
};
108108

109+
const handleClose = () => {
110+
if (excalidrawAPI) {
111+
handleSave();
112+
} else {
113+
setIsEditing(false);
114+
}
115+
};
116+
109117
const handleDelete = () => {
110118
setShowDeleteModal(true);
111119
};
@@ -129,7 +137,7 @@ export const ExcalidrawNodeView = ({
129137
<NodeViewWrapper className="excalidraw-node-wrapper">
130138
<Modal
131139
isOpen={isEditing && !!initialData}
132-
onClose={() => setIsEditing(false)}
140+
onClose={handleClose}
133141
title={
134142
<div className="flex items-center gap-2">
135143
{t("editor.editDiagram")}

app/_hooks/useNoteEditor.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export const useNoteEditor = ({
115115
registerNavigationGuard,
116116
unregisterNavigationGuard,
117117
executePendingNavigation,
118+
setHasUnsavedChanges: setProviderUnsaved,
118119
} = useNavigationGuard();
119120
const autosaveTimeoutRef = useRef<NodeJS.Timeout | null>(null);
120121

@@ -167,10 +168,15 @@ export const useNoteEditor = ({
167168
}, [isEditing]);
168169

169170
useEffect(() => {
170-
if (notesDefaultMode !== "edit" && !isEditing) return;
171+
if (notesDefaultMode !== "edit" && !isEditing) {
172+
setProviderUnsaved(false);
173+
return;
174+
}
171175
const titleChanged = title !== note.title;
172176
const categoryChanged = category !== (note.category || "Uncategorized");
173-
setHasUnsavedChanges(contentIsDirty || titleChanged || categoryChanged);
177+
const dirty = contentIsDirty || titleChanged || categoryChanged;
178+
setHasUnsavedChanges(dirty);
179+
setProviderUnsaved(dirty);
174180
}, [contentIsDirty, title, category, note, isEditing]);
175181

176182
const handleSave = useCallback(
@@ -275,6 +281,7 @@ export const useNoteEditor = ({
275281
setIsEditing(false);
276282
setIsEditingEncrypted(false);
277283
setContentIsDirty(false);
284+
setProviderUnsaved(false);
278285

279286
const categoryPath = buildCategoryPath(
280287
category || "Uncategorized",
@@ -352,6 +359,7 @@ export const useNoteEditor = ({
352359
setTitle(note.title);
353360
setCategory(note.category || "Uncategorized");
354361
setContentIsDirty(false);
362+
setProviderUnsaved(false);
355363
const { contentWithoutMetadata } = extractYamlMetadata(note.content || "");
356364
if (isMinimalMode) {
357365
setEditorContent(contentWithoutMetadata);

app/_providers/NavigationGuardProvider.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
useContext,
66
useState,
77
useCallback,
8+
useEffect,
89
ReactNode,
910
} from "react";
1011

@@ -59,6 +60,17 @@ export const NavigationGuardProvider = ({
5960
return navigationGuard ? !navigationGuard() : false;
6061
}, [navigationGuard]);
6162

63+
useEffect(() => {
64+
const onBeforeUnload = (e: BeforeUnloadEvent) => {
65+
if (hasUnsavedChanges) {
66+
e.preventDefault();
67+
}
68+
};
69+
70+
window.addEventListener("beforeunload", onBeforeUnload);
71+
return () => window.removeEventListener("beforeunload", onBeforeUnload);
72+
}, [hasUnsavedChanges]);
73+
6274
const executePendingNavigation = useCallback(() => {
6375
if (pendingNavigation) {
6476
pendingNavigation();

0 commit comments

Comments
 (0)