2
0
Эх сурвалжийг харах

add schedule for setCaretLine

reiji-h 1 жил өмнө
parent
commit
54fa783822

+ 1 - 1
apps/app/src/client/components/PageEditor/PageEditor.tsx

@@ -303,7 +303,7 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
   // set handler to set caret line
   useEffect(() => {
     const handler = (lineNumber?: number) => {
-      codeMirrorEditor?.setCaretLine(lineNumber);
+      codeMirrorEditor?.setCaretLine(lineNumber, true);
     };
     globalEmitter.on('setCaretLine', handler);
     if (globalEmitter.listenerCount('getCaretLine') >= 1 && codeMirrorEditor?.view != null) {

+ 44 - 48
packages/editor/src/client/services/use-codemirror-editor/utils/set-caret-line.ts

@@ -4,73 +4,69 @@ import { Compartment, StateEffect } from '@codemirror/state';
 import { EditorView } from '@codemirror/view';
 import type { ViewUpdate } from '@codemirror/view';
 
-export type SetCaretLine = (lineNumber?: number) => void;
+export type SetCaretLine = (lineNumber?: number, schedule?: boolean) => void;
 
 const setCaretLine = (view?: EditorView, lineNumber?: number): void => {
   const doc = view?.state.doc;
 
   if (doc == null || view == null) {
-    throw RangeError;
+    return;
   }
 
-  const posOfLineEnd = doc.line(lineNumber ?? 1).to;
-  view?.dispatch({
-    selection: {
-      anchor: posOfLineEnd,
-      head: posOfLineEnd,
-    },
-    scrollIntoView: true,
-    effects: EditorView.scrollIntoView(posOfLineEnd, {
-      x: 'start',
-      y: 'start',
-    }),
-  });
-  // focus
-  view?.focus();
+  try {
+    const posOfLineEnd = doc.line(lineNumber ?? 1).to;
+    view?.dispatch({
+      selection: {
+        anchor: posOfLineEnd,
+        head: posOfLineEnd,
+      },
+      scrollIntoView: true,
+    });
+    // focus
+    view?.focus();
+  }
+  catch (_: unknown) {
+    // if posOfLineEnd is not found.
+  }
 
 };
 
-export const useSetCaretLine = (view?: EditorView): SetCaretLine => {
-
-  return useCallback((lineNumber) => {
+const setCaretLineSchedule = (view?: EditorView, lineNumber?: number): void => {
 
-    try {
-      setCaretLine(view, lineNumber);
-    }
-    catch (_: unknown) {
-      //
-    }
+  // support Yjs lazy load doc
+  const compartment = new Compartment();
 
-    console.log('Oooops');
+  const initDocListenerExtension = EditorView.updateListener.of((v: ViewUpdate) => {
 
-    // support Yjs lazy load doc
-    const compartment = new Compartment();
+    // TODO: use ySyncAnnotation for if statement and remove "currentPageYjsData?.hasRevisionBodyDiff === false" in Header.tsx
+    // Ref: https://github.com/yjs/y-codemirror.next/pull/30
+    if (v.docChanged && v.changes.desc.length === 0) {
 
-    const initDocListenerExtension = EditorView.updateListener.of((v: ViewUpdate) => {
+      setCaretLine(view, lineNumber);
 
-      console.log(v);
-      // TODO: change conditional statement
-      if (v.docChanged && v.changes.desc.length === 0) {
+      view?.dispatch({
+        effects: compartment.reconfigure([]),
+      });
+    }
+  });
 
-        try {
-          setCaretLine(view, lineNumber);
-        }
-        catch (_: unknown) {
-          // if posOfLineEnd is not found.
-        }
+  view?.dispatch({
+    effects: StateEffect.appendConfig.of(
+      compartment.of(initDocListenerExtension),
+    ),
+  });
+};
 
-        view?.dispatch({
-          effects: compartment.reconfigure([]),
-        });
-      }
-    });
+export const useSetCaretLine = (view?: EditorView): SetCaretLine => {
 
-    view?.dispatch({
-      effects: StateEffect.appendConfig.of(
-        compartment.of(initDocListenerExtension),
-      ),
-    });
+  return useCallback((lineNumber?: number, schedule?: boolean) => {
 
+    if (schedule) {
+      setCaretLineSchedule(view, lineNumber);
+    }
+    else {
+      setCaretLine(view, lineNumber);
+    }
 
   }, [view]);