-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Comments
@xsro It is more difficult to add line numbers, and there is no plan to add this feature before finding a better way. |
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? |
Supporting this feature inclussion , probably aprop like |
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; |
Hi, from where to import CodeContainter and LineNumbers |
Looks nice, would be nice to see a working example with the full code needed. |
It would be really nice to have this feature :) |
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;
}
Screen.Recording.2024-08-05.at.14.37.45.mov |
@xsro Line breaks are added to align with the Textarea. |
@jaywcjlove the line break just adds visual padding, the trailing newline that you can enter in the editor (and that gets no |
@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',
}}
/>
);
}
2024-09-12.21.45.49.mp4 |
@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 This results in the following broken behaviour: Screen.Recording.2024-09-13.at.11.42.19.movI'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. |
No description provided.
The text was updated successfully, but these errors were encountered: