From b5a786d421553f6a134bbb281674c6c07a455418 Mon Sep 17 00:00:00 2001 From: NriotHrreion Date: Sat, 5 Nov 2022 19:53:52 +0800 Subject: [PATCH] feat: Add rename box in properties dialog --- src/client/components/Properties.tsx | 90 +++++++++++++++++------- src/client/style/dialogs/properties.less | 27 ++++--- 2 files changed, 79 insertions(+), 38 deletions(-) diff --git a/src/client/components/Properties.tsx b/src/client/components/Properties.tsx index f57266d..37f7950 100644 --- a/src/client/components/Properties.tsx +++ b/src/client/components/Properties.tsx @@ -1,14 +1,31 @@ -import React, { useState, useEffect } from "react"; +import React, { useState, useRef, useContext, useEffect } from "react"; +import Axios from "axios"; +import { Form } from "react-bootstrap"; +import MainContext from "../contexts/MainContext"; import Utils from "../../Utils"; import Emitter from "../utils/emitter"; import { DirectoryItem, PropertiesProps } from "../types"; +import { apiUrl } from "../global"; import FileIcon from "../../icons/file.svg"; import FolderIcon from "../../icons/folder_rate.svg"; const Properties: React.FC = (props) => { const [content, setContent] = useState(null); + const { isDemo } = useContext(MainContext); + const renameBox = useRef(null); + + const renameFile = (newName: string) => { + if(isDemo || !content) return; + + Axios.post(apiUrl +"/renameFile", { + path: (props.path +"/"+ content.fullName).replaceAll("/", "\\"), + newName + }).then(() => { + Emitter.get().emit("fileListUpdate"); + }); + }; useEffect(() => { Emitter.get().on("openProperties", (info: DirectoryItem) => { @@ -16,36 +33,55 @@ const Properties: React.FC = (props) => { }); }, []); + useEffect(() => { + if(renameBox.current && content) renameBox.current.value = content.fullName; + }, [content]); + return (
-
    -
  • - {Utils.$("page.explorer.list.properties.name")}: - {content?.fullName} -
  • -
  • - {Utils.$("page.explorer.list.properties.format")}: - - { - content?.isFile - ? content.format +" "+ Utils.$("page.explorer.list.file") - : Utils.$("page.explorer.list.folder") +
    + { + if(e.key === "Enter" && content) { + const newName = (e.target as HTMLInputElement).value; + var oldContent = content; + oldContent.fullName = newName; + + renameFile(newName); + setContent(oldContent); } - -
  • -
  • - {Utils.$("page.explorer.list.properties.path")}: - {props.path +"/"+ content?.fullName} -
  • - { - content?.isFile && content.size - ?
  • - {Utils.$("page.explorer.list.properties.size")}: - {Utils.formatFloat(content.size / 1024, 1) +"KB"} + }}/> +
      +
    • + {Utils.$("page.explorer.list.properties.name")}: + {content?.fullName} +
    • +
    • + {Utils.$("page.explorer.list.properties.format")}: + + { + content?.isFile + ? content.format +" "+ Utils.$("page.explorer.list.file") + : Utils.$("page.explorer.list.folder") + } +
    • - : null - } -
    +
  • + {Utils.$("page.explorer.list.properties.path")}: + {props.path +"/"+ content?.fullName} +
  • + { + content?.isFile && content.size + ?
  • + {Utils.$("page.explorer.list.properties.size")}: + {Utils.formatFloat(content.size / 1024, 1) +"KB ("+ content.size +")"} +
  • + : null + } +
+
diff --git a/src/client/style/dialogs/properties.less b/src/client/style/dialogs/properties.less index 9ad7c39..08818f4 100644 --- a/src/client/style/dialogs/properties.less +++ b/src/client/style/dialogs/properties.less @@ -2,18 +2,23 @@ display: flex; flex-direction: row; height: 100%; - ul { + .properties-main { flex: 3; - padding-top: 10px; - padding-left: 25px; - li { - list-style: none; - margin-bottom: 5px; - b { - display: inline-block; - width: 100px; - text-align: right; - margin-right: 6px; + display: flex; + flex-direction: column; + justify-content: flex-start; + ul { + padding-top: 10px; + padding-left: 25px; + li { + list-style: none; + margin-bottom: 5px; + b { + display: inline-block; + width: 100px; + text-align: right; + margin-right: 6px; + } } } }