uiwjs / react-codemirror

CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
https://uiwjs.github.io/react-codemirror/
MIT License
1.65k stars 132 forks source link

使用react-codemirror作日志展示,如果固定显示最后一行 #114

Closed WLyKan closed 2 years ago

WLyKan commented 3 years ago

使用react-codemirror作日志展示,如果固定显示最后一行。 我现在是这么做的

const { height, clientHeight } = textareaRef?.current?.editor?.getScrollInfo() || {}
...
textareaRef?.current?.editor?.scrollTo(0, height)

这有个问题,value重新设置的时候,会跳回到顶部,然后再scrollTo到底部,导致出现闪动,怎么解决这个问题。。。

jaywcjlove commented 2 years ago

@WLyKan 啊,忘记了这个 issue。 https://codesandbox.io/s/react-codemirror-example-codemirror-6-114-9kwk8?file=/src/App.js:0-2184

import React, { useEffect, useRef } from "react";
import CodeMirror, { EditorView } from "@uiw/react-codemirror";
import { javascript } from "@codemirror/lang-javascript";

export default function App() {
  const cmRef = useRef();
  useEffect(() => {
    setTimeout(() => {
      cmRef.current.view.dispatch({
        effects: EditorView.scrollIntoView(
          cmRef.current.view.state.doc.line(20).from,
          {
            y: "start",
            yMargin: 0
          }
        )
      });
      // Vim.handleKey((view as any).cm, "<Esc>");
    }, 3000);
  }, []);
  return (
    <CodeMirror
      ref={cmRef}
      value={new Array(4000).fill("Initial contents ".repeat(4)).join("\n")}
      height="200px"
      extensions={[javascript({ jsx: true })]}
      onChange={(value, viewUpdate) => {
        console.log("value:", value);
      }}
    />
  );
}