feat(nvim-editor): improve line numbers, and highlight active
This commit is contained in:
@@ -1,12 +1,11 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { CHAR_WIDTH } from "../Kitty";
|
|
||||||
|
|
||||||
export const NvimEditor = (props: { source: string | undefined }) => {
|
export const NvimEditor = (props: { source: string | undefined }) => {
|
||||||
const [data, setData] = useState<string>();
|
const [data, setData] = useState<string>();
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
const textRef = useRef<HTMLParagraphElement>(null);
|
const [selectedLine, setSelectedLine] = useState(0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!props.source) return;
|
if (!props.source) return;
|
||||||
@@ -18,14 +17,21 @@ export const NvimEditor = (props: { source: string | undefined }) => {
|
|||||||
});
|
});
|
||||||
}, [props.source]);
|
}, [props.source]);
|
||||||
|
|
||||||
const rows = data?.split("\n") ?? [];
|
let rows = data?.split("\n") ?? [];
|
||||||
|
// trim end empty lines
|
||||||
for (let i = rows.length - 1; i >= 0; i--) {
|
for (let i = rows.length - 1; i >= 0; i--) {
|
||||||
if (rows[i].trim().length === 0) {
|
if (rows[i].trim().length === 0) {
|
||||||
delete rows[i];
|
rows = rows.slice(0, rows.length - 1);
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// add spaces in empty lines
|
||||||
|
for (let i = 0; i < rows.length; i++) {
|
||||||
|
if (rows[i].trim().length === 0) {
|
||||||
|
rows[i] = " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return loading ? (
|
return loading ? (
|
||||||
<p>Loading...</p>
|
<p>Loading...</p>
|
||||||
@@ -33,10 +39,19 @@ export const NvimEditor = (props: { source: string | undefined }) => {
|
|||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
{rows.map((row, i) => (
|
{rows.map((row, i) => (
|
||||||
<tr key={i}>
|
<tr
|
||||||
|
key={i}
|
||||||
|
onMouseDown={() => setSelectedLine(i)}
|
||||||
|
onMouseMove={(e) => {
|
||||||
|
if (e.buttons === 1) setSelectedLine(i);
|
||||||
|
}}
|
||||||
|
>
|
||||||
<td
|
<td
|
||||||
className={`select-none pl-[2ch] pr-[1ch] text-right text-[#6b616d]`}
|
className={`flex select-none flex-col items-start pl-[2ch] pr-[1ch] text-right`}
|
||||||
style={{ width: `${3 + rows.length.toString().length}ch` }}
|
style={{
|
||||||
|
width: `${3 + rows.length.toString().length}ch`,
|
||||||
|
color: selectedLine === i ? "inherit" : "#6b616d",
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{i + 1}
|
{i + 1}
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
export const NvimInput = () => <div>Welcome to my website!!</div>;
|
export const NvimInput = () => (
|
||||||
|
<div className="select-none">Welcome to my website!!</div>
|
||||||
|
);
|
||||||
|
|||||||
@@ -18,6 +18,10 @@ body {
|
|||||||
font-family: "JetBrainsMono";
|
font-family: "JetBrainsMono";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*::selection {
|
||||||
|
background-color: #584e5a;
|
||||||
|
}
|
||||||
|
|
||||||
@font-face {
|
@font-face {
|
||||||
font-family: "JetBrainsMono";
|
font-family: "JetBrainsMono";
|
||||||
src:
|
src:
|
||||||
|
|||||||
Reference in New Issue
Block a user