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

[Feature Request] display line number #36

Open
xsro opened this issue Sep 3, 2021 · 12 comments
Open

[Feature Request] display line number #36

xsro opened this issue Sep 3, 2021 · 12 comments

Comments

@xsro
Copy link

xsro commented Sep 3, 2021

No description provided.

@jaywcjlove
Copy link
Member

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

@marabesi
Copy link

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?

@obonyojimmy
Copy link

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

@stephensanwo
Copy link

stephensanwo commented May 13, 2022

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;

@shawnz42
Copy link

Hi, from where to import CodeContainter and LineNumbers

@Sumis34
Copy link

Sumis34 commented Apr 19, 2023

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.

@jonashex
Copy link

It would be really nice to have this feature :)

@andreasvirkus
Copy link

andreasvirkus commented Aug 5, 2024

Line numbers can be implemented with a few lines of CSS:

.code-highlight {
  counter-reset: linenumber;
}

.code-line::before {
  position: absolute;
  translate: -130% 0; /* tweak accordingly */
  content: counter(linenumber);
  counter-increment: linenumber;
  color: #333;
  text-align: right;
  user-select: none;
  padding: 0 4px;
}

Screenshot 2024-08-05 at 14 39 23


⚠️ But the very last active line doesn't get a <span class="code-line"> wrapper unless it has content, resulting in a weird editor experience; @jaywcjlove do you know if this is a bug or intended behaviour? Any quick workaround?

Screen.Recording.2024-08-05.at.14.37.45.mov

@jaywcjlove
Copy link
Member

)}</code><br /></pre>`,

@xsro Line breaks are added to align with the Textarea.

@andreasvirkus
Copy link

@jaywcjlove the line break just adds visual padding, the trailing newline that you can enter in the editor (and that gets no span.code-line wrapper and also no line number) lives inside the code element instead. Is there some known workaround for this or is this a bug?

@jaywcjlove
Copy link
Member

@andreasvirkus Line numbers are not displayed by default. If needed, you can add them using the following method.

import rehypePrism from 'rehype-prism-plus';
import React, { useState } from "react";
import CodeEditor from '@uiw/react-textarea-code-editor';

export default function App() {
  const [code, setCode] = useState(
    `function add(a, b) {\n  return a + b;\n}`
  );
  return (
    <CodeEditor
      value={code}
      language="js"
      placeholder="Please enter JS code."
      onChange={(evn) => setCode(evn.target.value)}
      rehypePlugins={[
        [rehypePrism, { ignoreMissing: true, showLineNumbers: true }]
      ]}
      padding={15}
      style={{
        backgroundColor: "#f5f5f5",
        fontFamily: 'ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace',
      }}
    />
  );
}

const { rehypePlugins = [[rehypePrism, { ignoreMissing: true }]] as Pluggable[], ...reset } = props;

2024-09-12.21.45.49.mp4

@andreasvirkus
Copy link

@jaywcjlove thanks, I've already tried the rehype plugin but irrelevant of the implementation method, the last line (if just an empty newline) is still without a line number since it's not represented within the editor-preview (pre element) itself, only in the textarea:

Screenshot 2024-09-13 at 11 31 47

This results in the following broken behaviour:

Screen.Recording.2024-09-13.at.11.42.19.mov

I'd expect the line number "5" to appear immediately when pressing enter, otherwise it's as if the cursor is outside the bounds of the editor, trailing in the void.

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

9 participants