Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
WLyKan opened this issue Jun 4, 2021 · 1 comment
Closed

Comments

@WLyKan
Copy link

WLyKan commented Jun 4, 2021

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

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

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

@jaywcjlove
Copy link
Member

jaywcjlove commented Jan 11, 2022

@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);
      }}
    />
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants