feat(nvim-tree): better click handling

This commit is contained in:
Pihkaal
2024-05-31 10:47:45 +02:00
parent a4038ae294
commit 0c79ea457c

View File

@@ -1,6 +1,6 @@
import { useApp } from "~/hooks/useApp"; import { useApp } from "~/hooks/useApp";
import { CHAR_HEIGHT, CHAR_WIDTH } from "../Kitty"; import { CHAR_HEIGHT, CHAR_WIDTH } from "../Kitty";
import { type ReactNode, useEffect, useState } from "react"; import { type ReactNode, useEffect, useState, useCallback } from "react";
import { type InnerKittyProps } from "~/utils/types"; import { type InnerKittyProps } from "~/utils/types";
import { type Nvim } from "."; import { type Nvim } from ".";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
@@ -57,6 +57,21 @@ export const NvimTree = (props: InnerKittyProps<typeof Nvim>) => {
); );
const [selected, setSelected] = useState(files.length - 1); const [selected, setSelected] = useState(files.length - 1);
const handleOpen = (file: File) => {
if (file.type === "directory") {
file.folded = !file.folded;
} else {
let filePath = "";
if (file.directory) {
filePath += `${file.directory.name}/`;
}
navigate(`?view=${filePath}${file.name}`);
}
setFiles([...files]);
};
const tree: Array<ReactNode> = []; const tree: Array<ReactNode> = [];
let y = 0; let y = 0;
let selectedFile: File; let selectedFile: File;
@@ -70,10 +85,7 @@ export const NvimTree = (props: InnerKittyProps<typeof Nvim>) => {
className="text-[#a0b6ee]" className="text-[#a0b6ee]"
style={{ background: y === selected ? "#504651" : "" }} style={{ background: y === selected ? "#504651" : "" }}
onMouseDown={() => setSelected(dy)} onMouseDown={() => setSelected(dy)}
onDoubleClick={() => { onDoubleClick={() => handleOpen(fileOrDir)}
fileOrDir.folded = !fileOrDir.folded;
setFiles([...files]);
}}
> >
{fileOrDir.folded ? ( {fileOrDir.folded ? (
<> <>
@@ -102,6 +114,13 @@ export const NvimTree = (props: InnerKittyProps<typeof Nvim>) => {
key={y} key={y}
style={{ background: y === selected ? "#504651" : "" }} style={{ background: y === selected ? "#504651" : "" }}
onMouseDown={() => setSelected(fy)} onMouseDown={() => setSelected(fy)}
onDoubleClick={() =>
handleOpen({
type: "file",
name: fileOrDir.files[i],
directory: fileOrDir,
})
}
> >
{" "} {" "}
<span className="text-[#5b515b]"> <span className="text-[#5b515b]">
@@ -126,6 +145,7 @@ export const NvimTree = (props: InnerKittyProps<typeof Nvim>) => {
key={y} key={y}
style={{ background: y === selected ? "#504651" : "" }} style={{ background: y === selected ? "#504651" : "" }}
onMouseDown={() => setSelected(fy)} onMouseDown={() => setSelected(fy)}
onDoubleClick={() => handleOpen(fileOrDir)}
> >
{" "} {" "}
<span style={{ color: icon.color }}>{`${icon.char}`}</span> <span style={{ color: icon.color }}>{`${icon.char}`}</span>
@@ -153,18 +173,7 @@ export const NvimTree = (props: InnerKittyProps<typeof Nvim>) => {
break; break;
case "Enter": case "Enter":
if (selectedFile.type === "directory") { handleOpen(selectedFile);
selectedFile.folded = !selectedFile.folded;
} else {
let filePath = "";
if (selectedFile.directory) {
filePath += `${selectedFile.directory.name}/`;
}
navigate(`?view=${filePath}${selectedFile.name}`);
}
setFiles([...files]);
break; break;
} }
}; };