start file rename logic
This commit is contained in:
parent
1b6bd01989
commit
76b2fc7e0f
@ -13,13 +13,14 @@ import {
|
|||||||
import {
|
import {
|
||||||
ChevronLeft,
|
ChevronLeft,
|
||||||
ChevronRight,
|
ChevronRight,
|
||||||
|
FileJson,
|
||||||
RotateCw,
|
RotateCw,
|
||||||
TerminalSquare,
|
TerminalSquare,
|
||||||
} from "lucide-react"
|
} from "lucide-react"
|
||||||
import Tab from "../ui/tab"
|
import Tab from "../ui/tab"
|
||||||
import Sidebar from "./sidebar"
|
import Sidebar from "./sidebar"
|
||||||
import { useClerk } from "@clerk/nextjs"
|
import { useClerk } from "@clerk/nextjs"
|
||||||
import { TFile, TFileData, TFolder } from "./sidebar/types"
|
import { TFile, TFileData, TFolder, TTab } from "./sidebar/types"
|
||||||
|
|
||||||
import { io } from "socket.io-client"
|
import { io } from "socket.io-client"
|
||||||
import { set } from "zod"
|
import { set } from "zod"
|
||||||
@ -41,7 +42,7 @@ export default function CodeEditor({
|
|||||||
const [files, setFiles] = useState<(TFolder | TFile)[]>([])
|
const [files, setFiles] = useState<(TFolder | TFile)[]>([])
|
||||||
const [editorLanguage, setEditorLanguage] = useState("plaintext")
|
const [editorLanguage, setEditorLanguage] = useState("plaintext")
|
||||||
const [activeFile, setActiveFile] = useState<string | null>(null)
|
const [activeFile, setActiveFile] = useState<string | null>(null)
|
||||||
const [tabs, setTabs] = useState<TFile[]>([])
|
const [tabs, setTabs] = useState<TTab[]>([])
|
||||||
const [activeId, setActiveId] = useState<string | null>(null)
|
const [activeId, setActiveId] = useState<string | null>(null)
|
||||||
|
|
||||||
const socket = io(
|
const socket = io(
|
||||||
@ -81,7 +82,7 @@ export default function CodeEditor({
|
|||||||
|
|
||||||
const clerk = useClerk()
|
const clerk = useClerk()
|
||||||
|
|
||||||
const selectFile = (tab: TFile) => {
|
const selectFile = (tab: TTab) => {
|
||||||
setTabs((prev) => {
|
setTabs((prev) => {
|
||||||
const exists = prev.find((t) => t.id === tab.id)
|
const exists = prev.find((t) => t.id === tab.id)
|
||||||
if (exists) {
|
if (exists) {
|
||||||
@ -115,6 +116,13 @@ export default function CodeEditor({
|
|||||||
setTabs((prev) => prev.filter((t) => t.id !== tab.id))
|
setTabs((prev) => prev.filter((t) => t.id !== tab.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleFileNameChange = (id: string, newName: string) => {
|
||||||
|
socket.emit("renameFile", id, newName)
|
||||||
|
setTabs((prev) =>
|
||||||
|
prev.map((tab) => (tab.id === id ? { ...tab, name: newName } : tab))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Sidebar files={files} selectFile={selectFile} />
|
<Sidebar files={files} selectFile={selectFile} />
|
||||||
@ -129,6 +137,7 @@ export default function CodeEditor({
|
|||||||
{tabs.map((tab) => (
|
{tabs.map((tab) => (
|
||||||
<Tab
|
<Tab
|
||||||
key={tab.id}
|
key={tab.id}
|
||||||
|
saved={tab.saved}
|
||||||
selected={activeId === tab.id}
|
selected={activeId === tab.id}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
selectFile(tab)
|
selectFile(tab)
|
||||||
@ -143,6 +152,7 @@ export default function CodeEditor({
|
|||||||
{activeId === null ? (
|
{activeId === null ? (
|
||||||
<>
|
<>
|
||||||
<div className="w-full h-full flex items-center justify-center text-xl font-medium text-secondary select-none">
|
<div className="w-full h-full flex items-center justify-center text-xl font-medium text-secondary select-none">
|
||||||
|
<FileJson className="w-6 h-6 mr-3" />
|
||||||
No file selected.
|
No file selected.
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
@ -152,6 +162,13 @@ export default function CodeEditor({
|
|||||||
// defaultLanguage="typescript"
|
// defaultLanguage="typescript"
|
||||||
language={editorLanguage}
|
language={editorLanguage}
|
||||||
onMount={handleEditorMount}
|
onMount={handleEditorMount}
|
||||||
|
onChange={(value) => {
|
||||||
|
setTabs((prev) =>
|
||||||
|
prev.map((tab) =>
|
||||||
|
tab.id === activeId ? { ...tab, saved: false } : tab
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}}
|
||||||
options={{
|
options={{
|
||||||
minimap: {
|
minimap: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
@ -2,21 +2,32 @@
|
|||||||
|
|
||||||
import Image from "next/image"
|
import Image from "next/image"
|
||||||
import { getIconForFile } from "vscode-icons-js"
|
import { getIconForFile } from "vscode-icons-js"
|
||||||
import { TFile } from "./types"
|
import { TFile, TTab } from "./types"
|
||||||
import { useEffect, useState } from "react"
|
import { useEffect, useRef, useState } from "react"
|
||||||
|
|
||||||
export default function SidebarFile({
|
export default function SidebarFile({
|
||||||
data,
|
data,
|
||||||
selectFile,
|
selectFile,
|
||||||
}: {
|
}: {
|
||||||
data: TFile
|
data: TFile
|
||||||
selectFile: (file: TFile) => void
|
selectFile: (file: TTab) => void
|
||||||
}) {
|
}) {
|
||||||
const [imgSrc, setImgSrc] = useState(`/icons/${getIconForFile(data.name)}`)
|
const [imgSrc, setImgSrc] = useState(`/icons/${getIconForFile(data.name)}`)
|
||||||
|
const [editing, setEditing] = useState(false)
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (editing) {
|
||||||
|
inputRef.current?.focus()
|
||||||
|
}
|
||||||
|
}, [editing])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
onClick={() => selectFile(data)}
|
onClick={() => selectFile({ ...data, saved: true })}
|
||||||
|
onDoubleClick={() => {
|
||||||
|
setEditing(true)
|
||||||
|
}}
|
||||||
className="w-full flex items-center h-7 px-1 hover:bg-secondary rounded-sm cursor-pointer transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
|
className="w-full flex items-center h-7 px-1 hover:bg-secondary rounded-sm cursor-pointer transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
|
||||||
>
|
>
|
||||||
<Image
|
<Image
|
||||||
@ -27,7 +38,23 @@ export default function SidebarFile({
|
|||||||
className="mr-2"
|
className="mr-2"
|
||||||
onError={() => setImgSrc("/icons/default_file.svg")}
|
onError={() => setImgSrc("/icons/default_file.svg")}
|
||||||
/>
|
/>
|
||||||
{data.name}
|
<form
|
||||||
|
onSubmit={(e) => {
|
||||||
|
e.preventDefault()
|
||||||
|
console.log("submit")
|
||||||
|
setEditing(false)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
ref={inputRef}
|
||||||
|
className={`bg-transparent w-full ${
|
||||||
|
editing ? "" : "pointer-events-none"
|
||||||
|
}`}
|
||||||
|
disabled={!editing}
|
||||||
|
defaultValue={data.name}
|
||||||
|
onBlur={() => setEditing(false)}
|
||||||
|
/>
|
||||||
|
</form>
|
||||||
</button>
|
</button>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
import Image from "next/image"
|
import Image from "next/image"
|
||||||
import { useState } from "react"
|
import { useState } from "react"
|
||||||
import { getIconForFolder, getIconForOpenFolder } from "vscode-icons-js"
|
import { getIconForFolder, getIconForOpenFolder } from "vscode-icons-js"
|
||||||
import { TFile, TFolder } from "./types"
|
import { TFile, TFolder, TTab } from "./types"
|
||||||
import SidebarFile from "./file"
|
import SidebarFile from "./file"
|
||||||
|
|
||||||
export default function SidebarFolder({
|
export default function SidebarFolder({
|
||||||
@ -11,7 +11,7 @@ export default function SidebarFolder({
|
|||||||
selectFile,
|
selectFile,
|
||||||
}: {
|
}: {
|
||||||
data: TFolder
|
data: TFolder
|
||||||
selectFile: (file: TFile) => void
|
selectFile: (file: TTab) => void
|
||||||
}) {
|
}) {
|
||||||
const [isOpen, setIsOpen] = useState(false)
|
const [isOpen, setIsOpen] = useState(false)
|
||||||
const folder = isOpen
|
const folder = isOpen
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
import { FilePlus, FolderPlus, Loader2, Search } from "lucide-react"
|
import { FilePlus, FolderPlus, Loader2, Search } from "lucide-react"
|
||||||
import SidebarFile from "./file"
|
import SidebarFile from "./file"
|
||||||
import SidebarFolder from "./folder"
|
import SidebarFolder from "./folder"
|
||||||
import { TFile, TFolder } from "./types"
|
import { TFile, TFolder, TTab } from "./types"
|
||||||
|
|
||||||
// Note: add renaming validation:
|
// Note: add renaming validation:
|
||||||
// In general: must not contain / or \ or whitespace, not empty, no duplicates
|
// In general: must not contain / or \ or whitespace, not empty, no duplicates
|
||||||
@ -15,7 +15,7 @@ export default function Sidebar({
|
|||||||
selectFile,
|
selectFile,
|
||||||
}: {
|
}: {
|
||||||
files: (TFile | TFolder)[]
|
files: (TFile | TFolder)[]
|
||||||
selectFile: (tab: TFile) => void
|
selectFile: (tab: TTab) => void
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<div className="h-full w-56 select-none flex flex-col text-sm items-start p-2">
|
<div className="h-full w-56 select-none flex flex-col text-sm items-start p-2">
|
||||||
|
@ -11,6 +11,10 @@ export type TFile = {
|
|||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TTab = TFile & {
|
||||||
|
saved: boolean
|
||||||
|
}
|
||||||
|
|
||||||
export type TFileData = {
|
export type TFileData = {
|
||||||
id: string
|
id: string
|
||||||
data: string
|
data: string
|
||||||
|
@ -6,11 +6,13 @@ import { useEffect } from "react"
|
|||||||
|
|
||||||
export default function Tab({
|
export default function Tab({
|
||||||
children,
|
children,
|
||||||
selected,
|
saved = true,
|
||||||
|
selected = false,
|
||||||
onClick,
|
onClick,
|
||||||
onClose,
|
onClose,
|
||||||
}: {
|
}: {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
|
saved?: boolean
|
||||||
selected?: boolean
|
selected?: boolean
|
||||||
onClick?: () => void
|
onClick?: () => void
|
||||||
onClose?: () => void
|
onClose?: () => void
|
||||||
@ -20,7 +22,7 @@ export default function Tab({
|
|||||||
onClick={onClick ?? undefined}
|
onClick={onClick ?? undefined}
|
||||||
size="sm"
|
size="sm"
|
||||||
variant={"secondary"}
|
variant={"secondary"}
|
||||||
className={`group font-normal select-none ${
|
className={`font-normal select-none ${
|
||||||
selected
|
selected
|
||||||
? "bg-neutral-700 hover:bg-neutral-600 text-foreground"
|
? "bg-neutral-700 hover:bg-neutral-600 text-foreground"
|
||||||
: "text-muted-foreground"
|
: "text-muted-foreground"
|
||||||
@ -37,9 +39,16 @@ export default function Tab({
|
|||||||
}
|
}
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
className="h-5 w-5 ml-0.5 flex items-center justify-center translate-x-1 transition-colors bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm"
|
className="h-5 w-5 ml-0.5 group flex items-center justify-center translate-x-1 transition-colors bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm"
|
||||||
>
|
>
|
||||||
<X className="w-3 h-3" />
|
{saved ? (
|
||||||
|
<X className="w-3 h-3" />
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<X className="w-3 h-3 group-hover:block hidden" />
|
||||||
|
<div className="w-2 h-2 rounded-full bg-foreground group-hover:hidden" />
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</Button>
|
</Button>
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user