react-textarea-code-editor icon indicating copy to clipboard operation
react-textarea-code-editor copied to clipboard

[Feature Request] display line number

Open xsro opened this issue 2 years ago • 7 comments

xsro avatar Sep 03 '21 11:09 xsro

@xsro It is more difficult to add line numbers, and there is no plan to add this feature before finding a better way.

jaywcjlove avatar Sep 03 '21 16:09 jaywcjlove

Just came across this issue because I would like to support line numbers as well.

Will it be in the roadmap? Or should I try to find a replacement?

marabesi avatar Oct 30 '21 16:10 marabesi

Supporting this feature inclussion , probably aprop like showInlineLineNumbers as an inspiration from https://www.npmjs.com/package/react-syntax-highlighter

obonyojimmy avatar Apr 05 '22 23:04 obonyojimmy

I added line numbers by creating a div on the left side of the code editor. Using a useEffect hook ensures that the line numbers increases as the height of the code editor increase. Let me know what you think.

const Code: React.FC<CodeProps> = ({
  id,
  codeData,
  handleCodeChange,
  style,
  language,
  placeholder,
}) => {
  let numStart = 1;
  const ref: any = useRef("");
  const [num, setNum] = useState([numStart]);

  useEffect(() => {
    let currentHeight: number = ref.current.offsetHeight;
    let eachNum = Math.round((currentHeight - 15) / 20);
    let arr = Array.from({ length: eachNum }, (_, i) => i + 1);
    setNum(arr);
  }, [ref.current.offsetHeight]);

  return (
    <CodeContainter>
      <LineNumbers ref={ref}>
        {num.map((item, index) => (
          <LineNumber key={index}>{item}</LineNumber>
        ))}
      </LineNumbers>
      <CodeEditor
        id={id}
        value={codeData}
        language={language}
        placeholder={placeholder}
        onChange={(event) => handleCodeChange(event)}
        padding={15}
        style={style}
        data-color-mode="dark"
      />
    </CodeContainter>
  );
};
export default Code;

stephensanwo avatar May 13 '22 00:05 stephensanwo

Hi, from where to import CodeContainter and LineNumbers

shawnz42 avatar Sep 12 '22 06:09 shawnz42

I added line numbers by creating a div on the left side of the code editor. Using a useEffect hook ensures that the line numbers increases as the height of the code editor increase. Let me know what you think.

const Code: React.FC<CodeProps> = ({
  id,
  codeData,
  handleCodeChange,
  style,
  language,
  placeholder,
}) => {
  let numStart = 1;
  const ref: any = useRef("");
  const [num, setNum] = useState([numStart]);

  useEffect(() => {
    let currentHeight: number = ref.current.offsetHeight;
    let eachNum = Math.round((currentHeight - 15) / 20);
    let arr = Array.from({ length: eachNum }, (_, i) => i + 1);
    setNum(arr);
  }, [ref.current.offsetHeight]);

  return (
    <CodeContainter>
      <LineNumbers ref={ref}>
        {num.map((item, index) => (
          <LineNumber key={index}>{item}</LineNumber>
        ))}
      </LineNumbers>
      <CodeEditor
        id={id}
        value={codeData}
        language={language}
        placeholder={placeholder}
        onChange={(event) => handleCodeChange(event)}
        padding={15}
        style={style}
        data-color-mode="dark"
      />
    </CodeContainter>
  );
};
export default Code;

Looks nice, would be nice to see a working example with the full code needed.

Sumis34 avatar Apr 19 '23 11:04 Sumis34

It would be really nice to have this feature :)

jonashex avatar Apr 24 '24 17:04 jonashex