reiji-h 2 лет назад
Родитель
Сommit
1ca9575f96

+ 19 - 0
packages/editor/src/components/CodeMirrorEditor/CodeMirrorEditor.tsx

@@ -25,6 +25,7 @@ type Props = {
   acceptedFileType: AcceptedUploadFileType,
   acceptedFileType: AcceptedUploadFileType,
   onChange?: (value: string) => void,
   onChange?: (value: string) => void,
   onUpload?: (files: File[]) => void,
   onUpload?: (files: File[]) => void,
+  onScroll?: (line: number) => void,
   indentSize?: number,
   indentSize?: number,
 }
 }
 
 
@@ -34,6 +35,7 @@ export const CodeMirrorEditor = (props: Props): JSX.Element => {
     acceptedFileType,
     acceptedFileType,
     onChange,
     onChange,
     onUpload,
     onUpload,
+    onScroll,
     indentSize,
     indentSize,
   } = props;
   } = props;
 
 
@@ -100,6 +102,23 @@ export const CodeMirrorEditor = (props: Props): JSX.Element => {
 
 
   }, [codeMirrorEditor]);
   }, [codeMirrorEditor]);
 
 
+  useEffect(() => {
+
+    const handleScroll = () => {
+      if (onScroll != null && codeMirrorEditor?.view?.documentTop != null) {
+        onScroll(codeMirrorEditor.view.documentTop);
+      }
+    };
+
+    const extension = EditorView.domEventHandlers({
+      scroll: handleScroll,
+    });
+
+    const cleanupFunction = codeMirrorEditor?.appendExtensions(extension);
+    return cleanupFunction;
+
+  }, [onScroll, codeMirrorEditor]);
+
   const {
   const {
     getRootProps,
     getRootProps,
     isDragActive,
     isDragActive,

+ 3 - 1
packages/editor/src/components/CodeMirrorEditorMain.tsx

@@ -17,13 +17,14 @@ type Props = {
   onChange?: (value: string) => void,
   onChange?: (value: string) => void,
   onSave?: () => void,
   onSave?: () => void,
   onUpload?: (files: File[]) => void,
   onUpload?: (files: File[]) => void,
+  onScroll?: (line: number) => void,
   acceptedFileType?: AcceptedUploadFileType,
   acceptedFileType?: AcceptedUploadFileType,
   indentSize?: number,
   indentSize?: number,
 }
 }
 
 
 export const CodeMirrorEditorMain = (props: Props): JSX.Element => {
 export const CodeMirrorEditorMain = (props: Props): JSX.Element => {
   const {
   const {
-    onSave, onChange, onUpload, acceptedFileType, indentSize,
+    onSave, onChange, onUpload, onScroll, acceptedFileType, indentSize,
   } = props;
   } = props;
 
 
   const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);
   const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);
@@ -65,6 +66,7 @@ export const CodeMirrorEditorMain = (props: Props): JSX.Element => {
       editorKey={GlobalCodeMirrorEditorKey.MAIN}
       editorKey={GlobalCodeMirrorEditorKey.MAIN}
       onChange={onChange}
       onChange={onChange}
       onUpload={onUpload}
       onUpload={onUpload}
+      onScroll={onScroll}
       acceptedFileType={acceptedFileTypeNoOpt}
       acceptedFileType={acceptedFileTypeNoOpt}
       indentSize={indentSize}
       indentSize={indentSize}
     />
     />