Просмотр исходного кода

reset caretline returning to View

reiji-h 1 год назад
Родитель
Сommit
968e9b32d9

+ 13 - 8
apps/app/src/client/components/PageEditor/PageEditor.tsx

@@ -24,7 +24,7 @@ import { uploadAttachments } from '~/client/services/upload-attachments';
 import { toastError, toastSuccess, toastWarning } from '~/client/util/toastr';
 import { toastError, toastSuccess, toastWarning } from '~/client/util/toastr';
 import { useShouldExpandContent } from '~/services/layout/use-should-expand-content';
 import { useShouldExpandContent } from '~/services/layout/use-should-expand-content';
 import {
 import {
-  useSaveNextCaretLine,
+  useReservedNextCaretLine,
   useDefaultIndentSize, useCurrentUser,
   useDefaultIndentSize, useCurrentUser,
   useCurrentPathname, useIsEnabledAttachTitleHeader,
   useCurrentPathname, useIsEnabledAttachTitleHeader,
   useIsEditable, useIsIndentSizeForced,
   useIsEditable, useIsIndentSizeForced,
@@ -110,7 +110,7 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
   const { data: user } = useCurrentUser();
   const { data: user } = useCurrentUser();
   const { onEditorsUpdated } = useEditingUsers();
   const { onEditorsUpdated } = useEditingUsers();
   const onConflict = useConflictResolver();
   const onConflict = useConflictResolver();
-  const { data: nextCaretLine, mutate: mutateNextCaretLine } = useSaveNextCaretLine();
+  const { data: reservedNextCaretLine, mutate: mutateReservedNextCaretLine } = useReservedNextCaretLine();
 
 
   const { data: rendererOptions } = usePreviewOptions();
   const { data: rendererOptions } = usePreviewOptions();
 
 
@@ -300,20 +300,25 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
     }
     }
   }, [initialValue, isIndentSizeForced, mutateCurrentIndentSize]);
   }, [initialValue, isIndentSizeForced, mutateCurrentIndentSize]);
 
 
-  // set handler to set caret line
+
+  // set caret line if the edit button next to Header is clicked.
   useEffect(() => {
   useEffect(() => {
     if (codeMirrorEditor?.setCaretLine == null) {
     if (codeMirrorEditor?.setCaretLine == null) {
       return;
       return;
     }
     }
     if (editorMode === EditorMode.Editor) {
     if (editorMode === EditorMode.Editor) {
-      codeMirrorEditor.setCaretLine(nextCaretLine ?? 0, true);
+      codeMirrorEditor.setCaretLine(reservedNextCaretLine ?? 0, true);
     }
     }
 
 
-    return () => {
-      mutateNextCaretLine(0);
-    };
+  }, [codeMirrorEditor, editorMode, reservedNextCaretLine]);
+
+  // reset caret line if returning to the View.
+  useEffect(() => {
+    if (editorMode === EditorMode.View) {
+      mutateReservedNextCaretLine(0);
+    }
+  }, [editorMode, mutateReservedNextCaretLine]);
 
 
-  }, [codeMirrorEditor, editorMode, nextCaretLine, mutateNextCaretLine]);
 
 
   // TODO: Check the reproduction conditions that made this code necessary and confirm reproduction
   // TODO: Check the reproduction conditions that made this code necessary and confirm reproduction
   // // when transitioning to a different page, if the initialValue is the same,
   // // when transitioning to a different page, if the initialValue is the same,

+ 1 - 1
apps/app/src/client/components/ReactMarkdownComponents/Header.tsx

@@ -27,7 +27,7 @@ declare global {
 
 
 function setCaretLine(line?: number): void {
 function setCaretLine(line?: number): void {
   if (line != null) {
   if (line != null) {
-    globalEmitter.emit('saveNextCaretLine', line);
+    globalEmitter.emit('reservedNextCaretLine', line);
   }
   }
 }
 }
 
 

+ 3 - 3
apps/app/src/stores-universal/context.tsx

@@ -261,7 +261,7 @@ export const useIsEditable = (): SWRResponse<boolean, Error> => {
 };
 };
 
 
 
 
-export const useSaveNextCaretLine = (initialData?: number): SWRResponse<number> => {
+export const useReservedNextCaretLine = (initialData?: number): SWRResponse<number> => {
 
 
   const swrResponse = useSWRStatic('saveNextCaretLine', initialData, { fallbackData: 0 });
   const swrResponse = useSWRStatic('saveNextCaretLine', initialData, { fallbackData: 0 });
   const { mutate } = swrResponse;
   const { mutate } = swrResponse;
@@ -271,10 +271,10 @@ export const useSaveNextCaretLine = (initialData?: number): SWRResponse<number>
       mutate(lineNumber);
       mutate(lineNumber);
     };
     };
 
 
-    globalEmitter.on('saveNextCaretLine', handler);
+    globalEmitter.on('reservedNextCaretLine', handler);
 
 
     return function cleanup() {
     return function cleanup() {
-      globalEmitter.removeListener('saveNextCaretLine', handler);
+      globalEmitter.removeListener('reservedNextCaretLine', handler);
     };
     };
   }, [mutate]);
   }, [mutate]);