简体   繁体   English

如何在 React 中获取嵌入式 Monaco 编辑器的行数? (包括包装)

[英]How do I get the line count of an embedded Monaco editor in React? (including wrapping)

I want to extract the line count, including the wrapping, out of an embedded monaco editor in React.我想从 React 中的嵌入式 monaco 编辑器中提取行数,包括换行。 Here is a dummy setup:这是一个虚拟设置:

import React, { useEffect, useState, useRef } from 'react';
import Editor from '@monaco-editor/react'; 


function ExampleApp() {
  const customHTMLRef = useRef(null);
  const [lineCount, setLineCount = useState();

  function handleHTMLEditorDidMount(editor, monaco) {
    customHTMLRef.current = editor;
  }
}

return (
  <>
    <Editor 
      theme='vs-dark'
      height='100%'
      width='100%'
      defaultLanguage='html'
      onMount={handleHTMLEditorDidMount}
    />
    <div>Line Count is: {lineCount}</div>
  </>
)

Some resources I was using, maybe you can see what I dont:我正在使用的一些资源,也许你可以看到我没有看到的:

https://github.com/Microsoft/monaco-editor/issues/947 https://github.com/Microsoft/monaco-editor/issues/947

https://codepen.io/monir/pen/72958128d822b228ba0245b16171c293?editors=0010 https://codepen.io/monir/pen/72958128d822b228ba0245b16171c293?editors=0010

Actually, it seems that they removed the method to retreive line count with wraps.实际上,他们似乎删除了使用换行检索行数的方法。 But you still can count lines using the getLineCount() method.但是您仍然可以使用 getLineCount() 方法计算行数。 As you can see in the first ref that you show.正如您在展示的第一个参考文献中看到的那样。 Example here . 这里的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM