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

globalEmitter save line number in first time

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

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

@@ -303,11 +303,15 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
       codeMirrorEditor?.setCaretLineInit(lineNumber);
       codeMirrorEditor?.setCaretLineInit(lineNumber);
     };
     };
     globalEmitter.on('setCaretLine', handler);
     globalEmitter.on('setCaretLine', handler);
+    if (globalEmitter.listenerCount('getCaretLine') >= 1 && codeMirrorEditor?.view != null) {
+      globalEmitter.emit('getCaretLine', handler);
+      globalEmitter.removeAllListeners('getCaretLine');
+    }
 
 
     return function cleanup() {
     return function cleanup() {
       globalEmitter.removeListener('setCaretLine', handler);
       globalEmitter.removeListener('setCaretLine', handler);
     };
     };
-  }, [codeMirrorEditor]);
+  }, [codeMirrorEditor, codeMirrorEditor?.view]);
 
 
   // 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,

+ 3 - 0
apps/app/src/components/ReactMarkdownComponents/Header.tsx

@@ -26,6 +26,9 @@ declare global {
 function setCaretLine(line?: number): void {
 function setCaretLine(line?: number): void {
   if (line != null) {
   if (line != null) {
     globalEmitter.emit('setCaretLine', line);
     globalEmitter.emit('setCaretLine', line);
+    if (globalEmitter.listenerCount('setCaretLine') === 0) {
+      globalEmitter.on('getCaretLine', (handler: (_: number) => void) => handler(line));
+    }
   }
   }
 }
 }