Răsfoiți Sursa

refactor unsaved warning processing

Yuki Takei 2 ani în urmă
părinte
comite
859d5b6421
1 a modificat fișierele cu 10 adăugiri și 3 ștergeri
  1. 10 3
      apps/app/src/components/PageEditor/PageEditor.tsx

+ 10 - 3
apps/app/src/components/PageEditor/PageEditor.tsx

@@ -132,6 +132,7 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
   // for https://redmine.weseek.co.jp/issues/125923
   // for https://redmine.weseek.co.jp/issues/125923
   const currentRevisionId = currentPage?.revision?._id ?? createdPageRevisionIdWithAttachment;
   const currentRevisionId = currentPage?.revision?._id ?? createdPageRevisionIdWithAttachment;
 
 
+  const initialValueRef = useRef('');
   const initialValue = useMemo(() => {
   const initialValue = useMemo(() => {
     if (!isNotFound) {
     if (!isNotFound) {
       return editingMarkdown ?? '';
       return editingMarkdown ?? '';
@@ -145,18 +146,25 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
     if (templateBodyData != null) {
     if (templateBodyData != null) {
       initialValue += `${templateBodyData}\n`;
       initialValue += `${templateBodyData}\n`;
     }
     }
+
     return initialValue;
     return initialValue;
 
 
   }, [isNotFound, currentPathname, editingMarkdown, isEnabledAttachTitleHeader, templateBodyData]);
   }, [isNotFound, currentPathname, editingMarkdown, isEnabledAttachTitleHeader, templateBodyData]);
 
 
+  useEffect(() => {
+    // set to ref
+    initialValueRef.current = initialValue;
+  }, [initialValue]);
+
+
   const [markdownToPreview, setMarkdownToPreview] = useState<string>(initialValue);
   const [markdownToPreview, setMarkdownToPreview] = useState<string>(initialValue);
   const setMarkdownPreviewWithDebounce = useMemo(() => debounce(100, throttle(150, (value: string) => {
   const setMarkdownPreviewWithDebounce = useMemo(() => debounce(100, throttle(150, (value: string) => {
     setMarkdownToPreview(value);
     setMarkdownToPreview(value);
   })), []);
   })), []);
   const mutateIsEnabledUnsavedWarningWithDebounce = useMemo(() => debounce(600, throttle(900, (value: string) => {
   const mutateIsEnabledUnsavedWarningWithDebounce = useMemo(() => debounce(600, throttle(900, (value: string) => {
     // Displays an unsaved warning alert
     // Displays an unsaved warning alert
-    mutateIsEnabledUnsavedWarning(value !== initialValue);
-  })), [initialValue, mutateIsEnabledUnsavedWarning]);
+    mutateIsEnabledUnsavedWarning(value !== initialValueRef.current);
+  })), [mutateIsEnabledUnsavedWarning]);
 
 
   const useCodeMirrorEditorMainProps = useMemo<ReactCodeMirrorProps>(() => {
   const useCodeMirrorEditorMainProps = useMemo<ReactCodeMirrorProps>(() => {
     return {
     return {
@@ -471,7 +479,6 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
     if (initialValue == null) {
     if (initialValue == null) {
       return;
       return;
     }
     }
-    // markdownToSave.current = initialValue;
     codeMirrorEditor?.initDoc(initialValue);
     codeMirrorEditor?.initDoc(initialValue);
     setMarkdownToPreview(initialValue);
     setMarkdownToPreview(initialValue);
     mutateIsEnabledUnsavedWarning(false);
     mutateIsEnabledUnsavedWarning(false);